From 7d0c9cf54652c0630bf8f955fc3affb288b5e4cf Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 13 Aug 2018 18:40:47 +0300 Subject: Optional patterns in trait methods --- crates/libsyntax2/src/grammar/expressions/atom.rs | 2 +- crates/libsyntax2/src/grammar/items/mod.rs | 23 ++++++++++++++++------- crates/libsyntax2/src/grammar/items/traits.rs | 4 ++-- 3 files changed, 19 insertions(+), 10 deletions(-) (limited to 'crates/libsyntax2/src') diff --git a/crates/libsyntax2/src/grammar/expressions/atom.rs b/crates/libsyntax2/src/grammar/expressions/atom.rs index af9f47c5e..e62b16654 100644 --- a/crates/libsyntax2/src/grammar/expressions/atom.rs +++ b/crates/libsyntax2/src/grammar/expressions/atom.rs @@ -284,7 +284,7 @@ pub(super) fn block_expr(p: &mut Parser) -> CompletedMarker { // test block_items // fn a() { fn b() {} } let m = p.start(); - match items::maybe_item(p) { + match items::maybe_item(p, items::ItemFlavor::Mod) { items::MaybeItem::Item(kind) => { m.complete(p, kind); } diff --git a/crates/libsyntax2/src/grammar/items/mod.rs b/crates/libsyntax2/src/grammar/items/mod.rs index 883b5a946..fc02f0c5c 100644 --- a/crates/libsyntax2/src/grammar/items/mod.rs +++ b/crates/libsyntax2/src/grammar/items/mod.rs @@ -14,13 +14,17 @@ mod use_item; pub(super) fn mod_contents(p: &mut Parser, stop_on_r_curly: bool) { attributes::inner_attributes(p); while !p.at(EOF) && !(stop_on_r_curly && p.at(R_CURLY)) { - item_or_macro(p, stop_on_r_curly) + item_or_macro(p, stop_on_r_curly, ItemFlavor::Mod) } } -pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool) { +pub(super) enum ItemFlavor { + Mod, Trait +} + +pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemFlavor) { let m = p.start(); - match maybe_item(p) { + match maybe_item(p, flavor) { MaybeItem::Item(kind) => { m.complete(p, kind); } @@ -60,7 +64,7 @@ pub(super) enum MaybeItem { Modifiers, } -pub(super) fn maybe_item(p: &mut Parser) -> MaybeItem { +pub(super) fn maybe_item(p: &mut Parser, flavor: ItemFlavor) -> MaybeItem { attributes::outer_attributes(p); visibility(p); if let Some(kind) = items_without_modifiers(p) { @@ -107,7 +111,7 @@ pub(super) fn maybe_item(p: &mut Parser) -> MaybeItem { // test unsafe_fn // unsafe fn foo() {} FN_KW => { - function(p); + function(p, flavor); FN_DEF } @@ -217,7 +221,7 @@ fn extern_block(p: &mut Parser) { p.expect(R_CURLY); } -fn function(p: &mut Parser) { +fn function(p: &mut Parser, flavor: ItemFlavor) { assert!(p.at(FN_KW)); p.bump(); @@ -227,7 +231,12 @@ fn function(p: &mut Parser) { type_params::type_param_list(p); if p.at(L_PAREN) { - params::param_list(p); + match flavor { + ItemFlavor::Mod => + params::param_list(p), + ItemFlavor::Trait => + params::param_list_opt_patterns(p), + } } else { p.error("expected function arguments"); } diff --git a/crates/libsyntax2/src/grammar/items/traits.rs b/crates/libsyntax2/src/grammar/items/traits.rs index 53a636f6c..daecaff5c 100644 --- a/crates/libsyntax2/src/grammar/items/traits.rs +++ b/crates/libsyntax2/src/grammar/items/traits.rs @@ -20,7 +20,7 @@ pub(super) fn trait_def(p: &mut Parser) { // fn bar(&self); // } while !p.at(EOF) && !p.at(R_CURLY) { - item_or_macro(p, true); + item_or_macro(p, true, ItemFlavor::Trait); } p.expect(R_CURLY); } @@ -55,7 +55,7 @@ pub(super) fn impl_item(p: &mut Parser) { // fn bar(&self) {} // } while !p.at(EOF) && !p.at(R_CURLY) { - item_or_macro(p, true); + item_or_macro(p, true, ItemFlavor::Mod); } p.expect(R_CURLY); } -- cgit v1.2.3