diff --git a/crates/libsyntax2/src/ast/generated.rs b/crates/libsyntax2/src/ast/generated.rs index 2f813050ab..0767df334e 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 afce068c86..86de7bce87 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 eeb7ae6f63..7d3cdb93db 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 9ad2c2ec1a..22c61e9491 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"] ] ), (