2024-08-07 22:24:25 +00:00
|
|
|
use std::fmt::{self, Display, Formatter};
|
2024-08-07 14:03:33 +00:00
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2024-08-04 00:23:52 +00:00
|
|
|
use crate::Identifier;
|
|
|
|
|
2024-08-07 22:24:25 +00:00
|
|
|
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
|
2024-08-04 00:23:52 +00:00
|
|
|
pub enum Token {
|
2024-08-07 22:24:25 +00:00
|
|
|
Eof,
|
|
|
|
|
|
|
|
Identifier(Identifier),
|
|
|
|
|
|
|
|
// Hard-coded values
|
2024-08-07 14:41:27 +00:00
|
|
|
Boolean(bool),
|
2024-08-07 22:24:25 +00:00
|
|
|
Float(f64),
|
|
|
|
Integer(i64),
|
|
|
|
|
|
|
|
// Keywords
|
|
|
|
IsEven,
|
|
|
|
IsOdd,
|
|
|
|
Length,
|
|
|
|
|
|
|
|
// Symbols
|
2024-08-05 01:31:18 +00:00
|
|
|
Comma,
|
2024-08-05 18:31:08 +00:00
|
|
|
Dot,
|
2024-08-04 00:23:52 +00:00
|
|
|
Equal,
|
|
|
|
Plus,
|
|
|
|
Star,
|
|
|
|
LeftParenthesis,
|
|
|
|
RightParenthesis,
|
2024-08-05 01:31:18 +00:00
|
|
|
LeftSquareBrace,
|
|
|
|
RightSquareBrace,
|
2024-08-05 19:54:48 +00:00
|
|
|
}
|
2024-08-07 14:03:33 +00:00
|
|
|
|
2024-08-07 22:24:25 +00:00
|
|
|
impl Display for Token {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
2024-08-07 14:03:33 +00:00
|
|
|
match self {
|
2024-08-07 22:24:25 +00:00
|
|
|
Token::Eof => write!(f, "EOF"),
|
|
|
|
Token::Identifier(identifier) => write!(f, "{identifier}"),
|
|
|
|
Token::Boolean(boolean) => write!(f, "{boolean}"),
|
|
|
|
Token::Float(float) => write!(f, "{float}"),
|
|
|
|
Token::Integer(integer) => write!(f, "{integer}"),
|
|
|
|
Token::IsEven => write!(f, "is_even"),
|
|
|
|
Token::IsOdd => write!(f, "is_odd"),
|
|
|
|
Token::Length => write!(f, "length"),
|
|
|
|
Token::Comma => write!(f, ","),
|
|
|
|
Token::Dot => write!(f, "."),
|
|
|
|
Token::Equal => write!(f, "="),
|
|
|
|
Token::Plus => write!(f, "+"),
|
|
|
|
Token::Star => write!(f, "*"),
|
|
|
|
Token::LeftParenthesis => write!(f, "("),
|
|
|
|
Token::RightParenthesis => write!(f, ")"),
|
|
|
|
Token::LeftSquareBrace => write!(f, "["),
|
|
|
|
Token::RightSquareBrace => write!(f, "]"),
|
2024-08-07 14:03:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|