2019-02-20 12:47:32 +00:00
|
|
|
#[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;
|
2019-02-20 12:47:32 +00:00
|
|
|
mod grammar;
|
2019-02-20 13:24:39 +00:00
|
|
|
mod reparsing;
|
2019-02-20 12:47:32 +00:00
|
|
|
|
|
|
|
use crate::{
|
2019-02-21 09:03:42 +00:00
|
|
|
SyntaxKind, SyntaxError,
|
2019-02-20 19:52:32 +00:00
|
|
|
parsing::{
|
2019-02-21 09:03:42 +00:00
|
|
|
builder::TreeBuilder,
|
2019-02-20 19:52:32 +00:00
|
|
|
input::ParserInput,
|
2019-02-21 09:03:42 +00:00
|
|
|
event::process,
|
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,
|
2019-02-20 12:47:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2019-02-20 12:47:32 +00:00
|
|
|
pub(crate) use self::reparsing::incremental_reparse;
|
|
|
|
|
|
|
|
pub(crate) fn parse_text(text: &str) -> (GreenNode, Vec<SyntaxError>) {
|
|
|
|
let tokens = tokenize(&text);
|
2019-02-21 09:03:42 +00:00
|
|
|
let tree_sink = TreeBuilder::new(text, &tokens);
|
|
|
|
parse_with(tree_sink, text, &tokens, grammar::root)
|
2019-02-20 19:52:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_with<S: TreeSink>(
|
2019-02-21 09:03:42 +00:00
|
|
|
mut tree_sink: S,
|
2019-02-20 19:52:32 +00:00
|
|
|
text: &str,
|
|
|
|
tokens: &[Token],
|
|
|
|
f: fn(&mut Parser),
|
|
|
|
) -> S::Tree {
|
2019-02-21 09:03:42 +00:00
|
|
|
let events = {
|
2019-02-20 19:52:32 +00:00
|
|
|
let input = ParserInput::new(text, &tokens);
|
|
|
|
let mut p = Parser::new(&input);
|
|
|
|
f(&mut p);
|
|
|
|
p.finish()
|
|
|
|
};
|
2019-02-21 09:03:42 +00:00
|
|
|
process(&mut tree_sink, events);
|
|
|
|
tree_sink.finish()
|
2019-02-20 12:47:32 +00:00
|
|
|
}
|
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.
|
2019-02-21 09:03:42 +00:00
|
|
|
fn leaf(&mut self, kind: SyntaxKind, n_tokens: u8);
|
2019-02-20 19:19:12 +00:00
|
|
|
|
|
|
|
/// Start new branch and make it current.
|
2019-02-21 09:03:42 +00:00
|
|
|
fn start_branch(&mut self, kind: SyntaxKind, root: bool);
|
2019-02-20 19:19:12 +00:00
|
|
|
|
|
|
|
/// Finish current branch and restore previous
|
|
|
|
/// branch as current.
|
2019-02-21 09:03:42 +00:00
|
|
|
fn finish_branch(&mut self, root: bool);
|
2019-02-20 19:19:12 +00:00
|
|
|
|
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
|
|
|
}
|