#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u16)]
#[non_exhaustive]
pub enum SyntaxKind {
#[default]
Tombstone = 0,
Root = 1,
SimpleMessage = 2,
ComplexMessage = 3,
Pattern = 10,
Text = 11,
QuotedPattern = 12,
Placeholder = 13,
LiteralExpression = 20,
VariableExpression = 21,
FunctionExpression = 22,
Function = 23,
Option = 24,
Attribute = 25,
LocalDeclaration = 30,
InputDeclaration = 31,
ComplexBody = 32,
Matcher = 33,
Selector = 34,
Variant = 35,
VariantKey = 36,
CatchAllKey = 37,
Markup = 50,
MarkupOpen = 51,
MarkupStandalone = 52,
MarkupClose = 53,
QuotedLiteral = 60,
UnquotedLiteral = 61,
Name = 62,
Identifier = 63,
Variable = 64,
LeftBraceToken = 100,
RightBraceToken = 101,
LeftDoubleBraceToken = 102,
RightDoubleBraceToken = 103,
DotToken = 104,
AtToken = 105,
PipeToken = 106,
EqualsToken = 107,
ColonToken = 108,
DollarToken = 109,
SlashToken = 110,
StarToken = 111,
HashToken = 112,
InputKeyword = 150,
LocalKeyword = 151,
MatchKeyword = 152,
NameToken = 170,
TextToken = 171,
QuotedTextToken = 172,
EscapeToken = 173,
WhitespaceTrivia = 200,
BidiTrivia = 201,
Error = 300,
Missing = 301,
Unknown = 302,
}
impl SyntaxKind {
#[inline]
pub const fn as_u16(self) -> u16 {
self as u16
}
#[inline]
pub const fn is_error(self) -> bool {
matches!(self, Self::Error | Self::Missing | Self::Unknown)
}
#[inline]
pub const fn is_root(self) -> bool {
matches!(self, Self::Root)
}
#[inline]
pub const fn is_message(self) -> bool {
matches!(self, Self::SimpleMessage | Self::ComplexMessage)
}
#[inline]
pub const fn is_pattern(self) -> bool {
matches!(self, Self::Pattern | Self::QuotedPattern)
}
#[inline]
pub const fn is_expression(self) -> bool {
matches!(
self,
Self::LiteralExpression | Self::VariableExpression | Self::FunctionExpression
)
}
#[inline]
pub const fn is_declaration(self) -> bool {
matches!(self, Self::LocalDeclaration | Self::InputDeclaration)
}
#[inline]
pub const fn is_markup(self) -> bool {
matches!(
self,
Self::Markup | Self::MarkupOpen | Self::MarkupStandalone | Self::MarkupClose
)
}
#[inline]
pub const fn is_literal(self) -> bool {
matches!(self, Self::QuotedLiteral | Self::UnquotedLiteral)
}
#[inline]
pub const fn is_punctuation_token(self) -> bool {
matches!(
self,
Self::LeftBraceToken
| Self::RightBraceToken
| Self::LeftDoubleBraceToken
| Self::RightDoubleBraceToken
| Self::DotToken
| Self::AtToken
| Self::PipeToken
| Self::EqualsToken
| Self::ColonToken
| Self::DollarToken
| Self::SlashToken
| Self::StarToken
| Self::HashToken
)
}
#[inline]
pub const fn is_keyword_token(self) -> bool {
matches!(
self,
Self::InputKeyword | Self::LocalKeyword | Self::MatchKeyword
)
}
#[inline]
pub const fn is_trivia(self) -> bool {
matches!(self, Self::WhitespaceTrivia | Self::BidiTrivia)
}
#[inline]
pub const fn is_token(self) -> bool {
self.is_punctuation_token()
|| self.is_keyword_token()
|| matches!(
self,
Self::NameToken | Self::TextToken | Self::QuotedTextToken | Self::EscapeToken
)
}
#[inline]
pub const fn is_node(self) -> bool {
!self.is_token() && !self.is_trivia() && !matches!(self, Self::Tombstone)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn category_boundaries_are_stable() {
assert_eq!(SyntaxKind::Tombstone.as_u16(), 0);
assert_eq!(SyntaxKind::Root.as_u16(), 1);
assert_eq!(SyntaxKind::SimpleMessage.as_u16(), 2);
assert_eq!(SyntaxKind::ComplexMessage.as_u16(), 3);
assert_eq!(SyntaxKind::Pattern.as_u16(), 10);
assert_eq!(SyntaxKind::QuotedPattern.as_u16(), 12);
assert_eq!(SyntaxKind::Placeholder.as_u16(), 13);
assert_eq!(SyntaxKind::LiteralExpression.as_u16(), 20);
assert_eq!(SyntaxKind::VariableExpression.as_u16(), 21);
assert_eq!(SyntaxKind::FunctionExpression.as_u16(), 22);
assert_eq!(SyntaxKind::LocalDeclaration.as_u16(), 30);
assert_eq!(SyntaxKind::InputDeclaration.as_u16(), 31);
assert_eq!(SyntaxKind::Matcher.as_u16(), 33);
assert_eq!(SyntaxKind::Variant.as_u16(), 35);
assert_eq!(SyntaxKind::CatchAllKey.as_u16(), 37);
assert_eq!(SyntaxKind::Markup.as_u16(), 50);
assert_eq!(SyntaxKind::QuotedLiteral.as_u16(), 60);
assert_eq!(SyntaxKind::UnquotedLiteral.as_u16(), 61);
assert_eq!(SyntaxKind::Name.as_u16(), 62);
assert_eq!(SyntaxKind::Identifier.as_u16(), 63);
assert_eq!(SyntaxKind::Variable.as_u16(), 64);
assert_eq!(SyntaxKind::LeftBraceToken.as_u16(), 100);
assert_eq!(SyntaxKind::InputKeyword.as_u16(), 150);
assert_eq!(SyntaxKind::WhitespaceTrivia.as_u16(), 200);
assert_eq!(SyntaxKind::Error.as_u16(), 300);
assert_eq!(SyntaxKind::Missing.as_u16(), 301);
assert_eq!(SyntaxKind::Unknown.as_u16(), 302);
}
#[test]
fn category_predicates_are_consistent() {
assert!(SyntaxKind::Root.is_root());
assert!(SyntaxKind::SimpleMessage.is_message());
assert!(SyntaxKind::Pattern.is_pattern());
assert!(SyntaxKind::LiteralExpression.is_expression());
assert!(SyntaxKind::LocalDeclaration.is_declaration());
assert!(SyntaxKind::MarkupOpen.is_markup());
assert!(SyntaxKind::QuotedLiteral.is_literal());
assert!(SyntaxKind::LeftBraceToken.is_punctuation_token());
assert!(SyntaxKind::InputKeyword.is_keyword_token());
assert!(SyntaxKind::WhitespaceTrivia.is_trivia());
assert!(SyntaxKind::TextToken.is_token());
assert!(SyntaxKind::Pattern.is_node());
assert!(SyntaxKind::Error.is_error());
assert!(SyntaxKind::Missing.is_error());
assert!(SyntaxKind::Unknown.is_error());
assert!(!SyntaxKind::Root.is_error());
assert!(!SyntaxKind::LeftBraceToken.is_node());
assert!(!SyntaxKind::WhitespaceTrivia.is_node());
assert!(!SyntaxKind::Tombstone.is_node());
}
}