2019-02-21 06:24:42 -06:00
|
|
|
//! Syntax Tree library used throughout the rust analyzer.
|
2018-01-27 19:29:14 -06:00
|
|
|
//!
|
2019-02-21 06:24:42 -06:00
|
|
|
//! Properties:
|
|
|
|
//! - easy and fast incremental re-parsing
|
|
|
|
//! - graceful handling of errors
|
|
|
|
//! - full-fidelity representation (*any* text can be precisely represented as
|
|
|
|
//! a syntax tree)
|
2018-01-27 19:29:14 -06:00
|
|
|
//!
|
2019-02-21 06:24:42 -06:00
|
|
|
//! For more information, see the [RFC]. Current implementation is inspired by
|
|
|
|
//! the [Swift] one.
|
2018-01-27 19:29:14 -06:00
|
|
|
//!
|
2019-02-21 06:24:42 -06:00
|
|
|
//! The most interesting modules here are `syntax_node` (which defines concrete
|
|
|
|
//! syntax tree) and `ast` (which defines abstract syntax tree on top of the
|
|
|
|
//! CST). The actual parser live in a separate `ra_parser` crate, thought the
|
|
|
|
//! lexer lives in this crate.
|
2018-01-27 19:29:14 -06:00
|
|
|
//!
|
2019-02-21 06:24:42 -06:00
|
|
|
//! [RFC]: <https://github.com/rust-lang/rfcs/pull/2256>
|
|
|
|
//! [Swift]: <https://github.com/apple/swift/blob/13d593df6f359d0cb2fc81cfaac273297c539455/lib/Syntax/README.md>
|
2018-01-27 19:29:14 -06:00
|
|
|
|
2019-02-20 07:24:39 -06:00
|
|
|
mod syntax_node;
|
|
|
|
mod syntax_text;
|
|
|
|
mod syntax_error;
|
2019-02-20 06:47:32 -06:00
|
|
|
mod parsing;
|
2018-11-04 08:06:38 -06:00
|
|
|
mod string_lexing;
|
|
|
|
mod validation;
|
2019-01-23 08:37:10 -06:00
|
|
|
mod ptr;
|
2019-02-20 07:24:39 -06:00
|
|
|
|
|
|
|
pub mod algo;
|
|
|
|
pub mod ast;
|
2018-07-29 07:16:07 -05:00
|
|
|
|
2018-12-06 11:49:36 -06:00
|
|
|
pub use rowan::{SmolStr, TextRange, TextUnit};
|
2019-02-21 04:37:32 -06:00
|
|
|
pub use ra_parser::SyntaxKind;
|
2018-10-15 11:55:32 -05:00
|
|
|
pub use crate::{
|
2018-08-25 03:40:17 -05:00
|
|
|
ast::AstNode,
|
2019-02-20 07:16:14 -06:00
|
|
|
syntax_error::{SyntaxError, SyntaxErrorKind, Location},
|
|
|
|
syntax_text::SyntaxText,
|
|
|
|
syntax_node::{Direction, SyntaxNode, WalkEvent, TreeArc},
|
2019-01-23 09:26:02 -06:00
|
|
|
ptr::{SyntaxNodePtr, AstPtr},
|
2019-02-20 06:47:32 -06:00
|
|
|
parsing::{tokenize, Token},
|
2018-07-29 07:16:07 -05:00
|
|
|
};
|
|
|
|
|
2018-12-11 12:07:17 -06:00
|
|
|
use ra_text_edit::AtomTextEdit;
|
2019-02-12 09:41:57 -06:00
|
|
|
use crate::syntax_node::GreenNode;
|
2018-08-25 04:10:35 -05:00
|
|
|
|
2019-01-07 07:15:47 -06:00
|
|
|
/// `SourceFile` represents a parse tree for a single Rust file.
|
|
|
|
pub use crate::ast::SourceFile;
|
2018-08-25 03:40:17 -05:00
|
|
|
|
2019-01-07 07:15:47 -06:00
|
|
|
impl SourceFile {
|
2019-01-11 10:59:06 -06:00
|
|
|
fn new(green: GreenNode, errors: Vec<SyntaxError>) -> TreeArc<SourceFile> {
|
2018-10-02 09:07:12 -05:00
|
|
|
let root = SyntaxNode::new(green, errors);
|
2018-09-08 10:34:41 -05:00
|
|
|
if cfg!(debug_assertions) {
|
2019-02-21 06:51:22 -06:00
|
|
|
validation::validate_block_structure(&root);
|
2018-09-08 10:34:41 -05:00
|
|
|
}
|
2018-11-07 09:38:43 -06:00
|
|
|
assert_eq!(root.kind(), SyntaxKind::SOURCE_FILE);
|
2019-01-11 10:59:06 -06:00
|
|
|
TreeArc::cast(root)
|
2018-08-25 03:40:17 -05:00
|
|
|
}
|
2019-01-14 12:30:21 -06:00
|
|
|
|
2019-01-11 10:59:06 -06:00
|
|
|
pub fn parse(text: &str) -> TreeArc<SourceFile> {
|
2019-02-20 06:47:32 -06:00
|
|
|
let (green, errors) = parsing::parse_text(text);
|
2019-01-07 07:15:47 -06:00
|
|
|
SourceFile::new(green, errors)
|
2018-08-25 04:10:35 -05:00
|
|
|
}
|
2019-01-10 06:54:58 -06:00
|
|
|
|
2019-01-11 10:59:06 -06:00
|
|
|
pub fn reparse(&self, edit: &AtomTextEdit) -> TreeArc<SourceFile> {
|
2019-02-08 05:49:43 -06:00
|
|
|
self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit))
|
2018-08-25 05:17:54 -05:00
|
|
|
}
|
2019-01-10 06:54:58 -06:00
|
|
|
|
2019-01-11 10:59:06 -06:00
|
|
|
pub fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<TreeArc<SourceFile>> {
|
2019-02-20 06:47:32 -06:00
|
|
|
parsing::incremental_reparse(self.syntax(), edit, self.errors())
|
2019-01-07 07:15:47 -06:00
|
|
|
.map(|(green_node, errors)| SourceFile::new(green_node, errors))
|
2018-08-25 05:17:54 -05:00
|
|
|
}
|
2019-01-10 06:54:58 -06:00
|
|
|
|
2019-01-11 10:59:06 -06:00
|
|
|
fn full_reparse(&self, edit: &AtomTextEdit) -> TreeArc<SourceFile> {
|
2019-01-08 12:59:55 -06:00
|
|
|
let text = edit.apply(self.syntax().text().to_string());
|
2019-01-07 07:15:47 -06:00
|
|
|
SourceFile::parse(&text)
|
2018-08-25 03:40:17 -05:00
|
|
|
}
|
2019-01-10 06:54:58 -06:00
|
|
|
|
2018-08-25 03:40:17 -05:00
|
|
|
pub fn errors(&self) -> Vec<SyntaxError> {
|
2018-11-06 13:47:38 -06:00
|
|
|
let mut errors = self.syntax.root_data().clone();
|
2018-11-04 08:06:38 -06:00
|
|
|
errors.extend(validation::validate(self));
|
|
|
|
errors
|
2018-08-25 03:40:17 -05:00
|
|
|
}
|
2018-08-24 11:27:30 -05:00
|
|
|
}
|
2019-02-21 06:51:22 -06:00
|
|
|
|
|
|
|
pub fn check_fuzz_invariants(text: &str) {
|
|
|
|
let file = SourceFile::parse(text);
|
|
|
|
let root = file.syntax();
|
|
|
|
validation::validate_block_structure(root);
|
|
|
|
let _ = file.errors();
|
|
|
|
}
|