1 //! An experimental implementation of [Rust RFC#2256 libsyntax2.0][rfc#2256].
3 //! The intent is to be an IDE-ready parser, i.e. one that offers
5 //! - easy and fast incremental re-parsing,
6 //! - graceful handling of errors, and
7 //! - maintains all information in the source file.
9 //! For more information, see [the RFC][rfc#2265], or [the working draft][RFC.md].
11 //! [rfc#2256]: <https://github.com/rust-lang/rfcs/pull/2256>
12 //! [RFC.md]: <https://github.com/matklad/libsyntax2/blob/master/docs/RFC.md>
14 #![forbid(missing_debug_implementations, unconditional_recursion, future_incompatible)]
15 #![deny(bad_style, unsafe_code, missing_docs)]
16 //#![warn(unreachable_pub)] // rust-lang/rust#47816
18 extern crate unicode_xid;
25 #[cfg_attr(rustfmt, rustfmt_skip)]
26 #[allow(missing_docs)]
28 pub use text::{TextRange, TextUnit};
29 pub use tree::{File, FileBuilder, Node, Sink, SyntaxKind, Token};
30 pub use lexer::{next_token, tokenize};
31 pub use parser::parse;
33 /// Utilities for simple uses of the parser.
39 /// Parse a file and create a string representation of the resulting parse tree.
40 pub fn dump_tree(file: &File) -> String {
41 let mut result = String::new();
42 go(file.root(), &mut result, 0);
45 fn go(node: Node, buff: &mut String, level: usize) {
46 buff.push_str(&String::from(" ").repeat(level));
47 write!(buff, "{:?}\n", node).unwrap();
48 let my_errors = node.errors().filter(|e| e.after_child().is_none());
49 let parent_errors = node.parent()
51 .flat_map(|n| n.errors())
52 .filter(|e| e.after_child() == Some(node));
54 for err in my_errors {
55 buff.push_str(&String::from(" ").repeat(level));
56 write!(buff, "err: `{}`\n", err.message()).unwrap();
59 for child in node.children() {
60 go(child, buff, level + 1)
63 for err in parent_errors {
64 buff.push_str(&String::from(" ").repeat(level));
65 write!(buff, "err: `{}`\n", err.message()).unwrap();