2018-01-27 20:29:14 -05:00
|
|
|
//! 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 14:08:06 +03:00
|
|
|
#![forbid(
|
|
|
|
missing_debug_implementations,
|
|
|
|
unconditional_recursion,
|
|
|
|
future_incompatible
|
|
|
|
)]
|
2018-07-29 13:51:55 +03:00
|
|
|
#![deny(bad_style, missing_docs)]
|
|
|
|
#![allow(missing_docs)]
|
2018-01-27 20:29:14 -05:00
|
|
|
//#![warn(unreachable_pub)] // rust-lang/rust#47816
|
|
|
|
|
2018-07-31 15:40:40 +03:00
|
|
|
pub mod algo;
|
|
|
|
pub mod ast;
|
2017-12-29 00:56:36 +03:00
|
|
|
mod lexer;
|
2018-07-31 23:38:19 +03:00
|
|
|
#[macro_use]
|
2018-09-06 21:54:54 +08:00
|
|
|
mod token_set;
|
2018-07-31 23:38:19 +03:00
|
|
|
mod grammar;
|
2018-10-15 17:44:23 -04:00
|
|
|
mod parser_api;
|
2018-07-31 23:38:19 +03:00
|
|
|
mod parser_impl;
|
2018-09-15 13:35:55 +02:00
|
|
|
mod reparsing;
|
2018-11-04 15:06:38 +01:00
|
|
|
mod string_lexing;
|
2018-07-29 15:16:07 +03:00
|
|
|
mod syntax_kinds;
|
2018-07-30 15:25:52 +03:00
|
|
|
/// Utilities for simple uses of the parser.
|
|
|
|
pub mod utils;
|
2018-11-04 15:06:38 +01:00
|
|
|
mod validation;
|
2018-10-15 17:44:23 -04:00
|
|
|
mod yellow;
|
2018-07-29 15:16:07 +03:00
|
|
|
|
2018-12-06 20:49:36 +03:00
|
|
|
pub use rowan::{SmolStr, TextRange, TextUnit};
|
2018-10-15 19:55:32 +03:00
|
|
|
pub use crate::{
|
2018-08-25 11:40:17 +03:00
|
|
|
ast::AstNode,
|
2018-07-30 14:08:06 +03:00
|
|
|
lexer::{tokenize, Token},
|
2018-10-15 17:44:23 -04:00
|
|
|
syntax_kinds::SyntaxKind,
|
2019-01-07 16:15:47 +03:00
|
|
|
yellow::{Direction, SyntaxError, SyntaxNode, WalkEvent, Location, TreePtr},
|
2018-07-29 15:16:07 +03:00
|
|
|
};
|
|
|
|
|
2018-12-11 19:07:17 +01:00
|
|
|
use ra_text_edit::AtomTextEdit;
|
2018-10-15 17:44:23 -04:00
|
|
|
use crate::yellow::GreenNode;
|
2018-08-25 12:10:35 +03:00
|
|
|
|
2019-01-07 16:15:47 +03:00
|
|
|
/// `SourceFile` represents a parse tree for a single Rust file.
|
|
|
|
pub use crate::ast::SourceFile;
|
2018-08-25 11:40:17 +03:00
|
|
|
|
2019-01-07 16:15:47 +03:00
|
|
|
impl SourceFile {
|
|
|
|
fn new(green: GreenNode, errors: Vec<SyntaxError>) -> TreePtr<SourceFile> {
|
2018-10-02 17:07:12 +03:00
|
|
|
let root = SyntaxNode::new(green, errors);
|
2018-09-08 18:34:41 +03:00
|
|
|
if cfg!(debug_assertions) {
|
2019-01-07 16:15:47 +03:00
|
|
|
utils::validate_block_structure(&root);
|
2018-09-08 18:34:41 +03:00
|
|
|
}
|
2018-11-07 18:38:43 +03:00
|
|
|
assert_eq!(root.kind(), SyntaxKind::SOURCE_FILE);
|
2019-01-07 16:15:47 +03:00
|
|
|
TreePtr::cast(root)
|
2018-08-25 11:40:17 +03:00
|
|
|
}
|
2019-01-07 16:15:47 +03:00
|
|
|
pub fn parse(text: &str) -> TreePtr<SourceFile> {
|
2018-08-25 12:10:35 +03:00
|
|
|
let tokens = tokenize(&text);
|
2018-10-15 17:44:23 -04:00
|
|
|
let (green, errors) =
|
|
|
|
parser_impl::parse_with(yellow::GreenBuilder::new(), text, &tokens, grammar::root);
|
2019-01-07 16:15:47 +03:00
|
|
|
SourceFile::new(green, errors)
|
2018-08-25 12:10:35 +03:00
|
|
|
}
|
2019-01-07 16:15:47 +03:00
|
|
|
pub fn reparse(&self, edit: &AtomTextEdit) -> TreePtr<SourceFile> {
|
2018-10-15 17:44:23 -04:00
|
|
|
self.incremental_reparse(edit)
|
|
|
|
.unwrap_or_else(|| self.full_reparse(edit))
|
2018-08-25 13:17:54 +03:00
|
|
|
}
|
2019-01-07 16:15:47 +03:00
|
|
|
pub fn incremental_reparse(&self, edit: &AtomTextEdit) -> Option<TreePtr<SourceFile>> {
|
2018-09-15 13:35:55 +02:00
|
|
|
reparsing::incremental_reparse(self.syntax(), edit, self.errors())
|
2019-01-07 16:15:47 +03:00
|
|
|
.map(|(green_node, errors)| SourceFile::new(green_node, errors))
|
2018-08-25 13:17:54 +03:00
|
|
|
}
|
2019-01-07 16:15:47 +03:00
|
|
|
fn full_reparse(&self, edit: &AtomTextEdit) -> TreePtr<SourceFile> {
|
2019-01-08 21:59:55 +03:00
|
|
|
let text = edit.apply(self.syntax().text().to_string());
|
2019-01-07 16:15:47 +03:00
|
|
|
SourceFile::parse(&text)
|
2018-08-25 11:40:17 +03:00
|
|
|
}
|
|
|
|
pub fn errors(&self) -> Vec<SyntaxError> {
|
2018-11-06 22:47:38 +03:00
|
|
|
let mut errors = self.syntax.root_data().clone();
|
2018-11-04 15:06:38 +01:00
|
|
|
errors.extend(validation::validate(self));
|
|
|
|
errors
|
2018-08-25 11:40:17 +03:00
|
|
|
}
|
2018-08-24 19:27:30 +03:00
|
|
|
}
|