rust-analyzer/crates/ra_parser/src/grammar/items.rs

482 lines
12 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
2018-02-03 09:05:25 +00:00
mod consts;
mod adt;
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::{
adt::{enum_variant_list, record_field_def_list},
2019-11-25 14:30:50 +00:00
expressions::{match_arm_list, record_field_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);
2020-02-18 13:32:19 +00:00
while !(stop_on_r_curly && p.at(T!['}']) || p.at(EOF)) {
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
}
2018-11-21 15:34:20 +00: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,
2019-12-21 10:42:17 +00:00
CRATE_KW, USE_KW, MACRO_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) {
2019-03-18 04:03:04 +00:00
let m = p.start();
attributes::outer_attributes(p);
2019-03-18 04:03:04 +00:00
let m = match maybe_item(p, m, flavor) {
2019-06-26 03:36:14 +00:00
Ok(()) => {
if p.at(T![;]) {
p.err_and_bump(
"expected item, found `;`\n\
consider removing this semicolon",
);
}
return;
}
2019-03-18 04:03:04 +00:00
Err(m) => m,
};
if paths::is_use_path_start(p) {
match macro_call(p) {
BlockLike::Block => (),
BlockLike::NotBlock => {
2019-05-15 12:35:47 +00:00
p.expect(T![;]);
2018-08-04 10:17:24 +00:00
}
}
m.complete(p, MACRO_CALL);
} else {
m.abandon(p);
2019-05-15 12:35:47 +00:00
if p.at(T!['{']) {
error_block(p, "expected an item");
2019-05-15 12:35:47 +00:00
} else if p.at(T!['}']) && !stop_on_r_curly {
let e = p.start();
p.error("unmatched `}`");
2019-09-19 19:51:46 +00:00
p.bump(T!['}']);
e.complete(p, ERROR);
2019-05-15 12:35:47 +00:00
} else if !p.at(EOF) && !p.at(T!['}']) {
p.err_and_bump("expected an item");
} else {
p.error("expected an item");
2018-08-04 10:17:24 +00:00
}
2018-01-07 18:46:10 +00:00
}
}
2019-03-18 04:03:04 +00:00
pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Result<(), Marker> {
// test_err pub_expr
// fn foo() { pub 92; }
let has_visibility = opt_visibility(p);
let m = match items_without_modifiers(p, m) {
Ok(()) => return Ok(()),
Err(m) => m,
};
2018-08-04 10:17:24 +00:00
let mut has_mods = false;
2019-03-09 23:40:22 +00:00
2018-08-04 10:17:24 +00:00
// modifiers
2019-05-15 12:35:47 +00:00
has_mods |= p.eat(T![const]);
// test_err async_without_semicolon
// fn foo() { let _ = async {} }
2019-05-15 12:35:47 +00:00
if p.at(T![async]) && p.nth(1) != T!['{'] && p.nth(1) != T![move] && p.nth(1) != T![|] {
p.eat(T![async]);
has_mods = true;
}
// test_err unsafe_block_in_mod
// fn foo(){} unsafe { } fn bar(){}
if p.at(T![unsafe]) && p.nth(1) != T!['{'] {
p.eat(T![unsafe]);
has_mods = true;
}
2019-05-15 12:35:47 +00:00
if p.at(T![extern]) {
2018-08-04 10:17:24 +00:00
has_mods = true;
abi(p);
}
2019-05-15 12:35:47 +00:00
if p.at(IDENT) && p.at_contextual_kw("auto") && p.nth(1) == T![trait] {
p.bump_remap(T![auto]);
2018-08-04 10:17:24 +00:00
has_mods = true;
}
if p.at(IDENT)
&& p.at_contextual_kw("default")
&& (match p.nth(1) {
T![impl] => true,
2020-06-03 19:21:47 +00:00
T![unsafe] => {
2020-06-04 17:00:21 +00:00
// test default_unsafe_impl
// default unsafe impl Foo {}
if p.nth(2) == T![impl] {
2020-06-03 19:21:47 +00:00
p.bump_remap(T![default]);
2020-06-04 17:00:21 +00:00
p.bump(T![unsafe]);
2020-06-03 19:21:47 +00:00
has_mods = true;
}
false
}
T![fn] | T![type] | T![const] => {
if let ItemFlavor::Mod = flavor {
true
} else {
false
}
}
_ => false,
})
{
2019-05-15 12:35:47 +00:00
p.bump_remap(T![default]);
2018-08-04 10:17:24 +00:00
has_mods = true;
}
2019-06-11 13:24:14 +00:00
if p.at(IDENT) && p.at_contextual_kw("existential") && p.nth(1) == T![type] {
p.bump_remap(T![existential]);
has_mods = true;
}
2018-08-04 10:17:24 +00:00
// items
match p.current() {
2019-03-09 23:40:22 +00:00
// test async_fn
// async fn foo() {}
2018-08-04 10:17:24 +00:00
// 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() {}
// test combined_fns
// async unsafe fn foo() {}
// const unsafe fn bar() {}
// test_err wrong_order_fns
// unsafe async fn foo() {}
// unsafe const fn bar() {}
2019-05-15 12:35:47 +00:00
T![fn] => {
fn_def(p);
m.complete(p, 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 {}
2019-05-15 12:35:47 +00:00
T![trait] => {
2018-08-13 15:34:02 +00:00
traits::trait_def(p);
m.complete(p, 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_err default_fn_type
// trait T {
// default type T = Bar;
// default fn foo() {}
// }
// test default_fn_type
// impl T for Foo {
// default type T = Bar;
// default fn foo() {}
// }
2020-06-03 19:21:47 +00:00
T![const] => {
consts::const_def(p, m);
}
2018-08-04 10:17:24 +00:00
// test unsafe_default_impl
// unsafe default impl Foo {}
2019-05-23 22:48:44 +00:00
T![impl] => {
2020-02-29 20:24:40 +00:00
traits::impl_def(p);
m.complete(p, IMPL_DEF);
2018-07-31 11:00:22 +00:00
}
2019-06-11 13:24:14 +00:00
// test existential_type
// existential type Foo: Fn() -> usize;
T![type] => {
type_def(p, m);
}
_ => {
if !has_visibility && !has_mods {
2019-03-18 04:03:04 +00:00
return Err(m);
} else {
if has_mods {
2019-06-11 13:24:14 +00:00
p.error("expected existential, fn, trait or impl");
} else {
p.error("expected an item");
}
m.complete(p, ERROR);
}
2018-08-04 10:17:24 +00:00
}
}
2019-03-18 04:03:04 +00:00
Ok(())
2018-08-04 10:17:24 +00:00
}
fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
2018-08-04 10:17:24 +00:00
let la = p.nth(1);
match p.current() {
2018-08-04 10:17:24 +00:00
// test extern_crate
// extern crate foo;
2019-05-15 12:35:47 +00:00
T![extern] if la == T![crate] => extern_crate_item(p, m),
2019-06-11 13:24:14 +00:00
T![type] => {
type_def(p, m);
}
2019-05-15 12:35:47 +00:00
T![mod] => mod_item(p, m),
T![struct] => {
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,
// }
adt::struct_def(p, m);
2018-01-20 20:25:34 +00:00
}
2019-12-21 10:29:14 +00:00
// test pub_macro_def
// pub macro m($:ident) {}
T![macro] => {
macro_def(p, m);
}
IDENT if p.at_contextual_kw("union") && p.nth(1) == IDENT => {
2018-09-14 20:51:12 +00:00
// test union_items
// union Foo {}
// union Foo {
// a: i32,
// b: f32,
// }
adt::union_def(p, m);
2018-08-04 10:17:24 +00:00
}
T![enum] => adt::enum_def(p, m),
2019-05-15 12:35:47 +00:00
T![use] => use_item::use_item(p, m),
T![const] if (la == IDENT || la == T![_] || la == T![mut]) => consts::const_def(p, m),
2019-05-15 12:35:47 +00:00
T![static] => consts::static_def(p, m),
2018-08-04 10:17:24 +00:00
// test extern_block
// extern {}
2019-05-15 12:35:47 +00:00
T![extern]
if la == T!['{'] || ((la == STRING || la == RAW_STRING) && p.nth(2) == T!['{']) =>
{
2018-08-04 10:17:24 +00:00
abi(p);
2018-08-24 16:27:30 +00:00
extern_item_list(p);
m.complete(p, EXTERN_BLOCK);
2018-01-20 14:21:13 +00:00
}
_ => return Err(m),
2018-01-20 14:21:13 +00:00
};
Ok(())
2018-01-07 18:46:10 +00:00
}
fn extern_crate_item(p: &mut Parser, m: Marker) {
2019-05-15 12:35:47 +00:00
assert!(p.at(T![extern]));
2019-09-19 19:51:46 +00:00
p.bump(T![extern]);
2019-05-15 12:35:47 +00:00
assert!(p.at(T![crate]));
2019-09-19 19:51:46 +00:00
p.bump(T![crate]);
2019-08-09 10:16:47 +00:00
name_ref(p);
2018-08-23 22:19:38 +00:00
opt_alias(p);
2019-05-15 12:35:47 +00:00
p.expect(T![;]);
m.complete(p, EXTERN_CRATE_ITEM);
2018-01-08 21:06:42 +00:00
}
pub(crate) fn extern_item_list(p: &mut Parser) {
2019-05-15 12:35:47 +00:00
assert!(p.at(T!['{']));
2018-08-24 16:27:30 +00:00
let m = p.start();
2019-09-19 19:51:46 +00:00
p.bump(T!['{']);
2018-09-03 21:49:21 +00:00
mod_contents(p, true);
2019-05-15 12:35:47 +00:00
p.expect(T!['}']);
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
fn fn_def(p: &mut Parser) {
2019-05-15 12:35:47 +00:00
assert!(p.at(T![fn]));
2019-09-19 19:51:46 +00:00
p.bump(T![fn]);
2018-01-20 14:21:13 +00:00
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
2019-05-15 12:35:47 +00:00
if p.at(T!['(']) {
2020-02-07 12:36:33 +00:00
params::param_list_fn_def(p);
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(); }
2019-05-15 12:35:47 +00:00
if p.at(T![;]) {
2019-09-19 19:51:46 +00:00
p.bump(T![;]);
} else {
2020-05-02 12:34:39 +00:00
expressions::block_expr(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;
fn type_def(p: &mut Parser, m: Marker) {
2019-05-15 12:35:47 +00:00
assert!(p.at(T![type]));
2019-09-19 19:51:46 +00:00
p.bump(T![type]);
2018-02-10 09:35:40 +00:00
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
2019-05-15 12:35:47 +00:00
if p.at(T![:]) {
2018-08-07 21:53:03 +00:00
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);
2019-05-15 12:35:47 +00:00
if p.eat(T![=]) {
2018-08-07 21:53:03 +00:00
types::type_(p);
}
2019-05-15 12:35:47 +00:00
p.expect(T![;]);
m.complete(p, TYPE_ALIAS_DEF);
2018-02-10 09:35:40 +00:00
}
pub(crate) fn mod_item(p: &mut Parser, m: Marker) {
2019-05-15 12:35:47 +00:00
assert!(p.at(T![mod]));
2019-09-19 19:51:46 +00:00
p.bump(T![mod]);
2018-02-10 09:35:40 +00:00
2018-02-10 11:23:18 +00:00
name(p);
2019-05-15 12:35:47 +00:00
if p.at(T!['{']) {
2018-08-24 16:27:30 +00:00
mod_item_list(p);
2019-05-15 12:35:47 +00:00
} else if !p.eat(T![;]) {
2018-08-24 16:27:30 +00:00
p.error("expected `;` or `{`");
2018-02-10 09:35:40 +00:00
}
m.complete(p, MODULE);
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) {
2019-05-15 12:35:47 +00:00
assert!(p.at(T!['{']));
2018-08-24 16:27:30 +00:00
let m = p.start();
2019-09-19 19:51:46 +00:00
p.bump(T!['{']);
2018-08-24 16:27:30 +00:00
mod_contents(p, true);
2019-05-15 12:35:47 +00:00
p.expect(T!['}']);
2018-08-24 16:27:30 +00:00
m.complete(p, ITEM_LIST);
}
2019-12-21 10:29:14 +00:00
// test macro_def
// macro m { ($i:ident) => {} }
// macro m($i:ident) {}
fn macro_def(p: &mut Parser, m: Marker) {
p.expect(T![macro]);
name_r(p, ITEM_RECOVERY_SET);
2019-12-21 10:29:14 +00:00
if p.at(T!['{']) {
token_tree(p);
} else if !p.at(T!['(']) {
p.error("unmatched `(`");
} else {
let m = p.start();
token_tree(p);
match p.current() {
T!['{'] | T!['['] | T!['('] => token_tree(p),
_ => p.error("expected `{`, `[`, `(`"),
}
m.complete(p, TOKEN_TREE);
}
m.complete(p, MACRO_DEF);
}
2018-08-07 13:11:40 +00:00
fn macro_call(p: &mut Parser) -> BlockLike {
assert!(paths::is_use_path_start(p));
2018-08-05 11:08:46 +00:00
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 {
2019-05-15 12:35:47 +00:00
p.expect(T![!]);
2019-02-11 16:23:13 +00:00
if p.at(IDENT) {
name(p);
}
2020-04-30 14:07:46 +00:00
// Special-case `macro_rules! try`.
// This is a hack until we do proper edition support
// test try_macro_rules
// macro_rules! try { () => {} }
if p.at(T![try]) {
let m = p.start();
p.bump_remap(IDENT);
m.complete(p, NAME);
}
match p.current() {
2019-05-15 12:35:47 +00:00
T!['{'] => {
2018-08-05 11:08:46 +00:00
token_tree(p);
2018-08-07 13:11:40 +00:00
BlockLike::Block
2018-08-05 11:08:46 +00:00
}
2019-05-15 12:35:47 +00:00
T!['('] | T!['['] => {
2018-08-05 11:08:46 +00:00
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() {
2019-05-15 12:35:47 +00:00
T!['{'] => T!['}'],
T!['('] => T![')'],
T!['['] => T![']'],
2018-08-05 11:08:46 +00:00
_ => unreachable!(),
};
2018-08-16 09:51:40 +00:00
let m = p.start();
2019-09-09 21:59:29 +00:00
p.bump_any();
2018-08-05 11:08:46 +00:00
while !p.at(EOF) && !p.at(closing_paren_kind) {
match p.current() {
2019-05-15 12:35:47 +00:00
T!['{'] | T!['('] | T!['['] => token_tree(p),
T!['}'] => {
2018-09-08 07:28:53 +00:00
p.error("unmatched `}`");
m.complete(p, TOKEN_TREE);
return;
}
2019-05-15 12:35:47 +00:00
T![')'] | T![']'] => p.err_and_bump("unmatched brace"),
_ => p.bump_any(),
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
}