mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Refactor maybe_item to use Marker argument
This commit is contained in:
parent
aea9c98f53
commit
e570267515
2 changed files with 75 additions and 86 deletions
|
@ -54,7 +54,7 @@ pub(crate) fn expr_block_contents(p: &mut Parser) {
|
||||||
|
|
||||||
// test block_items
|
// test block_items
|
||||||
// fn a() { fn b() {} }
|
// fn a() { fn b() {} }
|
||||||
let m = p.start();
|
let mut m = p.start();
|
||||||
let has_attrs = p.at(POUND);
|
let has_attrs = p.at(POUND);
|
||||||
attributes::outer_attributes(p);
|
attributes::outer_attributes(p);
|
||||||
if p.at(LET_KW) {
|
if p.at(LET_KW) {
|
||||||
|
@ -62,47 +62,41 @@ pub(crate) fn expr_block_contents(p: &mut Parser) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
match items::maybe_item(p, items::ItemFlavor::Mod) {
|
m = match items::maybe_item(p, m, items::ItemFlavor::Mod) {
|
||||||
items::MaybeItem::Item(kind) => {
|
Some(m) => m,
|
||||||
m.complete(p, kind);
|
None => continue,
|
||||||
}
|
};
|
||||||
items::MaybeItem::Modifiers => {
|
|
||||||
|
// test pub_expr
|
||||||
|
// fn foo() { pub 92; } //FIXME
|
||||||
|
if has_attrs {
|
||||||
|
m.abandon(p);
|
||||||
|
p.error("expected a let statement or an item after attributes in block");
|
||||||
|
} else {
|
||||||
|
let is_blocklike = expressions::expr_stmt(p) == BlockLike::Block;
|
||||||
|
if p.at(R_CURLY) {
|
||||||
m.abandon(p);
|
m.abandon(p);
|
||||||
p.error("expected an item");
|
} else {
|
||||||
}
|
// test no_semi_after_block
|
||||||
// test pub_expr
|
// fn foo() {
|
||||||
// fn foo() { pub 92; } //FIXME
|
// if true {}
|
||||||
items::MaybeItem::None => {
|
// loop {}
|
||||||
if has_attrs {
|
// match () {}
|
||||||
m.abandon(p);
|
// while true {}
|
||||||
p.error("expected a let statement or an item after attributes in block");
|
// for _ in () {}
|
||||||
|
// {}
|
||||||
|
// {}
|
||||||
|
// macro_rules! test {
|
||||||
|
// () => {}
|
||||||
|
// }
|
||||||
|
// test!{}
|
||||||
|
// }
|
||||||
|
if is_blocklike {
|
||||||
|
p.eat(SEMI);
|
||||||
} else {
|
} else {
|
||||||
let is_blocklike = expressions::expr_stmt(p) == BlockLike::Block;
|
p.expect(SEMI);
|
||||||
if p.at(R_CURLY) {
|
|
||||||
m.abandon(p);
|
|
||||||
} else {
|
|
||||||
// test no_semi_after_block
|
|
||||||
// fn foo() {
|
|
||||||
// if true {}
|
|
||||||
// loop {}
|
|
||||||
// match () {}
|
|
||||||
// while true {}
|
|
||||||
// for _ in () {}
|
|
||||||
// {}
|
|
||||||
// {}
|
|
||||||
// macro_rules! test {
|
|
||||||
// () => {}
|
|
||||||
// }
|
|
||||||
// test!{}
|
|
||||||
// }
|
|
||||||
if is_blocklike {
|
|
||||||
p.eat(SEMI);
|
|
||||||
} else {
|
|
||||||
p.expect(SEMI);
|
|
||||||
}
|
|
||||||
m.complete(p, EXPR_STMT);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
m.complete(p, EXPR_STMT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,54 +35,42 @@ pub(super) const ITEM_RECOVERY_SET: TokenSet = token_set![
|
||||||
];
|
];
|
||||||
|
|
||||||
pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemFlavor) {
|
pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemFlavor) {
|
||||||
let m = p.start();
|
let mut m = p.start();
|
||||||
attributes::outer_attributes(p);
|
attributes::outer_attributes(p);
|
||||||
match maybe_item(p, flavor) {
|
m = match maybe_item(p, m, flavor) {
|
||||||
MaybeItem::Item(kind) => {
|
Some(m) => m,
|
||||||
m.complete(p, kind);
|
None => return,
|
||||||
}
|
};
|
||||||
MaybeItem::None => {
|
if paths::is_path_start(p) {
|
||||||
if paths::is_path_start(p) {
|
match macro_call(p) {
|
||||||
match macro_call(p) {
|
BlockLike::Block => (),
|
||||||
BlockLike::Block => (),
|
BlockLike::NotBlock => {
|
||||||
BlockLike::NotBlock => {
|
p.expect(SEMI);
|
||||||
p.expect(SEMI);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m.complete(p, MACRO_CALL);
|
|
||||||
} else {
|
|
||||||
m.abandon(p);
|
|
||||||
if p.at(L_CURLY) {
|
|
||||||
error_block(p, "expected an item");
|
|
||||||
} 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) {
|
|
||||||
p.err_and_bump("expected an item");
|
|
||||||
} else {
|
|
||||||
p.error("expected an item");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
MaybeItem::Modifiers => {
|
m.complete(p, MACRO_CALL);
|
||||||
p.error("expected fn, trait or impl");
|
} else {
|
||||||
m.complete(p, ERROR);
|
m.abandon(p);
|
||||||
|
if p.at(L_CURLY) {
|
||||||
|
error_block(p, "expected an item");
|
||||||
|
} 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) {
|
||||||
|
p.err_and_bump("expected an item");
|
||||||
|
} else {
|
||||||
|
p.error("expected an item");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) enum MaybeItem {
|
pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Option<Marker> {
|
||||||
None,
|
|
||||||
Item(SyntaxKind),
|
|
||||||
Modifiers,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) fn maybe_item(p: &mut Parser, flavor: ItemFlavor) -> MaybeItem {
|
|
||||||
opt_visibility(p);
|
opt_visibility(p);
|
||||||
if let Some(kind) = items_without_modifiers(p) {
|
if let Some(kind) = items_without_modifiers(p) {
|
||||||
return MaybeItem::Item(kind);
|
m.complete(p, kind);
|
||||||
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut has_mods = false;
|
let mut has_mods = false;
|
||||||
|
@ -115,7 +103,7 @@ pub(super) fn maybe_item(p: &mut Parser, flavor: ItemFlavor) -> MaybeItem {
|
||||||
}
|
}
|
||||||
|
|
||||||
// items
|
// items
|
||||||
let kind = match p.current() {
|
match p.current() {
|
||||||
// test async_fn
|
// test async_fn
|
||||||
// async fn foo() {}
|
// async fn foo() {}
|
||||||
|
|
||||||
|
@ -135,7 +123,8 @@ pub(super) fn maybe_item(p: &mut Parser, flavor: ItemFlavor) -> MaybeItem {
|
||||||
// unsafe fn foo() {}
|
// unsafe fn foo() {}
|
||||||
FN_KW => {
|
FN_KW => {
|
||||||
fn_def(p, flavor);
|
fn_def(p, flavor);
|
||||||
FN_DEF
|
m.complete(p, FN_DEF);
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
// test unsafe_trait
|
// test unsafe_trait
|
||||||
|
@ -148,7 +137,8 @@ pub(super) fn maybe_item(p: &mut Parser, flavor: ItemFlavor) -> MaybeItem {
|
||||||
// unsafe auto trait T {}
|
// unsafe auto trait T {}
|
||||||
TRAIT_KW => {
|
TRAIT_KW => {
|
||||||
traits::trait_def(p);
|
traits::trait_def(p);
|
||||||
TRAIT_DEF
|
m.complete(p, TRAIT_DEF);
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
// test unsafe_impl
|
// test unsafe_impl
|
||||||
|
@ -161,14 +151,19 @@ pub(super) fn maybe_item(p: &mut Parser, flavor: ItemFlavor) -> MaybeItem {
|
||||||
// unsafe default impl Foo {}
|
// unsafe default impl Foo {}
|
||||||
IMPL_KW => {
|
IMPL_KW => {
|
||||||
traits::impl_block(p);
|
traits::impl_block(p);
|
||||||
IMPL_BLOCK
|
m.complete(p, IMPL_BLOCK);
|
||||||
|
None
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
return if has_mods { MaybeItem::Modifiers } else { MaybeItem::None };
|
if has_mods {
|
||||||
|
p.error("expected fn, trait or impl");
|
||||||
|
m.complete(p, ERROR);
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(m)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
MaybeItem::Item(kind)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn items_without_modifiers(p: &mut Parser) -> Option<SyntaxKind> {
|
fn items_without_modifiers(p: &mut Parser) -> Option<SyntaxKind> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue