Move the rest of the tokens to generated/tokens

This commit is contained in:
Aleksey Kladov 2020-04-09 17:58:15 +02:00
parent c80795e274
commit 4a063e651f
4 changed files with 751 additions and 738 deletions

View file

@ -2,9 +2,9 @@
use super::tokens::*;
use crate::{
ast::{self, support, AstChildren, AstNode, AstToken},
ast::{self, support, AstChildren, AstNode},
SyntaxKind::{self, *},
SyntaxNode, SyntaxToken,
SyntaxNode,
};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SourceFile {
@ -5877,657 +5877,6 @@ impl AstNode for Stmt {
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum LeftDelimiter {
LParen(LParen),
LBrack(LBrack),
LCurly(LCurly),
}
impl From<LParen> for LeftDelimiter {
fn from(node: LParen) -> LeftDelimiter {
LeftDelimiter::LParen(node)
}
}
impl From<LBrack> for LeftDelimiter {
fn from(node: LBrack) -> LeftDelimiter {
LeftDelimiter::LBrack(node)
}
}
impl From<LCurly> for LeftDelimiter {
fn from(node: LCurly) -> LeftDelimiter {
LeftDelimiter::LCurly(node)
}
}
impl std::fmt::Display for LeftDelimiter {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl AstToken for LeftDelimiter {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
L_PAREN | L_BRACK | L_CURLY => true,
_ => false,
}
}
fn cast(syntax: SyntaxToken) -> Option<Self> {
let res = match syntax.kind() {
L_PAREN => LeftDelimiter::LParen(LParen { syntax }),
L_BRACK => LeftDelimiter::LBrack(LBrack { syntax }),
L_CURLY => LeftDelimiter::LCurly(LCurly { syntax }),
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxToken {
match self {
LeftDelimiter::LParen(it) => &it.syntax,
LeftDelimiter::LBrack(it) => &it.syntax,
LeftDelimiter::LCurly(it) => &it.syntax,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RightDelimiter {
RParen(RParen),
RBrack(RBrack),
RCurly(RCurly),
}
impl From<RParen> for RightDelimiter {
fn from(node: RParen) -> RightDelimiter {
RightDelimiter::RParen(node)
}
}
impl From<RBrack> for RightDelimiter {
fn from(node: RBrack) -> RightDelimiter {
RightDelimiter::RBrack(node)
}
}
impl From<RCurly> for RightDelimiter {
fn from(node: RCurly) -> RightDelimiter {
RightDelimiter::RCurly(node)
}
}
impl std::fmt::Display for RightDelimiter {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl AstToken for RightDelimiter {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
R_PAREN | R_BRACK | R_CURLY => true,
_ => false,
}
}
fn cast(syntax: SyntaxToken) -> Option<Self> {
let res = match syntax.kind() {
R_PAREN => RightDelimiter::RParen(RParen { syntax }),
R_BRACK => RightDelimiter::RBrack(RBrack { syntax }),
R_CURLY => RightDelimiter::RCurly(RCurly { syntax }),
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxToken {
match self {
RightDelimiter::RParen(it) => &it.syntax,
RightDelimiter::RBrack(it) => &it.syntax,
RightDelimiter::RCurly(it) => &it.syntax,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RangeSeparator {
Dotdot(Dotdot),
Dotdotdot(Dotdotdot),
Dotdoteq(Dotdoteq),
}
impl From<Dotdot> for RangeSeparator {
fn from(node: Dotdot) -> RangeSeparator {
RangeSeparator::Dotdot(node)
}
}
impl From<Dotdotdot> for RangeSeparator {
fn from(node: Dotdotdot) -> RangeSeparator {
RangeSeparator::Dotdotdot(node)
}
}
impl From<Dotdoteq> for RangeSeparator {
fn from(node: Dotdoteq) -> RangeSeparator {
RangeSeparator::Dotdoteq(node)
}
}
impl std::fmt::Display for RangeSeparator {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl AstToken for RangeSeparator {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
DOTDOT | DOTDOTDOT | DOTDOTEQ => true,
_ => false,
}
}
fn cast(syntax: SyntaxToken) -> Option<Self> {
let res = match syntax.kind() {
DOTDOT => RangeSeparator::Dotdot(Dotdot { syntax }),
DOTDOTDOT => RangeSeparator::Dotdotdot(Dotdotdot { syntax }),
DOTDOTEQ => RangeSeparator::Dotdoteq(Dotdoteq { syntax }),
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxToken {
match self {
RangeSeparator::Dotdot(it) => &it.syntax,
RangeSeparator::Dotdotdot(it) => &it.syntax,
RangeSeparator::Dotdoteq(it) => &it.syntax,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum BinOp {
Pipepipe(Pipepipe),
Ampamp(Ampamp),
Eqeq(Eqeq),
Neq(Neq),
Lteq(Lteq),
Gteq(Gteq),
LAngle(LAngle),
RAngle(RAngle),
Plus(Plus),
Star(Star),
Minus(Minus),
Slash(Slash),
Percent(Percent),
Shl(Shl),
Shr(Shr),
Caret(Caret),
Pipe(Pipe),
Amp(Amp),
Eq(Eq),
Pluseq(Pluseq),
Slasheq(Slasheq),
Stareq(Stareq),
Percenteq(Percenteq),
Shreq(Shreq),
Shleq(Shleq),
Minuseq(Minuseq),
Pipeeq(Pipeeq),
Ampeq(Ampeq),
Careteq(Careteq),
}
impl From<Pipepipe> for BinOp {
fn from(node: Pipepipe) -> BinOp {
BinOp::Pipepipe(node)
}
}
impl From<Ampamp> for BinOp {
fn from(node: Ampamp) -> BinOp {
BinOp::Ampamp(node)
}
}
impl From<Eqeq> for BinOp {
fn from(node: Eqeq) -> BinOp {
BinOp::Eqeq(node)
}
}
impl From<Neq> for BinOp {
fn from(node: Neq) -> BinOp {
BinOp::Neq(node)
}
}
impl From<Lteq> for BinOp {
fn from(node: Lteq) -> BinOp {
BinOp::Lteq(node)
}
}
impl From<Gteq> for BinOp {
fn from(node: Gteq) -> BinOp {
BinOp::Gteq(node)
}
}
impl From<LAngle> for BinOp {
fn from(node: LAngle) -> BinOp {
BinOp::LAngle(node)
}
}
impl From<RAngle> for BinOp {
fn from(node: RAngle) -> BinOp {
BinOp::RAngle(node)
}
}
impl From<Plus> for BinOp {
fn from(node: Plus) -> BinOp {
BinOp::Plus(node)
}
}
impl From<Star> for BinOp {
fn from(node: Star) -> BinOp {
BinOp::Star(node)
}
}
impl From<Minus> for BinOp {
fn from(node: Minus) -> BinOp {
BinOp::Minus(node)
}
}
impl From<Slash> for BinOp {
fn from(node: Slash) -> BinOp {
BinOp::Slash(node)
}
}
impl From<Percent> for BinOp {
fn from(node: Percent) -> BinOp {
BinOp::Percent(node)
}
}
impl From<Shl> for BinOp {
fn from(node: Shl) -> BinOp {
BinOp::Shl(node)
}
}
impl From<Shr> for BinOp {
fn from(node: Shr) -> BinOp {
BinOp::Shr(node)
}
}
impl From<Caret> for BinOp {
fn from(node: Caret) -> BinOp {
BinOp::Caret(node)
}
}
impl From<Pipe> for BinOp {
fn from(node: Pipe) -> BinOp {
BinOp::Pipe(node)
}
}
impl From<Amp> for BinOp {
fn from(node: Amp) -> BinOp {
BinOp::Amp(node)
}
}
impl From<Eq> for BinOp {
fn from(node: Eq) -> BinOp {
BinOp::Eq(node)
}
}
impl From<Pluseq> for BinOp {
fn from(node: Pluseq) -> BinOp {
BinOp::Pluseq(node)
}
}
impl From<Slasheq> for BinOp {
fn from(node: Slasheq) -> BinOp {
BinOp::Slasheq(node)
}
}
impl From<Stareq> for BinOp {
fn from(node: Stareq) -> BinOp {
BinOp::Stareq(node)
}
}
impl From<Percenteq> for BinOp {
fn from(node: Percenteq) -> BinOp {
BinOp::Percenteq(node)
}
}
impl From<Shreq> for BinOp {
fn from(node: Shreq) -> BinOp {
BinOp::Shreq(node)
}
}
impl From<Shleq> for BinOp {
fn from(node: Shleq) -> BinOp {
BinOp::Shleq(node)
}
}
impl From<Minuseq> for BinOp {
fn from(node: Minuseq) -> BinOp {
BinOp::Minuseq(node)
}
}
impl From<Pipeeq> for BinOp {
fn from(node: Pipeeq) -> BinOp {
BinOp::Pipeeq(node)
}
}
impl From<Ampeq> for BinOp {
fn from(node: Ampeq) -> BinOp {
BinOp::Ampeq(node)
}
}
impl From<Careteq> for BinOp {
fn from(node: Careteq) -> BinOp {
BinOp::Careteq(node)
}
}
impl std::fmt::Display for BinOp {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl AstToken for BinOp {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
PIPEPIPE | AMPAMP | EQEQ | NEQ | LTEQ | GTEQ | L_ANGLE | R_ANGLE | PLUS | STAR
| MINUS | SLASH | PERCENT | SHL | SHR | CARET | PIPE | AMP | EQ | PLUSEQ | SLASHEQ
| STAREQ | PERCENTEQ | SHREQ | SHLEQ | MINUSEQ | PIPEEQ | AMPEQ | CARETEQ => true,
_ => false,
}
}
fn cast(syntax: SyntaxToken) -> Option<Self> {
let res = match syntax.kind() {
PIPEPIPE => BinOp::Pipepipe(Pipepipe { syntax }),
AMPAMP => BinOp::Ampamp(Ampamp { syntax }),
EQEQ => BinOp::Eqeq(Eqeq { syntax }),
NEQ => BinOp::Neq(Neq { syntax }),
LTEQ => BinOp::Lteq(Lteq { syntax }),
GTEQ => BinOp::Gteq(Gteq { syntax }),
L_ANGLE => BinOp::LAngle(LAngle { syntax }),
R_ANGLE => BinOp::RAngle(RAngle { syntax }),
PLUS => BinOp::Plus(Plus { syntax }),
STAR => BinOp::Star(Star { syntax }),
MINUS => BinOp::Minus(Minus { syntax }),
SLASH => BinOp::Slash(Slash { syntax }),
PERCENT => BinOp::Percent(Percent { syntax }),
SHL => BinOp::Shl(Shl { syntax }),
SHR => BinOp::Shr(Shr { syntax }),
CARET => BinOp::Caret(Caret { syntax }),
PIPE => BinOp::Pipe(Pipe { syntax }),
AMP => BinOp::Amp(Amp { syntax }),
EQ => BinOp::Eq(Eq { syntax }),
PLUSEQ => BinOp::Pluseq(Pluseq { syntax }),
SLASHEQ => BinOp::Slasheq(Slasheq { syntax }),
STAREQ => BinOp::Stareq(Stareq { syntax }),
PERCENTEQ => BinOp::Percenteq(Percenteq { syntax }),
SHREQ => BinOp::Shreq(Shreq { syntax }),
SHLEQ => BinOp::Shleq(Shleq { syntax }),
MINUSEQ => BinOp::Minuseq(Minuseq { syntax }),
PIPEEQ => BinOp::Pipeeq(Pipeeq { syntax }),
AMPEQ => BinOp::Ampeq(Ampeq { syntax }),
CARETEQ => BinOp::Careteq(Careteq { syntax }),
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxToken {
match self {
BinOp::Pipepipe(it) => &it.syntax,
BinOp::Ampamp(it) => &it.syntax,
BinOp::Eqeq(it) => &it.syntax,
BinOp::Neq(it) => &it.syntax,
BinOp::Lteq(it) => &it.syntax,
BinOp::Gteq(it) => &it.syntax,
BinOp::LAngle(it) => &it.syntax,
BinOp::RAngle(it) => &it.syntax,
BinOp::Plus(it) => &it.syntax,
BinOp::Star(it) => &it.syntax,
BinOp::Minus(it) => &it.syntax,
BinOp::Slash(it) => &it.syntax,
BinOp::Percent(it) => &it.syntax,
BinOp::Shl(it) => &it.syntax,
BinOp::Shr(it) => &it.syntax,
BinOp::Caret(it) => &it.syntax,
BinOp::Pipe(it) => &it.syntax,
BinOp::Amp(it) => &it.syntax,
BinOp::Eq(it) => &it.syntax,
BinOp::Pluseq(it) => &it.syntax,
BinOp::Slasheq(it) => &it.syntax,
BinOp::Stareq(it) => &it.syntax,
BinOp::Percenteq(it) => &it.syntax,
BinOp::Shreq(it) => &it.syntax,
BinOp::Shleq(it) => &it.syntax,
BinOp::Minuseq(it) => &it.syntax,
BinOp::Pipeeq(it) => &it.syntax,
BinOp::Ampeq(it) => &it.syntax,
BinOp::Careteq(it) => &it.syntax,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PrefixOp {
Minus(Minus),
Excl(Excl),
Star(Star),
}
impl From<Minus> for PrefixOp {
fn from(node: Minus) -> PrefixOp {
PrefixOp::Minus(node)
}
}
impl From<Excl> for PrefixOp {
fn from(node: Excl) -> PrefixOp {
PrefixOp::Excl(node)
}
}
impl From<Star> for PrefixOp {
fn from(node: Star) -> PrefixOp {
PrefixOp::Star(node)
}
}
impl std::fmt::Display for PrefixOp {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl AstToken for PrefixOp {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
MINUS | EXCL | STAR => true,
_ => false,
}
}
fn cast(syntax: SyntaxToken) -> Option<Self> {
let res = match syntax.kind() {
MINUS => PrefixOp::Minus(Minus { syntax }),
EXCL => PrefixOp::Excl(Excl { syntax }),
STAR => PrefixOp::Star(Star { syntax }),
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxToken {
match self {
PrefixOp::Minus(it) => &it.syntax,
PrefixOp::Excl(it) => &it.syntax,
PrefixOp::Star(it) => &it.syntax,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RangeOp {
Dotdot(Dotdot),
Dotdoteq(Dotdoteq),
}
impl From<Dotdot> for RangeOp {
fn from(node: Dotdot) -> RangeOp {
RangeOp::Dotdot(node)
}
}
impl From<Dotdoteq> for RangeOp {
fn from(node: Dotdoteq) -> RangeOp {
RangeOp::Dotdoteq(node)
}
}
impl std::fmt::Display for RangeOp {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl AstToken for RangeOp {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
DOTDOT | DOTDOTEQ => true,
_ => false,
}
}
fn cast(syntax: SyntaxToken) -> Option<Self> {
let res = match syntax.kind() {
DOTDOT => RangeOp::Dotdot(Dotdot { syntax }),
DOTDOTEQ => RangeOp::Dotdoteq(Dotdoteq { syntax }),
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxToken {
match self {
RangeOp::Dotdot(it) => &it.syntax,
RangeOp::Dotdoteq(it) => &it.syntax,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum LiteralToken {
IntNumber(IntNumber),
FloatNumber(FloatNumber),
String(String),
RawString(RawString),
TrueKw(TrueKw),
FalseKw(FalseKw),
ByteString(ByteString),
RawByteString(RawByteString),
Char(Char),
Byte(Byte),
}
impl From<IntNumber> for LiteralToken {
fn from(node: IntNumber) -> LiteralToken {
LiteralToken::IntNumber(node)
}
}
impl From<FloatNumber> for LiteralToken {
fn from(node: FloatNumber) -> LiteralToken {
LiteralToken::FloatNumber(node)
}
}
impl From<String> for LiteralToken {
fn from(node: String) -> LiteralToken {
LiteralToken::String(node)
}
}
impl From<RawString> for LiteralToken {
fn from(node: RawString) -> LiteralToken {
LiteralToken::RawString(node)
}
}
impl From<TrueKw> for LiteralToken {
fn from(node: TrueKw) -> LiteralToken {
LiteralToken::TrueKw(node)
}
}
impl From<FalseKw> for LiteralToken {
fn from(node: FalseKw) -> LiteralToken {
LiteralToken::FalseKw(node)
}
}
impl From<ByteString> for LiteralToken {
fn from(node: ByteString) -> LiteralToken {
LiteralToken::ByteString(node)
}
}
impl From<RawByteString> for LiteralToken {
fn from(node: RawByteString) -> LiteralToken {
LiteralToken::RawByteString(node)
}
}
impl From<Char> for LiteralToken {
fn from(node: Char) -> LiteralToken {
LiteralToken::Char(node)
}
}
impl From<Byte> for LiteralToken {
fn from(node: Byte) -> LiteralToken {
LiteralToken::Byte(node)
}
}
impl std::fmt::Display for LiteralToken {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl AstToken for LiteralToken {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
INT_NUMBER | FLOAT_NUMBER | STRING | RAW_STRING | TRUE_KW | FALSE_KW | BYTE_STRING
| RAW_BYTE_STRING | CHAR | BYTE => true,
_ => false,
}
}
fn cast(syntax: SyntaxToken) -> Option<Self> {
let res = match syntax.kind() {
INT_NUMBER => LiteralToken::IntNumber(IntNumber { syntax }),
FLOAT_NUMBER => LiteralToken::FloatNumber(FloatNumber { syntax }),
STRING => LiteralToken::String(String { syntax }),
RAW_STRING => LiteralToken::RawString(RawString { syntax }),
TRUE_KW => LiteralToken::TrueKw(TrueKw { syntax }),
FALSE_KW => LiteralToken::FalseKw(FalseKw { syntax }),
BYTE_STRING => LiteralToken::ByteString(ByteString { syntax }),
RAW_BYTE_STRING => LiteralToken::RawByteString(RawByteString { syntax }),
CHAR => LiteralToken::Char(Char { syntax }),
BYTE => LiteralToken::Byte(Byte { syntax }),
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxToken {
match self {
LiteralToken::IntNumber(it) => &it.syntax,
LiteralToken::FloatNumber(it) => &it.syntax,
LiteralToken::String(it) => &it.syntax,
LiteralToken::RawString(it) => &it.syntax,
LiteralToken::TrueKw(it) => &it.syntax,
LiteralToken::FalseKw(it) => &it.syntax,
LiteralToken::ByteString(it) => &it.syntax,
LiteralToken::RawByteString(it) => &it.syntax,
LiteralToken::Char(it) => &it.syntax,
LiteralToken::Byte(it) => &it.syntax,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum NameRefToken {
Ident(Ident),
IntNumber(IntNumber),
}
impl From<Ident> for NameRefToken {
fn from(node: Ident) -> NameRefToken {
NameRefToken::Ident(node)
}
}
impl From<IntNumber> for NameRefToken {
fn from(node: IntNumber) -> NameRefToken {
NameRefToken::IntNumber(node)
}
}
impl std::fmt::Display for NameRefToken {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl AstToken for NameRefToken {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
IDENT | INT_NUMBER => true,
_ => false,
}
}
fn cast(syntax: SyntaxToken) -> Option<Self> {
let res = match syntax.kind() {
IDENT => NameRefToken::Ident(Ident { syntax }),
INT_NUMBER => NameRefToken::IntNumber(IntNumber { syntax }),
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxToken {
match self {
NameRefToken::Ident(it) => &it.syntax,
NameRefToken::IntNumber(it) => &it.syntax,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum FieldDefList {
RecordFieldDefList(RecordFieldDefList),
TupleFieldDefList(TupleFieldDefList),

View file

@ -3056,3 +3056,654 @@ impl AstToken for RDollar {
&self.syntax
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum LeftDelimiter {
LParen(LParen),
LBrack(LBrack),
LCurly(LCurly),
}
impl From<LParen> for LeftDelimiter {
fn from(node: LParen) -> LeftDelimiter {
LeftDelimiter::LParen(node)
}
}
impl From<LBrack> for LeftDelimiter {
fn from(node: LBrack) -> LeftDelimiter {
LeftDelimiter::LBrack(node)
}
}
impl From<LCurly> for LeftDelimiter {
fn from(node: LCurly) -> LeftDelimiter {
LeftDelimiter::LCurly(node)
}
}
impl std::fmt::Display for LeftDelimiter {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl AstToken for LeftDelimiter {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
L_PAREN | L_BRACK | L_CURLY => true,
_ => false,
}
}
fn cast(syntax: SyntaxToken) -> Option<Self> {
let res = match syntax.kind() {
L_PAREN => LeftDelimiter::LParen(LParen { syntax }),
L_BRACK => LeftDelimiter::LBrack(LBrack { syntax }),
L_CURLY => LeftDelimiter::LCurly(LCurly { syntax }),
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxToken {
match self {
LeftDelimiter::LParen(it) => &it.syntax,
LeftDelimiter::LBrack(it) => &it.syntax,
LeftDelimiter::LCurly(it) => &it.syntax,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RightDelimiter {
RParen(RParen),
RBrack(RBrack),
RCurly(RCurly),
}
impl From<RParen> for RightDelimiter {
fn from(node: RParen) -> RightDelimiter {
RightDelimiter::RParen(node)
}
}
impl From<RBrack> for RightDelimiter {
fn from(node: RBrack) -> RightDelimiter {
RightDelimiter::RBrack(node)
}
}
impl From<RCurly> for RightDelimiter {
fn from(node: RCurly) -> RightDelimiter {
RightDelimiter::RCurly(node)
}
}
impl std::fmt::Display for RightDelimiter {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl AstToken for RightDelimiter {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
R_PAREN | R_BRACK | R_CURLY => true,
_ => false,
}
}
fn cast(syntax: SyntaxToken) -> Option<Self> {
let res = match syntax.kind() {
R_PAREN => RightDelimiter::RParen(RParen { syntax }),
R_BRACK => RightDelimiter::RBrack(RBrack { syntax }),
R_CURLY => RightDelimiter::RCurly(RCurly { syntax }),
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxToken {
match self {
RightDelimiter::RParen(it) => &it.syntax,
RightDelimiter::RBrack(it) => &it.syntax,
RightDelimiter::RCurly(it) => &it.syntax,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RangeSeparator {
Dotdot(Dotdot),
Dotdotdot(Dotdotdot),
Dotdoteq(Dotdoteq),
}
impl From<Dotdot> for RangeSeparator {
fn from(node: Dotdot) -> RangeSeparator {
RangeSeparator::Dotdot(node)
}
}
impl From<Dotdotdot> for RangeSeparator {
fn from(node: Dotdotdot) -> RangeSeparator {
RangeSeparator::Dotdotdot(node)
}
}
impl From<Dotdoteq> for RangeSeparator {
fn from(node: Dotdoteq) -> RangeSeparator {
RangeSeparator::Dotdoteq(node)
}
}
impl std::fmt::Display for RangeSeparator {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl AstToken for RangeSeparator {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
DOTDOT | DOTDOTDOT | DOTDOTEQ => true,
_ => false,
}
}
fn cast(syntax: SyntaxToken) -> Option<Self> {
let res = match syntax.kind() {
DOTDOT => RangeSeparator::Dotdot(Dotdot { syntax }),
DOTDOTDOT => RangeSeparator::Dotdotdot(Dotdotdot { syntax }),
DOTDOTEQ => RangeSeparator::Dotdoteq(Dotdoteq { syntax }),
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxToken {
match self {
RangeSeparator::Dotdot(it) => &it.syntax,
RangeSeparator::Dotdotdot(it) => &it.syntax,
RangeSeparator::Dotdoteq(it) => &it.syntax,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum BinOp {
Pipepipe(Pipepipe),
Ampamp(Ampamp),
Eqeq(Eqeq),
Neq(Neq),
Lteq(Lteq),
Gteq(Gteq),
LAngle(LAngle),
RAngle(RAngle),
Plus(Plus),
Star(Star),
Minus(Minus),
Slash(Slash),
Percent(Percent),
Shl(Shl),
Shr(Shr),
Caret(Caret),
Pipe(Pipe),
Amp(Amp),
Eq(Eq),
Pluseq(Pluseq),
Slasheq(Slasheq),
Stareq(Stareq),
Percenteq(Percenteq),
Shreq(Shreq),
Shleq(Shleq),
Minuseq(Minuseq),
Pipeeq(Pipeeq),
Ampeq(Ampeq),
Careteq(Careteq),
}
impl From<Pipepipe> for BinOp {
fn from(node: Pipepipe) -> BinOp {
BinOp::Pipepipe(node)
}
}
impl From<Ampamp> for BinOp {
fn from(node: Ampamp) -> BinOp {
BinOp::Ampamp(node)
}
}
impl From<Eqeq> for BinOp {
fn from(node: Eqeq) -> BinOp {
BinOp::Eqeq(node)
}
}
impl From<Neq> for BinOp {
fn from(node: Neq) -> BinOp {
BinOp::Neq(node)
}
}
impl From<Lteq> for BinOp {
fn from(node: Lteq) -> BinOp {
BinOp::Lteq(node)
}
}
impl From<Gteq> for BinOp {
fn from(node: Gteq) -> BinOp {
BinOp::Gteq(node)
}
}
impl From<LAngle> for BinOp {
fn from(node: LAngle) -> BinOp {
BinOp::LAngle(node)
}
}
impl From<RAngle> for BinOp {
fn from(node: RAngle) -> BinOp {
BinOp::RAngle(node)
}
}
impl From<Plus> for BinOp {
fn from(node: Plus) -> BinOp {
BinOp::Plus(node)
}
}
impl From<Star> for BinOp {
fn from(node: Star) -> BinOp {
BinOp::Star(node)
}
}
impl From<Minus> for BinOp {
fn from(node: Minus) -> BinOp {
BinOp::Minus(node)
}
}
impl From<Slash> for BinOp {
fn from(node: Slash) -> BinOp {
BinOp::Slash(node)
}
}
impl From<Percent> for BinOp {
fn from(node: Percent) -> BinOp {
BinOp::Percent(node)
}
}
impl From<Shl> for BinOp {
fn from(node: Shl) -> BinOp {
BinOp::Shl(node)
}
}
impl From<Shr> for BinOp {
fn from(node: Shr) -> BinOp {
BinOp::Shr(node)
}
}
impl From<Caret> for BinOp {
fn from(node: Caret) -> BinOp {
BinOp::Caret(node)
}
}
impl From<Pipe> for BinOp {
fn from(node: Pipe) -> BinOp {
BinOp::Pipe(node)
}
}
impl From<Amp> for BinOp {
fn from(node: Amp) -> BinOp {
BinOp::Amp(node)
}
}
impl From<Eq> for BinOp {
fn from(node: Eq) -> BinOp {
BinOp::Eq(node)
}
}
impl From<Pluseq> for BinOp {
fn from(node: Pluseq) -> BinOp {
BinOp::Pluseq(node)
}
}
impl From<Slasheq> for BinOp {
fn from(node: Slasheq) -> BinOp {
BinOp::Slasheq(node)
}
}
impl From<Stareq> for BinOp {
fn from(node: Stareq) -> BinOp {
BinOp::Stareq(node)
}
}
impl From<Percenteq> for BinOp {
fn from(node: Percenteq) -> BinOp {
BinOp::Percenteq(node)
}
}
impl From<Shreq> for BinOp {
fn from(node: Shreq) -> BinOp {
BinOp::Shreq(node)
}
}
impl From<Shleq> for BinOp {
fn from(node: Shleq) -> BinOp {
BinOp::Shleq(node)
}
}
impl From<Minuseq> for BinOp {
fn from(node: Minuseq) -> BinOp {
BinOp::Minuseq(node)
}
}
impl From<Pipeeq> for BinOp {
fn from(node: Pipeeq) -> BinOp {
BinOp::Pipeeq(node)
}
}
impl From<Ampeq> for BinOp {
fn from(node: Ampeq) -> BinOp {
BinOp::Ampeq(node)
}
}
impl From<Careteq> for BinOp {
fn from(node: Careteq) -> BinOp {
BinOp::Careteq(node)
}
}
impl std::fmt::Display for BinOp {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl AstToken for BinOp {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
PIPEPIPE | AMPAMP | EQEQ | NEQ | LTEQ | GTEQ | L_ANGLE | R_ANGLE | PLUS | STAR
| MINUS | SLASH | PERCENT | SHL | SHR | CARET | PIPE | AMP | EQ | PLUSEQ | SLASHEQ
| STAREQ | PERCENTEQ | SHREQ | SHLEQ | MINUSEQ | PIPEEQ | AMPEQ | CARETEQ => true,
_ => false,
}
}
fn cast(syntax: SyntaxToken) -> Option<Self> {
let res = match syntax.kind() {
PIPEPIPE => BinOp::Pipepipe(Pipepipe { syntax }),
AMPAMP => BinOp::Ampamp(Ampamp { syntax }),
EQEQ => BinOp::Eqeq(Eqeq { syntax }),
NEQ => BinOp::Neq(Neq { syntax }),
LTEQ => BinOp::Lteq(Lteq { syntax }),
GTEQ => BinOp::Gteq(Gteq { syntax }),
L_ANGLE => BinOp::LAngle(LAngle { syntax }),
R_ANGLE => BinOp::RAngle(RAngle { syntax }),
PLUS => BinOp::Plus(Plus { syntax }),
STAR => BinOp::Star(Star { syntax }),
MINUS => BinOp::Minus(Minus { syntax }),
SLASH => BinOp::Slash(Slash { syntax }),
PERCENT => BinOp::Percent(Percent { syntax }),
SHL => BinOp::Shl(Shl { syntax }),
SHR => BinOp::Shr(Shr { syntax }),
CARET => BinOp::Caret(Caret { syntax }),
PIPE => BinOp::Pipe(Pipe { syntax }),
AMP => BinOp::Amp(Amp { syntax }),
EQ => BinOp::Eq(Eq { syntax }),
PLUSEQ => BinOp::Pluseq(Pluseq { syntax }),
SLASHEQ => BinOp::Slasheq(Slasheq { syntax }),
STAREQ => BinOp::Stareq(Stareq { syntax }),
PERCENTEQ => BinOp::Percenteq(Percenteq { syntax }),
SHREQ => BinOp::Shreq(Shreq { syntax }),
SHLEQ => BinOp::Shleq(Shleq { syntax }),
MINUSEQ => BinOp::Minuseq(Minuseq { syntax }),
PIPEEQ => BinOp::Pipeeq(Pipeeq { syntax }),
AMPEQ => BinOp::Ampeq(Ampeq { syntax }),
CARETEQ => BinOp::Careteq(Careteq { syntax }),
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxToken {
match self {
BinOp::Pipepipe(it) => &it.syntax,
BinOp::Ampamp(it) => &it.syntax,
BinOp::Eqeq(it) => &it.syntax,
BinOp::Neq(it) => &it.syntax,
BinOp::Lteq(it) => &it.syntax,
BinOp::Gteq(it) => &it.syntax,
BinOp::LAngle(it) => &it.syntax,
BinOp::RAngle(it) => &it.syntax,
BinOp::Plus(it) => &it.syntax,
BinOp::Star(it) => &it.syntax,
BinOp::Minus(it) => &it.syntax,
BinOp::Slash(it) => &it.syntax,
BinOp::Percent(it) => &it.syntax,
BinOp::Shl(it) => &it.syntax,
BinOp::Shr(it) => &it.syntax,
BinOp::Caret(it) => &it.syntax,
BinOp::Pipe(it) => &it.syntax,
BinOp::Amp(it) => &it.syntax,
BinOp::Eq(it) => &it.syntax,
BinOp::Pluseq(it) => &it.syntax,
BinOp::Slasheq(it) => &it.syntax,
BinOp::Stareq(it) => &it.syntax,
BinOp::Percenteq(it) => &it.syntax,
BinOp::Shreq(it) => &it.syntax,
BinOp::Shleq(it) => &it.syntax,
BinOp::Minuseq(it) => &it.syntax,
BinOp::Pipeeq(it) => &it.syntax,
BinOp::Ampeq(it) => &it.syntax,
BinOp::Careteq(it) => &it.syntax,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PrefixOp {
Minus(Minus),
Excl(Excl),
Star(Star),
}
impl From<Minus> for PrefixOp {
fn from(node: Minus) -> PrefixOp {
PrefixOp::Minus(node)
}
}
impl From<Excl> for PrefixOp {
fn from(node: Excl) -> PrefixOp {
PrefixOp::Excl(node)
}
}
impl From<Star> for PrefixOp {
fn from(node: Star) -> PrefixOp {
PrefixOp::Star(node)
}
}
impl std::fmt::Display for PrefixOp {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl AstToken for PrefixOp {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
MINUS | EXCL | STAR => true,
_ => false,
}
}
fn cast(syntax: SyntaxToken) -> Option<Self> {
let res = match syntax.kind() {
MINUS => PrefixOp::Minus(Minus { syntax }),
EXCL => PrefixOp::Excl(Excl { syntax }),
STAR => PrefixOp::Star(Star { syntax }),
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxToken {
match self {
PrefixOp::Minus(it) => &it.syntax,
PrefixOp::Excl(it) => &it.syntax,
PrefixOp::Star(it) => &it.syntax,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RangeOp {
Dotdot(Dotdot),
Dotdoteq(Dotdoteq),
}
impl From<Dotdot> for RangeOp {
fn from(node: Dotdot) -> RangeOp {
RangeOp::Dotdot(node)
}
}
impl From<Dotdoteq> for RangeOp {
fn from(node: Dotdoteq) -> RangeOp {
RangeOp::Dotdoteq(node)
}
}
impl std::fmt::Display for RangeOp {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl AstToken for RangeOp {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
DOTDOT | DOTDOTEQ => true,
_ => false,
}
}
fn cast(syntax: SyntaxToken) -> Option<Self> {
let res = match syntax.kind() {
DOTDOT => RangeOp::Dotdot(Dotdot { syntax }),
DOTDOTEQ => RangeOp::Dotdoteq(Dotdoteq { syntax }),
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxToken {
match self {
RangeOp::Dotdot(it) => &it.syntax,
RangeOp::Dotdoteq(it) => &it.syntax,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum LiteralToken {
IntNumber(IntNumber),
FloatNumber(FloatNumber),
String(String),
RawString(RawString),
TrueKw(TrueKw),
FalseKw(FalseKw),
ByteString(ByteString),
RawByteString(RawByteString),
Char(Char),
Byte(Byte),
}
impl From<IntNumber> for LiteralToken {
fn from(node: IntNumber) -> LiteralToken {
LiteralToken::IntNumber(node)
}
}
impl From<FloatNumber> for LiteralToken {
fn from(node: FloatNumber) -> LiteralToken {
LiteralToken::FloatNumber(node)
}
}
impl From<String> for LiteralToken {
fn from(node: String) -> LiteralToken {
LiteralToken::String(node)
}
}
impl From<RawString> for LiteralToken {
fn from(node: RawString) -> LiteralToken {
LiteralToken::RawString(node)
}
}
impl From<TrueKw> for LiteralToken {
fn from(node: TrueKw) -> LiteralToken {
LiteralToken::TrueKw(node)
}
}
impl From<FalseKw> for LiteralToken {
fn from(node: FalseKw) -> LiteralToken {
LiteralToken::FalseKw(node)
}
}
impl From<ByteString> for LiteralToken {
fn from(node: ByteString) -> LiteralToken {
LiteralToken::ByteString(node)
}
}
impl From<RawByteString> for LiteralToken {
fn from(node: RawByteString) -> LiteralToken {
LiteralToken::RawByteString(node)
}
}
impl From<Char> for LiteralToken {
fn from(node: Char) -> LiteralToken {
LiteralToken::Char(node)
}
}
impl From<Byte> for LiteralToken {
fn from(node: Byte) -> LiteralToken {
LiteralToken::Byte(node)
}
}
impl std::fmt::Display for LiteralToken {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl AstToken for LiteralToken {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
INT_NUMBER | FLOAT_NUMBER | STRING | RAW_STRING | TRUE_KW | FALSE_KW | BYTE_STRING
| RAW_BYTE_STRING | CHAR | BYTE => true,
_ => false,
}
}
fn cast(syntax: SyntaxToken) -> Option<Self> {
let res = match syntax.kind() {
INT_NUMBER => LiteralToken::IntNumber(IntNumber { syntax }),
FLOAT_NUMBER => LiteralToken::FloatNumber(FloatNumber { syntax }),
STRING => LiteralToken::String(String { syntax }),
RAW_STRING => LiteralToken::RawString(RawString { syntax }),
TRUE_KW => LiteralToken::TrueKw(TrueKw { syntax }),
FALSE_KW => LiteralToken::FalseKw(FalseKw { syntax }),
BYTE_STRING => LiteralToken::ByteString(ByteString { syntax }),
RAW_BYTE_STRING => LiteralToken::RawByteString(RawByteString { syntax }),
CHAR => LiteralToken::Char(Char { syntax }),
BYTE => LiteralToken::Byte(Byte { syntax }),
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxToken {
match self {
LiteralToken::IntNumber(it) => &it.syntax,
LiteralToken::FloatNumber(it) => &it.syntax,
LiteralToken::String(it) => &it.syntax,
LiteralToken::RawString(it) => &it.syntax,
LiteralToken::TrueKw(it) => &it.syntax,
LiteralToken::FalseKw(it) => &it.syntax,
LiteralToken::ByteString(it) => &it.syntax,
LiteralToken::RawByteString(it) => &it.syntax,
LiteralToken::Char(it) => &it.syntax,
LiteralToken::Byte(it) => &it.syntax,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum NameRefToken {
Ident(Ident),
IntNumber(IntNumber),
}
impl From<Ident> for NameRefToken {
fn from(node: Ident) -> NameRefToken {
NameRefToken::Ident(node)
}
}
impl From<IntNumber> for NameRefToken {
fn from(node: IntNumber) -> NameRefToken {
NameRefToken::IntNumber(node)
}
}
impl std::fmt::Display for NameRefToken {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl AstToken for NameRefToken {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
IDENT | INT_NUMBER => true,
_ => false,
}
}
fn cast(syntax: SyntaxToken) -> Option<Self> {
let res = match syntax.kind() {
IDENT => NameRefToken::Ident(Ident { syntax }),
INT_NUMBER => NameRefToken::IntNumber(IntNumber { syntax }),
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxToken {
match self {
NameRefToken::Ident(it) => &it.syntax,
NameRefToken::IntNumber(it) => &it.syntax,
}
}
}

View file

@ -227,6 +227,7 @@ pub(crate) const KINDS_SRC: KindsSrc = KindsSrc {
pub(crate) struct AstSrc<'a> {
pub(crate) nodes: &'a [AstNodeSrc<'a>],
pub(crate) enums: &'a [AstEnumSrc<'a>],
pub(crate) token_enums: &'a [AstEnumSrc<'a>],
}
pub(crate) struct AstNodeSrc<'a> {
@ -753,6 +754,13 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
// macro calls are parsed as expression statements */
}
enum FieldDefList {
RecordFieldDefList,
TupleFieldDefList,
}
},
token_enums: &ast_enums! {
enum LeftDelimiter { LParen, LBrack, LCurly }
enum RightDelimiter { RParen, RBrack, RCurly }
enum RangeSeparator { Dotdot, Dotdotdot, Dotdoteq}
@ -817,10 +825,5 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
Ident,
IntNumber
}
enum FieldDefList {
RecordFieldDefList,
TupleFieldDefList,
}
},
};

View file

@ -5,7 +5,7 @@
use std::{
borrow::Cow,
collections::{BTreeSet, HashMap, HashSet},
collections::{BTreeSet, HashSet},
};
use proc_macro2::{Punct, Spacing};
@ -57,6 +57,7 @@ fn generate_tokens(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> {
.chain(kinds.literals.into_iter().copied().map(|x| x.into()))
.chain(kinds.tokens.into_iter().copied().map(|x| x.into()))
.collect();
let tokens = all_token_kinds.iter().map(|kind_str| {
let kind_str = &**kind_str;
let kind = format_ident!("{}", kind_str);
@ -88,10 +89,67 @@ fn generate_tokens(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> {
}
});
let enums = grammar.token_enums.iter().map(|en| {
let variants = en.variants.iter().map(|var| format_ident!("{}", var)).collect::<Vec<_>>();
let name = format_ident!("{}", en.name);
let kinds = variants
.iter()
.map(|name| format_ident!("{}", to_upper_snake_case(&name.to_string())))
.collect::<Vec<_>>();
assert!(en.traits.is_empty());
quote! {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum #name {
#(#variants(#variants),)*
}
#(
impl From<#variants> for #name {
fn from(node: #variants) -> #name {
#name::#variants(node)
}
}
)*
impl std::fmt::Display for #name {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl AstToken for #name {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
#(#kinds)|* => true,
_ => false,
}
}
fn cast(syntax: SyntaxToken) -> Option<Self> {
let res = match syntax.kind() {
#(
#kinds => #name::#variants(#variants { syntax }),
)*
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxToken {
match self {
#(
#name::#variants(it) => &it.syntax,
)*
}
}
}
}
});
crate::reformat(quote! {
use crate::{SyntaxToken, SyntaxKind::{self, *}, ast::AstToken};
#(#tokens)*
#(#enums)*
})
}
@ -113,44 +171,15 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> {
.chain(kinds.tokens.into_iter().copied().map(|x| x.into()))
.collect();
let mut element_kinds_map = HashMap::new();
let mut token_kinds = HashSet::new();
for kind in &all_token_kinds {
let kind = &**kind;
let name = to_pascal_case(kind);
element_kinds_map.insert(
name,
ElementKinds {
kinds: Some(format_ident!("{}", kind)).into_iter().collect(),
has_nodes: false,
has_tokens: true,
},
);
token_kinds.insert(name);
}
for kind in kinds.nodes {
let name = to_pascal_case(kind);
element_kinds_map.insert(
name,
ElementKinds {
kinds: Some(format_ident!("{}", *kind)).into_iter().collect(),
has_nodes: true,
has_tokens: false,
},
);
}
for en in grammar.enums {
let mut element_kinds: ElementKinds = Default::default();
for variant in en.variants {
if let Some(variant_element_kinds) = element_kinds_map.get(*variant) {
element_kinds.kinds.extend(variant_element_kinds.kinds.iter().cloned());
element_kinds.has_tokens |= variant_element_kinds.has_tokens;
element_kinds.has_nodes |= variant_element_kinds.has_nodes;
} else {
panic!("Enum variant has type that does not exist or was not declared before the enum: {}", *variant);
}
}
element_kinds_map.insert(en.name.to_string(), element_kinds);
for en in grammar.token_enums {
token_kinds.insert(en.name.to_string());
}
let nodes = grammar.nodes.iter().map(|node| {
@ -182,7 +211,7 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> {
}
}
FieldSrc::Optional(_) | FieldSrc::Shorthand => {
let is_token = element_kinds_map[&ty.to_string()].has_tokens;
let is_token = token_kinds.contains(&ty.to_string());
if is_token {
quote! {
pub fn #method_name(&self) -> Option<#ty> {
@ -245,48 +274,6 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> {
quote!(impl ast::#trait_name for #name {})
});
let element_kinds = &element_kinds_map[&en.name.to_string()];
assert!(
element_kinds.has_nodes ^ element_kinds.has_tokens,
"{}: {:#?}",
name,
element_kinds
);
let specific_ast_trait = {
let (ast_trait, syntax_type) = if element_kinds.has_tokens {
(quote!(AstToken), quote!(SyntaxToken))
} else {
(quote!(AstNode), quote!(SyntaxNode))
};
quote! {
impl #ast_trait for #name {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
#(#kinds)|* => true,
_ => false,
}
}
fn cast(syntax: #syntax_type) -> Option<Self> {
let res = match syntax.kind() {
#(
#kinds => #name::#variants(#variants { syntax }),
)*
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &#syntax_type {
match self {
#(
#name::#variants(it) => &it.syntax,
)*
}
}
}
}
};
quote! {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum #name {
@ -307,7 +294,30 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> {
}
}
#specific_ast_trait
impl AstNode for #name {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
#(#kinds)|* => true,
_ => false,
}
}
fn cast(syntax: SyntaxNode) -> Option<Self> {
let res = match syntax.kind() {
#(
#kinds => #name::#variants(#variants { syntax }),
)*
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxNode {
match self {
#(
#name::#variants(it) => &it.syntax,
)*
}
}
}
#(#traits)*
}
@ -326,8 +336,8 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> {
let ast = quote! {
use crate::{
SyntaxNode, SyntaxToken, SyntaxKind::{self, *},
ast::{self, AstNode, AstToken, AstChildren, support},
SyntaxNode, SyntaxKind::{self, *},
ast::{self, AstNode, AstChildren, support},
};
use super::tokens::*;