aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-11 07:55:32 +0100
committerAleksey Kladov <[email protected]>2018-08-11 07:55:32 +0100
commitb18d2882f4f05078abfcf0595d08c226860de6c3 (patch)
tree247e87f3f8e03cec063d29dd6223e7727dadadb4
parent7581984601b35c113c4bcdf0f8b402b2635be0dc (diff)
Generate accessors
-rw-r--r--crates/libsyntax2/src/ast/generated.rs19
-rw-r--r--crates/libsyntax2/src/ast/generated.rs.tera26
-rw-r--r--crates/libsyntax2/src/ast/mod.rs13
-rw-r--r--crates/libsyntax2/src/grammar.ron4
4 files changed, 42 insertions, 20 deletions
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> {
20 fn syntax(&self) -> &SyntaxNode<R> { &self.syntax } 20 fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
21} 21}
22 22
23impl<R: TreeRoot> File<R> {
24 pub fn functions<'a>(&'a self) -> impl Iterator<Item = Function<R>> + 'a {
25 self.syntax()
26 .children()
27 .filter_map(Function::cast)
28 }
29}
30
23 31
24#[derive(Debug, Clone, Copy)] 32#[derive(Debug, Clone, Copy)]
25pub struct Function<R: TreeRoot = Arc<SyntaxRoot>> { 33pub struct Function<R: TreeRoot = Arc<SyntaxRoot>> {
@@ -36,6 +44,15 @@ impl<R: TreeRoot> AstNode<R> for Function<R> {
36 fn syntax(&self) -> &SyntaxNode<R> { &self.syntax } 44 fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
37} 45}
38 46
47impl<R: TreeRoot> Function<R> {
48 pub fn name(&self) -> Option<Name<R>> {
49 self.syntax()
50 .children()
51 .filter_map(Name::cast)
52 .next()
53 }
54}
55
39 56
40#[derive(Debug, Clone, Copy)] 57#[derive(Debug, Clone, Copy)]
41pub struct Name<R: TreeRoot = Arc<SyntaxRoot>> { 58pub struct Name<R: TreeRoot = Arc<SyntaxRoot>> {
@@ -52,3 +69,5 @@ impl<R: TreeRoot> AstNode<R> for Name<R> {
52 fn syntax(&self) -> &SyntaxNode<R> { &self.syntax } 69 fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
53} 70}
54 71
72impl<R: TreeRoot> Name<R> {}
73
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> {
21} 21}
22 22
23impl<R: TreeRoot> {{ Name }}<R> { 23impl<R: TreeRoot> {{ Name }}<R> {
24{% for (method_name, kind) in node.opts %} 24{%- if node.collections -%}
25{% set ChildName = kind | camel %} 25{%- for m in node.collections -%}
26 pub fn {{ method_name }}<'a>(&'a self) -> impl Iterator<Item = {{ ChildKind }}<R>> + 'a { 26{%- set method_name = m.0 -%}
27{%- set ChildName = m.1 | camel %}
28 pub fn {{ method_name }}<'a>(&'a self) -> impl Iterator<Item = {{ ChildName }}<R>> + 'a {
27 self.syntax() 29 self.syntax()
28 .children() 30 .children()
29 .filter_map({{ ChildKind }}::cast) 31 .filter_map({{ ChildName }}::cast)
30 } 32 }
31{% endfor %} 33{% endfor -%}
34{%- endif -%}
35
36{%- if node.options -%}
37{%- for m in node.options -%}
38{%- set method_name = m.0 -%}
39{%- set ChildName = m.1 | camel %}
40 pub fn {{ method_name }}(&self) -> Option<{{ ChildName }}<R>> {
41 self.syntax()
42 .children()
43 .filter_map({{ ChildName }}::cast)
44 .next()
45 }
46{% endfor -%}
47{%- endif -%}
32} 48}
33{% endfor %} 49{% 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> {
22 pub fn errors(&self) -> Vec<SyntaxError> { 22 pub fn errors(&self) -> Vec<SyntaxError> {
23 self.syntax().root.errors.clone() 23 self.syntax().root.errors.clone()
24 } 24 }
25
26 pub fn functions<'a>(&'a self) -> impl Iterator<Item = Function<R>> + 'a {
27 self.syntax()
28 .children()
29 .filter_map(Function::cast)
30 }
31} 25}
32 26
33impl<R: TreeRoot> Function<R> { 27impl<R: TreeRoot> Function<R> {
34 pub fn name(&self) -> Option<Name<R>> {
35 self.syntax()
36 .children()
37 .filter_map(Name::cast)
38 .next()
39 }
40
41 pub fn has_atom_attr(&self, atom: &str) -> bool { 28 pub fn has_atom_attr(&self, atom: &str) -> bool {
42 self.syntax() 29 self.syntax()
43 .children() 30 .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(
217 ( 217 (
218 kind: "FILE", 218 kind: "FILE",
219 collections: [ 219 collections: [
220 ("functions", "FUNCTION") 220 ["functions", "FUNCTION"]
221 ] 221 ]
222 ), 222 ),
223 ( 223 (
224 kind: "FUNCTION", 224 kind: "FUNCTION",
225 options: [ 225 options: [
226 ("name", "NAME") 226 ["name", "NAME"]
227 ] 227 ]
228 ), 228 ),
229 ( 229 (