rust/crates/libsyntax2/src/ast/generated.rs.tera

50 lines
1.3 KiB
Text
Raw Normal View History

2018-08-09 16:43:39 +02:00
use std::sync::Arc;
use {
SyntaxNode, SyntaxRoot, TreeRoot, AstNode,
SyntaxKind::*,
};
{% for node in ast %}
{% set Name = node.kind | camel %}
2018-08-10 14:07:43 +02:00
#[derive(Debug, Clone, Copy)]
2018-08-09 16:43:39 +02:00
pub struct {{ Name }}<R: TreeRoot = Arc<SyntaxRoot>> {
syntax: SyntaxNode<R>,
}
impl<R: TreeRoot> AstNode<R> for {{ Name }}<R> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
match syntax.kind() {
{{ node.kind }} => Some({{ Name }} { syntax }),
_ => None,
}
}
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
}
2018-08-11 08:38:27 +02:00
impl<R: TreeRoot> {{ Name }}<R> {
2018-08-11 08:55:32 +02:00
{%- 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 {
2018-08-11 08:38:27 +02:00
self.syntax()
.children()
2018-08-11 08:55:32 +02:00
.filter_map({{ ChildName }}::cast)
2018-08-11 08:38:27 +02:00
}
2018-08-11 08:55:32 +02:00
{% 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 -%}
2018-08-11 08:38:27 +02:00
}
2018-08-09 16:43:39 +02:00
{% endfor %}