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-07-31 07:40:40 -05:00
|
|
|
extern crate itertools;
|
2018-07-30 06:08:06 -05:00
|
|
|
extern crate unicode_xid;
|
2018-08-01 06:55:37 -05:00
|
|
|
extern crate drop_bomb;
|
2018-08-01 14:07:09 -05:00
|
|
|
extern crate parking_lot;
|
2018-08-13 06:24:22 -05:00
|
|
|
extern crate smol_str;
|
|
|
|
extern crate text_unit;
|
2017-12-29 14:33:04 -06:00
|
|
|
|
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]
|
|
|
|
mod parser_api;
|
|
|
|
mod grammar;
|
|
|
|
mod parser_impl;
|
|
|
|
|
2018-07-29 07:16:07 -05:00
|
|
|
mod syntax_kinds;
|
2018-08-08 11:44:16 -05:00
|
|
|
mod yellow;
|
2018-07-30 07:25:52 -05:00
|
|
|
/// Utilities for simple uses of the parser.
|
|
|
|
pub mod utils;
|
2018-08-24 05:41:25 -05:00
|
|
|
pub mod text_utils;
|
2018-07-29 07:16:07 -05:00
|
|
|
|
|
|
|
pub use {
|
2018-08-18 04:42:28 -05:00
|
|
|
text_unit::{TextRange, TextUnit},
|
|
|
|
smol_str::SmolStr,
|
2018-08-25 03:40:17 -05:00
|
|
|
ast::AstNode,
|
2018-07-30 06:08:06 -05:00
|
|
|
lexer::{tokenize, Token},
|
2018-07-29 07:16:07 -05:00
|
|
|
syntax_kinds::SyntaxKind,
|
2018-08-17 13:10:55 -05:00
|
|
|
yellow::{SyntaxNode, SyntaxNodeRef, OwnedRoot, RefRoot, TreeRoot, SyntaxError},
|
2018-07-29 07:16:07 -05:00
|
|
|
};
|
|
|
|
|
2018-08-25 03:40:17 -05:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct ParsedFile {
|
|
|
|
root: SyntaxNode
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ParsedFile {
|
|
|
|
pub fn parse(text: &str) -> Self {
|
|
|
|
let root = ::parse(text);
|
|
|
|
ParsedFile { root }
|
|
|
|
}
|
|
|
|
pub fn ast(&self) -> ast::File {
|
|
|
|
ast::File::cast(self.syntax()).unwrap()
|
|
|
|
}
|
|
|
|
pub fn syntax(&self) -> SyntaxNodeRef {
|
|
|
|
self.root.borrowed()
|
|
|
|
}
|
|
|
|
pub fn errors(&self) -> Vec<SyntaxError> {
|
|
|
|
self.syntax().root.syntax_root().errors.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-08-01 02:51:42 -05:00
|
|
|
|
2018-08-01 02:40:07 -05:00
|
|
|
pub fn parse(text: &str) -> SyntaxNode {
|
2018-07-29 07:16:07 -05:00
|
|
|
let tokens = tokenize(&text);
|
2018-08-24 11:27:30 -05:00
|
|
|
let res = parser_impl::parse::<yellow::GreenBuilder>(text, &tokens);
|
|
|
|
validate_block_structure(res.borrowed());
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2018-08-24 12:50:37 -05:00
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
fn validate_block_structure(_: SyntaxNodeRef) {}
|
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
2018-08-24 11:27:30 -05:00
|
|
|
fn validate_block_structure(root: SyntaxNodeRef) {
|
|
|
|
let mut stack = Vec::new();
|
|
|
|
for node in algo::walk::preorder(root) {
|
|
|
|
match node.kind() {
|
|
|
|
SyntaxKind::L_CURLY => {
|
|
|
|
stack.push(node)
|
|
|
|
}
|
|
|
|
SyntaxKind::R_CURLY => {
|
|
|
|
if let Some(pair) = stack.pop() {
|
2018-08-24 12:50:37 -05:00
|
|
|
assert_eq!(
|
|
|
|
node.parent(),
|
|
|
|
pair.parent(),
|
|
|
|
"unpaired curleys:\n{}",
|
|
|
|
utils::dump_tree(root),
|
|
|
|
);
|
2018-08-24 11:27:30 -05:00
|
|
|
assert!(
|
|
|
|
node.next_sibling().is_none() && pair.prev_sibling().is_none(),
|
|
|
|
"floating curlys at {:?}\nfile:\n{}\nerror:\n{}\n",
|
|
|
|
node,
|
|
|
|
root.text(),
|
|
|
|
node.text(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
2018-07-29 07:16:07 -05:00
|
|
|
}
|