From 7afd84febc76a75a3ed1be75c57ff35d7b8b3de6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Aug 2018 12:28:59 +0300 Subject: visitor --- crates/libsyntax2/src/ast/generated.rs | 80 +++++++++-------------------- crates/libsyntax2/src/ast/generated.rs.tera | 8 +++ crates/libsyntax2/src/ast/mod.rs | 9 ++++ 3 files changed, 41 insertions(+), 56 deletions(-) (limited to 'crates/libsyntax2/src/ast') diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs index 7a2a9c7d4..a4b116941 100644 --- a/crates/libsyntax2/src/ast/generated.rs +++ b/crates/libsyntax2/src/ast/generated.rs @@ -1,9 +1,11 @@ use std::sync::Arc; use { + ast, SyntaxNode, SyntaxRoot, TreeRoot, AstNode, SyntaxKind::*, }; +// ConstItem #[derive(Debug, Clone, Copy)] pub struct ConstItem> { syntax: SyntaxNode, @@ -19,15 +21,10 @@ impl AstNode for ConstItem { fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl ConstItem { - pub fn name(&self) -> Option> { - self.syntax() - .children() - .filter_map(Name::cast) - .next() - } -} +impl ast::NameOwner for ConstItem {} +impl ConstItem {} +// Enum #[derive(Debug, Clone, Copy)] pub struct Enum> { syntax: SyntaxNode, @@ -43,15 +40,10 @@ impl AstNode for Enum { fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl Enum { - pub fn name(&self) -> Option> { - self.syntax() - .children() - .filter_map(Name::cast) - .next() - } -} +impl ast::NameOwner for Enum {} +impl Enum {} +// File #[derive(Debug, Clone, Copy)] pub struct File> { syntax: SyntaxNode, @@ -75,6 +67,7 @@ impl File { } } +// Function #[derive(Debug, Clone, Copy)] pub struct Function> { syntax: SyntaxNode, @@ -90,15 +83,10 @@ impl AstNode for Function { fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl Function { - pub fn name(&self) -> Option> { - self.syntax() - .children() - .filter_map(Name::cast) - .next() - } -} +impl ast::NameOwner for Function {} +impl Function {} +// Module #[derive(Debug, Clone, Copy)] pub struct Module> { syntax: SyntaxNode, @@ -114,15 +102,10 @@ impl AstNode for Module { fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl Module { - pub fn name(&self) -> Option> { - self.syntax() - .children() - .filter_map(Name::cast) - .next() - } -} +impl ast::NameOwner for Module {} +impl Module {} +// Name #[derive(Debug, Clone, Copy)] pub struct Name> { syntax: SyntaxNode, @@ -140,6 +123,7 @@ impl AstNode for Name { impl Name {} +// StaticItem #[derive(Debug, Clone, Copy)] pub struct StaticItem> { syntax: SyntaxNode, @@ -155,15 +139,10 @@ impl AstNode for StaticItem { fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl StaticItem { - pub fn name(&self) -> Option> { - self.syntax() - .children() - .filter_map(Name::cast) - .next() - } -} +impl ast::NameOwner for StaticItem {} +impl StaticItem {} +// Struct #[derive(Debug, Clone, Copy)] pub struct Struct> { syntax: SyntaxNode, @@ -179,15 +158,10 @@ impl AstNode for Struct { fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl Struct { - pub fn name(&self) -> Option> { - self.syntax() - .children() - .filter_map(Name::cast) - .next() - } -} +impl ast::NameOwner for Struct {} +impl Struct {} +// Trait #[derive(Debug, Clone, Copy)] pub struct Trait> { syntax: SyntaxNode, @@ -203,12 +177,6 @@ impl AstNode for Trait { fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl Trait { - pub fn name(&self) -> Option> { - self.syntax() - .children() - .filter_map(Name::cast) - .next() - } -} +impl ast::NameOwner for Trait {} +impl Trait {} diff --git a/crates/libsyntax2/src/ast/generated.rs.tera b/crates/libsyntax2/src/ast/generated.rs.tera index 86b8b05d1..3d79b5543 100644 --- a/crates/libsyntax2/src/ast/generated.rs.tera +++ b/crates/libsyntax2/src/ast/generated.rs.tera @@ -1,9 +1,11 @@ use std::sync::Arc; use { + ast, SyntaxNode, SyntaxRoot, TreeRoot, AstNode, SyntaxKind::*, }; {% for node, methods in ast %} +// {{ node }} #[derive(Debug, Clone, Copy)] pub struct {{ node }}> { syntax: SyntaxNode, @@ -19,6 +21,12 @@ impl AstNode for {{ node }} { fn syntax(&self) -> &SyntaxNode { &self.syntax } } +{% if methods.traits -%} +{%- for t in methods.traits -%} +impl ast::{{ t }} for {{ node }} {} +{% endfor -%} +{%- endif -%} + impl {{ node }} { {%- if methods.collections -%} {%- for m in methods.collections -%} diff --git a/crates/libsyntax2/src/ast/mod.rs b/crates/libsyntax2/src/ast/mod.rs index 7d3cdb93d..56bc099fe 100644 --- a/crates/libsyntax2/src/ast/mod.rs +++ b/crates/libsyntax2/src/ast/mod.rs @@ -12,6 +12,15 @@ pub trait AstNode: Sized { fn syntax(&self) -> &SyntaxNode; } +pub trait NameOwner: AstNode { + fn name(&self) -> Option> { + self.syntax() + .children() + .filter_map(Name::cast) + .next() + } +} + impl File> { pub fn parse(text: &str) -> Self { File::cast(::parse(text)).unwrap() -- cgit v1.2.3