use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Position {
pub offset: u32,
pub line: u32,
pub column: u32,
}
impl Position {
pub const START: Position = Position {
offset: 0,
line: 1,
column: 1,
};
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Span {
pub start: Position,
pub end: Position,
}
impl Span {
pub const fn start() -> Span {
Span {
start: Position::START,
end: Position::START,
}
}
pub fn new(start: Position, end: Position) -> Span {
Span { start, end }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Keyword {
Break,
Case,
Catch,
Class,
Const,
Continue,
Debugger,
Default,
Delete,
Do,
Else,
Enum,
Export,
Extends,
False,
Finally,
For,
Function,
If,
Import,
In,
Instanceof,
New,
Null,
Return,
Super,
Switch,
This,
Throw,
True,
Try,
Typeof,
Var,
Void,
While,
With,
}
impl Keyword {
pub fn lookup(s: &str) -> Option<Keyword> {
use Keyword::*;
Some(match s {
"break" => Break,
"case" => Case,
"catch" => Catch,
"class" => Class,
"const" => Const,
"continue" => Continue,
"debugger" => Debugger,
"default" => Default,
"delete" => Delete,
"do" => Do,
"else" => Else,
"enum" => Enum,
"export" => Export,
"extends" => Extends,
"false" => False,
"finally" => Finally,
"for" => For,
"function" => Function,
"if" => If,
"import" => Import,
"in" => In,
"instanceof" => Instanceof,
"new" => New,
"null" => Null,
"return" => Return,
"super" => Super,
"switch" => Switch,
"this" => This,
"throw" => Throw,
"true" => True,
"try" => Try,
"typeof" => Typeof,
"var" => Var,
"void" => Void,
"while" => While,
"with" => With,
_ => return None,
})
}
pub fn as_str(self) -> &'static str {
use Keyword::*;
match self {
Break => "break",
Case => "case",
Catch => "catch",
Class => "class",
Const => "const",
Continue => "continue",
Debugger => "debugger",
Default => "default",
Delete => "delete",
Do => "do",
Else => "else",
Enum => "enum",
Export => "export",
Extends => "extends",
False => "false",
Finally => "finally",
For => "for",
Function => "function",
If => "if",
Import => "import",
In => "in",
Instanceof => "instanceof",
New => "new",
Null => "null",
Return => "return",
Super => "super",
Switch => "switch",
This => "this",
Throw => "throw",
True => "true",
Try => "try",
Typeof => "typeof",
Var => "var",
Void => "void",
While => "while",
With => "with",
}
}
pub fn is_strict_reserved(word: &str) -> bool {
matches!(
word,
"implements"
| "interface"
| "let"
| "package"
| "private"
| "protected"
| "public"
| "static"
| "yield"
)
}
}
impl fmt::Display for Keyword {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[allow(missing_docs)]
pub enum Punct {
LBrace,
RBrace,
LParen,
RParen,
LBracket,
RBracket,
Semicolon,
Comma,
Lt,
Gt,
LtEq,
GtEq,
EqEq,
NotEq,
EqEqEq,
NotEqEq,
Plus,
Minus,
Star,
Slash,
Percent,
StarStar,
PlusPlus,
MinusMinus,
Shl,
Shr,
UShr,
Amp,
Pipe,
Caret,
Bang,
Tilde,
AmpAmp,
PipePipe,
QuestionQuestion,
Question,
Colon,
Eq,
PlusEq,
MinusEq,
StarEq,
SlashEq,
PercentEq,
StarStarEq,
ShlEq,
ShrEq,
UShrEq,
AmpEq,
PipeEq,
CaretEq,
AmpAmpEq,
PipePipeEq,
QuestionQuestionEq,
Arrow,
Ellipsis,
Dot,
QuestionDot,
Hash,
}
impl Punct {
pub fn as_str(self) -> &'static str {
use Punct::*;
match self {
LBrace => "{",
RBrace => "}",
LParen => "(",
RParen => ")",
LBracket => "[",
RBracket => "]",
Semicolon => ";",
Comma => ",",
Lt => "<",
Gt => ">",
LtEq => "<=",
GtEq => ">=",
EqEq => "==",
NotEq => "!=",
EqEqEq => "===",
NotEqEq => "!==",
Plus => "+",
Minus => "-",
Star => "*",
Slash => "/",
Percent => "%",
StarStar => "**",
PlusPlus => "++",
MinusMinus => "--",
Shl => "<<",
Shr => ">>",
UShr => ">>>",
Amp => "&",
Pipe => "|",
Caret => "^",
Bang => "!",
Tilde => "~",
AmpAmp => "&&",
PipePipe => "||",
QuestionQuestion => "??",
Question => "?",
Colon => ":",
Eq => "=",
PlusEq => "+=",
MinusEq => "-=",
StarEq => "*=",
SlashEq => "/=",
PercentEq => "%=",
StarStarEq => "**=",
ShlEq => "<<=",
ShrEq => ">>=",
UShrEq => ">>>=",
AmpEq => "&=",
PipeEq => "|=",
CaretEq => "^=",
AmpAmpEq => "&&=",
PipePipeEq => "||=",
QuestionQuestionEq => "??=",
Arrow => "=>",
Ellipsis => "...",
Dot => ".",
QuestionDot => "?.",
Hash => "#",
}
}
pub fn is_asi_boundary(self) -> bool {
matches!(self, Punct::Semicolon | Punct::RBrace)
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum NumberLiteral {
Int(i32),
Float(f64),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BigIntLiteral {
pub digits: String,
pub radix: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TemplateKind {
NoSubstitution,
Head,
Middle,
Tail,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TemplateToken {
pub kind: TemplateKind,
pub cooked: Option<String>,
pub raw: String,
}
#[derive(Debug, Clone, PartialEq)]
pub enum TokenKind {
Eof,
Ident(String),
Keyword(Keyword),
Number(NumberLiteral),
BigInt(BigIntLiteral),
String(String),
RegExp {
pattern: String,
flags: String,
},
Template(TemplateToken),
Punct(Punct),
}
impl TokenKind {
pub fn describe(&self) -> String {
match self {
TokenKind::Eof => "end of input".to_string(),
TokenKind::Ident(name) => format!("identifier '{name}'"),
TokenKind::Keyword(kw) => format!("'{kw}'"),
TokenKind::Number(n) => match n {
NumberLiteral::Int(v) => format!("number '{v}'"),
NumberLiteral::Float(v) => format!("number '{v}'"),
},
TokenKind::BigInt(b) => format!("BigInt literal '{}n'", b.digits),
TokenKind::String(_) => "string literal".to_string(),
TokenKind::RegExp { .. } => "regular expression".to_string(),
TokenKind::Template(_) => "template literal".to_string(),
TokenKind::Punct(p) => format!("'{}'", p.as_str()),
}
}
pub fn as_punct(&self) -> Option<Punct> {
match self {
TokenKind::Punct(p) => Some(*p),
_ => None,
}
}
pub fn as_keyword(&self) -> Option<Keyword> {
match self {
TokenKind::Keyword(kw) => Some(*kw),
_ => None,
}
}
pub fn is_identifier(&self) -> bool {
matches!(self, TokenKind::Ident(_))
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Token {
pub kind: TokenKind,
pub span: Span,
pub newline_before: bool,
}
impl Token {
pub fn is_eof(&self) -> bool {
matches!(self.kind, TokenKind::Eof)
}
}