rust-analyzer/crates/ra_syntax/src/grammar/items/mod.rs

395 lines
9.4 KiB
Rust
Raw Normal View History

2018-02-03 09:05:25 +00:00
mod consts;
2018-09-08 07:55:09 +00:00
mod nominal;
2018-02-04 10:39:24 +00:00
mod traits;
2018-07-30 11:08:06 +00: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 10:17:54 +00:00
2018-08-05 11:08:46 +00:00
// test mod_contents
// fn foo() {}
// macro_rules! foo {}
// foo::bar!();
// super::baz! {}
// struct S;
2018-01-28 09:57:03 +00:00
pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) {
2018-01-09 19:35:55 +00:00
attributes::inner_attributes(p);
2018-01-28 09:57:03 +00:00
while !p.at(EOF) && !(stop_on_r_curly && p.at(R_CURLY)) {
2018-08-13 15:40:47 +00:00
item_or_macro(p, stop_on_r_curly, ItemFlavor::Mod)
2018-08-04 10:17:24 +00:00
}
}
2018-08-13 15:40:47 +00:00
pub(super) enum ItemFlavor {
Mod,
Trait,
2018-08-13 15:40:47 +00:00
}
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 10:35:48 +00:00
2018-08-13 15:40:47 +00:00
pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemFlavor) {
2018-08-04 10:17:24 +00:00
let m = p.start();
2018-08-13 15:40:47 +00:00
match maybe_item(p, flavor) {
2018-08-04 10:17:24 +00:00
MaybeItem::Item(kind) => {
m.complete(p, kind);
}
MaybeItem::None => {
2018-08-05 11:08:46 +00:00
if paths::is_path_start(p) {
match macro_call(p) {
2018-08-07 13:11:40 +00:00
BlockLike::Block => (),
BlockLike::NotBlock => {
2018-08-05 11:08:46 +00:00
p.expect(SEMI);
}
}
m.complete(p, MACRO_CALL);
2018-08-04 10:17:24 +00:00
} else {
2018-08-05 11:08:46 +00:00
m.abandon(p);
if p.at(L_CURLY) {
error_block(p, "expected an item");
2018-08-26 06:12:18 +00: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 11:08:46 +00:00
p.err_and_bump("expected an item");
} else {
p.error("expected an item");
}
2018-08-04 10:17:24 +00:00
}
}
MaybeItem::Modifiers => {
p.error("expected fn, trait or impl");
m.complete(p, ERROR);
}
2018-01-07 18:46:10 +00:00
}
}
2018-08-04 10:17:24 +00:00
pub(super) enum MaybeItem {
None,
Item(SyntaxKind),
Modifiers,
}
2018-08-13 15:40:47 +00:00
pub(super) fn maybe_item(p: &mut Parser, flavor: ItemFlavor) -> MaybeItem {
2018-01-07 18:46:10 +00:00
attributes::outer_attributes(p);
2018-08-23 21:16:29 +00:00
opt_visibility(p);
2018-08-04 10:17:24 +00: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 09:09:28 +00:00
fn_def(p, flavor);
2018-08-13 15:27:26 +00:00
FN_DEF
2018-07-31 15:24:30 +00:00
}
2018-08-04 10:17:24 +00:00
// test unsafe_trait
// unsafe trait T {}
2018-07-31 15:24:30 +00:00
// test auto_trait
// auto trait T {}
2018-08-04 10:17:24 +00:00
// test unsafe_auto_trait
// unsafe auto trait T {}
TRAIT_KW => {
2018-08-13 15:34:02 +00:00
traits::trait_def(p);
2018-08-13 15:27:26 +00:00
TRAIT_DEF
2018-07-31 15:24:30 +00:00
}
2018-08-04 10:17:24 +00:00
// test unsafe_impl
// unsafe impl Foo {}
2018-07-31 11:00:22 +00:00
// test default_impl
// default impl Foo {}
2018-08-04 10:17:24 +00:00
// test unsafe_default_impl
// unsafe default impl Foo {}
IMPL_KW => {
2018-07-31 11:00:22 +00:00
traits::impl_item(p);
IMPL_ITEM
}
_ => {
return if has_mods {
MaybeItem::Modifiers
} else {
MaybeItem::None
}
2018-08-04 10:17:24 +00:00
}
};
2018-07-31 11:00:22 +00:00
2018-08-04 10:17:24 +00: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 09:35:40 +00:00
}
TYPE_KW => {
2018-08-13 15:34:02 +00:00
type_def(p);
2018-08-13 15:27:26 +00:00
TYPE_DEF
2018-02-10 09:35:40 +00:00
}
2018-01-20 20:25:34 +00:00
MOD_KW => {
mod_item(p);
2018-08-11 07:56:40 +00:00
MODULE
2018-01-20 20:25:34 +00:00
}
STRUCT_KW => {
2018-09-14 20:51:12 +00: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 10:17:24 +00:00
if p.at(SEMI) {
p.err_and_bump(
"expected item, found `;`\n\
consider removing this semicolon",
2018-08-04 10:17:24 +00:00
);
}
2018-08-13 15:27:26 +00:00
STRUCT_DEF
2018-01-20 20:25:34 +00:00
}
2018-09-14 20:51:12 +00: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 19:59:18 +00:00
ENUM_KW => {
2018-09-08 07:55:09 +00:00
nominal::enum_def(p);
2018-08-13 15:27:26 +00:00
ENUM_DEF
2018-01-28 19:59:18 +00:00
}
2018-08-04 10:17:24 +00:00
USE_KW => {
use_item::use_item(p);
USE_ITEM
}
CONST_KW if (la == IDENT || la == MUT_KW) => {
2018-08-13 15:34:02 +00:00
consts::const_def(p);
2018-08-13 15:27:26 +00:00
CONST_DEF
2018-08-04 10:17:24 +00:00
}
STATIC_KW => {
2018-08-13 15:34:02 +00:00
consts::static_def(p);
2018-08-13 15:27:26 +00:00
STATIC_DEF
2018-08-04 10:17:24 +00:00
}
// test extern_block
// extern {}
EXTERN_KW
if la == L_CURLY || ((la == STRING || la == RAW_STRING) && p.nth(2) == L_CURLY) =>
{
2018-08-04 10:17:24 +00:00
abi(p);
2018-08-24 16:27:30 +00:00
extern_item_list(p);
EXTERN_BLOCK
2018-01-20 14:21:13 +00:00
}
2018-08-04 10:17:24 +00:00
_ => return None,
2018-01-20 14:21:13 +00:00
};
2018-08-04 10:17:24 +00:00
Some(kind)
2018-01-07 18:46:10 +00:00
}
2018-01-08 21:06:42 +00:00
fn extern_crate_item(p: &mut Parser) {
2018-01-20 14:21:13 +00:00
assert!(p.at(EXTERN_KW));
p.bump();
assert!(p.at(CRATE_KW));
p.bump();
2018-02-10 11:00:23 +00:00
name(p);
2018-08-23 22:19:38 +00:00
opt_alias(p);
2018-02-10 11:00:23 +00:00
p.expect(SEMI);
2018-01-08 21:06:42 +00:00
}
pub(crate) fn extern_item_list(p: &mut Parser) {
2018-02-02 20:45:15 +00:00
assert!(p.at(L_CURLY));
2018-08-24 16:27:30 +00:00
let m = p.start();
2018-02-02 20:45:15 +00:00
p.bump();
2018-09-03 21:49:21 +00:00
mod_contents(p, true);
2018-02-02 20:45:15 +00:00
p.expect(R_CURLY);
2018-08-24 16:27:30 +00:00
m.complete(p, EXTERN_ITEM_LIST);
2018-02-02 20:45:15 +00:00
}
2018-02-04 10:39:24 +00:00
2018-08-26 09:09:28 +00:00
fn fn_def(p: &mut Parser, flavor: ItemFlavor) {
2018-01-20 14:21:13 +00:00
assert!(p.at(FN_KW));
p.bump();
2018-08-31 10:35:48 +00:00
name_r(p, ITEM_RECOVERY_SET);
2018-08-09 14:44:40 +00:00
// test function_type_params
2018-07-31 16:58:12 +00:00
// fn foo<T: Clone + Copy>(){}
2018-08-23 23:14:10 +00:00
type_params::opt_type_param_list(p);
2018-07-31 16:58:12 +00:00
2018-01-28 11:26:24 +00:00
if p.at(L_PAREN) {
2018-08-13 15:40:47 +00:00
match flavor {
ItemFlavor::Mod => params::param_list(p),
ItemFlavor::Trait => params::param_list_opt_patterns(p),
2018-08-13 15:40:47 +00:00
}
2018-01-28 11:26:24 +00:00
} else {
2018-02-09 19:44:50 +00:00
p.error("expected function arguments");
2018-01-28 11:26:24 +00:00
}
2018-08-09 14:44:40 +00:00
// test function_ret_type
2018-07-30 12:32:19 +00:00
// fn foo() {}
// fn bar() -> () {}
2018-08-23 23:14:10 +00:00
opt_fn_ret_type(p);
2018-07-31 16:58:12 +00:00
2018-08-09 14:44:40 +00:00
// test function_where_clause
2018-07-31 16:58:12 +00:00
// fn foo<T>() where T: Copy {}
2018-08-23 23:14:10 +00:00
type_params::opt_where_clause(p);
2018-07-31 16:58:12 +00:00
2018-08-07 21:53:03 +00:00
// test fn_decl
// trait T { fn foo(); }
2018-08-25 10:21:43 +00:00
if p.at(SEMI) {
p.bump();
} else {
2018-08-25 10:21:43 +00:00
expressions::block(p)
2018-08-07 21:53:03 +00:00
}
2018-01-07 18:46:10 +00:00
}
2018-02-10 09:35:40 +00:00
// test type_item
// type Foo = Bar;
2018-08-13 15:34:02 +00:00
fn type_def(p: &mut Parser) {
2018-02-10 09:35:40 +00:00
assert!(p.at(TYPE_KW));
p.bump();
2018-02-10 11:22:31 +00:00
name(p);
2018-02-10 09:35:40 +00:00
// test type_item_type_params
// type Result<T> = ();
2018-08-23 23:14:10 +00:00
type_params::opt_type_param_list(p);
2018-02-10 09:35:40 +00:00
2018-08-07 21:53:03 +00:00
if p.at(COLON) {
type_params::bounds(p);
}
2018-02-10 09:35:40 +00:00
// test type_item_where_clause
// type Foo where Foo: Copy = ();
2018-08-23 23:14:10 +00:00
type_params::opt_where_clause(p);
2018-02-10 09:35:40 +00:00
2018-08-07 21:53:03 +00:00
if p.eat(EQ) {
types::type_(p);
}
2018-02-10 09:35:40 +00:00
p.expect(SEMI);
}
pub(crate) fn mod_item(p: &mut Parser) {
2018-02-10 09:35:40 +00:00
assert!(p.at(MOD_KW));
p.bump();
2018-02-10 11:23:18 +00:00
name(p);
2018-08-24 16:27:30 +00:00
if p.at(L_CURLY) {
mod_item_list(p);
} else if !p.eat(SEMI) {
p.error("expected `;` or `{`");
2018-02-10 09:35:40 +00:00
}
}
2018-08-05 11:08:46 +00:00
pub(crate) fn mod_item_list(p: &mut Parser) {
2018-08-24 16:27:30 +00: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 13:11:40 +00:00
fn macro_call(p: &mut Parser) -> BlockLike {
2018-08-05 11:08:46 +00:00
assert!(paths::is_path_start(p));
paths::use_path(p);
2018-08-05 11:16:38 +00:00
macro_call_after_excl(p)
}
2018-08-07 13:11:40 +00:00
pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike {
2018-08-05 11:08:46 +00:00
p.expect(EXCL);
p.eat(IDENT);
match p.current() {
2018-08-05 11:08:46 +00:00
L_CURLY => {
token_tree(p);
2018-08-07 13:11:40 +00:00
BlockLike::Block
2018-08-05 11:08:46 +00:00
}
L_PAREN | L_BRACK => {
token_tree(p);
2018-08-07 13:11:40 +00:00
BlockLike::NotBlock
2018-08-05 11:08:46 +00:00
}
_ => {
p.error("expected `{`, `[`, `(`");
2018-08-07 13:11:40 +00:00
BlockLike::NotBlock
}
}
2018-08-05 11:08:46 +00:00
}
pub(crate) fn token_tree(p: &mut Parser) {
2018-08-05 11:08:46 +00:00
let closing_paren_kind = match p.current() {
L_CURLY => R_CURLY,
L_PAREN => R_PAREN,
L_BRACK => R_BRACK,
_ => unreachable!(),
};
2018-08-16 09:51:40 +00:00
let m = p.start();
2018-08-05 11:08:46 +00: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 07:28:53 +00: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 11:08:46 +00:00
}
}
2018-08-05 11:08:46 +00:00
p.expect(closing_paren_kind);
2018-08-16 09:51:40 +00:00
m.complete(p, TOKEN_TREE);
2018-08-05 11:08:46 +00:00
}