use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TokenType {
Identifier,
Number,
Operator,
Comma,
ParaOpen,
ParaClose,
Unimplemented,
}
impl fmt::Display for TokenType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{self:?}")
}
}
#[derive(Debug, PartialEq)]
pub struct Token {
pub token_type: TokenType,
pub text: String,
pub position: usize,
}
impl fmt::Display for Token {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{{type: {}, text: '{}', pos: {}}}",
self.token_type, self.text, self.position
)
}
}
impl Token {
pub fn new(token_type: TokenType, text: String, position: usize) -> Self {
Token {
token_type,
text,
position,
}
}
}
#[derive(Debug)]
pub struct LexResult {
pub rhs: Vec<Token>,
}
impl fmt::Display for LexResult {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "LexResult [")?;
for token in &self.rhs {
writeln!(f, " {token},")?;
}
write!(f, "]")
}
}