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/algo/mod.rs | 1 + crates/libsyntax2/src/algo/visit.rs | 52 +++++++++++++++++++ crates/libsyntax2/src/ast/generated.rs | 80 +++++++++-------------------- crates/libsyntax2/src/ast/generated.rs.tera | 8 +++ crates/libsyntax2/src/ast/mod.rs | 9 ++++ crates/libsyntax2/src/grammar.ron | 28 +++------- crates/libsyntax2/src/yellow/syntax.rs | 6 +++ 7 files changed, 107 insertions(+), 77 deletions(-) create mode 100644 crates/libsyntax2/src/algo/visit.rs (limited to 'crates/libsyntax2/src') diff --git a/crates/libsyntax2/src/algo/mod.rs b/crates/libsyntax2/src/algo/mod.rs index d2de70fd4..263b58d97 100644 --- a/crates/libsyntax2/src/algo/mod.rs +++ b/crates/libsyntax2/src/algo/mod.rs @@ -1,4 +1,5 @@ pub mod walk; +pub mod visit; use {SyntaxNodeRef, TextUnit, TextRange}; diff --git a/crates/libsyntax2/src/algo/visit.rs b/crates/libsyntax2/src/algo/visit.rs new file mode 100644 index 000000000..dc5afa5a9 --- /dev/null +++ b/crates/libsyntax2/src/algo/visit.rs @@ -0,0 +1,52 @@ +use std::marker::PhantomData; +use {SyntaxNodeRef, AstNode, SyntaxRoot}; + + +pub fn visitor<'a, T>() -> impl Visitor<'a, Output=T> { + EmptyVisitor { ph: PhantomData } +} + +pub trait Visitor<'a>: Sized { + type Output; + fn accept(self, node: SyntaxNodeRef<'a>) -> Option; + fn visit(self, f: F) -> Vis + where N: AstNode<&'a SyntaxRoot>, + F: FnOnce(N) -> Self::Output, + { + Vis { inner: self, f, ph: PhantomData } + } +} + +#[derive(Debug)] +struct EmptyVisitor { + ph: PhantomData T> +} + +impl<'a, T> Visitor<'a> for EmptyVisitor { + type Output = T; + + fn accept(self, _node: SyntaxNodeRef<'a>) -> Option { + None + } +} + +#[derive(Debug)] +pub struct Vis { + inner: V, + f: F, + ph: PhantomData, +} + +impl<'a, V, N, F> Visitor<'a> for Vis + where + V: Visitor<'a>, + N: AstNode<&'a SyntaxRoot>, + F: FnOnce(N) -> >::Output, +{ + type Output = >::Output; + + fn accept(self, node: SyntaxNodeRef<'a>) -> Option { + let Vis { inner, f, .. } = self; + inner.accept(node).or_else(|| N::cast(node).map(f)) + } +} 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() diff --git a/crates/libsyntax2/src/grammar.ron b/crates/libsyntax2/src/grammar.ron index c9470d4fa..3fe8fdf0b 100644 --- a/crates/libsyntax2/src/grammar.ron +++ b/crates/libsyntax2/src/grammar.ron @@ -220,39 +220,25 @@ Grammar( ] ), "Function": ( - options: [ - ["name", "Name"] - ] + traits: ["NameOwner"] ), "Struct": ( - options: [ - ["name", "Name"] - ] + traits: ["NameOwner"] ), "Enum": ( - options: [ - ["name", "Name"] - ] + traits: ["NameOwner"] ), "Trait": ( - options: [ - ["name", "Name"] - ] + traits: ["NameOwner"] ), "Module": ( - options: [ - ["name", "Name"] - ] + traits: ["NameOwner"] ), "ConstItem": ( - options: [ - ["name", "Name"] - ] + traits: ["NameOwner"] ), "StaticItem": ( - options: [ - ["name", "Name"] - ] + traits: ["NameOwner"] ), "Name": (), }, diff --git a/crates/libsyntax2/src/yellow/syntax.rs b/crates/libsyntax2/src/yellow/syntax.rs index 6e33310f1..a22275ed9 100644 --- a/crates/libsyntax2/src/yellow/syntax.rs +++ b/crates/libsyntax2/src/yellow/syntax.rs @@ -28,6 +28,12 @@ impl Eq for SyntaxNode {} pub type SyntaxNodeRef<'a> = SyntaxNode<&'a SyntaxRoot>; +#[test] +fn syntax_node_ref_is_copy() { + fn assert_copy(){} + assert_copy::() +} + #[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)] pub struct SyntaxError { pub msg: String, -- cgit v1.2.3