rust/src/lib.rs

76 lines
2.6 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>
#![forbid(missing_debug_implementations, unconditional_recursion, future_incompatible)]
2018-07-29 05:51:55 -05:00
#![deny(bad_style, missing_docs)]
#![allow(missing_docs)]
//#![warn(unreachable_pub)] // rust-lang/rust#47816
2017-12-29 14:33:04 -06:00
extern crate unicode_xid;
2018-07-28 05:07:10 -05:00
extern crate text_unit;
2017-12-29 14:33:04 -06:00
2017-12-28 15:56:36 -06:00
mod tree;
mod lexer;
2017-12-31 14:34:29 -06:00
mod parser;
2018-07-29 05:51:55 -05:00
mod yellow;
2017-12-28 15:56:36 -06:00
pub mod syntax_kinds;
2018-07-28 05:07:10 -05:00
pub use text_unit::{TextRange, TextUnit};
2018-07-29 06:37:48 -05:00
pub use tree::{SyntaxKind, Token};
pub(crate) use tree::{Sink, GreenBuilder};
2017-12-31 08:54:33 -06:00
pub use lexer::{next_token, tokenize};
2018-07-29 05:51:55 -05:00
pub use yellow::SyntaxNode;
pub(crate) use yellow::SError;
2018-07-29 06:37:48 -05:00
pub use parser::{parse_green};
/// Utilities for simple uses of the parser.
pub mod utils {
use std::fmt::Write;
2018-07-29 06:37:48 -05:00
use {SyntaxNode};
2018-07-29 05:51:55 -05:00
use std::collections::BTreeSet;
use SError;
2018-07-29 05:51:55 -05:00
/// Parse a file and create a string representation of the resulting parse tree.
pub fn dump_tree_green(syntax: &SyntaxNode) -> String {
let mut errors: BTreeSet<_> = syntax.root.errors.iter().cloned().collect();
let mut result = String::new();
go(syntax, &mut result, 0, &mut errors);
return result;
fn go(node: &SyntaxNode, buff: &mut String, level: usize, errors: &mut BTreeSet<SError>) {
buff.push_str(&String::from(" ").repeat(level));
write!(buff, "{:?}\n", node).unwrap();
let my_errors: Vec<_> = errors.iter().filter(|e| e.offset == node.range().start())
.cloned().collect();
for err in my_errors {
errors.remove(&err);
buff.push_str(&String::from(" ").repeat(level));
write!(buff, "err: `{}`\n", err.message).unwrap();
}
for child in node.children().iter() {
go(child, buff, level + 1, errors)
}
let my_errors: Vec<_> = errors.iter().filter(|e| e.offset == node.range().end())
.cloned().collect();
for err in my_errors {
errors.remove(&err);
buff.push_str(&String::from(" ").repeat(level));
write!(buff, "err: `{}`\n", err.message).unwrap();
}
}
}
}