Refactor maybe_item to use Marker argument

This commit is contained in:
pcpthm 2019-03-17 22:04:25 +09:00
parent aea9c98f53
commit e570267515
2 changed files with 75 additions and 86 deletions

View file

@ -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,17 +62,13 @@ 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 => {
m.abandon(p);
p.error("expected an item");
}
// test pub_expr // test pub_expr
// fn foo() { pub 92; } //FIXME // fn foo() { pub 92; } //FIXME
items::MaybeItem::None => {
if has_attrs { if has_attrs {
m.abandon(p); m.abandon(p);
p.error("expected a let statement or an item after attributes in block"); p.error("expected a let statement or an item after attributes in block");
@ -104,8 +100,6 @@ pub(crate) fn expr_block_contents(p: &mut Parser) {
} }
} }
} }
}
}
// test let_stmt; // test let_stmt;
// fn foo() { // fn foo() {

View file

@ -35,13 +35,12 @@ 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 => (),
@ -66,23 +65,12 @@ pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemF
} }
} }
} }
MaybeItem::Modifiers => {
p.error("expected fn, trait or impl");
m.complete(p, ERROR);
}
}
}
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> {