#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u16)]
pub enum SyntaxKind {
Text = 0,
Whitespace,
Error,
At,
AtAt,
LBrace,
RBrace,
HashOpen,
SlashOpen,
ColonOpen,
DollarOpen,
PipeOpen,
PipeClose,
CommentLineOpen,
CommentLineClose,
CommentBlockOpen,
CommentBlockClose,
DoubleQuote,
SingleQuote,
Backtick,
DocCommentPrefix,
JsDocOpen,
JsDocClose,
Colon,
Semicolon,
LParen,
RParen,
LBracket,
RBracket,
Lt,
Gt,
Comma,
Eq,
Question,
Dot,
IfKw,
ElseKw,
ForKw,
WhileKw,
MatchKw,
CaseKw,
LetKw,
DoKw,
InKw,
TypeScriptKw,
MutKw,
AsKw,
FunctionKw,
ConstKw,
ClassKw,
InterfaceKw,
TypeKw,
ExportKw,
ImportKw,
ReturnKw,
NewKw,
Ident,
RustTokens,
Root,
Interpolation,
IdentBlock,
StringInterp,
TemplateLiteral,
StringPart,
LineComment,
BlockComment,
DocComment,
IfBlock,
IfLetBlock,
ElseClause,
ElseIfClause,
ForBlock,
WhileBlock,
WhileLetBlock,
MatchBlock,
MatchCase,
LetDirective,
DoDirective,
TypeScriptDirective,
TsStmt,
TsExpr,
TypeAnnotation,
TypeAssertion,
TsFunction,
TsParam,
TsTypeParams,
TsObject,
TsObjectProp,
TsClass,
TsClassMember,
BraceBlock,
ObjectPropLoop,
ExprPlaceholder,
TypePlaceholder,
IdentPlaceholder,
IdentNamePlaceholder,
StmtPlaceholder,
#[doc(hidden)]
__LAST,
}
impl SyntaxKind {
pub fn is_token(self) -> bool {
(self as u16) < (Self::Root as u16)
}
pub fn is_trivia(self) -> bool {
matches!(self, Self::Whitespace)
}
pub fn is_control_keyword(self) -> bool {
matches!(
self,
Self::IfKw | Self::ElseKw | Self::ForKw | Self::WhileKw | Self::MatchKw | Self::CaseKw
)
}
pub fn is_directive_keyword(self) -> bool {
matches!(
self,
Self::LetKw | Self::DoKw | Self::TypeScriptKw | Self::MutKw
)
}
pub fn is_ts_keyword(self) -> bool {
matches!(
self,
Self::AsKw
| Self::FunctionKw
| Self::ConstKw
| Self::ClassKw
| Self::InterfaceKw
| Self::TypeKw
| Self::ExportKw
| Self::ImportKw
| Self::ReturnKw
| Self::NewKw
)
}
}
impl From<SyntaxKind> for rowan::SyntaxKind {
fn from(kind: SyntaxKind) -> Self {
Self(kind as u16)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum TemplateLanguage {}
impl rowan::Language for TemplateLanguage {
type Kind = SyntaxKind;
fn kind_from_raw(raw: rowan::SyntaxKind) -> Self::Kind {
assert!(raw.0 < SyntaxKind::__LAST as u16);
unsafe { std::mem::transmute::<u16, SyntaxKind>(raw.0) }
}
fn kind_to_raw(kind: Self::Kind) -> rowan::SyntaxKind {
kind.into()
}
}
pub type SyntaxNode = rowan::SyntaxNode<TemplateLanguage>;
pub type SyntaxToken = rowan::SyntaxToken<TemplateLanguage>;
#[cfg(test)]
mod tests {
use super::*;
use rowan::Language;
#[test]
fn test_syntax_kind_is_token() {
assert!(SyntaxKind::Text.is_token());
assert!(SyntaxKind::At.is_token());
assert!(SyntaxKind::IfKw.is_token());
assert!(!SyntaxKind::Root.is_token());
assert!(!SyntaxKind::Interpolation.is_token());
}
#[test]
fn test_syntax_kind_conversions() {
let kind = SyntaxKind::Text;
let raw: rowan::SyntaxKind = kind.into();
let back = TemplateLanguage::kind_from_raw(raw);
assert_eq!(kind, back);
}
#[test]
fn test_is_control_keyword() {
assert!(SyntaxKind::IfKw.is_control_keyword());
assert!(SyntaxKind::ForKw.is_control_keyword());
assert!(!SyntaxKind::LetKw.is_control_keyword());
assert!(!SyntaxKind::Text.is_control_keyword());
}
}