#[derive(Clone, PartialEq)]
pub struct Token {
pub token_type: TokenType,
pub text: Option<String>,
}
impl Token {
pub fn new(token_type: TokenType, text: Option<String>) -> Self {
Self { token_type, text }
}
pub fn simple(token_type: TokenType) -> Self {
Self::new(token_type, None)
}
pub fn with_text(token_type: TokenType, text: &str) -> Self {
Self::new(token_type, Some(text.to_string()))
}
}
impl std::fmt::Debug for Token {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.text {
Some(text) => write!(f, "Token({:?}, {:?})", self.token_type, text),
None => write!(f, "Token({:?})", self.token_type),
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum TokenType {
Int,
Float,
String, RawString, StringPart, InterpolationStart, InterpolationEnd, True,
False,
Null,
DocComment, DocCommentBlock, InnerDocComment, InnerDocCommentBlock,
Ident,
Var,
Fn,
If,
Else,
While,
For,
Loop,
Return,
Break,
Continue,
Class,
Static,
Import,
As,
From,
Match,
Case,
Try,
Catch,
With,
And,
Or,
Xor,
Not,
In,
Is,
NotIn, IsNot, Pub,
Use,
Macro,
Hash,
Plus,
Minus,
Star,
Slash,
Percent,
StarStar,
Equal,
PlusEqual,
MinusEqual,
StarEqual,
SlashEqual,
PercentEqual,
EqualEqual,
Bang,
BangEqual,
Less,
Greater,
LessEqual,
GreaterEqual,
Pipe,
DotDot,
DotDotEqual,
LeftParen,
RightParen,
LeftBrace,
RightBrace,
LeftBracket,
RightBracket,
Comma,
Semicolon,
Dot,
Colon,
DoubleColon,
Eof,
}