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 {
Use,
Pub,
Fn,
Let,
Const,
Return,
If,
Else,
While,
For,
In,
Loop,
Break,
Continue,
Struct,
Enum,
Impl,
Match,
True,
False,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TokenKind<'src> {
StringLit(&'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,
Minus,
Star,
Slash,
Percent,
AmpAmp,
Pipe,
PipePipe,
Eof,
}
impl fmt::Display for TokenKind<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let text = match self {
Self::StringLit(_) => "<string>",
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::Minus => "-",
Self::Star => "*",
Self::Slash => "/",
Self::Percent => "%",
Self::AmpAmp => "&&",
Self::Pipe => "|",
Self::PipePipe => "||",
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 }
}
}