Generate accessors

This commit is contained in:
Aleksey Kladov 2018-08-11 09:55:32 +03:00
parent 7581984601
commit b18d2882f4
4 changed files with 42 additions and 20 deletions

View file

@ -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> {}

View file

@ -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)
}
{% endfor %}
.filter_map({{ ChildName }}::cast)
}
{% 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 %}

View file

@ -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()

View file

@ -217,13 +217,13 @@ Grammar(
(
kind: "FILE",
collections: [
("functions", "FUNCTION")
["functions", "FUNCTION"]
]
),
(
kind: "FUNCTION",
options: [
("name", "NAME")
["name", "NAME"]
]
),
(