From f73a6419d43b21d07b7ee5d3804bdd586ee8036f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 12 Aug 2020 14:26:36 +0200 Subject: Allow default everywhere closes #5681 --- crates/ra_parser/src/grammar.rs | 2 +- crates/ra_parser/src/grammar/expressions.rs | 2 +- crates/ra_parser/src/grammar/items.rs | 50 ++++++++++------------------ crates/ra_parser/src/grammar/items/traits.rs | 4 +-- 4 files changed, 22 insertions(+), 36 deletions(-) (limited to 'crates/ra_parser/src') diff --git a/crates/ra_parser/src/grammar.rs b/crates/ra_parser/src/grammar.rs index c2e1d701e..88468bc97 100644 --- a/crates/ra_parser/src/grammar.rs +++ b/crates/ra_parser/src/grammar.rs @@ -110,7 +110,7 @@ pub(crate) mod fragments { } pub(crate) fn item(p: &mut Parser) { - items::item_or_macro(p, true, items::ItemFlavor::Mod) + items::item_or_macro(p, true) } pub(crate) fn macro_items(p: &mut Parser) { diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs index 51eaf7af6..3291e3f14 100644 --- a/crates/ra_parser/src/grammar/expressions.rs +++ b/crates/ra_parser/src/grammar/expressions.rs @@ -73,7 +73,7 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi) { // test block_items // fn a() { fn b() {} } - let m = match items::maybe_item(p, m, items::ItemFlavor::Mod) { + let m = match items::maybe_item(p, m) { Ok(()) => return, Err(m) => m, }; diff --git a/crates/ra_parser/src/grammar/items.rs b/crates/ra_parser/src/grammar/items.rs index cca524cea..9b7623434 100644 --- a/crates/ra_parser/src/grammar/items.rs +++ b/crates/ra_parser/src/grammar/items.rs @@ -22,24 +22,19 @@ use super::*; pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) { attributes::inner_attributes(p); while !(stop_on_r_curly && p.at(T!['}']) || p.at(EOF)) { - item_or_macro(p, stop_on_r_curly, ItemFlavor::Mod) + item_or_macro(p, stop_on_r_curly) } } -pub(super) enum ItemFlavor { - Mod, - Trait, -} - 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, CRATE_KW, USE_KW, MACRO_KW ]; -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) { let m = p.start(); attributes::outer_attributes(p); - let m = match maybe_item(p, m, flavor) { + let m = match maybe_item(p, m) { Ok(()) => { if p.at(T![;]) { p.err_and_bump( @@ -76,7 +71,7 @@ pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemF } } -pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Result<(), Marker> { +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); @@ -114,38 +109,29 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker, flavor: ItemFlavor) -> Resul has_mods = true; } - if p.at(IDENT) - && p.at_contextual_kw("default") - && (match p.nth(1) { - T![impl] => true, + // 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; + } T![unsafe] => { - // test default_unsafe_impl - // default unsafe impl Foo {} - - // test default_unsafe_fn - // impl T for Foo { + // test default_unsafe_item + // default unsafe impl T for Foo { // default unsafe fn foo() {} // } - if p.nth(2) == T![impl] || p.nth(2) == T![fn] { + if matches!(p.nth(2), T![impl] | T![fn]) { p.bump_remap(T![default]); p.bump(T![unsafe]); has_mods = true; } - false - } - T![fn] | T![type] | T![const] => { - if let ItemFlavor::Mod = flavor { - true - } else { - false - } } - _ => false, - }) - { - p.bump_remap(T![default]); - has_mods = true; + _ => (), + } } + if p.at(IDENT) && p.at_contextual_kw("existential") && p.nth(1) == T![type] { p.bump_remap(T![existential]); has_mods = true; diff --git a/crates/ra_parser/src/grammar/items/traits.rs b/crates/ra_parser/src/grammar/items/traits.rs index ef9c8ff5b..751ce65f2 100644 --- a/crates/ra_parser/src/grammar/items/traits.rs +++ b/crates/ra_parser/src/grammar/items/traits.rs @@ -47,7 +47,7 @@ pub(crate) fn trait_item_list(p: &mut Parser) { error_block(p, "expected an item"); continue; } - item_or_macro(p, true, ItemFlavor::Trait); + item_or_macro(p, true); } p.expect(T!['}']); m.complete(p, ASSOC_ITEM_LIST); @@ -104,7 +104,7 @@ pub(crate) fn impl_item_list(p: &mut Parser) { error_block(p, "expected an item"); continue; } - item_or_macro(p, true, ItemFlavor::Mod); + item_or_macro(p, true); } p.expect(T!['}']); m.complete(p, ASSOC_ITEM_LIST); -- cgit v1.2.3