From 1c97c1ac11459d45f7bfc57dc72428d2b294520c Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Sun, 17 Feb 2019 19:08:34 +0200 Subject: Enable parsing of attributes inside a match block We allow invalid inner attributes to be parsed, e.g. inner attributes that are not directly after the opening brace of the match block. Instead we run validation on `MatchArmList` to allow better reporting of errors. --- crates/ra_syntax/src/grammar/attributes.rs | 10 +++++++ crates/ra_syntax/src/grammar/expressions/atom.rs | 33 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+) (limited to 'crates/ra_syntax/src/grammar') diff --git a/crates/ra_syntax/src/grammar/attributes.rs b/crates/ra_syntax/src/grammar/attributes.rs index cd30e8a45..2624d2e16 100644 --- a/crates/ra_syntax/src/grammar/attributes.rs +++ b/crates/ra_syntax/src/grammar/attributes.rs @@ -1,5 +1,15 @@ use super::*; +/// Parses both inner & outer attributes. +/// +/// Allowing to run validation for reporting errors +/// regarding attributes +pub(super) fn all_attributes(p: &mut Parser) { + while p.at(POUND) { + attribute(p, p.nth(1) == EXCL) + } +} + pub(super) fn inner_attributes(p: &mut Parser) { while p.current() == POUND && p.nth(1) == EXCL { attribute(p, true) diff --git a/crates/ra_syntax/src/grammar/expressions/atom.rs b/crates/ra_syntax/src/grammar/expressions/atom.rs index 27ba87657..67cd7e6b0 100644 --- a/crates/ra_syntax/src/grammar/expressions/atom.rs +++ b/crates/ra_syntax/src/grammar/expressions/atom.rs @@ -313,11 +313,44 @@ pub(crate) fn match_arm_list(p: &mut Parser) { assert!(p.at(L_CURLY)); let m = p.start(); p.eat(L_CURLY); + + // test match_arms_inner_attribute + // fn foo() { + // match () { + // #![doc("Inner attribute")] + // #![doc("Can be")] + // #![doc("Stacked")] + // _ => (), + // } + // } + attributes::inner_attributes(p); + while !p.at(EOF) && !p.at(R_CURLY) { if p.at(L_CURLY) { error_block(p, "expected match arm"); continue; } + + // This may result in invalid attributes + // if there are inner attributes mixed in together + // with the outer attributes, but we allow parsing + // those so we can run validation and report better errors + + // test match_arms_outer_attributes + // fn foo() { + // match () { + // #[cfg(feature = "some")] + // _ => (), + // #[cfg(feature = "other")] + // _ => (), + // #[cfg(feature = "many")] + // #[cfg(feature = "attributes")] + // #[cfg(feature = "before")] + // _ => (), + // } + // } + attributes::all_attributes(p); + // test match_arms_commas // fn foo() { // match () { -- cgit v1.2.3