diff options
author | Aleksey Kladov <[email protected]> | 2020-07-30 19:16:04 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-07-30 19:21:32 +0100 |
commit | fcce07d2d1b07cf4578af65b00a243e743a67f05 (patch) | |
tree | a6a9437d26f62040d62921eca2f7aafb5d1233f3 /crates/ra_syntax/src | |
parent | e28ea81b2b68a61b5c5eec3c815172b17256a25f (diff) |
Finalize attribute grammar
Diffstat (limited to 'crates/ra_syntax/src')
-rw-r--r-- | crates/ra_syntax/src/ast/generated/nodes.rs | 41 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/node_ext.rs | 28 |
2 files changed, 13 insertions, 56 deletions
diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs index e898c9181..05f75871d 100644 --- a/crates/ra_syntax/src/ast/generated/nodes.rs +++ b/crates/ra_syntax/src/ast/generated/nodes.rs | |||
@@ -24,7 +24,8 @@ impl Attr { | |||
24 | pub fn l_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['[']) } | 24 | pub fn l_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['[']) } |
25 | pub fn path(&self) -> Option<Path> { support::child(&self.syntax) } | 25 | pub fn path(&self) -> Option<Path> { support::child(&self.syntax) } |
26 | pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) } | 26 | pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) } |
27 | pub fn input(&self) -> Option<AttrInput> { support::child(&self.syntax) } | 27 | pub fn literal(&self) -> Option<Literal> { support::child(&self.syntax) } |
28 | pub fn token_tree(&self) -> Option<TokenTree> { support::child(&self.syntax) } | ||
28 | pub fn r_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![']']) } | 29 | pub fn r_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![']']) } |
29 | } | 30 | } |
30 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 31 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
@@ -1378,11 +1379,6 @@ pub enum GenericParam { | |||
1378 | } | 1379 | } |
1379 | impl ast::AttrsOwner for GenericParam {} | 1380 | impl ast::AttrsOwner for GenericParam {} |
1380 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 1381 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
1381 | pub enum AttrInput { | ||
1382 | Literal(Literal), | ||
1383 | TokenTree(TokenTree), | ||
1384 | } | ||
1385 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
1386 | pub enum Stmt { | 1382 | pub enum Stmt { |
1387 | LetStmt(LetStmt), | 1383 | LetStmt(LetStmt), |
1388 | ExprStmt(ExprStmt), | 1384 | ExprStmt(ExprStmt), |
@@ -3342,34 +3338,6 @@ impl AstNode for GenericParam { | |||
3342 | } | 3338 | } |
3343 | } | 3339 | } |
3344 | } | 3340 | } |
3345 | impl From<Literal> for AttrInput { | ||
3346 | fn from(node: Literal) -> AttrInput { AttrInput::Literal(node) } | ||
3347 | } | ||
3348 | impl From<TokenTree> for AttrInput { | ||
3349 | fn from(node: TokenTree) -> AttrInput { AttrInput::TokenTree(node) } | ||
3350 | } | ||
3351 | impl AstNode for AttrInput { | ||
3352 | fn can_cast(kind: SyntaxKind) -> bool { | ||
3353 | match kind { | ||
3354 | LITERAL | TOKEN_TREE => true, | ||
3355 | _ => false, | ||
3356 | } | ||
3357 | } | ||
3358 | fn cast(syntax: SyntaxNode) -> Option<Self> { | ||
3359 | let res = match syntax.kind() { | ||
3360 | LITERAL => AttrInput::Literal(Literal { syntax }), | ||
3361 | TOKEN_TREE => AttrInput::TokenTree(TokenTree { syntax }), | ||
3362 | _ => return None, | ||
3363 | }; | ||
3364 | Some(res) | ||
3365 | } | ||
3366 | fn syntax(&self) -> &SyntaxNode { | ||
3367 | match self { | ||
3368 | AttrInput::Literal(it) => &it.syntax, | ||
3369 | AttrInput::TokenTree(it) => &it.syntax, | ||
3370 | } | ||
3371 | } | ||
3372 | } | ||
3373 | impl From<LetStmt> for Stmt { | 3341 | impl From<LetStmt> for Stmt { |
3374 | fn from(node: LetStmt) -> Stmt { Stmt::LetStmt(node) } | 3342 | fn from(node: LetStmt) -> Stmt { Stmt::LetStmt(node) } |
3375 | } | 3343 | } |
@@ -3471,11 +3439,6 @@ impl std::fmt::Display for GenericParam { | |||
3471 | std::fmt::Display::fmt(self.syntax(), f) | 3439 | std::fmt::Display::fmt(self.syntax(), f) |
3472 | } | 3440 | } |
3473 | } | 3441 | } |
3474 | impl std::fmt::Display for AttrInput { | ||
3475 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
3476 | std::fmt::Display::fmt(self.syntax(), f) | ||
3477 | } | ||
3478 | } | ||
3479 | impl std::fmt::Display for Stmt { | 3442 | impl std::fmt::Display for Stmt { |
3480 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | 3443 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
3481 | std::fmt::Display::fmt(self.syntax(), f) | 3444 | std::fmt::Display::fmt(self.syntax(), f) |
diff --git a/crates/ra_syntax/src/ast/node_ext.rs b/crates/ra_syntax/src/ast/node_ext.rs index d2ee9586d..bba7310ad 100644 --- a/crates/ra_syntax/src/ast/node_ext.rs +++ b/crates/ra_syntax/src/ast/node_ext.rs | |||
@@ -7,7 +7,7 @@ use itertools::Itertools; | |||
7 | use ra_parser::SyntaxKind; | 7 | use ra_parser::SyntaxKind; |
8 | 8 | ||
9 | use crate::{ | 9 | use crate::{ |
10 | ast::{self, support, AstNode, AttrInput, NameOwner, SyntaxNode}, | 10 | ast::{self, support, AstNode, NameOwner, SyntaxNode}, |
11 | SmolStr, SyntaxElement, SyntaxToken, T, | 11 | SmolStr, SyntaxElement, SyntaxToken, T, |
12 | }; | 12 | }; |
13 | 13 | ||
@@ -39,29 +39,23 @@ pub enum AttrKind { | |||
39 | 39 | ||
40 | impl ast::Attr { | 40 | impl ast::Attr { |
41 | pub fn as_simple_atom(&self) -> Option<SmolStr> { | 41 | pub fn as_simple_atom(&self) -> Option<SmolStr> { |
42 | match self.input() { | 42 | if self.eq_token().is_some() || self.token_tree().is_some() { |
43 | None => self.simple_name(), | 43 | return None; |
44 | Some(_) => None, | ||
45 | } | 44 | } |
45 | self.simple_name() | ||
46 | } | 46 | } |
47 | 47 | ||
48 | pub fn as_simple_call(&self) -> Option<(SmolStr, ast::TokenTree)> { | 48 | pub fn as_simple_call(&self) -> Option<(SmolStr, ast::TokenTree)> { |
49 | match self.input() { | 49 | let tt = self.token_tree()?; |
50 | Some(AttrInput::TokenTree(tt)) => Some((self.simple_name()?, tt)), | 50 | Some((self.simple_name()?, tt)) |
51 | _ => None, | ||
52 | } | ||
53 | } | 51 | } |
54 | 52 | ||
55 | pub fn as_simple_key_value(&self) -> Option<(SmolStr, SmolStr)> { | 53 | pub fn as_simple_key_value(&self) -> Option<(SmolStr, SmolStr)> { |
56 | match self.input() { | 54 | let lit = self.literal()?; |
57 | Some(AttrInput::Literal(lit)) => { | 55 | let key = self.simple_name()?; |
58 | let key = self.simple_name()?; | 56 | // FIXME: escape? raw string? |
59 | // FIXME: escape? raw string? | 57 | let value = lit.syntax().first_token()?.text().trim_matches('"').into(); |
60 | let value = lit.syntax().first_token()?.text().trim_matches('"').into(); | 58 | Some((key, value)) |
61 | Some((key, value)) | ||
62 | } | ||
63 | _ => None, | ||
64 | } | ||
65 | } | 59 | } |
66 | 60 | ||
67 | pub fn simple_name(&self) -> Option<SmolStr> { | 61 | pub fn simple_name(&self) -> Option<SmolStr> { |