rust-analyzer/src/parser/event_parser/grammar/items/mod.rs

285 lines
6.8 KiB
Rust
Raw Normal View History

2018-01-07 18:46:10 +00:00
use super::*;
mod structs;
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-01-20 14:21:13 +00:00
item(p);
2018-01-07 18:46:10 +00:00
}
}
2018-01-28 20:14:00 +00: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 21:31:29 +00:00
2018-01-20 18:49:58 +00:00
fn item(p: &mut Parser) {
2018-01-20 20:25:34 +00:00
let item = p.start();
2018-01-07 18:46:10 +00:00
attributes::outer_attributes(p);
visibility(p);
2018-01-28 11:33:10 +00:00
let la = p.nth(1);
2018-01-20 20:25:34 +00:00
let item_kind = match p.current() {
2018-02-02 19:08:14 +00:00
USE_KW => {
use_item(p);
USE_ITEM
}
2018-01-20 20:25:34 +00:00
EXTERN_KW if la == CRATE_KW => {
extern_crate_item(p);
EXTERN_CRATE_ITEM
}
2018-02-02 19:08:14 +00:00
EXTERN_KW => {
abi(p);
match p.current() {
FN_KW => {
fn_item(p);
FN_ITEM
}
L_CURLY => {
extern_block(p);
EXTERN_BLOCK
}
_ => {
item.abandon(p);
p.error().message("expected `fn` or `{`").emit();
return;
}
}
}
2018-01-20 20:25:34 +00:00
MOD_KW => {
mod_item(p);
MOD_ITEM
}
STRUCT_KW => {
structs::struct_item(p);
2018-01-20 20:25:34 +00:00
STRUCT_ITEM
}
2018-01-28 19:59:18 +00:00
ENUM_KW => {
structs::enum_item(p);
ENUM_ITEM
}
2018-01-20 20:25:34 +00:00
FN_KW => {
fn_item(p);
FN_ITEM
}
2018-01-28 17:48:37 +00:00
L_CURLY => {
item.abandon(p);
error_block(p, "expected item");
return;
}
2018-01-20 14:21:13 +00:00
err_token => {
2018-01-20 20:25:34 +00:00
item.abandon(p);
2018-01-20 14:21:13 +00:00
let message = if err_token == SEMI {
2018-01-20 18:07:34 +00:00
//TODO: if the item is incomplete, this message is misleading
2018-01-20 14:21:13 +00:00
"expected item, found `;`\n\
2018-01-27 23:31:23 +00:00
consider removing this semicolon"
2018-01-20 14:21:13 +00:00
} else {
"expected item"
};
2018-01-27 21:05:31 +00:00
p.err_and_bump(message);
2018-01-20 14:21:13 +00:00
return;
}
};
2018-01-20 20:25:34 +00:00
item.complete(p, item_kind);
2018-01-07 18:46:10 +00:00
}
2018-01-28 15:53:53 +00:00
fn type_param_list(p: &mut Parser) {
if !p.at(L_ANGLE) {
return;
}
let m = p.start();
p.bump();
while !p.at(EOF) && !p.at(R_ANGLE) {
match p.current() {
LIFETIME => lifetime_param(p),
IDENT => type_param(p),
_ => p.err_and_bump("expected type parameter"),
}
if !p.at(R_ANGLE) && !p.expect(COMMA) {
break;
}
}
p.expect(R_ANGLE);
m.complete(p, TYPE_PARAM_LIST);
fn lifetime_param(p: &mut Parser) {
assert!(p.at(LIFETIME));
let m = p.start();
p.bump();
if p.eat(COLON) {
while p.at(LIFETIME) {
p.bump();
if !p.eat(PLUS) {
break;
}
}
}
m.complete(p, LIFETIME_PARAM);
}
fn type_param(p: &mut Parser) {
assert!(p.at(IDENT));
let m = p.start();
p.bump();
2018-01-30 19:53:19 +00:00
if p.eat(COLON) {
loop {
let has_paren = p.eat(L_PAREN);
p.eat(QUESTION);
if p.at(FOR_KW) {
//TODO
}
if p.at(LIFETIME) {
p.bump();
} else if paths::is_path_start(p) {
paths::type_path(p);
} else {
break;
}
if has_paren {
p.expect(R_PAREN);
}
if !p.eat(PLUS) {
break;
}
}
}
if p.at(EQ) {
types::type_ref(p)
}
2018-01-28 15:53:53 +00:00
m.complete(p, TYPE_PARAM);
}
}
2018-01-13 19:00:26 +00:00
2018-01-20 18:49:58 +00:00
fn where_clause(_: &mut Parser) {}
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-01-08 21:06:42 +00:00
p.expect(IDENT) && alias(p) && p.expect(SEMI);
}
2018-01-09 19:35:55 +00:00
fn mod_item(p: &mut Parser) {
2018-01-20 14:21:13 +00:00
assert!(p.at(MOD_KW));
p.bump();
if p.expect(IDENT) && !p.eat(SEMI) {
2018-01-28 09:57:03 +00:00
if p.expect(L_CURLY) {
mod_contents(p, true);
p.expect(R_CURLY);
}
2018-01-09 19:35:55 +00:00
}
}
2018-02-02 19:08:14 +00:00
fn extern_block(p: &mut Parser) {
assert!(p.at(L_CURLY));
p.bump();
p.expect(R_CURLY);
}
2018-01-13 10:42:19 +00:00
pub(super) fn is_use_tree_start(kind: SyntaxKind) -> bool {
kind == STAR || kind == L_CURLY
}
2018-01-09 20:32:18 +00:00
fn use_item(p: &mut Parser) {
2018-01-20 14:21:13 +00:00
assert!(p.at(USE_KW));
p.bump();
2018-01-20 20:25:34 +00:00
2018-01-13 10:42:19 +00:00
use_tree(p);
2018-01-09 20:32:18 +00:00
p.expect(SEMI);
2018-01-13 10:42:19 +00:00
2018-01-27 23:31:23 +00:00
fn use_tree(p: &mut Parser) {
2018-01-28 11:33:10 +00:00
let la = p.nth(1);
2018-01-20 20:25:34 +00:00
let m = p.start();
2018-01-20 18:49:58 +00:00
match (p.current(), la) {
2018-01-28 11:58:01 +00:00
(STAR, _) => p.bump(),
2018-01-20 18:49:58 +00:00
(COLONCOLON, STAR) => {
p.bump();
p.bump();
}
(L_CURLY, _) | (COLONCOLON, L_CURLY) => {
if p.at(COLONCOLON) {
p.bump();
}
2018-01-22 08:29:32 +00:00
nested_trees(p);
2018-01-20 18:49:58 +00:00
}
_ if paths::is_path_start(p) => {
2018-01-13 10:42:19 +00:00
paths::use_path(p);
match p.current() {
AS_KW => {
alias(p);
}
COLONCOLON => {
p.bump();
match p.current() {
STAR => {
p.bump();
}
2018-01-22 08:29:32 +00:00
L_CURLY => nested_trees(p),
2018-01-13 10:42:19 +00:00
_ => {
// is this unreachable?
2018-01-27 23:31:23 +00:00
p.error().message("expected `{` or `*`").emit();
2018-01-13 10:42:19 +00:00
}
}
}
_ => (),
}
2018-01-20 18:49:58 +00:00
}
2018-01-20 20:25:34 +00:00
_ => {
m.abandon(p);
2018-01-27 21:05:31 +00:00
p.err_and_bump("expected one of `*`, `::`, `{`, `self`, `super`, `indent`");
return;
2018-01-27 23:31:23 +00:00
}
2018-01-13 10:42:19 +00:00
}
2018-01-20 20:25:34 +00:00
m.complete(p, USE_TREE);
2018-01-13 10:42:19 +00:00
}
2018-01-22 08:29:32 +00:00
fn nested_trees(p: &mut Parser) {
assert!(p.at(L_CURLY));
2018-01-27 21:05:31 +00:00
p.bump();
while !p.at(EOF) && !p.at(R_CURLY) {
use_tree(p);
if !p.at(R_CURLY) {
p.expect(COMMA);
}
}
p.expect(R_CURLY);
2018-01-22 08:29:32 +00:00
}
2018-01-09 20:32:18 +00:00
}
2018-02-02 19:08:14 +00:00
fn abi(p: &mut Parser) {
assert!(p.at(EXTERN_KW));
let abi = p.start();
p.bump();
match p.current() {
STRING | RAW_STRING => p.bump(),
_ => (),
}
abi.complete(p, ABI);
}
2018-01-07 18:46:10 +00:00
fn fn_item(p: &mut Parser) {
2018-01-20 14:21:13 +00:00
assert!(p.at(FN_KW));
p.bump();
2018-01-28 11:26:24 +00:00
p.expect(IDENT);
if p.at(L_PAREN) {
fn_value_parameters(p);
} else {
p.error().message("expected function arguments").emit();
}
if p.at(L_CURLY) {
p.expect(L_CURLY);
p.expect(R_CURLY);
}
fn fn_value_parameters(p: &mut Parser) {
assert!(p.at(L_PAREN));
p.bump();
p.expect(R_PAREN);
}
2018-01-07 18:46:10 +00:00
}