rust/src/grammar/items/mod.rs

263 lines
5.8 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-08-04 05:17:24 -05:00
item(p, stop_on_r_curly)
}
}
pub(super) fn item(p: &mut Parser, stop_on_r_curly: bool) {
let m = p.start();
match maybe_item(p) {
MaybeItem::Item(kind) => {
m.complete(p, kind);
}
MaybeItem::None => {
m.abandon(p);
if p.at(L_CURLY) {
error_block(p, "expected an item");
} else if !p.at(EOF) && !(stop_on_r_curly && p.at(R_CURLY)) {
p.err_and_bump("expected an item");
} else {
p.error("expected an item");
}
}
MaybeItem::Modifiers => {
p.error("expected fn, trait or impl");
m.complete(p, ERROR);
}
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-08-04 05:17:24 -05:00
pub(super) enum MaybeItem {
None,
Item(SyntaxKind),
Modifiers,
}
pub(super) fn maybe_item(p: &mut Parser) -> MaybeItem {
2018-01-07 12:46:10 -06:00
attributes::outer_attributes(p);
visibility(p);
2018-08-04 05:17:24 -05:00
if let Some(kind) = items_without_modifiers(p) {
return MaybeItem::Item(kind);
}
let mut has_mods = false;
// modifiers
has_mods |= p.eat(CONST_KW);
// test unsafe_block_in_mod
// fn foo(){} unsafe { } fn bar(){}
if p.at(UNSAFE_KW) && p.nth(1) != L_CURLY {
p.eat(UNSAFE_KW);
has_mods = true;
}
if p.at(EXTERN_KW) {
has_mods = true;
abi(p);
}
if p.at(IDENT) && p.at_contextual_kw("auto") && p.nth(1) == TRAIT_KW {
p.bump_remap(AUTO_KW);
has_mods = true;
}
if p.at(IDENT) && p.at_contextual_kw("default") && p.nth(1) == IMPL_KW {
p.bump_remap(DEFAULT_KW);
has_mods = true;
}
// items
let kind = match p.current() {
// test extern_fn
// extern fn foo() {}
// test const_fn
// const fn foo() {}
// test const_unsafe_fn
// const unsafe fn foo() {}
// test unsafe_extern_fn
// unsafe extern "C" fn foo() {}
// test unsafe_fn
// unsafe fn foo() {}
FN_KW => {
fn_item(p);
FN_ITEM
2018-07-31 10:24:30 -05:00
}
2018-08-04 05:17:24 -05:00
// test unsafe_trait
// unsafe trait T {}
2018-07-31 10:24:30 -05:00
// test auto_trait
// auto trait T {}
2018-08-04 05:17:24 -05:00
// test unsafe_auto_trait
// unsafe auto trait T {}
TRAIT_KW => {
2018-07-31 10:24:30 -05:00
traits::trait_item(p);
TRAIT_ITEM
}
2018-08-04 05:17:24 -05:00
// test unsafe_impl
// unsafe impl Foo {}
2018-07-31 06:00:22 -05:00
// test default_impl
// default impl Foo {}
2018-08-04 05:17:24 -05:00
// test unsafe_default_impl
// unsafe default impl Foo {}
IMPL_KW => {
2018-07-31 06:00:22 -05:00
traits::impl_item(p);
IMPL_ITEM
}
2018-08-04 05:17:24 -05:00
_ => return if has_mods {
MaybeItem::Modifiers
} else {
MaybeItem::None
}
};
2018-07-31 06:00:22 -05:00
2018-08-04 05:17:24 -05:00
MaybeItem::Item(kind)
}
fn items_without_modifiers(p: &mut Parser) -> Option<SyntaxKind> {
let la = p.nth(1);
let kind = match p.current() {
// test extern_crate
// extern crate foo;
EXTERN_KW if la == CRATE_KW => {
extern_crate_item(p);
EXTERN_CRATE_ITEM
2018-02-10 03:35:40 -06:00
}
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-08-04 05:17:24 -05:00
if p.at(SEMI) {
p.err_and_bump(
"expected item, found `;`\n\
consider removing this semicolon"
);
}
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-08-04 05:17:24 -05:00
USE_KW => {
use_item::use_item(p);
USE_ITEM
}
CONST_KW if (la == IDENT || la == MUT_KW) => {
consts::const_item(p);
CONST_ITEM
}
STATIC_KW => {
consts::static_item(p);
STATIC_ITEM
}
// test extern_block
// extern {}
EXTERN_KW if la == L_CURLY || ((la == STRING || la == RAW_STRING) && p.nth(2) == L_CURLY) => {
abi(p);
extern_block(p);
EXTERN_BLOCK_EXPR
2018-01-20 08:21:13 -06:00
}
2018-08-04 05:17:24 -05:00
_ => return None,
2018-01-20 08:21:13 -06:00
};
2018-08-04 05:17:24 -05:00
Some(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-07-31 11:58:12 -05:00
// test fn_item_type_params
// fn foo<T: Clone + Copy>(){}
2018-07-31 15:16:07 -05:00
type_params::type_param_list(p);
2018-07-31 11:58:12 -05:00
2018-01-28 05:26:24 -06:00
if p.at(L_PAREN) {
2018-07-31 15:16:07 -05:00
params::param_list(p);
2018-01-28 05:26:24 -06:00
} 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);
2018-07-31 11:58:12 -05:00
// test fn_item_where_clause
// fn foo<T>() where T: Copy {}
type_params::where_clause(p);
2018-07-31 15:13:08 -05:00
expressions::block(p);
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> = ();
2018-07-31 15:16:07 -05:00
type_params::type_param_list(p);
2018-02-10 03:35:40 -06:00
// 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);
}
}
}