use crate::source_tracking::fragment::Fragment;
use std::fmt::{self, Display};
#[derive(Debug)]
pub struct Token {
pub variant: TokenTy,
pub fragment: Fragment,
}
#[rustfmt::skip] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[allow(missing_docs)]
pub enum TokenTy {
LeftCurly, RightCurly,
LeftBracket, RightBracket,
LeftParen, RightParen,
Plus, PlusEq,
Star, StarEq,
Div, DivEq,
Xor, XorEq,
Mod, ModEq,
Bang, BangEq,
Minus, MinusEq, SingleArrow,
Eq, EqEq, DoubleArrow,
Lt, LtEq, LtLt,
Gt, GtEq, GtGt,
And, AndEq, AndAnd,
Or, OrEq, OrOr,
Colon, ColonEq, ColonColon,
At,
Tilde,
Semi,
Dot,
Comma,
Hash,
Question,
Dollar,
Underscore,
Identifier,
OuterDocComment, OuterBlockDocComment,
InnerDocComment, InnerBlockDocComment,
UnterminatedBlockComment,
KwRecord,
KwType,
KwEnum,
KwUnion,
KwFunc,
KwPure,
KwRepr,
KwImpl,
KwConstraint,
KwReferences,
KwTrait,
KwUse,
KwAs,
KwConst,
KwMod,
KwIf,
KwElse,
KwMatch,
KwFor,
KwIn,
KwWhile,
KwTrue,
KwFalse,
KwLoop,
KwWhere,
KwPub,
IntegerLiteral,
StringLiteral { terminated: bool },
FormatStringLiteral { terminated: bool },
CharLiteral { terminated: bool },
Whitespace,
Unknown
}
impl Display for Token {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let replacements = match crate::util::supports_unicode::supports_unicode() {
true => &[("\n", "\u{240A}"), ("\r", "\u{240D}")],
false => &[("\n", "[nl]"), ("\r", "[cr]")],
};
let mut with_replacements = self.fragment.as_str().to_owned();
for (replace, replace_with) in replacements {
with_replacements = with_replacements.replace(replace, replace_with);
}
write!(f, "\"{with_replacements}\" ({:?})", self.variant)
}
}