From b18d2882f4f05078abfcf0595d08c226860de6c3 Mon Sep 17 00:00:00 2001
From: Aleksey Kladov <aleksey.kladov@gmail.com>
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/src')

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<R: TreeRoot> AstNode<R> for File<R> {
     fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
 }
 
+impl<R: TreeRoot> File<R> {
+    pub fn functions<'a>(&'a self) -> impl Iterator<Item = Function<R>> + 'a {
+        self.syntax()
+            .children()
+            .filter_map(Function::cast)
+    }
+}
+
 
 #[derive(Debug, Clone, Copy)]
 pub struct Function<R: TreeRoot = Arc<SyntaxRoot>> {
@@ -36,6 +44,15 @@ impl<R: TreeRoot> AstNode<R> for Function<R> {
     fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
 }
 
+impl<R: TreeRoot> Function<R> {
+    pub fn name(&self) -> Option<Name<R>> {
+        self.syntax()
+            .children()
+            .filter_map(Name::cast)
+            .next()
+    }
+}
+
 
 #[derive(Debug, Clone, Copy)]
 pub struct Name<R: TreeRoot = Arc<SyntaxRoot>> {
@@ -52,3 +69,5 @@ impl<R: TreeRoot> AstNode<R> for Name<R> {
     fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
 }
 
+impl<R: TreeRoot> Name<R> {}
+
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<R: TreeRoot> AstNode<R> for {{ Name }}<R> {
 }
 
 impl<R: TreeRoot> {{ Name }}<R> {
-{% for (method_name, kind) in node.opts %}
-{% set ChildName = kind | camel  %}
-    pub fn {{ method_name }}<'a>(&'a self) -> impl Iterator<Item = {{ ChildKind }}<R>> + '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<Item = {{ ChildName }}<R>> + '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 }}<R>> {
+        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<R: TreeRoot> File<R> {
     pub fn errors(&self) -> Vec<SyntaxError> {
         self.syntax().root.errors.clone()
     }
-
-    pub fn functions<'a>(&'a self) -> impl Iterator<Item = Function<R>> + 'a {
-        self.syntax()
-            .children()
-            .filter_map(Function::cast)
-    }
 }
 
 impl<R: TreeRoot> Function<R> {
-    pub fn name(&self) -> Option<Name<R>> {
-        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