token: Add Display impl for Type

This improves readability in identify_tokens
This commit is contained in:
John 2023-09-28 01:34:56 -05:00
parent 48f5e5248c
commit 545483dae6
3 changed files with 73 additions and 1 deletions

View File

@ -9,7 +9,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let line = line?; let line = line?;
let mut lexer = Lexer::new(&line); let mut lexer = Lexer::new(&line);
while let Some(token) = lexer.any() { while let Some(token) = lexer.any() {
println!("{:?}: {}", token, &line[token.range()]) println!("{:#19}{}", token.ty(), &line[token.range()])
} }
} }
Ok(()) Ok(())

View File

@ -5,6 +5,7 @@ pub mod token {
//! Stores a component of a file as a type and span //! Stores a component of a file as a type and span
use std::ops::Range; use std::ops::Range;
mod token_type;
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Type { pub enum Type {
Invalid, Invalid,

View File

@ -0,0 +1,71 @@
use super::Type;
use std::fmt::Display;
impl Display for Type {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Type::Invalid => Display::fmt("invalid", f),
Type::Comment => Display::fmt("comment", f),
Type::Identifier => Display::fmt("identifier", f),
Type::KwBreak => Display::fmt("break", f),
Type::KwElse => Display::fmt("else", f),
Type::KwFalse => Display::fmt("false", f),
Type::KwFor => Display::fmt("for", f),
Type::KwFn => Display::fmt("fn", f),
Type::KwIf => Display::fmt("if", f),
Type::KwIn => Display::fmt("in", f),
Type::KwLet => Display::fmt("let", f),
Type::KwTrue => Display::fmt("true", f),
Type::KwWhile => Display::fmt("while", f),
Type::LitInteger => Display::fmt("integer literal", f),
Type::LitFloat => Display::fmt("float literal", f),
Type::LitString => Display::fmt("string literal", f),
Type::LCurly => Display::fmt("left curly", f),
Type::RCurly => Display::fmt("right curly", f),
Type::LBrack => Display::fmt("left brack", f),
Type::RBrack => Display::fmt("right brack", f),
Type::LParen => Display::fmt("left paren", f),
Type::RParen => Display::fmt("right paren", f),
Type::Lsh => Display::fmt("shift left", f),
Type::Rsh => Display::fmt("shift right", f),
Type::AndAnd => Display::fmt("logical and", f),
Type::OrOr => Display::fmt("logical or", f),
Type::NotNot => Display::fmt("not-not", f),
Type::CatEar => Display::fmt("cat-ears", f),
Type::EqEq => Display::fmt("equal to", f),
Type::NotEq => Display::fmt("not equal to", f),
Type::StarEq => Display::fmt("star-assign", f),
Type::DivEq => Display::fmt("div-assign", f),
Type::AddEq => Display::fmt("add-assign", f),
Type::SubEq => Display::fmt("sub-assign", f),
Type::AndEq => Display::fmt("and-assign", f),
Type::OrEq => Display::fmt("or-assign", f),
Type::XorEq => Display::fmt("xor-assign", f),
Type::LshEq => Display::fmt("shift left-assign", f),
Type::RshEq => Display::fmt("shift right-assign", f),
Type::Arrow => Display::fmt("arrow", f),
Type::FatArrow => Display::fmt("fat arrow", f),
Type::Semi => Display::fmt("ignore", f),
Type::Dot => Display::fmt("dot", f),
Type::Star => Display::fmt("star", f),
Type::Div => Display::fmt("div", f),
Type::Plus => Display::fmt("add", f),
Type::Minus => Display::fmt("sub", f),
Type::Rem => Display::fmt("rem", f),
Type::Bang => Display::fmt("bang", f),
Type::Eq => Display::fmt("assign", f),
Type::Lt => Display::fmt("less than", f),
Type::Gt => Display::fmt("greater than", f),
Type::Amp => Display::fmt("and", f),
Type::Bar => Display::fmt("or", f),
Type::Xor => Display::fmt("xor", f),
Type::Hash => Display::fmt("hash", f),
Type::At => Display::fmt("at", f),
Type::Colon => Display::fmt("colon", f),
Type::Backslash => Display::fmt("backslash", f),
Type::Question => Display::fmt("huh?", f),
Type::Comma => Display::fmt("comma", f),
Type::Tilde => Display::fmt("tilde", f),
Type::Grave => Display::fmt("grave", f),
}
}
}