rust/src/lib.rs

95 lines
2.9 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-28 12:07:10 +02:00
extern crate text_unit;
2018-07-30 13:08:06 +02:00
extern crate unicode_xid;
2017-12-29 21:33:04 +01:00
2017-12-28 22:56:36 +01:00
mod lexer;
2017-12-31 21:34:29 +01:00
mod parser;
2018-07-29 14:16:07 +02:00
mod syntax_kinds;
2018-07-30 13:08:06 +02:00
mod yellow;
2018-07-29 14:16:07 +02:00
pub use {
2018-07-30 13:08:06 +02:00
lexer::{tokenize, Token},
2018-07-29 14:16:07 +02:00
syntax_kinds::SyntaxKind,
2018-07-30 13:08:06 +02:00
text_unit::{TextRange, TextUnit},
yellow::{SyntaxNode, SyntaxNodeRef},
2018-07-29 14:16:07 +02:00
};
2018-07-30 13:08:06 +02:00
pub(crate) use yellow::SyntaxError;
2018-07-29 14:16:07 +02:00
pub fn parse(text: String) -> SyntaxNode {
let tokens = tokenize(&text);
parser::parse::<yellow::GreenBuilder>(text, &tokens)
}
2017-12-28 22:56:36 +01:00
/// Utilities for simple uses of the parser.
pub mod utils {
2018-07-30 13:08:06 +02:00
use std::{collections::BTreeSet, fmt::Write};
2018-07-30 13:08:06 +02:00
use {SyntaxError, SyntaxNode, SyntaxNodeRef};
2018-07-29 12:51:55 +02:00
/// Parse a file and create a string representation of the resulting parse tree.
pub fn dump_tree_green(syntax: &SyntaxNode) -> String {
let syntax = syntax.borrow();
2018-07-29 12:51:55 +02:00
let mut errors: BTreeSet<_> = syntax.root.errors.iter().cloned().collect();
let mut result = String::new();
go(syntax, &mut result, 0, &mut errors);
return result;
2018-07-30 13:08:06 +02:00
fn go(
node: SyntaxNodeRef,
buff: &mut String,
level: usize,
errors: &mut BTreeSet<SyntaxError>,
) {
2018-07-29 12:51:55 +02:00
buff.push_str(&String::from(" ").repeat(level));
write!(buff, "{:?}\n", node).unwrap();
2018-07-30 13:08:06 +02:00
let my_errors: Vec<_> = errors
.iter()
.filter(|e| e.offset == node.range().start())
.cloned()
.collect();
2018-07-29 12:51:55 +02:00
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() {
2018-07-29 12:51:55 +02:00
go(child, buff, level + 1, errors)
}
2018-07-30 13:08:06 +02:00
let my_errors: Vec<_> = errors
.iter()
.filter(|e| e.offset == node.range().end())
.cloned()
.collect();
2018-07-29 12:51:55 +02:00
for err in my_errors {
errors.remove(&err);
buff.push_str(&String::from(" ").repeat(level));
write!(buff, "err: `{}`\n", err.message).unwrap();
}
}
}
}