rust-analyzer/crates/ra_syntax/src/parsing.rs

79 lines
2 KiB
Rust
Raw Normal View History

#[macro_use]
mod token_set;
mod builder;
mod lexer;
2019-02-20 19:52:32 +00:00
mod event;
mod input;
2019-02-20 20:05:59 +00:00
mod parser;
mod grammar;
mod reparsing;
use crate::{
2019-02-20 20:17:07 +00:00
SyntaxKind, SmolStr, SyntaxError,
2019-02-20 19:52:32 +00:00
parsing::{
builder::GreenBuilder,
input::ParserInput,
event::EventProcessor,
2019-02-20 20:05:59 +00:00
parser::Parser,
2019-02-20 19:52:32 +00:00
},
2019-02-20 13:16:14 +00:00
syntax_node::GreenNode,
};
pub use self::lexer::{tokenize, Token};
2019-02-20 20:17:07 +00:00
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ParseError(pub String);
pub(crate) use self::reparsing::incremental_reparse;
pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) {
let tokens = tokenize(&text);
2019-02-20 20:17:07 +00:00
parse_with(GreenBuilder::default(), text, &tokens, grammar::root)
2019-02-20 19:52:32 +00:00
}
fn parse_with<S: TreeSink>(
tree_sink: S,
text: &str,
tokens: &[Token],
f: fn(&mut Parser),
) -> S::Tree {
let mut events = {
let input = ParserInput::new(text, &tokens);
let mut p = Parser::new(&input);
f(&mut p);
p.finish()
};
EventProcessor::new(tree_sink, text, tokens, &mut events).process().finish()
}
2019-02-20 19:19:12 +00:00
/// `TreeSink` abstracts details of a particular syntax tree implementation.
trait TreeSink {
type Tree;
/// Adds new leaf to the current branch.
fn leaf(&mut self, kind: SyntaxKind, text: SmolStr);
/// Start new branch and make it current.
fn start_branch(&mut self, kind: SyntaxKind);
/// Finish current branch and restore previous
/// branch as current.
fn finish_branch(&mut self);
2019-02-20 20:17:07 +00:00
fn error(&mut self, error: ParseError);
2019-02-20 19:19:12 +00:00
/// Complete tree building. Make sure that
/// `start_branch` and `finish_branch` calls
/// are paired!
fn finish(self) -> Self::Tree;
}
/// `TokenSource` abstracts the source of the tokens parser operates one.
///
/// Hopefully this will allow us to treat text and token trees in the same way!
trait TokenSource {
2019-02-20 19:58:56 +00:00
fn token_kind(&self, pos: usize) -> SyntaxKind;
fn is_token_joint_to_next(&self, pos: usize) -> bool;
fn is_keyword(&self, pos: usize, kw: &str) -> bool;
2019-02-20 19:19:12 +00:00
}