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 } 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)] #[derive(Debug, Clone, Copy)]
pub struct Function<R: TreeRoot = Arc<SyntaxRoot>> { 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 } 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)] #[derive(Debug, Clone, Copy)]
pub struct Name<R: TreeRoot = Arc<SyntaxRoot>> { 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 } 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> { impl<R: TreeRoot> {{ Name }}<R> {
{% for (method_name, kind) in node.opts %} {%- if node.collections -%}
{% set ChildName = kind | camel %} {%- for m in node.collections -%}
pub fn {{ method_name }}<'a>(&'a self) -> impl Iterator<Item = {{ ChildKind }}<R>> + 'a { {%- 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() self.syntax()
.children() .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 %} {% endfor %}

View file

@ -22,22 +22,9 @@ impl<R: TreeRoot> File<R> {
pub fn errors(&self) -> Vec<SyntaxError> { pub fn errors(&self) -> Vec<SyntaxError> {
self.syntax().root.errors.clone() 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> { 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 { pub fn has_atom_attr(&self, atom: &str) -> bool {
self.syntax() self.syntax()
.children() .children()

View file

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