use cstree::Syntax;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Syntax)]
#[repr(u32)]
pub enum SyntaxKind {
Whitespace,
LineComment,
DocComment,
RegionComment,
EndRegionComment,
LineContinuation,
NewlinePhys,
Bom,
Newline,
Indent,
Dedent,
Int,
Float,
String,
StringName,
NodePath,
Ident,
#[static_text("true")]
True,
#[static_text("false")]
False,
#[static_text("null")]
Null,
#[static_text("PI")]
ConstPi,
#[static_text("TAU")]
ConstTau,
#[static_text("INF")]
ConstInf,
#[static_text("NAN")]
ConstNan,
#[static_text("if")]
IfKw,
#[static_text("elif")]
ElifKw,
#[static_text("else")]
ElseKw,
#[static_text("for")]
ForKw,
#[static_text("while")]
WhileKw,
#[static_text("match")]
MatchKw,
#[static_text("when")]
WhenKw,
#[static_text("break")]
BreakKw,
#[static_text("continue")]
ContinueKw,
#[static_text("pass")]
PassKw,
#[static_text("return")]
ReturnKw,
#[static_text("var")]
VarKw,
#[static_text("const")]
ConstKw,
#[static_text("enum")]
EnumKw,
#[static_text("func")]
FuncKw,
#[static_text("static")]
StaticKw,
#[static_text("signal")]
SignalKw,
#[static_text("class")]
ClassKw,
#[static_text("class_name")]
ClassNameKw,
#[static_text("extends")]
ExtendsKw,
#[static_text("is")]
IsKw,
#[static_text("in")]
InKw,
#[static_text("as")]
AsKw,
#[static_text("self")]
SelfKw,
#[static_text("super")]
SuperKw,
#[static_text("void")]
VoidKw,
#[static_text("await")]
AwaitKw,
#[static_text("preload")]
PreloadKw,
#[static_text("assert")]
AssertKw,
#[static_text("breakpoint")]
BreakpointKw,
#[static_text("not")]
NotKw,
#[static_text("and")]
AndKw,
#[static_text("or")]
OrKw,
#[static_text("yield")]
YieldKw,
#[static_text("namespace")]
NamespaceKw,
#[static_text("trait")]
TraitKw,
#[static_text("(")]
LParen,
#[static_text(")")]
RParen,
#[static_text("[")]
LBrack,
#[static_text("]")]
RBrack,
#[static_text("{")]
LBrace,
#[static_text("}")]
RBrace,
#[static_text(",")]
Comma,
#[static_text(":")]
Colon,
#[static_text(";")]
Semicolon,
#[static_text(".")]
Dot,
#[static_text("..")]
DotDot,
#[static_text("...")]
Ellipsis,
#[static_text("@")]
At,
#[static_text("$")]
Dollar,
#[static_text("%")]
Percent,
#[static_text("&")]
Amp,
#[static_text("->")]
Arrow,
#[static_text(":=")]
ColonEq,
#[static_text("+")]
Plus,
#[static_text("-")]
Minus,
#[static_text("*")]
Star,
#[static_text("/")]
Slash,
#[static_text("**")]
StarStar,
#[static_text("=")]
Eq,
#[static_text("==")]
EqEq,
#[static_text("!=")]
Neq,
#[static_text("<")]
Lt,
#[static_text(">")]
Gt,
#[static_text("<=")]
Le,
#[static_text(">=")]
Ge,
#[static_text("&&")]
AmpAmp,
#[static_text("||")]
PipePipe,
#[static_text("!")]
Bang,
#[static_text("~")]
Tilde,
#[static_text("|")]
Pipe,
#[static_text("^")]
Caret,
#[static_text("<<")]
Shl,
#[static_text(">>")]
Shr,
#[static_text("+=")]
PlusEq,
#[static_text("-=")]
MinusEq,
#[static_text("*=")]
StarEq,
#[static_text("/=")]
SlashEq,
#[static_text("**=")]
StarStarEq,
#[static_text("%=")]
PercentEq,
#[static_text("&=")]
AmpEq,
#[static_text("|=")]
PipeEq,
#[static_text("^=")]
CaretEq,
#[static_text("<<=")]
ShlEq,
#[static_text(">>=")]
ShrEq,
Error,
Eof,
SourceFile,
ExtendsClause,
ClassNameDecl,
Annotation,
AnnotationArgList,
InnerClassDecl,
ClassBody,
FuncDecl,
ParamList,
Param,
VarargParam,
VarDecl,
ConstDecl,
EnumDecl,
EnumVariant,
SignalDecl,
PropertyBody,
Getter,
Setter,
Name,
TypeRef,
TypedArray,
TypedDict,
Block,
IfStmt,
ElifClause,
ElseClause,
ForStmt,
WhileStmt,
MatchStmt,
MatchArm,
ReturnStmt,
BreakStmt,
ContinueStmt,
PassStmt,
AssertStmt,
BreakpointStmt,
ExprStmt,
VarStmt,
PatternLiteral,
PatternBind,
PatternWildcard,
PatternArray,
PatternDict,
PatternRest,
PatternGuard,
BinExpr,
UnaryExpr,
TernaryExpr,
CastExpr,
IsExpr,
InExpr,
CallExpr,
ArgList,
IndexExpr,
FieldExpr,
AwaitExpr,
LambdaExpr,
ParenExpr,
ArrayLit,
DictLit,
DictEntry,
NameRef,
Literal,
GetNodeExpr,
UniqueNodeExpr,
PreloadExpr,
ErrorNode,
Tombstone,
}
impl SyntaxKind {
#[must_use]
pub const fn is_trivia(self) -> bool {
matches!(
self,
Self::Whitespace
| Self::LineComment
| Self::DocComment
| Self::RegionComment
| Self::EndRegionComment
| Self::LineContinuation
| Self::NewlinePhys
| Self::Bom
)
}
#[must_use]
pub const fn is_synthetic_layout(self) -> bool {
matches!(self, Self::Newline | Self::Indent | Self::Dedent)
}
#[must_use]
pub const fn is_node(self) -> bool {
(self as u32) >= (Self::SourceFile as u32)
}
}
pub type GdNode = cstree::syntax::ResolvedNode<SyntaxKind>;
pub type GdToken = cstree::syntax::ResolvedToken<SyntaxKind>;
pub type SyntaxNode = cstree::syntax::SyntaxNode<SyntaxKind>;
#[cfg(test)]
mod tests {
use super::*;
use cstree::build::GreenNodeBuilder;
use cstree::syntax::ResolvedNode;
#[test]
fn three_node_tree_round_trips() {
let mut builder: GreenNodeBuilder<'_, '_, SyntaxKind> = GreenNodeBuilder::new();
builder.start_node(SyntaxKind::SourceFile);
builder.start_node(SyntaxKind::FuncDecl);
builder.static_token(SyntaxKind::FuncKw); builder.token(SyntaxKind::Whitespace, " ");
builder.start_node(SyntaxKind::Name);
builder.token(SyntaxKind::Ident, "foo");
builder.finish_node(); builder.token(SyntaxKind::Newline, ""); builder.finish_node(); builder.finish_node();
let (green, cache) = builder.finish();
let interner = cache.unwrap().into_interner().unwrap();
let root = ResolvedNode::<SyntaxKind>::new_root_with_resolver(green, interner);
assert_eq!(root.to_string(), "func foo");
assert_eq!(root.kind(), SyntaxKind::SourceFile);
}
#[test]
fn raw_kind_round_trips() {
for raw in 0..(SyntaxKind::Tombstone as u32) {
let kind = <SyntaxKind as Syntax>::from_raw(cstree::RawSyntaxKind(raw));
assert_eq!(<SyntaxKind as Syntax>::into_raw(kind).0, raw);
}
}
#[test]
fn classification_helpers() {
assert!(SyntaxKind::Whitespace.is_trivia());
assert!(!SyntaxKind::Newline.is_trivia());
assert!(SyntaxKind::Indent.is_synthetic_layout());
assert!(SyntaxKind::FuncDecl.is_node());
assert!(!SyntaxKind::FuncKw.is_node());
}
}