rust/src/parser/mod.rs

59 lines
1.3 KiB
Rust
Raw Normal View History

2018-02-04 11:53:47 +01:00
use {File, SyntaxKind, Token};
2017-12-31 15:54:33 +01:00
2017-12-31 21:34:29 +01:00
use syntax_kinds::*;
2018-02-04 11:53:47 +01:00
#[macro_use]
mod parser;
mod input;
2018-02-04 11:53:47 +01:00
mod event;
mod grammar;
use self::event::Event;
2018-01-01 13:10:56 +01:00
/// Parse a sequence of tokens into the representative node tree
2017-12-31 21:34:29 +01:00
pub fn parse(text: String, tokens: &[Token]) -> File {
2018-02-04 11:56:51 +01:00
let events = {
let input = input::ParserInput::new(&text, tokens);
let mut parser = parser::Parser::new(&input);
2018-02-04 11:56:51 +01:00
grammar::file(&mut parser);
parser.into_events()
};
2018-02-04 11:53:47 +01:00
event::to_file(text, tokens, events)
2018-01-01 13:10:56 +01:00
}
2018-01-01 20:13:04 +01:00
fn is_insignificant(kind: SyntaxKind) -> bool {
match kind {
WHITESPACE | COMMENT => true,
_ => false,
}
2018-01-08 20:40:14 +01:00
}
2018-02-11 11:13:06 +01:00
impl<'p> parser::Parser<'p> {
fn at(&self, kind: SyntaxKind) -> bool {
self.current() == kind
}
fn err_and_bump(&mut self, message: &str) {
let err = self.start();
self.error(message);
self.bump();
err.complete(self, ERROR);
}
fn expect(&mut self, kind: SyntaxKind) -> bool {
if self.at(kind) {
self.bump();
true
} else {
self.error(format!("expected {:?}", kind));
false
}
}
fn eat(&mut self, kind: SyntaxKind) -> bool {
self.at(kind) && {
self.bump();
true
}
}
}