Generalize Owned nodes

This commit is contained in:
Aleksey Kladov 2018-11-06 21:52:00 +03:00
parent 21797bf0ef
commit a5301e94d5
6 changed files with 1249 additions and 1818 deletions

View file

@ -8,27 +8,12 @@ the below applies to the result of this template
use crate::{
ast,
SyntaxNode, SyntaxNodeRef, AstNode,
yellow::{TreeRoot, RaTypes, OwnedRoot, RefRoot},
SyntaxKind::*,
};
{% for node, methods in ast %}
// {{ node }}
#[derive(Debug, Clone)]
pub struct {{ node }}Node(SyntaxNode);
impl {{ node }}Node {
pub fn ast(&self) -> {{ node }} {
{{ node }}::cast(self.0.borrowed()).unwrap()
}
}
impl<'a> From<{{ node }}<'a>> for {{ node }}Node {
fn from(ast: {{ node}}<'a>) -> {{ node }}Node {
let syntax = ast.syntax().owned();
{{ node }}Node(syntax)
}
}
{%- if methods.enum %}
#[derive(Debug, Clone, Copy)]
pub enum {{ node }}<'a> {
@ -56,9 +41,10 @@ impl<'a> AstNode<'a> for {{ node }}<'a> {
}
{% else %}
#[derive(Debug, Clone, Copy)]
pub struct {{ node }}<'a> {
syntax: SyntaxNodeRef<'a>,
pub struct {{ node }}Node<R: TreeRoot<RaTypes> = OwnedRoot> {
syntax: SyntaxNode<R>,
}
pub type {{ node }}<'a> = {{ node }}Node<RefRoot<'a>>;
impl<'a> AstNode<'a> for {{ node }}<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
@ -69,6 +55,16 @@ impl<'a> AstNode<'a> for {{ node }}<'a> {
}
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 -%}