rust/src/parser/event_parser/grammar/items.rs

239 lines
5.7 KiB
Rust
Raw Normal View History

2018-01-07 12:46:10 -06:00
use super::*;
2018-01-09 13:35:55 -06:00
pub(super) fn mod_contents(p: &mut Parser) {
attributes::inner_attributes(p);
2018-01-20 08:21:13 -06:00
while !p.at(EOF) {
item(p);
2018-01-07 12:46:10 -06:00
}
}
2018-01-20 12:49:58 -06:00
fn item(p: &mut Parser) {
2018-01-12 11:32:37 -06:00
let attrs_start = p.mark();
2018-01-07 12:46:10 -06:00
attributes::outer_attributes(p);
visibility(p);
2018-01-20 08:21:13 -06:00
let la = p.raw_lookahead(1);
2018-01-12 11:32:37 -06:00
let item_start = p.mark();
2018-01-20 08:21:13 -06:00
match p.current() {
EXTERN_KW if la == CRATE_KW => extern_crate_item(p),
MOD_KW => mod_item(p),
USE_KW => use_item(p),
STRUCT_KW => struct_item(p),
FN_KW => fn_item(p),
err_token => {
p.start(ERROR);
let message = if err_token == SEMI {
2018-01-20 12:07:34 -06:00
//TODO: if the item is incomplete, this message is misleading
2018-01-20 08:21:13 -06:00
"expected item, found `;`\n\
consider removing this semicolon"
} else {
"expected item"
};
p.error()
.message(message)
.emit();
p.bump();
p.finish();
return;
}
};
2018-01-15 13:41:57 -06:00
p.forward_parent(attrs_start, item_start);
2018-01-07 12:46:10 -06:00
}
fn struct_item(p: &mut Parser) {
2018-01-20 08:21:13 -06:00
p.start(STRUCT_ITEM);
assert!(p.at(STRUCT_KW));
p.bump();
struct_inner(p);
p.finish();
fn struct_inner(p: &mut Parser) {
if !p.expect(IDENT) {
p.finish();
2018-01-20 12:49:58 -06:00
return;
2018-01-13 13:00:26 -06:00
}
2018-01-20 08:21:13 -06:00
generic_parameters(p);
match p.current() {
WHERE_KW => {
where_clause(p);
match p.current() {
SEMI => {
p.bump();
2018-01-20 12:49:58 -06:00
return;
2018-01-20 08:21:13 -06:00
}
L_CURLY => named_fields(p),
_ => { //TODO: special case `(` error message
p.error()
.message("expected `;` or `{`")
.emit();
2018-01-20 12:49:58 -06:00
return;
2018-01-20 08:21:13 -06:00
}
}
}
SEMI => {
p.bump();
2018-01-20 12:49:58 -06:00
return;
2018-01-20 08:21:13 -06:00
}
L_CURLY => named_fields(p),
L_PAREN => {
tuple_fields(p);
p.expect(SEMI);
2018-01-20 12:49:58 -06:00
}
2018-01-20 08:21:13 -06:00
_ => {
p.error()
.message("expected `;`, `{`, or `(`")
.emit();
2018-01-20 12:49:58 -06:00
return;
2018-01-20 08:21:13 -06:00
}
2018-01-13 13:00:26 -06:00
}
}
}
fn named_fields(p: &mut Parser) {
p.curly_block(|p| comma_list(p, EOF, |p| {
named_field(p);
true
}));
fn named_field(p: &mut Parser) {
2018-01-20 12:07:34 -06:00
p.start(NAMED_FIELD);
visibility(p);
if p.expect(IDENT) && p.expect(COLON) {
types::type_ref(p);
};
p.finish()
2018-01-13 13:00:26 -06:00
}
}
fn tuple_fields(p: &mut Parser) {
if !p.expect(L_PAREN) {
2018-01-20 12:49:58 -06:00
return;
2018-01-13 13:00:26 -06:00
}
comma_list(p, R_PAREN, |p| {
tuple_field(p);
true
});
p.expect(R_PAREN);
fn tuple_field(p: &mut Parser) {
2018-01-20 12:07:34 -06:00
p.start(POS_FIELD);
visibility(p);
types::type_ref(p);
p.finish();
2018-01-13 13:00:26 -06:00
}
}
2018-01-20 12:49:58 -06:00
fn generic_parameters(_: &mut Parser) {}
2018-01-13 13:00:26 -06:00
2018-01-20 12:49:58 -06:00
fn where_clause(_: &mut Parser) {}
2018-01-07 12:46:10 -06:00
2018-01-08 15:06:42 -06:00
fn extern_crate_item(p: &mut Parser) {
2018-01-20 08:21:13 -06:00
p.start(EXTERN_CRATE_ITEM);
assert!(p.at(EXTERN_KW));
p.bump();
assert!(p.at(CRATE_KW));
p.bump();
2018-01-08 15:06:42 -06:00
p.expect(IDENT) && alias(p) && p.expect(SEMI);
2018-01-20 08:21:13 -06:00
p.finish();
2018-01-08 15:06:42 -06:00
}
2018-01-09 13:35:55 -06:00
fn mod_item(p: &mut Parser) {
2018-01-20 08:21:13 -06:00
p.start(MOD_ITEM);
assert!(p.at(MOD_KW));
p.bump();
if p.expect(IDENT) && !p.eat(SEMI) {
p.curly_block(mod_contents);
2018-01-09 13:35:55 -06:00
}
2018-01-20 08:21:13 -06:00
p.finish()
2018-01-09 13:35:55 -06:00
}
2018-01-13 04:42:19 -06:00
pub(super) fn is_use_tree_start(kind: SyntaxKind) -> bool {
kind == STAR || kind == L_CURLY
}
2018-01-09 14:32:18 -06:00
fn use_item(p: &mut Parser) {
2018-01-20 08:21:13 -06:00
p.start(USE_ITEM);
assert!(p.at(USE_KW));
p.bump();
2018-01-13 04:42:19 -06:00
use_tree(p);
2018-01-09 14:32:18 -06:00
p.expect(SEMI);
2018-01-20 08:21:13 -06:00
p.finish();
2018-01-13 04:42:19 -06:00
2018-01-20 12:49:58 -06:00
fn use_tree(p: &mut Parser) -> bool {
let la = p.raw_lookahead(1);
match (p.current(), la) {
(STAR, _) => {
p.start(USE_TREE);
p.bump();
}
(COLONCOLON, STAR) => {
p.start(USE_TREE);
p.bump();
p.bump();
}
(L_CURLY, _) | (COLONCOLON, L_CURLY) => {
p.start(USE_TREE);
if p.at(COLONCOLON) {
p.bump();
}
2018-01-13 04:42:19 -06:00
p.curly_block(|p| {
comma_list(p, EOF, use_tree);
});
2018-01-20 12:49:58 -06:00
}
_ if paths::is_path_start(p) => {
p.start(USE_TREE);
2018-01-13 04:42:19 -06:00
paths::use_path(p);
match p.current() {
AS_KW => {
alias(p);
}
COLONCOLON => {
p.bump();
match p.current() {
STAR => {
p.bump();
}
L_CURLY => {
p.curly_block(|p| {
comma_list(p, EOF, use_tree);
});
}
_ => {
// is this unreachable?
p.error()
.message("expected `{` or `*`")
.emit();
}
}
}
_ => (),
}
2018-01-20 12:49:58 -06:00
}
_ => return false,
2018-01-13 04:42:19 -06:00
}
2018-01-20 12:49:58 -06:00
p.finish();
return true;
2018-01-13 04:42:19 -06:00
}
2018-01-09 14:32:18 -06:00
}
2018-01-13 13:00:26 -06:00
2018-01-07 12:46:10 -06:00
fn fn_item(p: &mut Parser) {
2018-01-20 08:21:13 -06:00
p.start(FN_ITEM);
assert!(p.at(FN_KW));
p.bump();
2018-01-07 12:46:10 -06:00
p.expect(IDENT) && p.expect(L_PAREN) && p.expect(R_PAREN)
2018-01-08 13:40:14 -06:00
&& p.curly_block(|_| ());
2018-01-20 08:21:13 -06:00
p.finish();
2018-01-07 12:46:10 -06:00
}