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

339 lines
8.5 KiB
Rust
Raw Normal View History

2018-01-07 12:46:10 -06:00
use super::*;
2018-02-03 03:05:25 -06:00
mod consts;
2018-07-30 06:08:06 -05:00
mod structs;
2018-02-04 04:39:24 -06:00
mod traits;
2018-07-30 06:08:06 -05:00
mod use_item;
2018-01-28 03:57:03 -06:00
pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) {
2018-01-09 13:35:55 -06:00
attributes::inner_attributes(p);
2018-01-28 03:57:03 -06:00
while !p.at(EOF) && !(stop_on_r_curly && p.at(R_CURLY)) {
2018-01-20 08:21:13 -06:00
item(p);
2018-01-07 12:46:10 -06:00
}
}
2018-07-30 06:08:06 -05: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-20 15:31:29 -06:00
2018-01-20 12:49:58 -06:00
fn item(p: &mut Parser) {
2018-01-20 14:25:34 -06:00
let item = p.start();
2018-01-07 12:46:10 -06:00
attributes::outer_attributes(p);
visibility(p);
2018-01-28 05:33:10 -06:00
let la = p.nth(1);
2018-01-20 14:25:34 -06:00
let item_kind = match p.current() {
2018-02-02 13:08:14 -06:00
USE_KW => {
use_item::use_item(p);
2018-02-02 13:08:14 -06:00
USE_ITEM
}
2018-02-04 03:28:30 -06:00
// test extern_crate
// extern crate foo;
2018-01-20 14:25:34 -06:00
EXTERN_KW if la == CRATE_KW => {
extern_crate_item(p);
EXTERN_CRATE_ITEM
}
2018-02-02 13:08:14 -06:00
EXTERN_KW => {
abi(p);
match p.current() {
2018-02-04 03:28:30 -06:00
// test extern_fn
// extern fn foo() {}
2018-02-02 13:08:14 -06:00
FN_KW => {
fn_item(p);
FN_ITEM
}
2018-02-04 03:28:30 -06:00
// test extern_block
// extern {}
2018-02-02 13:08:14 -06:00
L_CURLY => {
extern_block(p);
EXTERN_BLOCK
}
2018-02-04 03:28:30 -06:00
// test extern_struct
// extern struct Foo;
2018-02-02 13:08:14 -06:00
_ => {
item.abandon(p);
2018-02-09 13:44:50 -06:00
p.error("expected `fn` or `{`");
2018-02-02 13:08:14 -06:00
return;
}
}
}
2018-02-02 14:45:15 -06:00
STATIC_KW => {
2018-02-03 03:05:25 -06:00
consts::static_item(p);
2018-02-02 14:45:15 -06:00
STATIC_ITEM
}
2018-02-03 03:05:25 -06:00
CONST_KW => match p.nth(1) {
2018-02-03 13:34:35 -06:00
// test const_fn
// const fn foo() {}
2018-02-03 03:05:25 -06:00
FN_KW => {
p.bump();
fn_item(p);
FN_ITEM
}
2018-02-03 13:34:35 -06:00
// test const_unsafe_fn
// const unsafe fn foo() {}
2018-02-03 03:05:25 -06: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 08:06:00 -06:00
UNSAFE_KW => {
2018-02-04 04:39:24 -06:00
p.bump();
2018-02-04 08:06:00 -06:00
let la = p.nth(1);
match p.current() {
// test unsafe_trait
// unsafe trait T {}
TRAIT_KW => {
traits::trait_item(p);
TRAIT_ITEM
}
2018-02-04 07:46:26 -06:00
2018-02-04 08:06:00 -06:00
// test unsafe_auto_trait
// unsafe auto trait T {}
2018-02-11 08:58:33 -06:00
IDENT if p.at_contextual_kw("auto") && la == TRAIT_KW => {
2018-02-04 08:06:00 -06:00
p.bump_remap(AUTO_KW);
traits::trait_item(p);
TRAIT_ITEM
}
2018-02-04 07:46:26 -06:00
2018-02-04 08:06:00 -06:00
// test unsafe_impl
// unsafe impl Foo {}
IMPL_KW => {
traits::impl_item(p);
IMPL_ITEM
}
2018-02-04 07:46:26 -06:00
2018-02-04 08:06:00 -06:00
// test unsafe_default_impl
// unsafe default impl Foo {}
2018-02-11 08:58:33 -06:00
IDENT if p.at_contextual_kw("default") && la == IMPL_KW => {
2018-02-04 08:06:00 -06:00
p.bump_remap(DEFAULT_KW);
traits::impl_item(p);
IMPL_ITEM
}
// test unsafe_extern_fn
// unsafe extern "C" fn foo() {}
EXTERN_KW => {
abi(p);
if !p.at(FN_KW) {
item.abandon(p);
2018-02-09 13:44:50 -06:00
p.error("expected function");
2018-02-04 08:06:43 -06:00
return;
2018-02-04 08:06:00 -06:00
}
fn_item(p);
FN_ITEM
}
// test unsafe_fn
// unsafe fn foo() {}
FN_KW => {
fn_item(p);
FN_ITEM
}
t => {
item.abandon(p);
let message = "expected `trait`, `impl` or `fn`";
// test unsafe_block_in_mod
// fn foo(){} unsafe { } fn bar(){}
if t == L_CURLY {
error_block(p, message);
} else {
2018-02-09 13:44:50 -06:00
p.error(message);
2018-02-04 08:06:00 -06:00
}
return;
}
}
2018-02-04 07:46:26 -06:00
}
2018-07-31 10:24:30 -05:00
TRAIT_KW => {
traits::trait_item(p);
TRAIT_ITEM
}
// test auto_trait
// auto trait T {}
IDENT if p.at_contextual_kw("auto") && la == TRAIT_KW => {
p.bump_remap(AUTO_KW);
traits::trait_item(p);
TRAIT_ITEM
}
2018-07-31 06:00:22 -05:00
IMPL_KW => {
traits::impl_item(p);
IMPL_ITEM
}
// test default_impl
// default impl Foo {}
IDENT if p.at_contextual_kw("default") && la == IMPL_KW => {
p.bump_remap(DEFAULT_KW);
traits::impl_item(p);
IMPL_ITEM
}
2018-02-10 03:35:40 -06:00
FN_KW => {
fn_item(p);
FN_ITEM
}
TYPE_KW => {
type_item(p);
TYPE_ITEM
}
2018-01-20 14:25:34 -06:00
MOD_KW => {
mod_item(p);
MOD_ITEM
}
STRUCT_KW => {
structs::struct_item(p);
2018-01-20 14:25:34 -06:00
STRUCT_ITEM
}
2018-01-28 13:59:18 -06:00
ENUM_KW => {
structs::enum_item(p);
ENUM_ITEM
}
2018-01-28 11:48:37 -06:00
L_CURLY => {
item.abandon(p);
error_block(p, "expected item");
return;
}
2018-01-20 08:21:13 -06:00
err_token => {
2018-01-20 14:25:34 -06:00
item.abandon(p);
2018-01-20 08:21:13 -06:00
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\
2018-01-27 17:31:23 -06:00
consider removing this semicolon"
2018-01-20 08:21:13 -06:00
} else {
"expected item"
};
2018-01-27 15:05:31 -06:00
p.err_and_bump(message);
2018-01-20 08:21:13 -06:00
return;
}
};
2018-01-20 14:25:34 -06:00
item.complete(p, item_kind);
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
assert!(p.at(EXTERN_KW));
p.bump();
assert!(p.at(CRATE_KW));
p.bump();
2018-02-10 05:00:23 -06:00
name(p);
alias(p);
p.expect(SEMI);
2018-01-08 15:06:42 -06:00
}
2018-02-02 14:45:15 -06:00
fn extern_block(p: &mut Parser) {
assert!(p.at(L_CURLY));
p.bump();
p.expect(R_CURLY);
}
2018-02-04 04:39:24 -06:00
2018-01-07 12:46:10 -06:00
fn fn_item(p: &mut Parser) {
2018-01-20 08:21:13 -06:00
assert!(p.at(FN_KW));
p.bump();
2018-02-10 05:04:31 -06:00
name(p);
2018-01-28 05:26:24 -06:00
if p.at(L_PAREN) {
fn_value_parameters(p);
} else {
2018-02-09 13:44:50 -06:00
p.error("expected function arguments");
2018-01-28 05:26:24 -06:00
}
2018-07-30 07:32:19 -05:00
// test fn_item_ret_type
// fn foo() {}
// fn bar() -> () {}
fn_ret_type(p);
block(p);
2018-07-31 04:59:52 -05:00
// test block
// fn a() {}
// fn b() { let _ = 1; }
// fn c() { 1; 2; }
// fn d() { 1; 2 }
fn block(p: &mut Parser) {
if !p.at(L_CURLY) {
p.error("expected block");
}
let m = p.start();
p.bump();
while !p.at(EOF) && !p.at(R_CURLY) {
match p.current() {
LET_KW => let_stmt(p),
2018-07-31 09:43:44 -05:00
c => {
// test block_items
// fn a() { fn b() {} }
if ITEM_FIRST.contains(c) {
item(p)
2018-07-31 04:59:52 -05:00
} else {
2018-07-31 09:43:44 -05:00
let expr_stmt = p.start();
expressions::expr(p);
if p.eat(SEMI) {
expr_stmt.complete(p, EXPR_STMT);
} else {
expr_stmt.abandon(p);
}
2018-07-31 04:59:52 -05:00
}
}
}
}
2018-01-28 05:26:24 -06:00
p.expect(R_CURLY);
m.complete(p, BLOCK);
}
2018-07-31 07:33:51 -05:00
// test let_stmt;
// fn foo() {
// let a;
// let b: i32;
// let c = 92;
// let d: i32 = 92;
// }
fn let_stmt(p: &mut Parser) {
assert!(p.at(LET_KW));
let m = p.start();
p.bump();
patterns::pattern(p);
2018-07-31 07:35:59 -05:00
if p.at(COLON) {
types::ascription(p);
2018-07-31 07:33:51 -05:00
}
if p.eat(EQ) {
expressions::expr(p);
}
p.expect(SEMI);
m.complete(p, LET_STMT);
2018-01-28 05:26:24 -06:00
}
2018-01-07 12:46:10 -06:00
}
2018-02-10 03:35:40 -06:00
// test type_item
// type Foo = Bar;
fn type_item(p: &mut Parser) {
assert!(p.at(TYPE_KW));
p.bump();
2018-02-10 05:22:31 -06:00
name(p);
2018-02-10 03:35:40 -06:00
// test type_item_type_params
// type Result<T> = ();
type_params::list(p);
// test type_item_where_clause
// type Foo where Foo: Copy = ();
type_params::where_clause(p);
p.expect(EQ);
2018-02-11 02:01:00 -06:00
types::type_(p);
2018-02-10 03:35:40 -06:00
p.expect(SEMI);
}
fn mod_item(p: &mut Parser) {
assert!(p.at(MOD_KW));
p.bump();
2018-02-10 05:23:18 -06:00
name(p);
if !p.eat(SEMI) {
2018-02-10 03:35:40 -06:00
if p.expect(L_CURLY) {
mod_contents(p, true);
p.expect(R_CURLY);
}
}
}