aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/ast/generated.rs.tera
blob: bf9db4ee87f61fec1a380358008fb17c54c51497 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
{# 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 rowan::TransparentNewType;

use crate::{
    SyntaxNode, SyntaxKind::*,
    yellow::{RaTypes, TreeArc},
    ast::{self, AstNode},
};
{% for node, methods in ast %}
// {{ node }}

{%- if methods.enum %}
#[derive(Debug, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct {{ node }} {
    pub(crate) syntax: SyntaxNode,
}
unsafe impl TransparentNewType for {{ node }} {
    type Repr = rowan::SyntaxNode<RaTypes>;
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum {{ node }}Kind<'a> {
{%- for kind in methods.enum %}
    {{ kind }}(&'a {{ kind }}),
{%- endfor %}
}

impl AstNode for {{ node }} {
    fn cast(syntax: &SyntaxNode) -> Option<&Self> {
        match syntax.kind() {
            {%- for kind in methods.enum %}
            | {{ kind | SCREAM }}
            {%- endfor %} => Some({{ node }}::from_repr(syntax.into_repr())),
            _ => None,
        }
    }
    fn syntax(&self) -> &SyntaxNode { &self.syntax }
    fn to_owned(&self) -> TreeArc<{{ node }}> { TreeArc::cast(self.syntax.to_owned()) }
}

impl {{ node }} {
    pub fn kind(&self) -> {{ node }}Kind {
        match self.syntax.kind() {
            {%- for kind in methods.enum %}
            {{ kind | SCREAM }} => {{ node }}Kind::{{ kind }}({{ kind }}::cast(&self.syntax).unwrap()),
            {%- endfor %}
            _ => unreachable!(),
        }
    }
}
{% else %}
#[derive(Debug, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct {{ node }} {
    pub(crate) syntax: SyntaxNode,
}
unsafe impl TransparentNewType for {{ node }} {
    type Repr = rowan::SyntaxNode<RaTypes>;
}

impl AstNode for {{ node }} {
    fn cast(syntax: &SyntaxNode) -> Option<&Self> {
        match syntax.kind() {
            {{ node | SCREAM }} => Some({{ node }}::from_repr(syntax.into_repr())),
            _ => None,
        }
    }
    fn syntax(&self) -> &SyntaxNode { &self.syntax }
    fn to_owned(&self) -> TreeArc<{{ node }}> { TreeArc::cast(self.syntax.to_owned()) }
}

{% endif %}
{% if methods.traits -%}

{%- for t in methods.traits -%}
impl ast::{{ t }} for {{ node }} {}
{% endfor -%}

{%- endif -%}

impl {{ node }} {
{%- 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 }}> {
        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 }}> {
        super::child_opt(self)
    }
{% endfor -%}
{%- endif -%}
}
{% endfor %}