use std::fmt;
use strum::{Display, EnumString};
use xur_span::Span;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display, EnumString)]
#[strum(serialize_all = "lowercase")]
pub enum Keyword {
As,
Use,
Pub,
Fn,
Let,
Const,
Return,
If,
Else,
While,
For,
In,
Loop,
Break,
Continue,
Struct,
Enum,
Impl,
Match,
True,
False,
Is,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TokenKind<'src> {
StringLit(&'src str),
ByteStrLit(&'src str),
IntLit(&'src str),
FloatLit(&'src str),
Ident(&'src str),
Keyword(Keyword),
Underscore,
LParen,
RParen,
LBrace,
RBrace,
LBracket,
RBracket,
Semi,
Comma,
Dot,
DotDot,
DotDotEq,
ColonColon,
Colon,
Hash,
Arrow,
FatArrow,
Question,
Eq,
EqEq,
BangEq,
Bang,
Lt,
LtEq,
Gt,
GtEq,
Plus,
PlusEq,
Minus,
MinusEq,
Star,
StarEq,
Slash,
Percent,
Amp,
AmpAmp,
Pipe,
PipePipe,
Caret,
Tilde,
At,
Eof,
}
impl fmt::Display for TokenKind<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let text = match self {
Self::StringLit(_) => "<string>",
Self::ByteStrLit(_) => "<bytes>",
Self::IntLit(_) => "<int>",
Self::FloatLit(_) => "<float>",
Self::Ident(_) => "<ident>",
Self::Keyword(keyword) => return keyword.fmt(f),
Self::Underscore => "_",
Self::LParen => "(",
Self::RParen => ")",
Self::LBrace => "{",
Self::RBrace => "}",
Self::LBracket => "[",
Self::RBracket => "]",
Self::Semi => ";",
Self::Comma => ",",
Self::Dot => ".",
Self::DotDot => "..",
Self::DotDotEq => "..=",
Self::ColonColon => "::",
Self::Colon => ":",
Self::Hash => "#",
Self::Arrow => "->",
Self::FatArrow => "=>",
Self::Question => "?",
Self::Eq => "=",
Self::EqEq => "==",
Self::BangEq => "!=",
Self::Bang => "!",
Self::Lt => "<",
Self::LtEq => "<=",
Self::Gt => ">",
Self::GtEq => ">=",
Self::Plus => "+",
Self::PlusEq => "+=",
Self::Minus => "-",
Self::MinusEq => "-=",
Self::Star => "*",
Self::StarEq => "*=",
Self::Slash => "/",
Self::Percent => "%",
Self::Amp => "&",
Self::AmpAmp => "&&",
Self::Pipe => "|",
Self::PipePipe => "||",
Self::Caret => "^",
Self::Tilde => "~",
Self::At => "@",
Self::Eof => "<eof>",
};
f.write_str(text)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Token<'src> {
pub kind: TokenKind<'src>,
pub span: Span,
}
impl<'src> Token<'src> {
pub(crate) fn new(kind: TokenKind<'src>, span: Span) -> Self {
Self { kind, span }
}
}