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>
|
|
|
|
|
|
|
|
#![forbid(missing_debug_implementations, unconditional_recursion, future_incompatible)]
|
|
|
|
#![deny(bad_style, unsafe_code, missing_docs)]
|
|
|
|
//#![warn(unreachable_pub)] // rust-lang/rust#47816
|
|
|
|
|
2017-12-29 14:33:04 -06:00
|
|
|
extern crate unicode_xid;
|
|
|
|
|
2017-12-28 15:56:36 -06:00
|
|
|
mod text;
|
|
|
|
mod tree;
|
|
|
|
mod lexer;
|
2017-12-31 14:34:29 -06:00
|
|
|
mod parser;
|
2017-12-28 15:56:36 -06:00
|
|
|
|
|
|
|
pub mod syntax_kinds;
|
2018-01-27 17:31:23 -06:00
|
|
|
pub use text::{TextRange, TextUnit};
|
2018-01-28 02:18:17 -06:00
|
|
|
pub use tree::{File, Node, SyntaxKind, Token};
|
2018-02-09 13:44:50 -06:00
|
|
|
pub(crate) use tree::{FileBuilder, Sink, ErrorMsg};
|
2017-12-31 08:54:33 -06:00
|
|
|
pub use lexer::{next_token, tokenize};
|
2017-12-31 14:34:29 -06:00
|
|
|
pub use parser::parse;
|
2018-01-21 17:12:26 -06:00
|
|
|
|
2018-01-27 19:29:14 -06:00
|
|
|
/// Utilities for simple uses of the parser.
|
2018-01-21 17:12:26 -06:00
|
|
|
pub mod utils {
|
|
|
|
use std::fmt::Write;
|
|
|
|
|
|
|
|
use {File, Node};
|
|
|
|
|
2018-01-27 19:29:14 -06:00
|
|
|
/// Parse a file and create a string representation of the resulting parse tree.
|
2018-01-21 17:12:26 -06:00
|
|
|
pub fn dump_tree(file: &File) -> String {
|
|
|
|
let mut result = String::new();
|
|
|
|
go(file.root(), &mut result, 0);
|
|
|
|
return result;
|
|
|
|
|
|
|
|
fn go(node: Node, buff: &mut String, level: usize) {
|
|
|
|
buff.push_str(&String::from(" ").repeat(level));
|
|
|
|
write!(buff, "{:?}\n", node).unwrap();
|
|
|
|
let my_errors = node.errors().filter(|e| e.after_child().is_none());
|
2018-01-27 17:31:23 -06:00
|
|
|
let parent_errors = node.parent()
|
|
|
|
.into_iter()
|
2018-01-21 17:12:26 -06:00
|
|
|
.flat_map(|n| n.errors())
|
|
|
|
.filter(|e| e.after_child() == Some(node));
|
|
|
|
|
|
|
|
for err in my_errors {
|
|
|
|
buff.push_str(&String::from(" ").repeat(level));
|
|
|
|
write!(buff, "err: `{}`\n", err.message()).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
for child in node.children() {
|
|
|
|
go(child, buff, level + 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
for err in parent_errors {
|
|
|
|
buff.push_str(&String::from(" ").repeat(level));
|
|
|
|
write!(buff, "err: `{}`\n", err.message()).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|