rust-analyzer/crates/libsyntax2/src/ast/generated.rs.tera
2018-08-14 12:38:20 +03:00

82 lines
2.1 KiB
Text

use std::sync::Arc;
use {
ast,
SyntaxNode, SyntaxRoot, TreeRoot, AstNode,
SyntaxKind::*,
};
{% for node, methods in ast %}
// {{ node }}
{%- if methods.enum %}
#[derive(Debug, Clone, Copy)]
pub enum {{ node }}<R: TreeRoot = Arc<SyntaxRoot>> {
{%- for kind in methods.enum %}
{{ kind }}({{ kind }}<R>),
{%- endfor %}
}
impl<R: TreeRoot> AstNode<R> for {{ node }}<R> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
match syntax.kind() {
{%- for kind in methods.enum %}
{{ kind | SCREAM }} => Some({{ node }}::{{ kind }}({{ kind }} { syntax })),
{%- endfor %}
_ => None,
}
}
fn syntax(&self) -> &SyntaxNode<R> {
match self {
{%- for kind in methods.enum %}
{{ node }}::{{ kind }}(inner) => inner.syntax(),
{%- endfor %}
}
}
}
{% else %}
#[derive(Debug, Clone, Copy)]
pub struct {{ node }}<R: TreeRoot = Arc<SyntaxRoot>> {
syntax: SyntaxNode<R>,
}
impl<R: TreeRoot> AstNode<R> for {{ node }}<R> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
match syntax.kind() {
{{ node | SCREAM }} => Some({{ node }} { syntax }),
_ => None,
}
}
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
}
{% endif %}
{% if methods.traits -%}
{%- for t in methods.traits -%}
impl<R: TreeRoot> ast::{{ t }}<R> for {{ node }}<R> {}
{% endfor -%}
{%- endif -%}
impl<R: TreeRoot> {{ node }}<R> {
{%- if methods.collections -%}
{%- for m in methods.collections -%}
{%- set method_name = m.0 -%}
{%- set ChildName = m.1 %}
pub fn {{ method_name }}<'a>(&'a self) -> impl Iterator<Item = {{ ChildName }}<R>> + 'a {
self.syntax()
.children()
.filter_map({{ ChildName }}::cast)
}
{% endfor -%}
{%- endif -%}
{%- if methods.options -%}
{%- for m in methods.options -%}
{%- set method_name = m.0 -%}
{%- set ChildName = m.1 %}
pub fn {{ method_name }}(&self) -> Option<{{ ChildName }}<R>> {
self.syntax()
.children()
.filter_map({{ ChildName }}::cast)
.next()
}
{% endfor -%}
{%- endif -%}
}
{% endfor %}