rust-analyzer/crates/ra_syntax/src/lib.rs

104 lines
2.8 KiB
Rust
Raw Normal View History

//! An experimental implementation of [Rust RFC#2256 libsyntax2.0][rfc#2256].
//!
//! The intent is to be an IDE-ready parser, i.e. one that offers
//!
//! - easy and fast incremental re-parsing,
//! - graceful handling of errors, and
//! - maintains all information in the source file.
//!
//! For more information, see [the RFC][rfc#2265], or [the working draft][RFC.md].
//!
//! [rfc#2256]: <https://github.com/rust-lang/rfcs/pull/2256>
//! [RFC.md]: <https://github.com/matklad/libsyntax2/blob/master/docs/RFC.md>
2018-07-30 11:08:06 +00:00
#![forbid(
missing_debug_implementations,
unconditional_recursion,
future_incompatible
)]
2018-07-29 10:51:55 +00:00
#![deny(bad_style, missing_docs)]
#![allow(missing_docs)]
//#![warn(unreachable_pub)] // rust-lang/rust#47816
2018-07-31 12:40:40 +00:00
extern crate itertools;
2018-07-30 11:08:06 +00:00
extern crate unicode_xid;
2018-08-01 11:55:37 +00:00
extern crate drop_bomb;
2018-08-01 19:07:09 +00:00
extern crate parking_lot;
2018-10-02 14:07:12 +00:00
extern crate rowan;
2017-12-29 20:33:04 +00:00
2018-09-15 12:35:30 +00:00
#[cfg(test)]
#[macro_use]
extern crate test_utils;
2018-07-31 12:40:40 +00:00
pub mod algo;
pub mod ast;
2017-12-28 21:56:36 +00:00
mod lexer;
2018-07-31 20:38:19 +00:00
#[macro_use]
2018-09-06 13:54:54 +00:00
mod token_set;
2018-07-31 20:38:19 +00:00
mod parser_api;
mod grammar;
mod parser_impl;
mod reparsing;
2018-07-31 20:38:19 +00:00
2018-07-29 12:16:07 +00:00
mod syntax_kinds;
2018-08-08 16:44:16 +00:00
mod yellow;
2018-07-30 12:25:52 +00:00
/// Utilities for simple uses of the parser.
pub mod utils;
2018-08-24 10:41:25 +00:00
pub mod text_utils;
2018-07-29 12:16:07 +00:00
pub use {
2018-10-02 14:07:12 +00:00
rowan::{SmolStr, TextRange, TextUnit},
2018-08-25 08:40:17 +00:00
ast::AstNode,
2018-07-30 11:08:06 +00:00
lexer::{tokenize, Token},
2018-07-29 12:16:07 +00:00
syntax_kinds::SyntaxKind,
2018-10-02 15:14:33 +00:00
yellow::{SyntaxNode, SyntaxNodeRef, OwnedRoot, RefRoot, TreeRoot, SyntaxError, Direction},
reparsing::AtomEdit,
2018-07-29 12:16:07 +00:00
};
2018-08-25 10:17:54 +00:00
use {
2018-10-02 14:07:12 +00:00
yellow::{GreenNode},
2018-08-25 10:17:54 +00:00
};
2018-08-25 09:10:35 +00:00
2018-09-11 07:31:04 +00:00
#[derive(Clone, Debug, Hash)]
2018-08-25 08:44:58 +00:00
pub struct File {
2018-08-25 08:40:17 +00:00
root: SyntaxNode
}
2018-08-25 08:44:58 +00:00
impl File {
2018-08-25 10:57:13 +00:00
fn new(green: GreenNode, errors: Vec<SyntaxError>) -> File {
2018-10-02 14:07:12 +00:00
let root = SyntaxNode::new(green, errors);
2018-09-08 15:34:41 +00:00
if cfg!(debug_assertions) {
utils::validate_block_structure(root.borrowed());
}
2018-08-25 08:44:58 +00:00
File { root }
2018-08-25 08:40:17 +00:00
}
2018-08-25 09:44:26 +00:00
pub fn parse(text: &str) -> File {
2018-08-25 09:10:35 +00:00
let tokens = tokenize(&text);
2018-10-08 12:44:00 +00:00
let (green, errors) = parser_impl::parse_with(
yellow::GreenBuilder::new(),
2018-08-25 11:45:17 +00:00
text, &tokens, grammar::root,
);
2018-08-25 10:57:13 +00:00
File::new(green, errors)
2018-08-25 09:10:35 +00:00
}
2018-08-25 10:17:54 +00:00
pub fn reparse(&self, edit: &AtomEdit) -> File {
2018-08-25 10:42:40 +00:00
self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit))
2018-08-25 10:17:54 +00:00
}
2018-08-25 11:45:17 +00:00
pub fn incremental_reparse(&self, edit: &AtomEdit) -> Option<File> {
reparsing::incremental_reparse(self.syntax(), edit, self.errors())
.map(|(green_node, errors)| File::new(green_node, errors))
2018-08-25 10:17:54 +00:00
}
fn full_reparse(&self, edit: &AtomEdit) -> File {
let text = text_utils::replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert);
2018-08-25 10:17:54 +00:00
File::parse(&text)
}
2018-08-25 08:44:17 +00:00
pub fn ast(&self) -> ast::Root {
ast::Root::cast(self.syntax()).unwrap()
2018-08-25 08:40:17 +00:00
}
pub fn syntax(&self) -> SyntaxNodeRef {
self.root.borrowed()
}
pub fn errors(&self) -> Vec<SyntaxError> {
2018-10-02 14:07:12 +00:00
self.root.root_data().clone()
2018-08-25 08:40:17 +00:00
}
2018-08-24 16:27:30 +00:00
}