rust-analyzer/crates/ra_syntax/src/ast/generated.rs.tera
2018-11-06 23:26:26 +03:00

118 lines
3.4 KiB
Text

{# THIS File is not automatically generated:
the below applies to the result of this template
#}// This file is automatically generated based on the file `./generated.rs.tera` when `cargo gen-syntax` is run
// Do not edit manually
//! This module contains auto-generated Rust AST. Like `SyntaxNode`s, AST nodes
//! are generic over ownership: `X<'a>` things are `Copy` references, `XNode`
//! are Arc-based. You can switch between the two variants using `.owned` and
//! `.borrowed` functions. Most of the code works with borowed mode, and only
//! this mode has all AST accessors.
#![cfg_attr(rustfmt, rustfmt_skip)]
use std::hash::{Hash, Hasher};
use crate::{
ast,
SyntaxNode, SyntaxNodeRef, AstNode,
yellow::{TreeRoot, RaTypes, OwnedRoot, RefRoot},
SyntaxKind::*,
};
{% for node, methods in ast %}
// {{ node }}
{%- if methods.enum %}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum {{ node }}<'a> {
{%- for kind in methods.enum %}
{{ kind }}({{ kind }}<'a>),
{%- endfor %}
}
impl<'a> AstNode<'a> for {{ node }}<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
{%- for kind in methods.enum %}
{{ kind | SCREAM }} => Some({{ node }}::{{ kind }}({{ kind }} { syntax })),
{%- endfor %}
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> {
match self {
{%- for kind in methods.enum %}
{{ node }}::{{ kind }}(inner) => inner.syntax(),
{%- endfor %}
}
}
}
{% else %}
#[derive(Debug, Clone, Copy,)]
pub struct {{ node }}Node<R: TreeRoot<RaTypes> = OwnedRoot> {
pub(crate) syntax: SyntaxNode<R>,
}
pub type {{ node }}<'a> = {{ node }}Node<RefRoot<'a>>;
impl<R1: TreeRoot<RaTypes>, R2: TreeRoot<RaTypes>> PartialEq<{{node}}Node<R1>> for {{node}}Node<R2> {
fn eq(&self, other: &{{node}}Node<R1>) -> bool { self.syntax == other.syntax }
}
impl<R: TreeRoot<RaTypes>> Eq for {{node}}Node<R> {}
impl<R: TreeRoot<RaTypes>> Hash for {{node}}Node<R> {
fn hash<H: Hasher>(&self, state: &mut H) { self.syntax.hash(state) }
}
impl<'a> AstNode<'a> for {{ node }}<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
{{ node | SCREAM }} => Some({{ node }} { syntax }),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<R: TreeRoot<RaTypes>> {{ node }}Node<R> {
pub fn borrowed(&self) -> {{ node }} {
{{ node }}Node { syntax: self.syntax.borrowed() }
}
pub fn owned(&self) -> {{ node }}Node {
{{ node }}Node { syntax: self.syntax.owned() }
}
}
{% endif %}
{% if methods.traits -%}
{%- for t in methods.traits -%}
impl<'a> ast::{{ t }}<'a> for {{ node }}<'a> {}
{% endfor -%}
{%- endif -%}
impl<'a> {{ node }}<'a> {
{%- if methods.collections -%}
{%- for m in methods.collections -%}
{%- set method_name = m.0 -%}
{%- set ChildName = m.1 %}
pub fn {{ method_name }}(self) -> impl Iterator<Item = {{ ChildName }}<'a>> + 'a {
super::children(self)
}
{% endfor -%}
{%- endif -%}
{%- if methods.options -%}
{%- for m in methods.options -%}
{%- if m is string -%}
{%- set method_name = m | snake -%}
{%- set ChildName = m %}
{%- else -%}
{%- set method_name = m.0 -%}
{%- set ChildName = m.1 %}
{%- endif %}
pub fn {{ method_name }}(self) -> Option<{{ ChildName }}<'a>> {
super::child_opt(self)
}
{% endfor -%}
{%- endif -%}
}
{% endfor %}