aboutsummaryrefslogtreecommitdiff
path: root/crates/libsyntax2/src/ast/generated.rs.tera
blob: f83da032686a67d76868f77b91f8e6f58b7e0a2a (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
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 %}