use oak_core::{Token, TokenType, UniversalTokenRole};
pub type JavaScriptToken = Token<JavaScriptTokenType>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
pub enum JavaScriptTokenType {
Abstract,
As,
Async,
Await,
Break,
Case,
Catch,
Class,
Const,
Continue,
Debugger,
Default,
Delete,
Do,
Else,
Enum,
Export,
Extends,
False,
Finally,
For,
Function,
If,
Implements,
Import,
In,
Instanceof,
Interface,
Let,
New,
Null,
Package,
Private,
Protected,
Public,
Return,
Static,
Super,
Switch,
This,
Throw,
True,
Try,
Typeof,
Undefined,
Var,
Void,
While,
With,
Yield,
Plus,
Minus,
Star,
Slash,
Percent,
StarStar,
PlusPlus,
MinusMinus,
LeftShift,
RightShift,
UnsignedRightShift,
Less,
Greater,
LessEqual,
GreaterEqual,
EqualEqual,
NotEqual,
EqualEqualEqual,
NotEqualEqual,
Ampersand,
Pipe,
Caret,
Exclamation,
Tilde,
AmpersandAmpersand,
PipePipe,
Question,
QuestionQuestion,
QuestionDot,
Equal,
PlusEqual,
MinusEqual,
StarEqual,
SlashEqual,
PercentEqual,
StarStarEqual,
LeftShiftEqual,
RightShiftEqual,
UnsignedRightShiftEqual,
AmpersandEqual,
PipeEqual,
CaretEqual,
AmpersandAmpersandEqual,
PipePipeEqual,
QuestionQuestionEqual,
LeftParen,
RightParen,
LeftBrace,
RightBrace,
LeftBracket,
RightBracket,
Semicolon,
Comma,
Dot,
DotDotDot,
Colon,
Arrow,
StringLiteral,
NumericLiteral,
BigIntLiteral,
RegexLiteral,
TemplateString,
TemplateHead,
TemplateMiddle,
TemplateTail,
IdentifierName,
LineComment,
BlockComment,
Whitespace,
Newline,
Eof,
Error,
}
impl JavaScriptTokenType {
pub fn is_keyword(&self) -> bool {
matches!(
self,
Self::Abstract
| Self::As
| Self::Async
| Self::Await
| Self::Break
| Self::Case
| Self::Catch
| Self::Class
| Self::Const
| Self::Continue
| Self::Debugger
| Self::Default
| Self::Delete
| Self::Do
| Self::Else
| Self::Enum
| Self::Export
| Self::Extends
| Self::False
| Self::Finally
| Self::For
| Self::Function
| Self::If
| Self::Implements
| Self::Import
| Self::In
| Self::Instanceof
| Self::Interface
| Self::Let
| Self::New
| Self::Null
| Self::Package
| Self::Private
| Self::Protected
| Self::Public
| Self::Return
| Self::Static
| Self::Super
| Self::Switch
| Self::This
| Self::Throw
| Self::True
| Self::Try
| Self::Typeof
| Self::Undefined
| Self::Var
| Self::Void
| Self::While
| Self::With
| Self::Yield
)
}
pub fn from_keyword(s: &str) -> Option<Self> {
match s {
"abstract" => Some(Self::Abstract),
"as" => Some(Self::As),
"async" => Some(Self::Async),
"await" => Some(Self::Await),
"break" => Some(Self::Break),
"case" => Some(Self::Case),
"catch" => Some(Self::Catch),
"class" => Some(Self::Class),
"const" => Some(Self::Const),
"continue" => Some(Self::Continue),
"debugger" => Some(Self::Debugger),
"default" => Some(Self::Default),
"delete" => Some(Self::Delete),
"do" => Some(Self::Do),
"else" => Some(Self::Else),
"enum" => Some(Self::Enum),
"export" => Some(Self::Export),
"extends" => Some(Self::Extends),
"false" => Some(Self::False),
"finally" => Some(Self::Finally),
"for" => Some(Self::For),
"function" => Some(Self::Function),
"if" => Some(Self::If),
"implements" => Some(Self::Implements),
"import" => Some(Self::Import),
"in" => Some(Self::In),
"instanceof" => Some(Self::Instanceof),
"interface" => Some(Self::Interface),
"let" => Some(Self::Let),
"new" => Some(Self::New),
"null" => Some(Self::Null),
"package" => Some(Self::Package),
"private" => Some(Self::Private),
"protected" => Some(Self::Protected),
"public" => Some(Self::Public),
"return" => Some(Self::Return),
"static" => Some(Self::Static),
"super" => Some(Self::Super),
"switch" => Some(Self::Switch),
"this" => Some(Self::This),
"throw" => Some(Self::Throw),
"true" => Some(Self::True),
"try" => Some(Self::Try),
"typeof" => Some(Self::Typeof),
"undefined" => Some(Self::Undefined),
"var" => Some(Self::Var),
"void" => Some(Self::Void),
"while" => Some(Self::While),
"with" => Some(Self::With),
"yield" => Some(Self::Yield),
_ => None,
}
}
}
impl JavaScriptTokenType {
pub fn is_trivia(&self) -> bool {
matches!(self, Self::Whitespace | Self::Newline | Self::LineComment | Self::BlockComment)
}
}
impl TokenType for JavaScriptTokenType {
type Role = UniversalTokenRole;
const END_OF_STREAM: Self = Self::Error;
fn is_ignored(&self) -> bool {
false
}
fn role(&self) -> Self::Role {
match self {
_ => UniversalTokenRole::None,
}
}
}