Use dict for AST

This commit is contained in:
Aleksey Kladov 2018-08-11 10:11:58 +03:00
parent e69c70e2a2
commit 2e971cdcbb
3 changed files with 18 additions and 26 deletions

View file

@ -4,7 +4,6 @@ use {
SyntaxKind::*, SyntaxKind::*,
}; };
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct File<R: TreeRoot = Arc<SyntaxRoot>> { pub struct File<R: TreeRoot = Arc<SyntaxRoot>> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
@ -28,7 +27,6 @@ impl<R: TreeRoot> File<R> {
} }
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct Function<R: TreeRoot = Arc<SyntaxRoot>> { pub struct Function<R: TreeRoot = Arc<SyntaxRoot>> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
@ -53,7 +51,6 @@ impl<R: TreeRoot> Function<R> {
} }
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct Name<R: TreeRoot = Arc<SyntaxRoot>> { pub struct Name<R: TreeRoot = Arc<SyntaxRoot>> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,

View file

@ -3,28 +3,27 @@ use {
SyntaxNode, SyntaxRoot, TreeRoot, AstNode, SyntaxNode, SyntaxRoot, TreeRoot, AstNode,
SyntaxKind::*, SyntaxKind::*,
}; };
{% for node in ast %} {% for node, methods in ast %}
{% set Name = node.kind | camel %}
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct {{ Name }}<R: TreeRoot = Arc<SyntaxRoot>> { pub struct {{ node }}<R: TreeRoot = Arc<SyntaxRoot>> {
syntax: SyntaxNode<R>, syntax: SyntaxNode<R>,
} }
impl<R: TreeRoot> AstNode<R> for {{ Name }}<R> { impl<R: TreeRoot> AstNode<R> for {{ node }}<R> {
fn cast(syntax: SyntaxNode<R>) -> Option<Self> { fn cast(syntax: SyntaxNode<R>) -> Option<Self> {
match syntax.kind() { match syntax.kind() {
{{ node.kind }} => Some({{ Name }} { syntax }), {{ node | upper }} => Some({{ node }} { syntax }),
_ => None, _ => None,
} }
} }
fn syntax(&self) -> &SyntaxNode<R> { &self.syntax } fn syntax(&self) -> &SyntaxNode<R> { &self.syntax }
} }
impl<R: TreeRoot> {{ Name }}<R> { impl<R: TreeRoot> {{ node }}<R> {
{%- if node.collections -%} {%- if methods.collections -%}
{%- for m in node.collections -%} {%- for m in methods.collections -%}
{%- set method_name = m.0 -%} {%- set method_name = m.0 -%}
{%- set ChildName = m.1 | camel %} {%- set ChildName = m.1 %}
pub fn {{ method_name }}<'a>(&'a self) -> impl Iterator<Item = {{ ChildName }}<R>> + 'a { pub fn {{ method_name }}<'a>(&'a self) -> impl Iterator<Item = {{ ChildName }}<R>> + 'a {
self.syntax() self.syntax()
.children() .children()
@ -33,10 +32,10 @@ impl<R: TreeRoot> {{ Name }}<R> {
{% endfor -%} {% endfor -%}
{%- endif -%} {%- endif -%}
{%- if node.options -%} {%- if methods.options -%}
{%- for m in node.options -%} {%- for m in methods.options -%}
{%- set method_name = m.0 -%} {%- set method_name = m.0 -%}
{%- set ChildName = m.1 | camel %} {%- set ChildName = m.1 %}
pub fn {{ method_name }}(&self) -> Option<{{ ChildName }}<R>> { pub fn {{ method_name }}(&self) -> Option<{{ ChildName }}<R>> {
self.syntax() self.syntax()
.children() .children()

View file

@ -213,21 +213,17 @@ Grammar(
"SELF_PARAM", "SELF_PARAM",
"ARG_LIST", "ARG_LIST",
], ],
ast: [ ast: {
( "File": (
kind: "FILE",
collections: [ collections: [
["functions", "FUNCTION"] ["functions", "Function"]
] ]
), ),
( "Function": (
kind: "FUNCTION",
options: [ options: [
["name", "NAME"] ["name", "Name"]
] ]
), ),
( "Name": (),
kind: "NAME" },
),
]
) )