From b18d2882f4f05078abfcf0595d08c226860de6c3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Aug 2018 09:55:32 +0300 Subject: Generate accessors --- crates/libsyntax2/src/ast/generated.rs | 19 +++++++++++++++++++ crates/libsyntax2/src/ast/generated.rs.tera | 26 +++++++++++++++++++++----- crates/libsyntax2/src/ast/mod.rs | 13 ------------- crates/libsyntax2/src/grammar.ron | 4 ++-- 4 files changed, 42 insertions(+), 20 deletions(-) (limited to 'crates/libsyntax2') diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs index 2f813050a..0767df334 100644 --- a/crates/libsyntax2/src/ast/generated.rs +++ b/crates/libsyntax2/src/ast/generated.rs @@ -20,6 +20,14 @@ impl AstNode for File { fn syntax(&self) -> &SyntaxNode { &self.syntax } } +impl File { + pub fn functions<'a>(&'a self) -> impl Iterator> + 'a { + self.syntax() + .children() + .filter_map(Function::cast) + } +} + #[derive(Debug, Clone, Copy)] pub struct Function> { @@ -36,6 +44,15 @@ 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() + } +} + #[derive(Debug, Clone, Copy)] pub struct Name> { @@ -52,3 +69,5 @@ impl AstNode for Name { fn syntax(&self) -> &SyntaxNode { &self.syntax } } +impl Name {} + diff --git a/crates/libsyntax2/src/ast/generated.rs.tera b/crates/libsyntax2/src/ast/generated.rs.tera index afce068c8..86de7bce8 100644 --- a/crates/libsyntax2/src/ast/generated.rs.tera +++ b/crates/libsyntax2/src/ast/generated.rs.tera @@ -21,13 +21,29 @@ impl AstNode for {{ Name }} { } impl {{ Name }} { -{% for (method_name, kind) in node.opts %} -{% set ChildName = kind | camel %} - pub fn {{ method_name }}<'a>(&'a self) -> impl Iterator> + 'a { +{%- if node.collections -%} +{%- for m in node.collections -%} +{%- set method_name = m.0 -%} +{%- set ChildName = m.1 | camel %} + pub fn {{ method_name }}<'a>(&'a self) -> impl Iterator> + 'a { self.syntax() .children() - .filter_map({{ ChildKind }}::cast) + .filter_map({{ ChildName }}::cast) } -{% endfor %} +{% endfor -%} +{%- endif -%} + +{%- if node.options -%} +{%- for m in node.options -%} +{%- set method_name = m.0 -%} +{%- set ChildName = m.1 | camel %} + pub fn {{ method_name }}(&self) -> Option<{{ ChildName }}> { + self.syntax() + .children() + .filter_map({{ ChildName }}::cast) + .next() + } +{% endfor -%} +{%- endif -%} } {% endfor %} diff --git a/crates/libsyntax2/src/ast/mod.rs b/crates/libsyntax2/src/ast/mod.rs index eeb7ae6f6..7d3cdb93d 100644 --- a/crates/libsyntax2/src/ast/mod.rs +++ b/crates/libsyntax2/src/ast/mod.rs @@ -22,22 +22,9 @@ impl File { pub fn errors(&self) -> Vec { self.syntax().root.errors.clone() } - - pub fn functions<'a>(&'a self) -> impl Iterator> + 'a { - self.syntax() - .children() - .filter_map(Function::cast) - } } impl Function { - pub fn name(&self) -> Option> { - self.syntax() - .children() - .filter_map(Name::cast) - .next() - } - pub fn has_atom_attr(&self, atom: &str) -> bool { self.syntax() .children() diff --git a/crates/libsyntax2/src/grammar.ron b/crates/libsyntax2/src/grammar.ron index 9ad2c2ec1..22c61e949 100644 --- a/crates/libsyntax2/src/grammar.ron +++ b/crates/libsyntax2/src/grammar.ron @@ -217,13 +217,13 @@ Grammar( ( kind: "FILE", collections: [ - ("functions", "FUNCTION") + ["functions", "FUNCTION"] ] ), ( kind: "FUNCTION", options: [ - ("name", "NAME") + ["name", "NAME"] ] ), ( -- cgit v1.2.3