]> git.lizzy.rs Git - rust.git/blob - src/lib.rs
87a9d11eaf5af4b31eeac7a4f890fd64e7cf6258
[rust.git] / src / lib.rs
1 //! An experimental implementation of [Rust RFC#2256 libsyntax2.0][rfc#2256].
2 //!
3 //! The intent is to be an IDE-ready parser, i.e. one that offers
4 //!
5 //! - easy and fast incremental re-parsing,
6 //! - graceful handling of errors, and
7 //! - maintains all information in the source file.
8 //!
9 //! For more information, see [the RFC][rfc#2265], or [the working draft][RFC.md].
10 //!
11 //!   [rfc#2256]: <https://github.com/rust-lang/rfcs/pull/2256>
12 //!   [RFC.md]: <https://github.com/matklad/libsyntax2/blob/master/docs/RFC.md>
13
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
17
18 extern crate unicode_xid;
19
20 mod text;
21 mod tree;
22 mod lexer;
23 mod parser;
24
25 #[cfg_attr(rustfmt, rustfmt_skip)]
26 #[allow(missing_docs)]
27 pub mod syntax_kinds;
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;
32
33 /// Utilities for simple uses of the parser.
34 pub mod utils {
35     use std::fmt::Write;
36
37     use {File, Node};
38
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);
43         return result;
44
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()
50                 .into_iter()
51                 .flat_map(|n| n.errors())
52                 .filter(|e| e.after_child() == Some(node));
53
54             for err in my_errors {
55                 buff.push_str(&String::from("  ").repeat(level));
56                 write!(buff, "err: `{}`\n", err.message()).unwrap();
57             }
58
59             for child in node.children() {
60                 go(child, buff, level + 1)
61             }
62
63             for err in parent_errors {
64                 buff.push_str(&String::from("  ").repeat(level));
65                 write!(buff, "err: `{}`\n", err.message()).unwrap();
66             }
67         }
68     }
69 }