From 73b075c5562f351b84b6c96b7852547c7b50a1ef Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Sun, 27 Jan 2019 14:55:03 +0000 Subject: Correctly parse inner attributes of impl blocks --- crates/ra_syntax/src/grammar/items/traits.rs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/grammar/items/traits.rs b/crates/ra_syntax/src/grammar/items/traits.rs index 0a0621753..d5a8ccd98 100644 --- a/crates/ra_syntax/src/grammar/items/traits.rs +++ b/crates/ra_syntax/src/grammar/items/traits.rs @@ -78,6 +78,13 @@ pub(crate) fn impl_item_list(p: &mut Parser) { assert!(p.at(L_CURLY)); let m = p.start(); p.bump(); + // test impl_inner_attributes + // enum F{} + // impl F { + // //! This is a doc comment + // #![doc("This is also a doc comment")] + // } + attributes::inner_attributes(p); while !p.at(EOF) && !p.at(R_CURLY) { if p.at(L_CURLY) { -- cgit v1.2.3 From 00e6b5d26c82d5faff066c24418a0eb5741efcd1 Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Mon, 28 Jan 2019 20:03:56 +0000 Subject: Parse and validate attributes in blocks --- crates/ra_syntax/src/ast/generated.rs | 1 + crates/ra_syntax/src/grammar.ron | 3 +++ crates/ra_syntax/src/grammar/expressions.rs | 2 ++ crates/ra_syntax/src/validation.rs | 2 ++ crates/ra_syntax/src/validation/block.rs | 24 ++++++++++++++++++++++++ crates/ra_syntax/src/yellow/syntax_error.rs | 4 ++++ 6 files changed, 36 insertions(+) create mode 100644 crates/ra_syntax/src/validation/block.rs (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 3ace6533c..ce559882b 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -272,6 +272,7 @@ impl ToOwned for Block { } +impl ast::AttrsOwner for Block {} impl Block { pub fn statements(&self) -> impl Iterator { super::children(self) diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index 85fc79038..f4841241f 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -571,6 +571,9 @@ Grammar( options: [ "Expr" ], collections: [ ["statements", "Stmt"], + ], + traits: [ + "AttrsOwner", ] ), "ParamList": ( diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs index d27eb8b7e..6b88c5685 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -42,6 +42,8 @@ pub(crate) fn block(p: &mut Parser) { } let m = p.start(); p.bump(); + // This is checked by a validator + attributes::inner_attributes(p); while !p.at(EOF) && !p.at(R_CURLY) { match p.current() { diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs index 73e1d20b9..ac6cc3dd6 100644 --- a/crates/ra_syntax/src/validation.rs +++ b/crates/ra_syntax/src/validation.rs @@ -2,6 +2,7 @@ mod byte; mod byte_string; mod char; mod string; +mod block; use crate::{ SourceFile, yellow::SyntaxError, AstNode, @@ -17,6 +18,7 @@ pub(crate) fn validate(file: &SourceFile) -> Vec { .visit::(self::byte_string::validate_byte_string_node) .visit::(self::char::validate_char_node) .visit::(self::string::validate_string_node) + .visit::(self::block::validate_block_node) .accept(node); } errors diff --git a/crates/ra_syntax/src/validation/block.rs b/crates/ra_syntax/src/validation/block.rs new file mode 100644 index 000000000..9e1949124 --- /dev/null +++ b/crates/ra_syntax/src/validation/block.rs @@ -0,0 +1,24 @@ +use crate::{SyntaxKind::*, + ast::{self, AttrsOwner, AstNode}, + yellow::{ + SyntaxError, + SyntaxErrorKind::*, + }, +}; + +pub(crate) fn validate_block_node(node: &ast::Block, errors: &mut Vec) { + if let Some(parent) = node.syntax().parent() { + match parent.kind() { + FN_DEF => return, + BLOCK_EXPR => match parent.parent().map(|v| v.kind()) { + Some(EXPR_STMT) | Some(BLOCK) => return, + _ => {} + }, + _ => {} + } + } + errors.extend( + node.attrs() + .map(|attr| SyntaxError::new(InvalidBlockAttr, attr.syntax().range())), + ) +} diff --git a/crates/ra_syntax/src/yellow/syntax_error.rs b/crates/ra_syntax/src/yellow/syntax_error.rs index 534f3511e..c52c44cc3 100644 --- a/crates/ra_syntax/src/yellow/syntax_error.rs +++ b/crates/ra_syntax/src/yellow/syntax_error.rs @@ -94,6 +94,7 @@ pub enum SyntaxErrorKind { UnicodeEscapeOutOfRange, UnclosedString, InvalidSuffix, + InvalidBlockAttr, } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -136,6 +137,9 @@ impl fmt::Display for SyntaxErrorKind { UnicodeEscapeOutOfRange => write!(f, "Unicode escape code should be at most 0x10FFFF"), UnclosedString => write!(f, "Unclosed string literal"), InvalidSuffix => write!(f, "Invalid literal suffix"), + InvalidBlockAttr => { + write!(f, "A block in this position cannot accept inner attributes") + } ParseError(msg) => write!(f, "{}", msg.0), } } -- cgit v1.2.3