Add AstElement trait, generate tokens, support tokens in enums

- Adds a new AstElement trait that is implemented by all generated
  node, token and enum structs

- Overhauls the code generators to code-generate all tokens, and
  also enhances enums to support including tokens, node, and nested
  enums
This commit is contained in:
Luca Barbieri 2020-04-03 21:12:08 +02:00 committed by Aleksey Kladov
parent 4762c6d9c6
commit 68196ccc10
3 changed files with 380 additions and 94 deletions

View file

@ -1,26 +1,10 @@
//! There are many AstNodes, but only a few tokens, so we hand-write them here.
use crate::{
ast::AstToken,
SyntaxKind::{COMMENT, RAW_STRING, STRING, WHITESPACE},
SyntaxToken, TextRange, TextUnit,
ast::{AstToken, Comment, RawString, String, Whitespace},
TextRange, TextUnit,
};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Comment(SyntaxToken);
impl AstToken for Comment {
fn cast(token: SyntaxToken) -> Option<Self> {
match token.kind() {
COMMENT => Some(Comment(token)),
_ => None,
}
}
fn syntax(&self) -> &SyntaxToken {
&self.0
}
}
impl Comment {
pub fn kind(&self) -> CommentKind {
kind_by_prefix(self.text())
@ -89,20 +73,6 @@ fn prefix_by_kind(kind: CommentKind) -> &'static str {
unreachable!()
}
pub struct Whitespace(SyntaxToken);
impl AstToken for Whitespace {
fn cast(token: SyntaxToken) -> Option<Self> {
match token.kind() {
WHITESPACE => Some(Whitespace(token)),
_ => None,
}
}
fn syntax(&self) -> &SyntaxToken {
&self.0
}
}
impl Whitespace {
pub fn spans_multiple_lines(&self) -> bool {
let text = self.text();
@ -168,20 +138,6 @@ pub trait HasStringValue: HasQuotes {
fn value(&self) -> Option<std::string::String>;
}
pub struct String(SyntaxToken);
impl AstToken for String {
fn cast(token: SyntaxToken) -> Option<Self> {
match token.kind() {
STRING => Some(String(token)),
_ => None,
}
}
fn syntax(&self) -> &SyntaxToken {
&self.0
}
}
impl HasStringValue for String {
fn value(&self) -> Option<std::string::String> {
let text = self.text().as_str();
@ -201,20 +157,6 @@ impl HasStringValue for String {
}
}
pub struct RawString(SyntaxToken);
impl AstToken for RawString {
fn cast(token: SyntaxToken) -> Option<Self> {
match token.kind() {
RAW_STRING => Some(RawString(token)),
_ => None,
}
}
fn syntax(&self) -> &SyntaxToken {
&self.0
}
}
impl HasStringValue for RawString {
fn value(&self) -> Option<std::string::String> {
let text = self.text().as_str();