//! The Token defines an interface between lexer and parser use crate::span::Span; #[derive(Clone, Debug)] pub struct Token { pub lexeme: Lexeme, pub kind: TKind, pub span: Span, } impl Token { pub fn kind(&self) -> TKind { self.kind } } #[derive(Clone, Debug)] pub enum Lexeme { String(String), Integer(u128, u32), Char(char), } impl Lexeme { pub fn string(self) -> Option { match self { Self::String(s) => Some(s), _ => None, } } pub fn str(&self) -> Option<&str> { match self { Self::String(s) => Some(s), _ => None, } } pub fn int(&self) -> Option { match self { Self::Integer(i, _) => Some(*i), _ => None, } } pub fn char(&self) -> Option { match self { Self::Char(c) => Some(*c), _ => None, } } } impl std::fmt::Display for Lexeme { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::String(v) => v.fmt(f), Self::Integer(v, _) => v.fmt(f), Self::Char(v) => v.fmt(f), } } } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum TKind { Comment, // Line or block comment Doc, // Doc comment As, Break, Const, Do, Else, Enum, False, Fn, For, If, Impl, In, Let, Loop, Macro, Match, Mod, Mut, Pub, Return, Static, Struct, True, Type, Use, While, Identifier, // or Keyword Character, String, Integer, // 0(x[0-9A-Fa-f]* | d[0-9]* | o[0-7]* | b[0-1]*) | [1-9][0-9]* LCurly, // { RCurly, // } LBrack, // [ RBrack, // ] LParen, // ( RParen, // ) Amp, // & AmpAmp, // && AmpEq, // &= Arrow, // -> At, // @ Backslash, // \ Bang, // ! BangBang, // !! BangEq, // != Bar, // | BarBar, // || BarEq, // |= Colon, // : ColonColon, // :: Comma, // , Dot, // . DotDot, // .. DotDotEq, // ..= Eq, // = EqEq, // == FatArrow, // => Grave, // ` Gt, // > GtEq, // >= GtGt, // >> GtGtEq, // >>= Hash, // # HashBang, // #! Lt, // < LtEq, // <= LtLt, // << LtLtEq, // <<= Minus, // - MinusEq, // -= Plus, // + PlusEq, // += Question, // ? Rem, // % RemEq, // %= Semi, // ; Slash, // / SlashEq, // /= Star, // * StarEq, // *= Tilde, // ~ Xor, // ^ XorEq, // ^= XorXor, // ^^ }