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-01 15:58:46 +00:00
|
|
|
// Items //
|
|
|
|
|
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-06 18:54:55 +00:00
|
|
|
inner_attributes(p);
|
2018-01-07 11:56:08 +00:00
|
|
|
many(p, |p| {
|
|
|
|
skip_to_first(
|
|
|
|
p, item_first, item,
|
|
|
|
"expected item",
|
|
|
|
)
|
|
|
|
});
|
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 item_first(p: &Parser) -> bool {
|
2018-01-07 18:09:05 +00:00
|
|
|
match p.current() {
|
2018-01-07 13:01:30 +00:00
|
|
|
STRUCT_KW | FN_KW => true,
|
2018-01-06 18:54:55 +00:00
|
|
|
_ => false,
|
2018-01-01 15:58:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-06 18:54:55 +00:00
|
|
|
fn item(p: &mut Parser) {
|
|
|
|
outer_attributes(p);
|
|
|
|
visibility(p);
|
2018-01-07 13:01:30 +00:00
|
|
|
node_if(p, STRUCT_KW, STRUCT_ITEM, struct_item)
|
|
|
|
|| node_if(p, FN_KW, FN_ITEM, fn_item);
|
2018-01-01 15:58:46 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 18:54:55 +00:00
|
|
|
fn struct_item(p: &mut Parser) {
|
|
|
|
p.expect(IDENT)
|
2018-01-07 18:40:18 +00:00
|
|
|
&& p.curly_block(|p| comma_list(p, EOF, struct_field));
|
2018-01-06 14:16:00 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 18:54:55 +00:00
|
|
|
fn struct_field(p: &mut Parser) -> bool {
|
|
|
|
node_if(p, IDENT, STRUCT_FIELD, |p| {
|
|
|
|
p.expect(COLON) && p.expect(IDENT);
|
|
|
|
})
|
2018-01-01 20:22:01 +00:00
|
|
|
}
|
2018-01-01 15:58:46 +00:00
|
|
|
|
2018-01-07 13:01:30 +00: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 15:58:46 +00:00
|
|
|
// Paths, types, attributes, and stuff //
|
|
|
|
|
2018-01-07 16:50:54 +00:00
|
|
|
fn inner_attributes(p: &mut Parser) {
|
2018-01-07 18:40:18 +00:00
|
|
|
many(p, |p| attribute(p, true))
|
2018-01-07 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
2018-01-07 18:40:18 +00:00
|
|
|
fn attribute(p: &mut Parser, inner: bool) -> bool {
|
|
|
|
let attr_start = inner && p.lookahead(&[POUND, EXCL, L_BRACK])
|
|
|
|
|| !inner && p.lookahead(&[POUND, L_BRACK]);
|
|
|
|
if !attr_start {
|
2018-01-07 16:50:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
node(p, ATTR, |p| {
|
2018-01-07 18:40:18 +00:00
|
|
|
p.bump_n(if inner { 3 } else { 2 });
|
|
|
|
meta_item(p) && p.expect(R_BRACK);
|
2018-01-07 16:50:54 +00:00
|
|
|
});
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2018-01-07 18:40:18 +00:00
|
|
|
fn meta_item(p: &mut Parser) -> bool {
|
|
|
|
node_if(p, IDENT, META_ITEM, |p| {
|
|
|
|
if p.eat(EQ) {
|
|
|
|
if !literal(p) {
|
|
|
|
p.error()
|
|
|
|
.message("expected literal")
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
} else if p.eat(L_PAREN) {
|
|
|
|
comma_list(p, R_PAREN, meta_item_inner);
|
|
|
|
p.expect(R_PAREN);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn meta_item_inner(p: &mut Parser) -> bool {
|
|
|
|
meta_item(p) || literal(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn literal(p: &mut Parser) -> bool {
|
|
|
|
p.eat(INT_NUMBER) || p.eat(FLOAT_NUMBER)
|
|
|
|
}
|
|
|
|
|
2018-01-06 18:54:55 +00:00
|
|
|
fn outer_attributes(_: &mut Parser) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Expressions //
|
|
|
|
|
|
|
|
// Error recovery and high-order utils //
|
|
|
|
|
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
|
|
|
}
|