rust/crates/ra_syntax/src/grammar/items.rs

395 lines
9.4 KiB
Rust
Raw Normal View History

2018-02-03 03:05:25 -06:00
mod consts;
2018-09-08 02:55:09 -05:00
mod nominal;
2018-02-04 04:39:24 -06:00
mod traits;
2018-07-30 06:08:06 -05:00
mod use_item;
pub(crate) use self::{
expressions::{match_arm_list, named_field_list},
nominal::{enum_variant_list, named_field_def_list},
traits::{impl_item_list, trait_item_list},
use_item::use_tree_list,
};
use super::*;
2018-08-25 05:17:54 -05:00
2018-08-05 06:08:46 -05:00
// test mod_contents
// fn foo() {}
// macro_rules! foo {}
// foo::bar!();
// super::baz! {}
// struct S;
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-13 10:40:47 -05:00
item_or_macro(p, stop_on_r_curly, ItemFlavor::Mod)
2018-08-04 05:17:24 -05:00
}
}
2018-08-13 10:40:47 -05:00
pub(super) enum ItemFlavor {
Mod,
Trait,
2018-08-13 10:40:47 -05:00
}
2018-11-21 09:34:20 -06:00
pub(super) const ITEM_RECOVERY_SET: TokenSet = token_set![
FN_KW, STRUCT_KW, ENUM_KW, IMPL_KW, TRAIT_KW, CONST_KW, STATIC_KW, LET_KW, MOD_KW, PUB_KW,
CRATE_KW
];
2018-08-31 05:35:48 -05:00
2018-08-13 10:40:47 -05:00
pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemFlavor) {
2018-08-04 05:17:24 -05:00
let m = p.start();
2018-08-13 10:40:47 -05:00
match maybe_item(p, flavor) {
2018-08-04 05:17:24 -05:00
MaybeItem::Item(kind) => {
m.complete(p, kind);
}
MaybeItem::None => {
2018-08-05 06:08:46 -05:00
if paths::is_path_start(p) {
match macro_call(p) {
2018-08-07 08:11:40 -05:00
BlockLike::Block => (),
BlockLike::NotBlock => {
2018-08-05 06:08:46 -05:00
p.expect(SEMI);
}
}
m.complete(p, MACRO_CALL);
2018-08-04 05:17:24 -05:00
} else {
2018-08-05 06:08:46 -05:00
m.abandon(p);
if p.at(L_CURLY) {
error_block(p, "expected an item");
2018-08-26 01:12:18 -05:00
} else if p.at(R_CURLY) && !stop_on_r_curly {
let e = p.start();
p.error("unmatched `}`");
p.bump();
e.complete(p, ERROR);
} else if !p.at(EOF) && !p.at(R_CURLY) {
2018-08-05 06:08:46 -05:00
p.err_and_bump("expected an item");
} else {
p.error("expected an item");
}
2018-08-04 05:17:24 -05:00
}
}
MaybeItem::Modifiers => {
p.error("expected fn, trait or impl");
m.complete(p, ERROR);
}
2018-01-07 12:46:10 -06:00
}
}
2018-08-04 05:17:24 -05:00
pub(super) enum MaybeItem {
None,
Item(SyntaxKind),
Modifiers,
}
2018-08-13 10:40:47 -05:00
pub(super) fn maybe_item(p: &mut Parser, flavor: ItemFlavor) -> MaybeItem {
2018-01-07 12:46:10 -06:00
attributes::outer_attributes(p);
2018-08-23 16:16:29 -05:00
opt_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 => {
2018-08-26 04:09:28 -05:00
fn_def(p, flavor);
2018-08-13 10:27:26 -05:00
FN_DEF
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-08-13 10:34:02 -05:00
traits::trait_def(p);
2018-08-13 10:27:26 -05:00
TRAIT_DEF
2018-07-31 10:24:30 -05:00
}
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
}
_ => {
return if has_mods {
MaybeItem::Modifiers
} else {
MaybeItem::None
};
2018-08-04 05:17:24 -05:00
}
};
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 => {
2018-08-13 10:34:02 -05:00
type_def(p);
2018-08-13 10:27:26 -05:00
TYPE_DEF
2018-02-10 03:35:40 -06:00
}
2018-01-20 14:25:34 -06:00
MOD_KW => {
mod_item(p);
2018-08-11 02:56:40 -05:00
MODULE
2018-01-20 14:25:34 -06:00
}
STRUCT_KW => {
2018-09-14 15:51:12 -05:00
// test struct_items
// struct Foo;
// struct Foo {}
// struct Foo();
// struct Foo(String, usize);
// struct Foo {
// a: i32,
// b: f32,
// }
nominal::struct_def(p, STRUCT_KW);
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-08-04 05:17:24 -05:00
);
}
2018-08-13 10:27:26 -05:00
STRUCT_DEF
2018-01-20 14:25:34 -06:00
}
2018-09-14 15:51:12 -05:00
IDENT if p.at_contextual_kw("union") => {
// test union_items
// union Foo {}
// union Foo {
// a: i32,
// b: f32,
// }
nominal::struct_def(p, UNION_KW);
STRUCT_DEF
}
2018-01-28 13:59:18 -06:00
ENUM_KW => {
2018-09-08 02:55:09 -05:00
nominal::enum_def(p);
2018-08-13 10:27:26 -05:00
ENUM_DEF
2018-01-28 13:59:18 -06:00
}
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) => {
2018-08-13 10:34:02 -05:00
consts::const_def(p);
2018-08-13 10:27:26 -05:00
CONST_DEF
2018-08-04 05:17:24 -05:00
}
STATIC_KW => {
2018-08-13 10:34:02 -05:00
consts::static_def(p);
2018-08-13 10:27:26 -05:00
STATIC_DEF
2018-08-04 05:17:24 -05:00
}
// test extern_block
// extern {}
EXTERN_KW
if la == L_CURLY || ((la == STRING || la == RAW_STRING) && p.nth(2) == L_CURLY) =>
{
2018-08-04 05:17:24 -05:00
abi(p);
2018-08-24 11:27:30 -05:00
extern_item_list(p);
EXTERN_BLOCK
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);
2018-08-23 17:19:38 -05:00
opt_alias(p);
2018-02-10 05:00:23 -06:00
p.expect(SEMI);
2018-01-08 15:06:42 -06:00
}
pub(crate) fn extern_item_list(p: &mut Parser) {
2018-02-02 14:45:15 -06:00
assert!(p.at(L_CURLY));
2018-08-24 11:27:30 -05:00
let m = p.start();
2018-02-02 14:45:15 -06:00
p.bump();
2018-09-03 16:49:21 -05:00
mod_contents(p, true);
2018-02-02 14:45:15 -06:00
p.expect(R_CURLY);
2018-08-24 11:27:30 -05:00
m.complete(p, EXTERN_ITEM_LIST);
2018-02-02 14:45:15 -06:00
}
2018-02-04 04:39:24 -06:00
2018-08-26 04:09:28 -05:00
fn fn_def(p: &mut Parser, flavor: ItemFlavor) {
2018-01-20 08:21:13 -06:00
assert!(p.at(FN_KW));
p.bump();
2018-08-31 05:35:48 -05:00
name_r(p, ITEM_RECOVERY_SET);
2018-08-09 09:44:40 -05:00
// test function_type_params
2018-07-31 11:58:12 -05:00
// fn foo<T: Clone + Copy>(){}
2018-08-23 18:14:10 -05:00
type_params::opt_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-08-13 10:40:47 -05:00
match flavor {
ItemFlavor::Mod => params::param_list(p),
ItemFlavor::Trait => params::param_list_opt_patterns(p),
2018-08-13 10:40:47 -05:00
}
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-08-09 09:44:40 -05:00
// test function_ret_type
2018-07-30 07:32:19 -05:00
// fn foo() {}
// fn bar() -> () {}
2018-08-23 18:14:10 -05:00
opt_fn_ret_type(p);
2018-07-31 11:58:12 -05:00
2018-08-09 09:44:40 -05:00
// test function_where_clause
2018-07-31 11:58:12 -05:00
// fn foo<T>() where T: Copy {}
2018-08-23 18:14:10 -05:00
type_params::opt_where_clause(p);
2018-07-31 11:58:12 -05:00
2018-08-07 16:53:03 -05:00
// test fn_decl
// trait T { fn foo(); }
2018-08-25 05:21:43 -05:00
if p.at(SEMI) {
p.bump();
} else {
2018-08-25 05:21:43 -05:00
expressions::block(p)
2018-08-07 16:53:03 -05:00
}
2018-01-07 12:46:10 -06:00
}
2018-02-10 03:35:40 -06:00
// test type_item
// type Foo = Bar;
2018-08-13 10:34:02 -05:00
fn type_def(p: &mut Parser) {
2018-02-10 03:35:40 -06:00
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-08-23 18:14:10 -05:00
type_params::opt_type_param_list(p);
2018-02-10 03:35:40 -06:00
2018-08-07 16:53:03 -05:00
if p.at(COLON) {
type_params::bounds(p);
}
2018-02-10 03:35:40 -06:00
// test type_item_where_clause
// type Foo where Foo: Copy = ();
2018-08-23 18:14:10 -05:00
type_params::opt_where_clause(p);
2018-02-10 03:35:40 -06:00
2018-08-07 16:53:03 -05:00
if p.eat(EQ) {
types::type_(p);
}
2018-02-10 03:35:40 -06:00
p.expect(SEMI);
}
pub(crate) fn mod_item(p: &mut Parser) {
2018-02-10 03:35:40 -06:00
assert!(p.at(MOD_KW));
p.bump();
2018-02-10 05:23:18 -06:00
name(p);
2018-08-24 11:27:30 -05:00
if p.at(L_CURLY) {
mod_item_list(p);
} else if !p.eat(SEMI) {
p.error("expected `;` or `{`");
2018-02-10 03:35:40 -06:00
}
}
2018-08-05 06:08:46 -05:00
pub(crate) fn mod_item_list(p: &mut Parser) {
2018-08-24 11:27:30 -05:00
assert!(p.at(L_CURLY));
let m = p.start();
p.bump();
mod_contents(p, true);
p.expect(R_CURLY);
m.complete(p, ITEM_LIST);
}
2018-08-07 08:11:40 -05:00
fn macro_call(p: &mut Parser) -> BlockLike {
2018-08-05 06:08:46 -05:00
assert!(paths::is_path_start(p));
paths::use_path(p);
2018-08-05 06:16:38 -05:00
macro_call_after_excl(p)
}
2018-08-07 08:11:40 -05:00
pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike {
2018-08-05 06:08:46 -05:00
p.expect(EXCL);
p.eat(IDENT);
match p.current() {
2018-08-05 06:08:46 -05:00
L_CURLY => {
token_tree(p);
2018-08-07 08:11:40 -05:00
BlockLike::Block
2018-08-05 06:08:46 -05:00
}
L_PAREN | L_BRACK => {
token_tree(p);
2018-08-07 08:11:40 -05:00
BlockLike::NotBlock
2018-08-05 06:08:46 -05:00
}
_ => {
p.error("expected `{`, `[`, `(`");
2018-08-07 08:11:40 -05:00
BlockLike::NotBlock
}
}
2018-08-05 06:08:46 -05:00
}
pub(crate) fn token_tree(p: &mut Parser) {
2018-08-05 06:08:46 -05:00
let closing_paren_kind = match p.current() {
L_CURLY => R_CURLY,
L_PAREN => R_PAREN,
L_BRACK => R_BRACK,
_ => unreachable!(),
};
2018-08-16 04:51:40 -05:00
let m = p.start();
2018-08-05 06:08:46 -05:00
p.bump();
while !p.at(EOF) && !p.at(closing_paren_kind) {
match p.current() {
L_CURLY | L_PAREN | L_BRACK => token_tree(p),
2018-09-08 02:28:53 -05:00
R_CURLY => {
p.error("unmatched `}`");
m.complete(p, TOKEN_TREE);
return;
}
R_PAREN | R_BRACK => p.err_and_bump("unmatched brace"),
_ => p.bump(),
2018-08-05 06:08:46 -05:00
}
}
2018-08-05 06:08:46 -05:00
p.expect(closing_paren_kind);
2018-08-16 04:51:40 -05:00
m.complete(p, TOKEN_TREE);
2018-08-05 06:08:46 -05:00
}