2018-01-01 13:05:46 +00:00
|
|
|
use super::parser::Parser;
|
2018-01-06 18:54:55 +00:00
|
|
|
use {SyntaxKind};
|
2018-01-07 18:09:05 +00:00
|
|
|
use tree::EOF;
|
2018-01-01 13:05:46 +00:00
|
|
|
use syntax_kinds::*;
|
|
|
|
|
2018-01-07 18:46:10 +00:00
|
|
|
mod items;
|
|
|
|
mod attributes;
|
|
|
|
mod expressions;
|
2018-01-01 15:58:46 +00:00
|
|
|
|
2018-01-07 17:14:26 +00:00
|
|
|
pub(crate) fn file(p: &mut Parser) {
|
2018-01-06 18:54:55 +00:00
|
|
|
node(p, FILE, |p| {
|
2018-01-07 12:34:11 +00:00
|
|
|
p.optional(SHEBANG);
|
2018-01-07 18:46:10 +00:00
|
|
|
attributes::inner_attributes(p);
|
2018-01-08 18:57:19 +00:00
|
|
|
items::mod_items(p);
|
2018-01-06 18:54:55 +00:00
|
|
|
})
|
2018-01-01 15:58:46 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 18:54:55 +00:00
|
|
|
fn visibility(_: &mut Parser) {
|
2018-01-01 15:58:46 +00:00
|
|
|
}
|
|
|
|
|
2018-01-07 18:40:18 +00:00
|
|
|
fn node_if<F: FnOnce(&mut Parser)>(
|
|
|
|
p: &mut Parser,
|
|
|
|
first: SyntaxKind,
|
|
|
|
node_kind: SyntaxKind,
|
|
|
|
rest: F
|
|
|
|
) -> bool {
|
2018-01-07 18:09:05 +00:00
|
|
|
p.current() == first && { node(p, node_kind, |p| { p.bump(); rest(p); }); true }
|
2018-01-01 15:58:46 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 18:54:55 +00:00
|
|
|
fn node<F: FnOnce(&mut Parser)>(p: &mut Parser, node_kind: SyntaxKind, rest: F) {
|
|
|
|
p.start(node_kind);
|
|
|
|
rest(p);
|
2018-01-01 13:05:46 +00:00
|
|
|
p.finish();
|
2018-01-06 14:16:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 18:54:55 +00:00
|
|
|
fn many<F: Fn(&mut Parser) -> bool>(p: &mut Parser, f: F) {
|
|
|
|
while f(p) { }
|
2018-01-06 14:16:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-07 18:40:18 +00:00
|
|
|
fn comma_list<F: Fn(&mut Parser) -> bool>(p: &mut Parser, end: SyntaxKind, f: F) {
|
2018-01-06 18:54:55 +00:00
|
|
|
many(p, |p| {
|
2018-01-07 18:40:18 +00:00
|
|
|
if !f(p) || p.current() == end {
|
2018-01-07 09:32:29 +00:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
p.expect(COMMA);
|
|
|
|
true
|
|
|
|
}
|
2018-01-06 18:54:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-07 11:56:08 +00:00
|
|
|
fn skip_to_first<C, F>(p: &mut Parser, cond: C, f: F, message: &str) -> bool
|
2018-01-06 18:54:55 +00:00
|
|
|
where
|
|
|
|
C: Fn(&Parser) -> bool,
|
|
|
|
F: FnOnce(&mut Parser),
|
|
|
|
{
|
2018-01-07 11:56:08 +00:00
|
|
|
let mut skipped = false;
|
2018-01-06 14:16:00 +00:00
|
|
|
loop {
|
2018-01-06 18:54:55 +00:00
|
|
|
if cond(p) {
|
2018-01-07 11:56:08 +00:00
|
|
|
if skipped {
|
|
|
|
p.finish();
|
|
|
|
}
|
2018-01-06 18:54:55 +00:00
|
|
|
f(p);
|
|
|
|
return true;
|
2018-01-06 14:16:00 +00:00
|
|
|
}
|
2018-01-07 18:09:05 +00:00
|
|
|
if p.current() == EOF {
|
2018-01-07 11:56:08 +00:00
|
|
|
if skipped {
|
|
|
|
p.finish();
|
|
|
|
}
|
2018-01-06 18:54:55 +00:00
|
|
|
return false;
|
2018-01-06 14:16:00 +00:00
|
|
|
}
|
2018-01-07 11:56:08 +00:00
|
|
|
if !skipped {
|
|
|
|
p.start(ERROR);
|
|
|
|
p.error()
|
|
|
|
.message(message)
|
|
|
|
.emit();
|
|
|
|
}
|
2018-01-07 18:09:05 +00:00
|
|
|
p.bump();
|
2018-01-07 11:56:08 +00:00
|
|
|
skipped = true;
|
2018-01-06 14:16:00 +00:00
|
|
|
}
|
2018-01-06 18:54:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'p> Parser<'p> {
|
|
|
|
pub(crate) fn expect(&mut self, kind: SyntaxKind) -> bool {
|
2018-01-07 18:09:05 +00:00
|
|
|
if self.current() == kind {
|
2018-01-07 09:13:01 +00:00
|
|
|
self.bump();
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
self.error()
|
|
|
|
.message(format!("expected {:?}", kind))
|
|
|
|
.emit();
|
|
|
|
false
|
|
|
|
}
|
2018-01-06 18:54:55 +00:00
|
|
|
}
|
2018-01-07 12:34:11 +00:00
|
|
|
|
2018-01-07 16:50:54 +00:00
|
|
|
fn optional(&mut self, kind: SyntaxKind) {
|
2018-01-07 18:09:05 +00:00
|
|
|
if self.current() == kind {
|
2018-01-07 12:34:11 +00:00
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
}
|
2018-01-07 16:50:54 +00:00
|
|
|
|
|
|
|
fn bump_n(&mut self, n: u8) {
|
|
|
|
for _ in 0..n {
|
|
|
|
self.bump();
|
|
|
|
}
|
|
|
|
}
|
2018-01-07 18:40:18 +00:00
|
|
|
|
|
|
|
fn eat(&mut self, kind: SyntaxKind) -> bool {
|
|
|
|
self.current() == kind && { self.bump(); true }
|
|
|
|
}
|
2018-01-01 13:05:46 +00:00
|
|
|
}
|