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 }}> { {%- for kind in methods.enum %} {{ kind }}({{ kind }}), {%- endfor %} } impl AstNode for {{ node }} { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { {%- for kind in methods.enum %} {{ kind | SCREAM }} => Some({{ node }}::{{ kind }}({{ kind }} { syntax })), {%- endfor %} _ => None, } } fn syntax(&self) -> &SyntaxNode { match self { {%- for kind in methods.enum %} {{ node }}::{{ kind }}(inner) => inner.syntax(), {%- endfor %} } } } {% else %} #[derive(Debug, Clone, Copy)] pub struct {{ node }}> { syntax: SyntaxNode, } impl AstNode for {{ node }} { fn cast(syntax: SyntaxNode) -> Option { match syntax.kind() { {{ node | SCREAM }} => Some({{ node }} { syntax }), _ => None, } } fn syntax(&self) -> &SyntaxNode { &self.syntax } } {% 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 }}<'a>(&'a self) -> impl Iterator> + '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 }}> { self.syntax() .children() .filter_map({{ ChildName }}::cast) .next() } {% endfor -%} {%- endif -%} } {% endfor %}