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-09 00:19:07 +00:00
|
|
|
/// Source code token.
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
2024-08-08 20:19:14 +00:00
|
|
|
pub enum Token<'src> {
|
2024-08-07 22:24:25 +00:00
|
|
|
Eof,
|
|
|
|
|
2024-08-08 20:19:14 +00:00
|
|
|
Identifier(&'src str),
|
2024-08-07 22:24:25 +00:00
|
|
|
|
|
|
|
// 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),
|
2024-08-08 20:19:14 +00:00
|
|
|
String(&'src str),
|
2024-08-07 22:24:25 +00:00
|
|
|
|
|
|
|
// Keywords
|
|
|
|
IsEven,
|
|
|
|
IsOdd,
|
|
|
|
Length,
|
2024-08-08 17:49:40 +00:00
|
|
|
ReadLine,
|
|
|
|
WriteLine,
|
2024-08-07 22:24:25 +00:00
|
|
|
|
|
|
|
// 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,
|
|
|
|
LeftParenthesis,
|
2024-08-05 01:31:18 +00:00
|
|
|
LeftSquareBrace,
|
2024-08-08 17:49:40 +00:00
|
|
|
Plus,
|
|
|
|
RightParenthesis,
|
2024-08-05 01:31:18 +00:00
|
|
|
RightSquareBrace,
|
2024-08-08 17:49:40 +00:00
|
|
|
Star,
|
2024-08-05 19:54:48 +00:00
|
|
|
}
|
2024-08-07 14:03:33 +00:00
|
|
|
|
2024-08-09 00:19:07 +00:00
|
|
|
impl<'src> Token<'src> {
|
|
|
|
pub fn to_owned(&self) -> TokenOwned {
|
|
|
|
match self {
|
|
|
|
Token::Eof => TokenOwned::Eof,
|
|
|
|
Token::Identifier(text) => TokenOwned::Identifier(text.to_string()),
|
|
|
|
Token::Boolean(boolean) => TokenOwned::Boolean(*boolean),
|
|
|
|
Token::Float(float) => TokenOwned::Float(*float),
|
|
|
|
Token::Integer(integer) => TokenOwned::Integer(*integer),
|
|
|
|
Token::String(text) => TokenOwned::String(text.to_string()),
|
|
|
|
Token::IsEven => TokenOwned::IsEven,
|
|
|
|
Token::IsOdd => TokenOwned::IsOdd,
|
|
|
|
Token::Length => TokenOwned::Length,
|
|
|
|
Token::ReadLine => TokenOwned::ReadLine,
|
|
|
|
Token::WriteLine => TokenOwned::WriteLine,
|
|
|
|
Token::Comma => TokenOwned::Comma,
|
|
|
|
Token::Dot => TokenOwned::Dot,
|
|
|
|
Token::Equal => TokenOwned::Equal,
|
|
|
|
Token::Plus => TokenOwned::Plus,
|
|
|
|
Token::Star => TokenOwned::Star,
|
|
|
|
Token::LeftParenthesis => TokenOwned::LeftParenthesis,
|
|
|
|
Token::RightParenthesis => TokenOwned::RightParenthesis,
|
|
|
|
Token::LeftSquareBrace => TokenOwned::LeftSquareBrace,
|
|
|
|
Token::RightSquareBrace => TokenOwned::RightSquareBrace,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-08 20:19:14 +00:00
|
|
|
impl<'src> Display for Token<'src> {
|
2024-08-07 22:24:25 +00:00
|
|
|
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"),
|
2024-08-08 20:19:14 +00:00
|
|
|
Token::Identifier(text) => write!(f, "{text}"),
|
2024-08-07 22:24:25 +00:00
|
|
|
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}"),
|
2024-08-07 22:24:25 +00:00
|
|
|
Token::IsEven => write!(f, "is_even"),
|
|
|
|
Token::IsOdd => write!(f, "is_odd"),
|
|
|
|
Token::Length => write!(f, "length"),
|
2024-08-08 17:49:40 +00:00
|
|
|
Token::ReadLine => write!(f, "read_line"),
|
|
|
|
Token::WriteLine => write!(f, "write_line"),
|
2024-08-07 22:24:25 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-08 20:19:14 +00:00
|
|
|
|
2024-08-09 00:19:07 +00:00
|
|
|
/// Owned version of `Token`, which owns all the strings.
|
|
|
|
///
|
|
|
|
/// This is used for errors.
|
2024-08-08 20:19:14 +00:00
|
|
|
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
|
|
|
|
pub enum TokenOwned {
|
|
|
|
Eof,
|
|
|
|
|
|
|
|
Identifier(String),
|
|
|
|
|
|
|
|
// Hard-coded values
|
|
|
|
Boolean(bool),
|
|
|
|
Float(f64),
|
|
|
|
Integer(i64),
|
|
|
|
String(String),
|
|
|
|
|
|
|
|
// Keywords
|
|
|
|
IsEven,
|
|
|
|
IsOdd,
|
|
|
|
Length,
|
|
|
|
ReadLine,
|
|
|
|
WriteLine,
|
|
|
|
|
|
|
|
// Symbols
|
|
|
|
Comma,
|
|
|
|
Dot,
|
|
|
|
Equal,
|
|
|
|
LeftParenthesis,
|
|
|
|
LeftSquareBrace,
|
|
|
|
Plus,
|
|
|
|
RightParenthesis,
|
|
|
|
RightSquareBrace,
|
|
|
|
Star,
|
|
|
|
}
|