From a5515d9d6f215da4351b482d839aab5212fa0e6f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Aug 2018 13:11:20 +0300 Subject: Add derive handles cursor --- crates/libsyntax2/src/ast/generated.rs | 1 + crates/libsyntax2/src/ast/mod.rs | 30 +++++++++++++++++++++--------- crates/libsyntax2/src/grammar.ron | 5 ++++- 3 files changed, 26 insertions(+), 10 deletions(-) (limited to 'crates/libsyntax2/src') diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs index c575e15df..be3f73f7e 100644 --- a/crates/libsyntax2/src/ast/generated.rs +++ b/crates/libsyntax2/src/ast/generated.rs @@ -339,6 +339,7 @@ impl AstNode for NominalDef { } } +impl ast::AttrsOwner for NominalDef {} impl NominalDef {} // ParenType diff --git a/crates/libsyntax2/src/ast/mod.rs b/crates/libsyntax2/src/ast/mod.rs index fe8f91d15..d53b12ab8 100644 --- a/crates/libsyntax2/src/ast/mod.rs +++ b/crates/libsyntax2/src/ast/mod.rs @@ -52,19 +52,31 @@ impl File { impl FnDef { pub fn has_atom_attr(&self, atom: &str) -> bool { self.attrs() - .filter_map(|x| x.value()) - .filter_map(|x| as_atom(x)) + .filter_map(|x| x.as_atom()) .any(|x| x == atom) } } -fn as_atom(tt: TokenTree) -> Option { - let syntax = tt.syntax_ref(); - let (_bra, attr, _ket) = syntax.children().collect_tuple()?; - if attr.kind() == IDENT { - Some(attr.leaf_text().unwrap()) - } else { - None +impl Attr { + pub fn as_atom(&self) -> Option { + let tt = self.value()?; + let (_bra, attr, _ket) = tt.syntax().children().collect_tuple()?; + if attr.kind() == IDENT { + Some(attr.leaf_text().unwrap()) + } else { + None + } + } + + pub fn as_call(&self) -> Option<(SmolStr, TokenTree)> { + let tt = self.value()?; + let (_bra, attr, args, _ket) = tt.syntax().children().collect_tuple()?; + let args = TokenTree::cast(args)?; + if attr.kind() == IDENT { + Some((attr.leaf_text().unwrap(), args)) + } else { + None + } } } diff --git a/crates/libsyntax2/src/grammar.ron b/crates/libsyntax2/src/grammar.ron index 4e523da9a..abeffb2c3 100644 --- a/crates/libsyntax2/src/grammar.ron +++ b/crates/libsyntax2/src/grammar.ron @@ -272,6 +272,9 @@ Grammar( "DynTraitType", ]), - "NominalDef": ( enum: ["StructDef", "EnumDef"]), + "NominalDef": ( + enum: ["StructDef", "EnumDef"], + traits: [ "AttrsOwner" ], + ), }, ) -- cgit v1.2.3