2018-01-27 19:29:14 -06: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 06:08:06 -05:00
|
|
|
#![forbid(
|
|
|
|
missing_debug_implementations,
|
|
|
|
unconditional_recursion,
|
|
|
|
future_incompatible
|
|
|
|
)]
|
2018-07-29 05:51:55 -05:00
|
|
|
#![deny(bad_style, missing_docs)]
|
|
|
|
#![allow(missing_docs)]
|
2018-01-27 19:29:14 -06:00
|
|
|
//#![warn(unreachable_pub)] // rust-lang/rust#47816
|
|
|
|
|
2018-11-07 04:58:34 -06:00
|
|
|
extern crate arrayvec;
|
2018-08-01 06:55:37 -05:00
|
|
|
extern crate drop_bomb;
|
2018-10-15 16:44:23 -05:00
|
|
|
extern crate itertools;
|
2018-08-01 14:07:09 -05:00
|
|
|
extern crate parking_lot;
|
2018-10-02 09:07:12 -05:00
|
|
|
extern crate rowan;
|
2018-10-15 16:44:23 -05:00
|
|
|
extern crate unicode_xid;
|
2017-12-29 14:33:04 -06:00
|
|
|
|
2018-09-15 07:35:30 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
#[macro_use]
|
|
|
|
extern crate test_utils;
|
|
|
|
|
2018-07-31 07:40:40 -05:00
|
|
|
pub mod algo;
|
|
|
|
pub mod ast;
|
2017-12-28 15:56:36 -06:00
|
|
|
mod lexer;
|
2018-07-31 15:38:19 -05:00
|
|
|
#[macro_use]
|
2018-09-06 08:54:54 -05:00
|
|
|
mod token_set;
|
2018-07-31 15:38:19 -05:00
|
|
|
mod grammar;
|
2018-10-15 16:44:23 -05:00
|
|
|
mod parser_api;
|
2018-07-31 15:38:19 -05:00
|
|
|
mod parser_impl;
|
2018-09-15 06:35:55 -05:00
|
|
|
mod reparsing;
|
2018-11-04 08:06:38 -06:00
|
|
|
mod string_lexing;
|
2018-07-29 07:16:07 -05:00
|
|
|
mod syntax_kinds;
|
2018-10-15 16:44:23 -05:00
|
|
|
pub mod text_utils;
|
2018-07-30 07:25:52 -05:00
|
|
|
/// Utilities for simple uses of the parser.
|
|
|
|
pub mod utils;
|
2018-11-04 08:06:38 -06:00
|
|
|
mod validation;
|
2018-10-15 16:44:23 -05:00
|
|
|
mod yellow;
|
2018-07-29 07:16:07 -05:00
|
|
|
|
2018-10-15 11:55:32 -05:00
|
|
|
pub use crate::{
|
2018-08-25 03:40:17 -05:00
|
|
|
ast::AstNode,
|
2018-07-30 06:08:06 -05:00
|
|
|
lexer::{tokenize, Token},
|
2018-09-15 06:35:55 -05:00
|
|
|
reparsing::AtomEdit,
|
2018-10-15 16:44:23 -05:00
|
|
|
rowan::{SmolStr, TextRange, TextUnit},
|
|
|
|
syntax_kinds::SyntaxKind,
|
2018-10-31 15:41:43 -05:00
|
|
|
yellow::{
|
2018-11-05 11:38:34 -06:00
|
|
|
Direction, OwnedRoot, RefRoot, SyntaxError, SyntaxNode, SyntaxNodeRef, TreeRoot, WalkEvent, Location,
|
2018-10-31 15:41:43 -05:00
|
|
|
},
|
2018-07-29 07:16:07 -05:00
|
|
|
};
|
|
|
|
|
2018-10-15 16:44:23 -05:00
|
|
|
use crate::yellow::GreenNode;
|
2018-08-25 04:10:35 -05:00
|
|
|
|
2018-11-07 09:38:43 -06:00
|
|
|
/// `SourceFileNode` represents a parse tree for a single Rust file.
|
|
|
|
pub use crate::ast::SourceFileNode;
|
2018-08-25 03:40:17 -05:00
|
|
|
|
2018-11-07 09:32:33 -06:00
|
|
|
impl SourceFileNode {
|
|
|
|
fn new(green: GreenNode, errors: Vec<SyntaxError>) -> SourceFileNode {
|
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) {
|
|
|
|
utils::validate_block_structure(root.borrowed());
|
|
|
|
}
|
2018-11-07 09:38:43 -06:00
|
|
|
assert_eq!(root.kind(), SyntaxKind::SOURCE_FILE);
|
|
|
|
ast::SourceFileNode { syntax: root }
|
2018-08-25 03:40:17 -05:00
|
|
|
}
|
2018-11-07 09:32:33 -06:00
|
|
|
pub fn parse(text: &str) -> SourceFileNode {
|
2018-08-25 04:10:35 -05:00
|
|
|
let tokens = tokenize(&text);
|
2018-10-15 16:44:23 -05:00
|
|
|
let (green, errors) =
|
|
|
|
parser_impl::parse_with(yellow::GreenBuilder::new(), text, &tokens, grammar::root);
|
2018-11-07 09:32:33 -06:00
|
|
|
SourceFileNode::new(green, errors)
|
2018-08-25 04:10:35 -05:00
|
|
|
}
|
2018-11-07 09:32:33 -06:00
|
|
|
pub fn reparse(&self, edit: &AtomEdit) -> SourceFileNode {
|
2018-10-15 16:44:23 -05:00
|
|
|
self.incremental_reparse(edit)
|
|
|
|
.unwrap_or_else(|| self.full_reparse(edit))
|
2018-08-25 05:17:54 -05:00
|
|
|
}
|
2018-11-07 09:32:33 -06:00
|
|
|
pub fn incremental_reparse(&self, edit: &AtomEdit) -> Option<SourceFileNode> {
|
2018-09-15 06:35:55 -05:00
|
|
|
reparsing::incremental_reparse(self.syntax(), edit, self.errors())
|
2018-11-07 09:32:33 -06:00
|
|
|
.map(|(green_node, errors)| SourceFileNode::new(green_node, errors))
|
2018-08-25 05:17:54 -05:00
|
|
|
}
|
2018-11-07 09:32:33 -06:00
|
|
|
fn full_reparse(&self, edit: &AtomEdit) -> SourceFileNode {
|
2018-10-15 16:44:23 -05:00
|
|
|
let text =
|
|
|
|
text_utils::replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert);
|
2018-11-07 09:32:33 -06:00
|
|
|
SourceFileNode::parse(&text)
|
2018-08-25 05:17:54 -05:00
|
|
|
}
|
2018-11-05 06:47:59 -06:00
|
|
|
/// Typed AST representation of the parse tree.
|
2018-11-07 09:38:43 -06:00
|
|
|
pub fn ast(&self) -> ast::SourceFile {
|
2018-11-06 13:47:38 -06:00
|
|
|
self.borrowed()
|
2018-08-25 03:40:17 -05:00
|
|
|
}
|
2018-11-05 06:47:59 -06:00
|
|
|
/// Untyped homogeneous representation of the parse tree.
|
2018-08-25 03:40:17 -05:00
|
|
|
pub fn syntax(&self) -> SyntaxNodeRef {
|
2018-11-06 13:47:38 -06:00
|
|
|
self.syntax.borrowed()
|
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
|
|
|
}
|