2024-08-09 01:59:09 +00:00
|
|
|
//! Token and TokenOwned types.
|
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.
|
2024-08-09 04:31:38 +00:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2024-08-08 20:19:14 +00:00
|
|
|
pub enum Token<'src> {
|
2024-08-09 22:14:46 +00:00
|
|
|
// End of file
|
2024-08-07 22:24:25 +00:00
|
|
|
Eof,
|
|
|
|
|
|
|
|
// Hard-coded values
|
2024-08-09 18:01:01 +00:00
|
|
|
Boolean(&'src str),
|
|
|
|
Float(&'src str),
|
2024-08-09 22:14:46 +00:00
|
|
|
Identifier(&'src str),
|
2024-08-09 18:01:01 +00:00
|
|
|
Integer(&'src str),
|
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,
|
2024-08-10 09:23:43 +00:00
|
|
|
While,
|
2024-08-08 17:49:40 +00:00
|
|
|
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-09 08:56:24 +00:00
|
|
|
DoubleAmpersand,
|
2024-08-09 11:15:09 +00:00
|
|
|
DoubleEqual,
|
2024-08-09 08:56:24 +00:00
|
|
|
DoublePipe,
|
2024-08-04 00:23:52 +00:00
|
|
|
Equal,
|
2024-08-09 07:00:48 +00:00
|
|
|
Greater,
|
|
|
|
GreaterEqual,
|
2024-08-09 09:18:39 +00:00
|
|
|
LeftCurlyBrace,
|
2024-08-04 00:23:52 +00:00
|
|
|
LeftParenthesis,
|
2024-08-05 01:31:18 +00:00
|
|
|
LeftSquareBrace,
|
2024-08-09 07:00:48 +00:00
|
|
|
Less,
|
|
|
|
LessEqual,
|
2024-08-09 03:28:47 +00:00
|
|
|
Minus,
|
2024-08-09 11:02:55 +00:00
|
|
|
Percent,
|
2024-08-08 17:49:40 +00:00
|
|
|
Plus,
|
2024-08-09 22:14:46 +00:00
|
|
|
PlusEqual,
|
2024-08-09 09:18:39 +00:00
|
|
|
RightCurlyBrace,
|
2024-08-08 17:49:40 +00:00
|
|
|
RightParenthesis,
|
2024-08-05 01:31:18 +00:00
|
|
|
RightSquareBrace,
|
2024-08-09 15:41:23 +00:00
|
|
|
Semicolon,
|
2024-08-09 10:46:24 +00:00
|
|
|
Slash,
|
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 {
|
2024-08-09 18:01:01 +00:00
|
|
|
Token::Boolean(boolean) => TokenOwned::Boolean(boolean.to_string()),
|
2024-08-09 07:00:48 +00:00
|
|
|
Token::Comma => TokenOwned::Comma,
|
|
|
|
Token::Dot => TokenOwned::Dot,
|
2024-08-09 08:56:24 +00:00
|
|
|
Token::DoubleAmpersand => TokenOwned::DoubleAmpersand,
|
2024-08-09 11:15:09 +00:00
|
|
|
Token::DoubleEqual => TokenOwned::DoubleEqual,
|
2024-08-09 08:56:24 +00:00
|
|
|
Token::DoublePipe => TokenOwned::DoublePipe,
|
2024-08-09 07:00:48 +00:00
|
|
|
Token::Eof => TokenOwned::Eof,
|
|
|
|
Token::Equal => TokenOwned::Equal,
|
2024-08-09 18:01:01 +00:00
|
|
|
Token::Float(float) => TokenOwned::Float(float.to_string()),
|
2024-08-09 07:00:48 +00:00
|
|
|
Token::Greater => TokenOwned::Greater,
|
|
|
|
Token::GreaterEqual => TokenOwned::GreaterOrEqual,
|
|
|
|
Token::Identifier(text) => TokenOwned::Identifier(text.to_string()),
|
2024-08-09 18:01:01 +00:00
|
|
|
Token::Integer(integer) => TokenOwned::Integer(integer.to_string()),
|
2024-08-09 00:19:07 +00:00
|
|
|
Token::IsEven => TokenOwned::IsEven,
|
|
|
|
Token::IsOdd => TokenOwned::IsOdd,
|
2024-08-09 09:18:39 +00:00
|
|
|
Token::LeftCurlyBrace => TokenOwned::LeftCurlyBrace,
|
2024-08-09 07:00:48 +00:00
|
|
|
Token::LeftParenthesis => TokenOwned::LeftParenthesis,
|
|
|
|
Token::LeftSquareBrace => TokenOwned::LeftSquareBrace,
|
2024-08-09 00:19:07 +00:00
|
|
|
Token::Length => TokenOwned::Length,
|
2024-08-09 07:00:48 +00:00
|
|
|
Token::Less => TokenOwned::Less,
|
|
|
|
Token::LessEqual => TokenOwned::LessOrEqual,
|
|
|
|
Token::Minus => TokenOwned::Minus,
|
2024-08-09 11:02:55 +00:00
|
|
|
Token::Percent => TokenOwned::Percent,
|
2024-08-09 00:19:07 +00:00
|
|
|
Token::Plus => TokenOwned::Plus,
|
2024-08-09 22:14:46 +00:00
|
|
|
Token::PlusEqual => TokenOwned::PlusEqual,
|
2024-08-09 07:00:48 +00:00
|
|
|
Token::ReadLine => TokenOwned::ReadLine,
|
2024-08-09 09:18:39 +00:00
|
|
|
Token::RightCurlyBrace => TokenOwned::RightCurlyBrace,
|
2024-08-09 00:19:07 +00:00
|
|
|
Token::RightParenthesis => TokenOwned::RightParenthesis,
|
|
|
|
Token::RightSquareBrace => TokenOwned::RightSquareBrace,
|
2024-08-09 15:41:23 +00:00
|
|
|
Token::Semicolon => TokenOwned::Semicolon,
|
2024-08-09 07:00:48 +00:00
|
|
|
Token::Star => TokenOwned::Star,
|
2024-08-09 10:46:24 +00:00
|
|
|
Token::Slash => TokenOwned::Slash,
|
2024-08-09 07:00:48 +00:00
|
|
|
Token::String(text) => TokenOwned::String(text.to_string()),
|
2024-08-10 09:23:43 +00:00
|
|
|
Token::While => TokenOwned::While,
|
2024-08-09 07:00:48 +00:00
|
|
|
Token::WriteLine => TokenOwned::WriteLine,
|
2024-08-09 00:19:07 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-09 05:55:34 +00:00
|
|
|
|
2024-08-09 18:01:01 +00:00
|
|
|
pub fn as_str(&self) -> &str {
|
2024-08-09 05:55:34 +00:00
|
|
|
match self {
|
2024-08-09 18:01:01 +00:00
|
|
|
Token::Boolean(boolean_text) => boolean_text,
|
|
|
|
Token::Identifier(text) => text,
|
|
|
|
Token::Integer(integer_text) => integer_text,
|
2024-08-09 07:00:48 +00:00
|
|
|
Token::Comma => ",",
|
|
|
|
Token::Dot => ".",
|
2024-08-09 08:56:24 +00:00
|
|
|
Token::DoubleAmpersand => "&&",
|
2024-08-09 11:15:09 +00:00
|
|
|
Token::DoubleEqual => "==",
|
2024-08-09 08:56:24 +00:00
|
|
|
Token::DoublePipe => "||",
|
2024-08-09 07:00:48 +00:00
|
|
|
Token::Eof => "EOF",
|
|
|
|
Token::Equal => "=",
|
2024-08-09 05:55:34 +00:00
|
|
|
Token::Float(_) => "float",
|
2024-08-09 07:00:48 +00:00
|
|
|
Token::Greater => ">",
|
|
|
|
Token::GreaterEqual => ">=",
|
2024-08-09 05:55:34 +00:00
|
|
|
Token::IsEven => "is_even",
|
|
|
|
Token::IsOdd => "is_odd",
|
2024-08-09 09:18:39 +00:00
|
|
|
Token::LeftCurlyBrace => "{",
|
2024-08-09 07:00:48 +00:00
|
|
|
Token::LeftParenthesis => "(",
|
|
|
|
Token::LeftSquareBrace => "[",
|
2024-08-09 05:55:34 +00:00
|
|
|
Token::Length => "length",
|
2024-08-09 07:00:48 +00:00
|
|
|
Token::Less => "<",
|
|
|
|
Token::LessEqual => "<=",
|
|
|
|
Token::Minus => "-",
|
2024-08-09 11:02:55 +00:00
|
|
|
Token::Percent => "%",
|
2024-08-09 05:55:34 +00:00
|
|
|
Token::Plus => "+",
|
2024-08-09 22:14:46 +00:00
|
|
|
Token::PlusEqual => "+=",
|
2024-08-09 07:00:48 +00:00
|
|
|
Token::ReadLine => "read_line",
|
2024-08-09 09:18:39 +00:00
|
|
|
Token::RightCurlyBrace => "}",
|
2024-08-09 05:55:34 +00:00
|
|
|
Token::RightParenthesis => ")",
|
|
|
|
Token::RightSquareBrace => "]",
|
2024-08-09 15:41:23 +00:00
|
|
|
Token::Semicolon => ";",
|
2024-08-09 07:00:48 +00:00
|
|
|
Token::Star => "*",
|
|
|
|
Token::String(_) => "string",
|
2024-08-09 10:46:24 +00:00
|
|
|
Token::Slash => "/",
|
2024-08-10 09:23:43 +00:00
|
|
|
Token::While => "while",
|
2024-08-09 07:00:48 +00:00
|
|
|
Token::WriteLine => "write_line",
|
2024-08-09 05:55:34 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-10 04:01:50 +00:00
|
|
|
|
|
|
|
pub fn is_eof(&self) -> bool {
|
|
|
|
matches!(self, Token::Eof)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn precedence(&self) -> u8 {
|
|
|
|
match self {
|
2024-08-10 04:14:38 +00:00
|
|
|
Token::Dot => 12,
|
|
|
|
Token::Star | Token::Slash | Token::Percent => 10,
|
|
|
|
Token::Plus | Token::Minus => 9,
|
|
|
|
Token::DoubleEqual
|
2024-08-10 04:01:50 +00:00
|
|
|
| Token::Less
|
|
|
|
| Token::LessEqual
|
2024-08-10 04:14:38 +00:00
|
|
|
| Token::Greater
|
|
|
|
| Token::GreaterEqual => 8,
|
|
|
|
Token::DoubleAmpersand => 7,
|
|
|
|
Token::DoublePipe => 6,
|
|
|
|
Token::Equal | Token::PlusEqual => 5,
|
|
|
|
Token::Semicolon => 4,
|
2024-08-10 04:01:50 +00:00
|
|
|
_ => 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_left_associative(&self) -> bool {
|
|
|
|
!self.is_right_associative()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_right_associative(&self) -> bool {
|
|
|
|
matches!(self, Token::Semicolon)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_postfix(&self) -> bool {
|
|
|
|
matches!(self, Token::Semicolon)
|
|
|
|
}
|
2024-08-09 00:19:07 +00:00
|
|
|
}
|
|
|
|
|
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-09 18:01:01 +00:00
|
|
|
write!(f, "{}", self.as_str())
|
2024-08-07 14:03:33 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-08 20:19:14 +00:00
|
|
|
|
2024-08-09 04:31:38 +00:00
|
|
|
impl<'src> PartialEq for Token<'src> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
match (self, other) {
|
2024-08-09 07:00:48 +00:00
|
|
|
(Token::Boolean(left), Token::Boolean(right)) => left == right,
|
|
|
|
(Token::Comma, Token::Comma) => true,
|
|
|
|
(Token::Dot, Token::Dot) => true,
|
2024-08-09 11:15:09 +00:00
|
|
|
(Token::DoubleAmpersand, Token::DoubleAmpersand) => true,
|
|
|
|
(Token::DoubleEqual, Token::DoubleEqual) => true,
|
|
|
|
(Token::DoublePipe, Token::DoublePipe) => true,
|
2024-08-09 05:55:34 +00:00
|
|
|
(Token::Eof, Token::Eof) => true,
|
2024-08-09 07:00:48 +00:00
|
|
|
(Token::Equal, Token::Equal) => true,
|
2024-08-09 18:01:01 +00:00
|
|
|
(Token::Float(left), Token::Float(right)) => left == right,
|
2024-08-09 07:00:48 +00:00
|
|
|
(Token::Greater, Token::Greater) => true,
|
|
|
|
(Token::GreaterEqual, Token::GreaterEqual) => true,
|
2024-08-09 05:55:34 +00:00
|
|
|
(Token::Identifier(left), Token::Identifier(right)) => left == right,
|
|
|
|
(Token::Integer(left), Token::Integer(right)) => left == right,
|
|
|
|
(Token::IsEven, Token::IsEven) => true,
|
|
|
|
(Token::IsOdd, Token::IsOdd) => true,
|
2024-08-09 09:18:39 +00:00
|
|
|
(Token::LeftCurlyBrace, Token::LeftCurlyBrace) => true,
|
2024-08-09 07:00:48 +00:00
|
|
|
(Token::LeftParenthesis, Token::LeftParenthesis) => true,
|
|
|
|
(Token::LeftSquareBrace, Token::LeftSquareBrace) => true,
|
2024-08-09 05:55:34 +00:00
|
|
|
(Token::Length, Token::Length) => true,
|
2024-08-09 07:00:48 +00:00
|
|
|
(Token::Less, Token::Less) => true,
|
|
|
|
(Token::LessEqual, Token::LessEqual) => true,
|
|
|
|
(Token::Minus, Token::Minus) => true,
|
2024-08-09 11:02:55 +00:00
|
|
|
(Token::Percent, Token::Percent) => true,
|
2024-08-09 05:55:34 +00:00
|
|
|
(Token::Plus, Token::Plus) => true,
|
2024-08-09 22:14:46 +00:00
|
|
|
(Token::PlusEqual, Token::PlusEqual) => true,
|
2024-08-09 07:00:48 +00:00
|
|
|
(Token::ReadLine, Token::ReadLine) => true,
|
2024-08-09 09:18:39 +00:00
|
|
|
(Token::RightCurlyBrace, Token::RightCurlyBrace) => true,
|
2024-08-09 05:55:34 +00:00
|
|
|
(Token::RightParenthesis, Token::RightParenthesis) => true,
|
|
|
|
(Token::RightSquareBrace, Token::RightSquareBrace) => true,
|
2024-08-09 15:41:23 +00:00
|
|
|
(Token::Semicolon, Token::Semicolon) => true,
|
2024-08-09 07:00:48 +00:00
|
|
|
(Token::Star, Token::Star) => true,
|
2024-08-09 10:46:24 +00:00
|
|
|
(Token::Slash, Token::Slash) => true,
|
2024-08-09 07:00:48 +00:00
|
|
|
(Token::String(left), Token::String(right)) => left == right,
|
2024-08-10 09:23:43 +00:00
|
|
|
(Token::While, Token::While) => true,
|
2024-08-09 07:00:48 +00:00
|
|
|
(Token::WriteLine, Token::WriteLine) => true,
|
2024-08-09 05:55:34 +00:00
|
|
|
_ => false,
|
2024-08-09 04:31:38 +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
|
2024-08-09 18:01:01 +00:00
|
|
|
Boolean(String),
|
|
|
|
Float(String),
|
|
|
|
Integer(String),
|
2024-08-08 20:19:14 +00:00
|
|
|
String(String),
|
|
|
|
|
|
|
|
// Keywords
|
|
|
|
IsEven,
|
|
|
|
IsOdd,
|
|
|
|
Length,
|
|
|
|
ReadLine,
|
2024-08-10 09:23:43 +00:00
|
|
|
While,
|
2024-08-08 20:19:14 +00:00
|
|
|
WriteLine,
|
|
|
|
|
|
|
|
// Symbols
|
|
|
|
Comma,
|
|
|
|
Dot,
|
2024-08-09 08:56:24 +00:00
|
|
|
DoubleAmpersand,
|
2024-08-09 11:15:09 +00:00
|
|
|
DoubleEqual,
|
2024-08-09 08:56:24 +00:00
|
|
|
DoublePipe,
|
2024-08-08 20:19:14 +00:00
|
|
|
Equal,
|
2024-08-09 07:00:48 +00:00
|
|
|
Greater,
|
|
|
|
GreaterOrEqual,
|
2024-08-09 09:18:39 +00:00
|
|
|
LeftCurlyBrace,
|
2024-08-08 20:19:14 +00:00
|
|
|
LeftParenthesis,
|
|
|
|
LeftSquareBrace,
|
2024-08-09 07:00:48 +00:00
|
|
|
Less,
|
|
|
|
LessOrEqual,
|
2024-08-09 03:28:47 +00:00
|
|
|
Minus,
|
2024-08-09 11:02:55 +00:00
|
|
|
Percent,
|
2024-08-08 20:19:14 +00:00
|
|
|
Plus,
|
2024-08-09 22:14:46 +00:00
|
|
|
PlusEqual,
|
2024-08-09 09:18:39 +00:00
|
|
|
RightCurlyBrace,
|
2024-08-08 20:19:14 +00:00
|
|
|
RightParenthesis,
|
|
|
|
RightSquareBrace,
|
2024-08-09 15:41:23 +00:00
|
|
|
Semicolon,
|
2024-08-08 20:19:14 +00:00
|
|
|
Star,
|
2024-08-09 10:46:24 +00:00
|
|
|
Slash,
|
2024-08-08 20:19:14 +00:00
|
|
|
}
|
2024-08-09 00:58:56 +00:00
|
|
|
|
|
|
|
impl Display for TokenOwned {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
TokenOwned::Boolean(boolean) => write!(f, "{boolean}"),
|
2024-08-09 07:00:48 +00:00
|
|
|
TokenOwned::Comma => Token::Comma.fmt(f),
|
|
|
|
TokenOwned::Dot => Token::Dot.fmt(f),
|
2024-08-09 08:56:24 +00:00
|
|
|
TokenOwned::DoubleAmpersand => Token::DoubleAmpersand.fmt(f),
|
2024-08-09 11:15:09 +00:00
|
|
|
TokenOwned::DoubleEqual => Token::DoubleEqual.fmt(f),
|
2024-08-09 08:56:24 +00:00
|
|
|
TokenOwned::DoublePipe => Token::DoublePipe.fmt(f),
|
2024-08-09 07:00:48 +00:00
|
|
|
TokenOwned::Eof => Token::Eof.fmt(f),
|
|
|
|
TokenOwned::Equal => Token::Equal.fmt(f),
|
2024-08-09 00:58:56 +00:00
|
|
|
TokenOwned::Float(float) => write!(f, "{float}"),
|
2024-08-09 07:00:48 +00:00
|
|
|
TokenOwned::Greater => Token::Greater.fmt(f),
|
|
|
|
TokenOwned::GreaterOrEqual => Token::GreaterEqual.fmt(f),
|
|
|
|
TokenOwned::Identifier(text) => write!(f, "{text}"),
|
2024-08-09 00:58:56 +00:00
|
|
|
TokenOwned::Integer(integer) => write!(f, "{integer}"),
|
2024-08-09 05:55:34 +00:00
|
|
|
TokenOwned::IsEven => Token::IsEven.fmt(f),
|
|
|
|
TokenOwned::IsOdd => Token::IsOdd.fmt(f),
|
2024-08-09 09:18:39 +00:00
|
|
|
TokenOwned::LeftCurlyBrace => Token::LeftCurlyBrace.fmt(f),
|
2024-08-09 07:00:48 +00:00
|
|
|
TokenOwned::LeftParenthesis => Token::LeftParenthesis.fmt(f),
|
|
|
|
TokenOwned::LeftSquareBrace => Token::LeftSquareBrace.fmt(f),
|
2024-08-09 05:55:34 +00:00
|
|
|
TokenOwned::Length => Token::Length.fmt(f),
|
2024-08-09 07:00:48 +00:00
|
|
|
TokenOwned::Less => Token::Less.fmt(f),
|
|
|
|
TokenOwned::LessOrEqual => Token::LessEqual.fmt(f),
|
|
|
|
TokenOwned::Minus => Token::Minus.fmt(f),
|
2024-08-09 11:02:55 +00:00
|
|
|
TokenOwned::Percent => Token::Percent.fmt(f),
|
2024-08-09 05:55:34 +00:00
|
|
|
TokenOwned::Plus => Token::Plus.fmt(f),
|
2024-08-09 22:14:46 +00:00
|
|
|
TokenOwned::PlusEqual => Token::PlusEqual.fmt(f),
|
2024-08-09 07:00:48 +00:00
|
|
|
TokenOwned::ReadLine => Token::ReadLine.fmt(f),
|
2024-08-09 09:18:39 +00:00
|
|
|
TokenOwned::RightCurlyBrace => Token::RightCurlyBrace.fmt(f),
|
2024-08-09 05:55:34 +00:00
|
|
|
TokenOwned::RightParenthesis => Token::RightParenthesis.fmt(f),
|
|
|
|
TokenOwned::RightSquareBrace => Token::RightSquareBrace.fmt(f),
|
2024-08-09 15:41:23 +00:00
|
|
|
TokenOwned::Semicolon => Token::Semicolon.fmt(f),
|
2024-08-09 07:00:48 +00:00
|
|
|
TokenOwned::Star => Token::Star.fmt(f),
|
2024-08-09 10:46:24 +00:00
|
|
|
TokenOwned::Slash => Token::Slash.fmt(f),
|
2024-08-09 07:00:48 +00:00
|
|
|
TokenOwned::String(string) => write!(f, "{string}"),
|
2024-08-10 09:23:43 +00:00
|
|
|
TokenOwned::While => Token::While.fmt(f),
|
2024-08-09 07:00:48 +00:00
|
|
|
TokenOwned::WriteLine => Token::WriteLine.fmt(f),
|
2024-08-09 00:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|