rust/src/parser/event_parser/grammar.rs

167 lines
3.4 KiB
Rust
Raw Normal View History

2018-01-01 07:05:46 -06:00
use super::parser::Parser;
2018-01-06 12:54:55 -06:00
use {SyntaxKind};
2018-01-01 07:05:46 -06:00
use syntax_kinds::*;
2018-01-01 09:58:46 -06:00
// Items //
2018-01-07 11:14:26 -06:00
pub(crate) fn file(p: &mut Parser) {
2018-01-06 12:54:55 -06:00
node(p, FILE, |p| {
2018-01-07 06:34:11 -06:00
p.optional(SHEBANG);
2018-01-06 12:54:55 -06:00
inner_attributes(p);
2018-01-07 05:56:08 -06:00
many(p, |p| {
skip_to_first(
p, item_first, item,
"expected item",
)
});
2018-01-06 12:54:55 -06:00
})
2018-01-01 09:58:46 -06:00
}
2018-01-06 12:54:55 -06:00
fn item_first(p: &Parser) -> bool {
2018-01-07 07:01:30 -06:00
let current = match p.current() {
Some(c) => c,
None => return false,
};
match current {
STRUCT_KW | FN_KW => true,
2018-01-06 12:54:55 -06:00
_ => false,
2018-01-01 09:58:46 -06:00
}
}
2018-01-06 12:54:55 -06:00
fn item(p: &mut Parser) {
outer_attributes(p);
visibility(p);
2018-01-07 07:01:30 -06:00
node_if(p, STRUCT_KW, STRUCT_ITEM, struct_item)
|| node_if(p, FN_KW, FN_ITEM, fn_item);
2018-01-01 09:58:46 -06:00
}
2018-01-06 12:54:55 -06:00
fn struct_item(p: &mut Parser) {
p.expect(IDENT)
&& p.curly_block(|p| comma_list(p, struct_field));
2018-01-06 08:16:00 -06:00
}
2018-01-06 12:54:55 -06:00
fn struct_field(p: &mut Parser) -> bool {
node_if(p, IDENT, STRUCT_FIELD, |p| {
p.expect(COLON) && p.expect(IDENT);
})
2018-01-01 14:22:01 -06:00
}
2018-01-01 09:58:46 -06:00
2018-01-07 07:01:30 -06:00
fn fn_item(p: &mut Parser) {
p.expect(IDENT) && p.expect(L_PAREN) && p.expect(R_PAREN)
&& p.curly_block(|p| ());
}
2018-01-01 09:58:46 -06:00
// Paths, types, attributes, and stuff //
2018-01-07 10:50:54 -06:00
fn inner_attributes(p: &mut Parser) {
many(p, inner_attribute)
}
fn inner_attribute(p: &mut Parser) -> bool {
if !(p.lookahead(&[EXCL, POUND])) {
return false;
}
node(p, ATTR, |p| {
p.bump_n(2);
});
true
}
2018-01-06 12:54:55 -06:00
fn outer_attributes(_: &mut Parser) {
2018-01-01 09:58:46 -06:00
}
2018-01-06 12:54:55 -06:00
fn visibility(_: &mut Parser) {
2018-01-01 09:58:46 -06:00
}
// Expressions //
// Error recovery and high-order utils //
2018-01-06 12:54:55 -06:00
fn node_if<F: FnOnce(&mut Parser)>(p: &mut Parser, first: SyntaxKind, node_kind: SyntaxKind, rest: F) -> bool {
p.current_is(first) && { node(p, node_kind, |p| { p.bump(); rest(p); }); true }
2018-01-01 09:58:46 -06:00
}
2018-01-06 12:54:55 -06:00
fn node<F: FnOnce(&mut Parser)>(p: &mut Parser, node_kind: SyntaxKind, rest: F) {
p.start(node_kind);
rest(p);
2018-01-01 07:05:46 -06:00
p.finish();
2018-01-06 08:16:00 -06:00
}
2018-01-06 12:54:55 -06:00
fn many<F: Fn(&mut Parser) -> bool>(p: &mut Parser, f: F) {
while f(p) { }
2018-01-06 08:16:00 -06:00
}
2018-01-06 12:54:55 -06:00
fn comma_list<F: Fn(&mut Parser) -> bool>(p: &mut Parser, f: F) {
many(p, |p| {
f(p);
2018-01-07 03:32:29 -06:00
if p.is_eof() {
false
} else {
p.expect(COMMA);
true
}
2018-01-06 12:54:55 -06:00
})
}
2018-01-07 05:56:08 -06:00
fn skip_to_first<C, F>(p: &mut Parser, cond: C, f: F, message: &str) -> bool
2018-01-06 12:54:55 -06:00
where
C: Fn(&Parser) -> bool,
F: FnOnce(&mut Parser),
{
2018-01-07 05:56:08 -06:00
let mut skipped = false;
2018-01-06 08:16:00 -06:00
loop {
2018-01-06 12:54:55 -06:00
if cond(p) {
2018-01-07 05:56:08 -06:00
if skipped {
p.finish();
}
2018-01-06 12:54:55 -06:00
f(p);
return true;
2018-01-06 08:16:00 -06:00
}
2018-01-07 05:56:08 -06:00
if p.is_eof() {
if skipped {
p.finish();
}
2018-01-06 12:54:55 -06:00
return false;
2018-01-06 08:16:00 -06:00
}
2018-01-07 05:56:08 -06:00
if !skipped {
p.start(ERROR);
p.error()
.message(message)
.emit();
}
p.bump().unwrap();
skipped = true;
2018-01-06 08:16:00 -06:00
}
2018-01-06 12:54:55 -06:00
}
impl<'p> Parser<'p> {
fn current_is(&self, kind: SyntaxKind) -> bool {
self.current() == Some(kind)
}
pub(crate) fn expect(&mut self, kind: SyntaxKind) -> bool {
2018-01-07 03:13:01 -06:00
if self.current_is(kind) {
self.bump();
true
} else {
self.error()
.message(format!("expected {:?}", kind))
.emit();
false
}
2018-01-06 12:54:55 -06:00
}
2018-01-07 06:34:11 -06:00
2018-01-07 10:50:54 -06:00
fn optional(&mut self, kind: SyntaxKind) {
2018-01-07 06:34:11 -06:00
if self.current_is(kind) {
self.bump();
}
}
2018-01-07 10:50:54 -06:00
fn bump_n(&mut self, n: u8) {
for _ in 0..n {
self.bump();
}
}
2018-01-01 07:05:46 -06:00
}