Files
rust/src/parser/grammar/items/mod.rs

215 lines
5.1 KiB
Rust
Raw Normal View History

2018-01-07 21:46:10 +03:00
use super::*;
mod structs;
mod use_item;
2018-02-03 12:05:25 +03:00
mod consts;
2018-02-04 13:39:24 +03:00
mod traits;
2018-01-28 12:57:03 +03:00
pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) {
2018-01-09 22:35:55 +03:00
attributes::inner_attributes(p);
2018-01-28 12:57:03 +03:00
while !p.at(EOF) && !(stop_on_r_curly && p.at(R_CURLY)) {
2018-01-20 17:21:13 +03:00
item(p);
2018-01-07 21:46:10 +03:00
}
}
2018-01-28 23:14:00 +03:00
pub(super) const ITEM_FIRST: TokenSet = token_set![
EXTERN_KW, MOD_KW, USE_KW, STRUCT_KW, ENUM_KW, FN_KW, PUB_KW, POUND
];
2018-01-21 00:31:29 +03:00
2018-01-20 21:49:58 +03:00
fn item(p: &mut Parser) {
2018-01-20 23:25:34 +03:00
let item = p.start();
2018-01-07 21:46:10 +03:00
attributes::outer_attributes(p);
visibility(p);
2018-01-28 14:33:10 +03:00
let la = p.nth(1);
2018-01-20 23:25:34 +03:00
let item_kind = match p.current() {
2018-02-02 22:08:14 +03:00
USE_KW => {
use_item::use_item(p);
2018-02-02 22:08:14 +03:00
USE_ITEM
}
2018-02-04 12:28:30 +03:00
// test extern_crate
// extern crate foo;
2018-01-20 23:25:34 +03:00
EXTERN_KW if la == CRATE_KW => {
extern_crate_item(p);
EXTERN_CRATE_ITEM
}
2018-02-02 22:08:14 +03:00
EXTERN_KW => {
abi(p);
match p.current() {
2018-02-04 12:28:30 +03:00
// test extern_fn
// extern fn foo() {}
2018-02-02 22:08:14 +03:00
FN_KW => {
fn_item(p);
FN_ITEM
}
2018-02-04 12:28:30 +03:00
// test extern_block
// extern {}
2018-02-02 22:08:14 +03:00
L_CURLY => {
extern_block(p);
EXTERN_BLOCK
}
2018-02-04 12:28:30 +03:00
// test extern_struct
// extern struct Foo;
2018-02-02 22:08:14 +03:00
_ => {
item.abandon(p);
p.error().message("expected `fn` or `{`").emit();
return;
}
}
}
2018-02-02 23:45:15 +03:00
STATIC_KW => {
2018-02-03 12:05:25 +03:00
consts::static_item(p);
2018-02-02 23:45:15 +03:00
STATIC_ITEM
}
2018-02-03 12:05:25 +03:00
CONST_KW => match p.nth(1) {
2018-02-03 22:34:35 +03:00
// test const_fn
// const fn foo() {}
2018-02-03 12:05:25 +03:00
FN_KW => {
p.bump();
fn_item(p);
FN_ITEM
}
2018-02-03 22:34:35 +03:00
// test const_unsafe_fn
// const unsafe fn foo() {}
2018-02-03 12:05:25 +03:00
UNSAFE_KW if p.nth(2) == FN_KW => {
p.bump();
p.bump();
fn_item(p);
FN_ITEM
}
_ => {
consts::const_item(p);
CONST_ITEM
}
},
2018-02-04 13:39:24 +03:00
// test unsafe_trait
// unsafe trait T {}
UNSAFE_KW if la == TRAIT_KW => {
p.bump();
traits::trait_item(p);
TRAIT_ITEM
}
2018-02-04 16:46:26 +03:00
// test unsafe_auto_trait
// unsafe auto trait T {}
UNSAFE_KW if p.at_kw(1, "auto") && p.nth(2) == TRAIT_KW => {
p.bump();
p.bump_remap(AUTO_KW);
traits::trait_item(p);
TRAIT_ITEM
}
2018-02-04 13:39:24 +03:00
// test unsafe_impl
// unsafe impl Foo {}
UNSAFE_KW if la == IMPL_KW => {
p.bump();
traits::impl_item(p);
IMPL_ITEM
}
2018-02-04 16:46:26 +03:00
// test unsafe_default_impl
// unsafe default impl Foo {}
UNSAFE_KW if p.at_kw(1, "default") && p.nth(2) == IMPL_KW => {
p.bump();
p.bump_remap(DEFAULT_KW);
traits::impl_item(p);
IMPL_ITEM
}
2018-01-20 23:25:34 +03:00
MOD_KW => {
mod_item(p);
MOD_ITEM
}
STRUCT_KW => {
structs::struct_item(p);
2018-01-20 23:25:34 +03:00
STRUCT_ITEM
}
2018-01-28 22:59:18 +03:00
ENUM_KW => {
structs::enum_item(p);
ENUM_ITEM
}
2018-01-20 23:25:34 +03:00
FN_KW => {
fn_item(p);
FN_ITEM
}
2018-01-28 20:48:37 +03:00
L_CURLY => {
item.abandon(p);
error_block(p, "expected item");
return;
}
2018-01-20 17:21:13 +03:00
err_token => {
2018-01-20 23:25:34 +03:00
item.abandon(p);
2018-01-20 17:21:13 +03:00
let message = if err_token == SEMI {
2018-01-20 21:07:34 +03:00
//TODO: if the item is incomplete, this message is misleading
2018-01-20 17:21:13 +03:00
"expected item, found `;`\n\
2018-01-27 18:31:23 -05:00
consider removing this semicolon"
2018-01-20 17:21:13 +03:00
} else {
"expected item"
};
2018-01-28 00:05:31 +03:00
p.err_and_bump(message);
2018-01-20 17:21:13 +03:00
return;
}
};
2018-01-20 23:25:34 +03:00
item.complete(p, item_kind);
2018-01-07 21:46:10 +03:00
}
2018-01-09 00:06:42 +03:00
fn extern_crate_item(p: &mut Parser) {
2018-01-20 17:21:13 +03:00
assert!(p.at(EXTERN_KW));
p.bump();
assert!(p.at(CRATE_KW));
p.bump();
2018-01-09 00:06:42 +03:00
p.expect(IDENT) && alias(p) && p.expect(SEMI);
}
2018-02-02 23:45:15 +03:00
fn extern_block(p: &mut Parser) {
assert!(p.at(L_CURLY));
p.bump();
p.expect(R_CURLY);
}
2018-02-04 13:39:24 +03:00
2018-01-09 22:35:55 +03:00
fn mod_item(p: &mut Parser) {
2018-01-20 17:21:13 +03:00
assert!(p.at(MOD_KW));
p.bump();
if p.expect(IDENT) && !p.eat(SEMI) {
2018-01-28 12:57:03 +03:00
if p.expect(L_CURLY) {
mod_contents(p, true);
p.expect(R_CURLY);
}
2018-01-09 22:35:55 +03:00
}
}
2018-02-02 22:08:14 +03:00
fn abi(p: &mut Parser) {
assert!(p.at(EXTERN_KW));
let abi = p.start();
p.bump();
match p.current() {
STRING | RAW_STRING => p.bump(),
_ => (),
}
abi.complete(p, ABI);
}
2018-01-07 21:46:10 +03:00
fn fn_item(p: &mut Parser) {
2018-01-20 17:21:13 +03:00
assert!(p.at(FN_KW));
p.bump();
2018-01-28 14:26:24 +03:00
p.expect(IDENT);
if p.at(L_PAREN) {
fn_value_parameters(p);
} else {
p.error().message("expected function arguments").emit();
}
if p.at(L_CURLY) {
p.expect(L_CURLY);
p.expect(R_CURLY);
}
fn fn_value_parameters(p: &mut Parser) {
assert!(p.at(L_PAREN));
p.bump();
p.expect(R_PAREN);
}
2018-01-07 21:46:10 +03:00
}