Files
rust/crates/parser/src/grammar/items.rs

472 lines
11 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
2018-02-03 12:05:25 +03:00
mod consts;
mod adt;
2018-02-04 13:39:24 +03:00
mod traits;
2018-07-30 14:08:06 +03:00
mod use_item;
pub(crate) use self::{
2020-08-13 17:58:35 +02:00
adt::{record_field_list, variant_list},
expressions::{match_arm_list, record_expr_field_list},
traits::assoc_item_list,
use_item::use_tree_list,
};
use super::*;
2018-08-25 13:17:54 +03:00
2018-08-05 14:08:46 +03:00
// test mod_contents
// fn foo() {}
// macro_rules! foo {}
// foo::bar!();
// super::baz! {}
// struct S;
2018-01-28 12:57:03 +03:00
pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) {
2020-08-13 17:58:35 +02:00
attributes::inner_attrs(p);
2020-02-18 15:32:19 +02:00
while !(stop_on_r_curly && p.at(T!['}']) || p.at(EOF)) {
2020-08-12 14:26:36 +02:00
item_or_macro(p, stop_on_r_curly)
2018-08-04 13:17:24 +03:00
}
}
2020-08-27 18:11:33 +02:00
pub(super) const ITEM_RECOVERY_SET: TokenSet = TokenSet::new(&[
2020-08-13 18:28:23 +02:00
FN_KW,
STRUCT_KW,
ENUM_KW,
IMPL_KW,
TRAIT_KW,
CONST_KW,
STATIC_KW,
LET_KW,
MOD_KW,
PUB_KW,
CRATE_KW,
USE_KW,
MACRO_KW,
T![;],
2020-08-27 18:11:33 +02:00
]);
2018-08-31 13:35:48 +03:00
2020-08-12 14:26:36 +02:00
pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool) {
2019-03-18 13:03:04 +09:00
let m = p.start();
2020-08-13 17:58:35 +02:00
attributes::outer_attrs(p);
2020-08-12 14:26:36 +02:00
let m = match maybe_item(p, m) {
2019-06-26 11:36:14 +08:00
Ok(()) => {
if p.at(T![;]) {
p.err_and_bump(
"expected item, found `;`\n\
consider removing this semicolon",
);
}
return;
}
2019-03-18 13:03:04 +09:00
Err(m) => m,
};
if paths::is_use_path_start(p) {
match macro_call(p) {
BlockLike::Block => (),
BlockLike::NotBlock => {
2019-05-15 15:35:47 +03:00
p.expect(T![;]);
2018-08-04 13:17:24 +03:00
}
}
m.complete(p, MACRO_CALL);
} else {
m.abandon(p);
2019-05-15 15:35:47 +03:00
if p.at(T!['{']) {
error_block(p, "expected an item");
2019-05-15 15:35:47 +03:00
} else if p.at(T!['}']) && !stop_on_r_curly {
let e = p.start();
p.error("unmatched `}`");
2019-09-19 15:51:46 -04:00
p.bump(T!['}']);
e.complete(p, ERROR);
2019-05-15 15:35:47 +03: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 13:17:24 +03:00
}
2018-01-07 21:46:10 +03:00
}
}
2020-08-12 14:26:36 +02:00
pub(super) fn maybe_item(p: &mut Parser, m: Marker) -> 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 13:17:24 +03:00
let mut has_mods = false;
2019-03-09 20:40:22 -03:00
2018-08-04 13:17:24 +03:00
// modifiers
2020-12-23 02:15:30 +01:00
if p.at(T![const]) && p.nth(1) != T!['{'] {
p.eat(T![const]);
has_mods = true;
}
// test_err async_without_semicolon
// fn foo() { let _ = async {} }
2019-05-15 15:35:47 +03: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;
}
2020-11-22 20:43:00 -08:00
if p.at(T![extern]) && p.nth(1) != T!['{'] && (p.nth(1) != STRING || p.nth(2) != T!['{']) {
2018-08-04 13:17:24 +03:00
has_mods = true;
abi(p);
}
2019-05-15 15:35:47 +03:00
if p.at(IDENT) && p.at_contextual_kw("auto") && p.nth(1) == T![trait] {
p.bump_remap(T![auto]);
2018-08-04 13:17:24 +03:00
has_mods = true;
}
2020-08-12 14:26:36 +02:00
// test default_item
// default impl T for Foo {}
if p.at(IDENT) && p.at_contextual_kw("default") {
match p.nth(1) {
T![fn] | T![type] | T![const] | T![impl] => {
p.bump_remap(T![default]);
has_mods = true;
}
2020-06-03 15:21:47 -04:00
T![unsafe] => {
2020-08-12 14:26:36 +02:00
// test default_unsafe_item
// default unsafe impl T for Foo {
2020-06-08 17:49:06 -04:00
// default unsafe fn foo() {}
// }
2020-08-12 14:26:36 +02:00
if matches!(p.nth(2), T![impl] | T![fn]) {
2020-06-03 15:21:47 -04:00
p.bump_remap(T![default]);
2020-06-04 13:00:21 -04:00
p.bump(T![unsafe]);
2020-06-03 15:21:47 -04:00
has_mods = true;
}
}
2020-08-12 14:26:36 +02:00
_ => (),
}
2018-08-04 13:17:24 +03:00
}
2020-08-12 14:26:36 +02:00
2020-08-12 14:52:37 +02:00
// test existential_type
// existential type Foo: Fn() -> usize;
2019-06-11 21:24:14 +08: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 13:17:24 +03:00
// items
match p.current() {
2020-08-12 14:52:37 +02:00
// test fn
// fn foo() {}
2019-05-15 15:35:47 +03:00
T![fn] => {
2020-08-13 17:58:35 +02:00
fn_(p);
2020-07-30 14:51:08 +02:00
m.complete(p, FN);
2018-07-31 18:24:30 +03:00
}
2018-08-04 13:17:24 +03:00
2020-08-12 14:52:37 +02:00
// test trait
// trait T {}
2019-05-15 15:35:47 +03:00
T![trait] => {
2020-08-13 17:58:35 +02:00
traits::trait_(p);
2020-07-30 18:17:28 +02:00
m.complete(p, TRAIT);
2018-07-31 18:24:30 +03:00
}
2018-08-04 13:17:24 +03:00
2020-12-23 02:15:30 +01:00
T![const] if p.nth(1) != T!['{'] => {
2020-08-13 17:58:35 +02:00
consts::konst(p, m);
2020-06-03 15:21:47 -04:00
}
2020-08-12 14:52:37 +02:00
// test impl
// impl T for S {}
2019-05-24 01:48:44 +03:00
T![impl] => {
2020-08-13 17:58:35 +02:00
traits::impl_(p);
2020-07-30 18:28:28 +02:00
m.complete(p, IMPL);
2018-07-31 14:00:22 +03:00
}
2019-06-11 21:24:14 +08:00
T![type] => {
2020-08-13 17:58:35 +02:00
type_alias(p, m);
2019-06-11 21:24:14 +08:00
}
2020-11-22 20:43:00 -08:00
// unsafe extern "C" {}
T![extern] => {
abi(p);
extern_item_list(p);
m.complete(p, EXTERN_BLOCK);
}
_ => {
if !has_visibility && !has_mods {
2019-03-18 13:03:04 +09:00
return Err(m);
} else {
if has_mods {
2019-06-11 21:24:14 +08:00
p.error("expected existential, fn, trait or impl");
} else {
p.error("expected an item");
}
m.complete(p, ERROR);
}
2018-08-04 13:17:24 +03:00
}
}
2019-03-18 13:03:04 +09:00
Ok(())
2018-08-04 13:17:24 +03:00
}
fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
2018-08-04 13:17:24 +03:00
let la = p.nth(1);
match p.current() {
2018-08-04 13:17:24 +03:00
// test extern_crate
// extern crate foo;
2020-08-13 17:58:35 +02:00
T![extern] if la == T![crate] => extern_crate(p, m),
2019-06-11 21:24:14 +08:00
T![type] => {
2020-08-13 17:58:35 +02:00
type_alias(p, m);
2019-06-11 21:24:14 +08:00
}
2019-05-15 15:35:47 +03:00
T![mod] => mod_item(p, m),
T![struct] => {
2018-09-14 22:51:12 +02:00
// test struct_items
// struct Foo;
// struct Foo {}
// struct Foo();
// struct Foo(String, usize);
// struct Foo {
// a: i32,
// b: f32,
// }
2020-08-13 17:58:35 +02:00
adt::strukt(p, m);
2018-01-20 23:25:34 +03:00
}
2019-12-21 18:29:14 +08:00
// test pub_macro_def
// pub macro m($:ident) {}
T![macro] => {
macro_def(p, m);
}
2020-12-15 15:37:37 +01:00
IDENT if p.at_contextual_kw("macro_rules") && p.nth(1) == BANG => {
macro_rules(p, m);
}
IDENT if p.at_contextual_kw("union") && p.nth(1) == IDENT => {
2018-09-14 22:51:12 +02:00
// test union_items
// union Foo {}
// union Foo {
// a: i32,
// b: f32,
// }
2020-08-13 17:58:35 +02:00
adt::union(p, m);
2018-08-04 13:17:24 +03:00
}
2020-08-13 17:58:35 +02:00
T![enum] => adt::enum_(p, m),
T![use] => use_item::use_(p, m),
T![const] if (la == IDENT || la == T![_] || la == T![mut]) => consts::konst(p, m),
T![static] => consts::static_(p, m),
2018-08-04 13:17:24 +03:00
// test extern_block
// extern {}
T![extern] if la == T!['{'] || (la == STRING && p.nth(2) == T!['{']) => {
2018-08-04 13:17:24 +03:00
abi(p);
2018-08-24 19:27:30 +03:00
extern_item_list(p);
m.complete(p, EXTERN_BLOCK);
2018-01-20 17:21:13 +03:00
}
_ => return Err(m),
2018-01-20 17:21:13 +03:00
};
Ok(())
2018-01-07 21:46:10 +03:00
}
2020-08-13 17:58:35 +02:00
fn extern_crate(p: &mut Parser, m: Marker) {
2019-05-15 15:35:47 +03:00
assert!(p.at(T![extern]));
2019-09-19 15:51:46 -04:00
p.bump(T![extern]);
2019-05-15 15:35:47 +03:00
assert!(p.at(T![crate]));
2019-09-19 15:51:46 -04:00
p.bump(T![crate]);
2020-07-28 20:59:48 +02:00
if p.at(T![self]) {
p.bump(T![self]);
} else {
name_ref(p);
}
2020-08-13 17:58:35 +02:00
opt_rename(p);
2019-05-15 15:35:47 +03:00
p.expect(T![;]);
2020-07-30 12:26:57 +02:00
m.complete(p, EXTERN_CRATE);
2018-01-09 00:06:42 +03:00
}
pub(crate) fn extern_item_list(p: &mut Parser) {
2019-05-15 15:35:47 +03:00
assert!(p.at(T!['{']));
2018-08-24 19:27:30 +03:00
let m = p.start();
2019-09-19 15:51:46 -04:00
p.bump(T!['{']);
2018-09-04 00:49:21 +03:00
mod_contents(p, true);
2019-05-15 15:35:47 +03:00
p.expect(T!['}']);
2018-08-24 19:27:30 +03:00
m.complete(p, EXTERN_ITEM_LIST);
2018-02-02 23:45:15 +03:00
}
2018-02-04 13:39:24 +03:00
2020-08-13 17:58:35 +02:00
fn fn_(p: &mut Parser) {
2019-05-15 15:35:47 +03:00
assert!(p.at(T![fn]));
2019-09-19 15:51:46 -04:00
p.bump(T![fn]);
2018-01-20 17:21:13 +03:00
2018-08-31 13:35:48 +03:00
name_r(p, ITEM_RECOVERY_SET);
2018-08-09 17:44:40 +03:00
// test function_type_params
2018-07-31 19:58:12 +03:00
// fn foo<T: Clone + Copy>(){}
2020-08-13 17:58:35 +02:00
type_params::opt_generic_param_list(p);
2018-07-31 19:58:12 +03:00
2019-05-15 15:35:47 +03:00
if p.at(T!['(']) {
2020-02-07 12:36:33 +00:00
params::param_list_fn_def(p);
2018-01-28 14:26:24 +03:00
} else {
2018-02-09 22:44:50 +03:00
p.error("expected function arguments");
2018-01-28 14:26:24 +03:00
}
2018-08-09 17:44:40 +03:00
// test function_ret_type
2018-07-30 15:32:19 +03:00
// fn foo() {}
// fn bar() -> () {}
2020-08-13 17:58:35 +02:00
opt_ret_type(p);
2018-07-31 19:58:12 +03:00
2018-08-09 17:44:40 +03:00
// test function_where_clause
2018-07-31 19:58:12 +03:00
// fn foo<T>() where T: Copy {}
2018-08-24 02:14:10 +03:00
type_params::opt_where_clause(p);
2018-07-31 19:58:12 +03:00
2018-08-08 00:53:03 +03:00
// test fn_decl
// trait T { fn foo(); }
2019-05-15 15:35:47 +03:00
if p.at(T![;]) {
2019-09-19 15:51:46 -04:00
p.bump(T![;]);
} else {
2020-05-02 14:34:39 +02:00
expressions::block_expr(p)
2018-08-08 00:53:03 +03:00
}
2018-01-07 21:46:10 +03:00
}
2018-02-10 12:35:40 +03:00
// test type_item
// type Foo = Bar;
2020-08-13 17:58:35 +02:00
fn type_alias(p: &mut Parser, m: Marker) {
2019-05-15 15:35:47 +03:00
assert!(p.at(T![type]));
2019-09-19 15:51:46 -04:00
p.bump(T![type]);
2018-02-10 12:35:40 +03:00
2018-02-10 14:22:31 +03:00
name(p);
2018-02-10 12:35:40 +03:00
// test type_item_type_params
// type Result<T> = ();
2020-08-13 17:58:35 +02:00
type_params::opt_generic_param_list(p);
2018-02-10 12:35:40 +03:00
2019-05-15 15:35:47 +03:00
if p.at(T![:]) {
2018-08-08 00:53:03 +03:00
type_params::bounds(p);
}
2018-02-10 12:35:40 +03:00
// test type_item_where_clause
// type Foo where Foo: Copy = ();
2018-08-24 02:14:10 +03:00
type_params::opt_where_clause(p);
2019-05-15 15:35:47 +03:00
if p.eat(T![=]) {
2018-08-08 00:53:03 +03:00
types::type_(p);
}
2019-05-15 15:35:47 +03:00
p.expect(T![;]);
2020-07-30 15:25:46 +02:00
m.complete(p, TYPE_ALIAS);
2018-02-10 12:35:40 +03:00
}
pub(crate) fn mod_item(p: &mut Parser, m: Marker) {
2019-05-15 15:35:47 +03:00
assert!(p.at(T![mod]));
2019-09-19 15:51:46 -04:00
p.bump(T![mod]);
2018-02-10 12:35:40 +03:00
2018-02-10 14:23:18 +03:00
name(p);
2019-05-15 15:35:47 +03:00
if p.at(T!['{']) {
2020-08-13 17:58:35 +02:00
item_list(p);
2019-05-15 15:35:47 +03:00
} else if !p.eat(T![;]) {
2018-08-24 19:27:30 +03:00
p.error("expected `;` or `{`");
2018-02-10 12:35:40 +03:00
}
m.complete(p, MODULE);
2018-02-10 12:35:40 +03:00
}
2018-08-05 14:08:46 +03:00
2020-08-13 17:58:35 +02:00
pub(crate) fn item_list(p: &mut Parser) {
2019-05-15 15:35:47 +03:00
assert!(p.at(T!['{']));
2018-08-24 19:27:30 +03:00
let m = p.start();
2019-09-19 15:51:46 -04:00
p.bump(T!['{']);
2018-08-24 19:27:30 +03:00
mod_contents(p, true);
2019-05-15 15:35:47 +03:00
p.expect(T!['}']);
2018-08-24 19:27:30 +03:00
m.complete(p, ITEM_LIST);
}
2020-12-15 15:37:37 +01:00
fn macro_rules(p: &mut Parser, m: Marker) {
assert!(p.at_contextual_kw("macro_rules"));
p.bump_remap(T![macro_rules]);
p.expect(T![!]);
if p.at(IDENT) {
name(p);
}
// 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() {
T!['{'] => {
token_tree(p);
}
_ => p.error("expected `{`"),
}
m.complete(p, MACRO_RULES);
}
2019-12-21 18:29:14 +08: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 18:29:14 +08: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 16:11:40 +03:00
fn macro_call(p: &mut Parser) -> BlockLike {
assert!(paths::is_use_path_start(p));
2018-08-05 14:08:46 +03:00
paths::use_path(p);
2018-08-05 14:16:38 +03:00
macro_call_after_excl(p)
}
2018-08-07 16:11:40 +03:00
pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike {
2019-05-15 15:35:47 +03:00
p.expect(T![!]);
2020-04-30 22:07:46 +08:00
match p.current() {
2019-05-15 15:35:47 +03:00
T!['{'] => {
2018-08-05 14:08:46 +03:00
token_tree(p);
2018-08-07 16:11:40 +03:00
BlockLike::Block
2018-08-05 14:08:46 +03:00
}
2019-05-15 15:35:47 +03:00
T!['('] | T!['['] => {
2018-08-05 14:08:46 +03:00
token_tree(p);
2018-08-07 16:11:40 +03:00
BlockLike::NotBlock
2018-08-05 14:08:46 +03:00
}
_ => {
p.error("expected `{`, `[`, `(`");
2018-08-07 16:11:40 +03:00
BlockLike::NotBlock
}
}
2018-08-05 14:08:46 +03:00
}
pub(crate) fn token_tree(p: &mut Parser) {
2018-08-05 14:08:46 +03:00
let closing_paren_kind = match p.current() {
2019-05-15 15:35:47 +03:00
T!['{'] => T!['}'],
T!['('] => T![')'],
T!['['] => T![']'],
2018-08-05 14:08:46 +03:00
_ => unreachable!(),
};
2018-08-16 12:51:40 +03:00
let m = p.start();
2019-09-10 00:59:29 +03:00
p.bump_any();
2018-08-05 14:08:46 +03:00
while !p.at(EOF) && !p.at(closing_paren_kind) {
match p.current() {
2019-05-15 15:35:47 +03:00
T!['{'] | T!['('] | T!['['] => token_tree(p),
T!['}'] => {
2018-09-08 10:28:53 +03:00
p.error("unmatched `}`");
m.complete(p, TOKEN_TREE);
return;
}
2019-05-15 15:35:47 +03:00
T![')'] | T![']'] => p.err_and_bump("unmatched brace"),
_ => p.bump_any(),
2018-08-05 14:08:46 +03:00
}
}
2018-08-05 14:08:46 +03:00
p.expect(closing_paren_kind);
2018-08-16 12:51:40 +03:00
m.complete(p, TOKEN_TREE);
2018-08-05 14:08:46 +03:00
}