rust/crates/libsyntax2/src/lib.rs

84 lines
2.3 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 13:08:06 +02:00
#![forbid(
missing_debug_implementations,
unconditional_recursion,
future_incompatible
)]
2018-07-29 12:51:55 +02:00
#![deny(bad_style, missing_docs)]
#![allow(missing_docs)]
//#![warn(unreachable_pub)] // rust-lang/rust#47816
2018-07-31 14:40:40 +02:00
extern crate itertools;
2018-07-30 13:08:06 +02:00
extern crate unicode_xid;
2018-08-01 13:55:37 +02:00
extern crate drop_bomb;
2018-08-01 21:07:09 +02:00
extern crate parking_lot;
2018-08-13 13:24:22 +02:00
extern crate smol_str;
extern crate text_unit;
2017-12-29 21:33:04 +01:00
2018-07-31 14:40:40 +02:00
pub mod algo;
pub mod ast;
2017-12-28 22:56:36 +01:00
mod lexer;
2018-07-31 22:38:19 +02:00
#[macro_use]
mod parser_api;
mod grammar;
mod parser_impl;
2018-07-29 14:16:07 +02:00
mod syntax_kinds;
2018-08-08 18:44:16 +02:00
mod yellow;
2018-07-30 14:25:52 +02:00
/// Utilities for simple uses of the parser.
pub mod utils;
2018-08-24 12:41:25 +02:00
pub mod text_utils;
2018-07-29 14:16:07 +02:00
pub use {
2018-08-18 11:42:28 +02:00
text_unit::{TextRange, TextUnit},
smol_str::SmolStr,
2018-08-17 21:00:13 +02:00
ast::{AstNode, ParsedFile},
2018-07-30 13:08:06 +02:00
lexer::{tokenize, Token},
2018-07-29 14:16:07 +02:00
syntax_kinds::SyntaxKind,
2018-08-17 20:10:55 +02:00
yellow::{SyntaxNode, SyntaxNodeRef, OwnedRoot, RefRoot, TreeRoot, SyntaxError},
2018-07-29 14:16:07 +02:00
};
2018-08-01 09:51:42 +02:00
2018-08-01 09:40:07 +02:00
pub fn parse(text: &str) -> SyntaxNode {
2018-07-29 14:16:07 +02:00
let tokens = tokenize(&text);
2018-08-24 18:27:30 +02:00
let res = parser_impl::parse::<yellow::GreenBuilder>(text, &tokens);
validate_block_structure(res.borrowed());
res
}
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() {
assert_eq!(node.parent(), pair.parent());
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 14:16:07 +02:00
}