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/validation/match_armlist.rs | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 crates/ra_syntax/src/validation/match_armlist.rs (limited to 'crates/ra_syntax/src/validation') diff --git a/crates/ra_syntax/src/validation/match_armlist.rs b/crates/ra_syntax/src/validation/match_armlist.rs new file mode 100644 index 000000000..c43ed7092 --- /dev/null +++ b/crates/ra_syntax/src/validation/match_armlist.rs @@ -0,0 +1,28 @@ +use crate::{ + ast::{self, AttrsOwner, AstNode}, + syntax_node::{ + SyntaxError, + SyntaxErrorKind::*, + Direction, + }, +}; + +pub(crate) fn validate_match_armlist(node: &ast::MatchArmList, errors: &mut Vec) { + // Report errors for any inner attribute + // which has a preceding matcharm or an outer attribute + for inner_attr in node.attrs().filter(|s| s.is_inner()) { + let any_errors = inner_attr.syntax().siblings(Direction::Prev).any(|s| { + match (ast::MatchArm::cast(s), ast::Attr::cast(s)) { + (Some(_), _) => true, + // Outer attributes which preceed an inner attribute are not allowed + (_, Some(a)) if !a.is_inner() => true, + (_, Some(_)) => false, + (None, None) => false, + } + }); + + if any_errors { + errors.push(SyntaxError::new(InvalidMatchInnerAttr, inner_attr.syntax().range())); + } + } +} -- cgit v1.2.3