1
0
dust/dust-lang/src/token.rs

60 lines
1.5 KiB
Rust
Raw Normal View History

use std::fmt::{self, Display, Formatter};
use serde::{Deserialize, Serialize};
2024-08-04 00:23:52 +00:00
use crate::Identifier;
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
2024-08-04 00:23:52 +00:00
pub enum Token {
Eof,
Identifier(Identifier),
// Hard-coded values
2024-08-07 14:41:27 +00:00
Boolean(bool),
Float(f64),
Integer(i64),
2024-08-08 17:08:53 +00:00
String(String),
// 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,
}
impl Display for Token {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
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}"),
2024-08-08 17:08:53 +00:00
Token::String(string) => write!(f, "{string}"),
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, "]"),
}
}
}