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

622 lines
20 KiB
Rust
Raw Normal View History

2024-08-09 01:59:09 +00:00
//! Token and TokenOwned types.
use std::fmt::{self, Display, Formatter};
use serde::{Deserialize, Serialize};
2024-08-09 00:19:07 +00:00
/// Source code token.
2024-08-13 17:28:22 +00:00
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub enum Token<'src> {
2024-08-09 22:14:46 +00:00
// End of file
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),
String(&'src str),
// Keywords
2024-08-14 03:45:17 +00:00
Async,
Bool,
Else,
FloatKeyword,
If,
Int,
IsEven,
IsOdd,
Length,
Let,
2024-08-14 05:13:43 +00:00
Mut,
ReadLine,
Str,
Struct,
ToString,
2024-08-10 09:23:43 +00:00
While,
WriteLine,
// Symbols
2024-08-15 01:15:37 +00:00
BangEqual,
2024-08-12 09:44:05 +00:00
Bang,
Colon,
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-12 14:08:34 +00:00
DoubleDot,
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,
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,
Less,
LessEqual,
Minus,
MinusEqual,
2024-08-09 11:02:55 +00:00
Percent,
Plus,
2024-08-09 22:14:46 +00:00
PlusEqual,
2024-08-09 09:18:39 +00:00
RightCurlyBrace,
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,
Star,
}
2024-08-09 00:19:07 +00:00
impl<'src> Token<'src> {
pub fn to_owned(&self) -> TokenOwned {
match self {
2024-08-14 03:45:17 +00:00
Token::Async => TokenOwned::Async,
2024-08-15 01:15:37 +00:00
Token::BangEqual => TokenOwned::BangEqual,
2024-08-12 09:44:05 +00:00
Token::Bang => TokenOwned::Bang,
Token::Bool => TokenOwned::Bool,
2024-08-09 18:01:01 +00:00
Token::Boolean(boolean) => TokenOwned::Boolean(boolean.to_string()),
Token::Colon => TokenOwned::Colon,
Token::Comma => TokenOwned::Comma,
Token::Dot => TokenOwned::Dot,
2024-08-09 08:56:24 +00:00
Token::DoubleAmpersand => TokenOwned::DoubleAmpersand,
2024-08-12 14:08:34 +00:00
Token::DoubleDot => TokenOwned::DoubleDot,
2024-08-09 11:15:09 +00:00
Token::DoubleEqual => TokenOwned::DoubleEqual,
2024-08-09 08:56:24 +00:00
Token::DoublePipe => TokenOwned::DoublePipe,
Token::Else => TokenOwned::Else,
Token::Eof => TokenOwned::Eof,
Token::Equal => TokenOwned::Equal,
2024-08-09 18:01:01 +00:00
Token::Float(float) => TokenOwned::Float(float.to_string()),
Token::FloatKeyword => TokenOwned::FloatKeyword,
Token::Greater => TokenOwned::Greater,
Token::GreaterEqual => TokenOwned::GreaterOrEqual,
Token::Identifier(text) => TokenOwned::Identifier(text.to_string()),
Token::If => TokenOwned::If,
Token::Int => TokenOwned::Int,
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,
Token::LeftParenthesis => TokenOwned::LeftParenthesis,
Token::LeftSquareBrace => TokenOwned::LeftSquareBrace,
2024-08-09 00:19:07 +00:00
Token::Length => TokenOwned::Length,
Token::Let => TokenOwned::Let,
Token::Less => TokenOwned::Less,
Token::LessEqual => TokenOwned::LessOrEqual,
Token::Minus => TokenOwned::Minus,
Token::MinusEqual => TokenOwned::MinusEqual,
2024-08-14 05:13:43 +00:00
Token::Mut => TokenOwned::Mut,
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,
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,
Token::Star => TokenOwned::Star,
2024-08-09 10:46:24 +00:00
Token::Slash => TokenOwned::Slash,
Token::String(text) => TokenOwned::String(text.to_string()),
Token::Str => TokenOwned::Str,
Token::Struct => TokenOwned::Struct,
Token::ToString => TokenOwned::ToString,
2024-08-10 09:23:43 +00:00
Token::While => TokenOwned::While,
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::Float(float_text) => float_text,
2024-08-09 18:01:01 +00:00
Token::Identifier(text) => text,
Token::Integer(integer_text) => integer_text,
Token::String(text) => text,
2024-08-14 03:45:17 +00:00
Token::Async => "async",
2024-08-15 01:15:37 +00:00
Token::BangEqual => "!=",
2024-08-12 09:44:05 +00:00
Token::Bang => "!",
Token::Bool => "bool",
Token::Colon => ":",
Token::Comma => ",",
Token::Dot => ".",
2024-08-09 08:56:24 +00:00
Token::DoubleAmpersand => "&&",
2024-08-12 14:08:34 +00:00
Token::DoubleDot => "..",
2024-08-09 11:15:09 +00:00
Token::DoubleEqual => "==",
2024-08-09 08:56:24 +00:00
Token::DoublePipe => "||",
Token::Else => "else",
Token::Eof => "EOF",
Token::Equal => "=",
Token::FloatKeyword => "float",
Token::Greater => ">",
Token::GreaterEqual => ">=",
Token::If => "if",
Token::Int => "int",
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 => "{",
Token::LeftParenthesis => "(",
Token::LeftSquareBrace => "[",
Token::Let => "let",
2024-08-09 05:55:34 +00:00
Token::Length => "length",
Token::Less => "<",
Token::LessEqual => "<=",
Token::Minus => "-",
Token::MinusEqual => "-=",
2024-08-14 05:13:43 +00:00
Token::Mut => "mut",
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 => "+=",
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 => ";",
Token::Star => "*",
2024-08-09 10:46:24 +00:00
Token::Slash => "/",
Token::Str => "str",
Token::Struct => "struct",
Token::ToString => "to_string",
2024-08-10 09:23:43 +00:00
Token::While => "while",
Token::WriteLine => "write_line",
2024-08-09 05:55:34 +00:00
}
}
2024-08-12 14:08:34 +00:00
pub fn kind(&self) -> TokenKind {
match self {
2024-08-14 03:45:17 +00:00
Token::Async => TokenKind::Async,
2024-08-15 01:15:37 +00:00
Token::BangEqual => TokenKind::BangEqual,
2024-08-12 14:08:34 +00:00
Token::Bang => TokenKind::Bang,
Token::Bool => TokenKind::Bool,
2024-08-12 14:08:34 +00:00
Token::Boolean(_) => TokenKind::Boolean,
Token::Colon => TokenKind::Colon,
2024-08-12 14:08:34 +00:00
Token::Comma => TokenKind::Comma,
Token::Dot => TokenKind::Dot,
Token::DoubleAmpersand => TokenKind::DoubleAmpersand,
Token::DoubleDot => TokenKind::DoubleDot,
Token::DoubleEqual => TokenKind::DoubleEqual,
Token::DoublePipe => TokenKind::DoublePipe,
Token::Else => TokenKind::Else,
Token::Eof => TokenKind::Eof,
Token::Equal => TokenKind::Equal,
Token::Float(_) => TokenKind::Float,
Token::FloatKeyword => TokenKind::FloatKeyword,
2024-08-12 14:08:34 +00:00
Token::Greater => TokenKind::Greater,
Token::GreaterEqual => TokenKind::GreaterOrEqual,
Token::Identifier(_) => TokenKind::Identifier,
Token::If => TokenKind::If,
Token::Int => TokenKind::Int,
2024-08-12 14:08:34 +00:00
Token::Integer(_) => TokenKind::Integer,
Token::IsEven => TokenKind::IsEven,
Token::IsOdd => TokenKind::IsOdd,
Token::LeftCurlyBrace => TokenKind::LeftCurlyBrace,
Token::LeftParenthesis => TokenKind::LeftParenthesis,
Token::LeftSquareBrace => TokenKind::LeftSquareBrace,
Token::Let => TokenKind::Let,
2024-08-12 14:08:34 +00:00
Token::Length => TokenKind::Length,
Token::Less => TokenKind::Less,
Token::LessEqual => TokenKind::LessOrEqual,
Token::Minus => TokenKind::Minus,
Token::MinusEqual => TokenKind::MinusEqual,
2024-08-14 05:13:43 +00:00
Token::Mut => TokenKind::Mut,
2024-08-12 14:08:34 +00:00
Token::Percent => TokenKind::Percent,
Token::Plus => TokenKind::Plus,
Token::PlusEqual => TokenKind::PlusEqual,
Token::ReadLine => TokenKind::ReadLine,
Token::RightCurlyBrace => TokenKind::RightCurlyBrace,
Token::RightParenthesis => TokenKind::RightParenthesis,
Token::RightSquareBrace => TokenKind::RightSquareBrace,
Token::Semicolon => TokenKind::Semicolon,
Token::Star => TokenKind::Star,
Token::Slash => TokenKind::Slash,
Token::Str => TokenKind::Str,
2024-08-12 14:08:34 +00:00
Token::String(_) => TokenKind::String,
Token::Struct => TokenKind::Struct,
2024-08-12 14:08:34 +00:00
Token::ToString => TokenKind::ToString,
Token::While => TokenKind::While,
Token::WriteLine => TokenKind::WriteLine,
}
}
pub fn is_eof(&self) -> bool {
matches!(self, Token::Eof)
}
pub fn precedence(&self) -> u8 {
match self {
2024-08-15 04:20:36 +00:00
Token::Dot => 9,
2024-08-16 01:22:24 +00:00
Token::LeftParenthesis | Token::LeftSquareBrace => 8,
2024-08-15 04:20:36 +00:00
Token::Star | Token::Slash | Token::Percent => 7,
2024-08-12 23:06:57 +00:00
Token::Minus | Token::Plus => 6,
2024-08-10 04:14:38 +00:00
Token::DoubleEqual
| Token::Less
| Token::LessEqual
2024-08-10 04:14:38 +00:00
| Token::Greater
2024-08-12 09:44:05 +00:00
| Token::GreaterEqual => 5,
Token::DoubleAmpersand => 4,
Token::DoublePipe => 3,
2024-08-15 04:20:36 +00:00
Token::DoubleDot => 2,
Token::Equal | Token::MinusEqual | Token::PlusEqual => 1,
_ => 0,
}
}
pub fn is_left_associative(&self) -> bool {
2024-08-12 20:57:10 +00:00
matches!(
self,
2024-08-15 04:20:36 +00:00
Token::Dot
| Token::DoubleAmpersand
2024-08-12 20:57:10 +00:00
| Token::DoublePipe
| Token::Plus
| Token::Minus
| Token::Star
| Token::Slash
| Token::Percent
)
}
pub fn is_right_associative(&self) -> bool {
2024-08-15 04:20:36 +00:00
matches!(self, Token::Equal | Token::MinusEqual | Token::PlusEqual)
2024-08-12 09:44:05 +00:00
}
pub fn is_prefix(&self) -> bool {
matches!(self, Token::Bang | Token::Minus)
}
pub fn is_postfix(&self) -> bool {
2024-08-15 04:20:36 +00:00
matches!(
self,
Token::Dot | Token::LeftCurlyBrace | Token::LeftParenthesis | Token::LeftSquareBrace
)
}
2024-08-09 00:19:07 +00:00
}
impl<'src> Display for Token<'src> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
2024-08-09 18:01:01 +00:00
write!(f, "{}", self.as_str())
}
}
2024-08-09 00:19:07 +00:00
/// Owned version of `Token`, which owns all the strings.
///
/// This is used for errors.
#[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),
String(String),
// Keywords
Bool,
Else,
FloatKeyword,
If,
Int,
IsEven,
IsOdd,
Let,
Length,
2024-08-14 05:13:43 +00:00
Mut,
ReadLine,
Str,
ToString,
2024-08-10 09:23:43 +00:00
While,
WriteLine,
// Symbols
2024-08-14 03:45:17 +00:00
Async,
2024-08-12 09:44:05 +00:00
Bang,
2024-08-15 01:15:37 +00:00
BangEqual,
Colon,
Comma,
Dot,
2024-08-09 08:56:24 +00:00
DoubleAmpersand,
2024-08-12 14:08:34 +00:00
DoubleDot,
2024-08-09 11:15:09 +00:00
DoubleEqual,
2024-08-09 08:56:24 +00:00
DoublePipe,
Equal,
Greater,
GreaterOrEqual,
2024-08-09 09:18:39 +00:00
LeftCurlyBrace,
LeftParenthesis,
LeftSquareBrace,
Less,
LessOrEqual,
Minus,
MinusEqual,
2024-08-09 11:02:55 +00:00
Percent,
Plus,
2024-08-09 22:14:46 +00:00
PlusEqual,
2024-08-09 09:18:39 +00:00
RightCurlyBrace,
RightParenthesis,
RightSquareBrace,
2024-08-09 15:41:23 +00:00
Semicolon,
Star,
Struct,
2024-08-09 10:46:24 +00:00
Slash,
}
2024-08-09 00:58:56 +00:00
impl Display for TokenOwned {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
2024-08-14 03:45:17 +00:00
TokenOwned::Async => Token::Async.fmt(f),
2024-08-12 09:44:05 +00:00
TokenOwned::Bang => Token::Bang.fmt(f),
2024-08-15 01:15:37 +00:00
TokenOwned::BangEqual => Token::BangEqual.fmt(f),
TokenOwned::Bool => Token::Bool.fmt(f),
2024-08-09 00:58:56 +00:00
TokenOwned::Boolean(boolean) => write!(f, "{boolean}"),
TokenOwned::Colon => Token::Colon.fmt(f),
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-12 14:08:34 +00:00
TokenOwned::DoubleDot => Token::DoubleDot.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),
TokenOwned::Else => Token::Else.fmt(f),
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}"),
TokenOwned::FloatKeyword => Token::FloatKeyword.fmt(f),
TokenOwned::Greater => Token::Greater.fmt(f),
TokenOwned::GreaterOrEqual => Token::GreaterEqual.fmt(f),
TokenOwned::Identifier(text) => write!(f, "{text}"),
TokenOwned::If => Token::If.fmt(f),
TokenOwned::Int => Token::Int.fmt(f),
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),
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),
TokenOwned::Let => Token::Let.fmt(f),
TokenOwned::Less => Token::Less.fmt(f),
TokenOwned::LessOrEqual => Token::LessEqual.fmt(f),
TokenOwned::Minus => Token::Minus.fmt(f),
TokenOwned::MinusEqual => Token::MinusEqual.fmt(f),
2024-08-14 05:13:43 +00:00
TokenOwned::Mut => Token::Mut.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),
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),
TokenOwned::Star => Token::Star.fmt(f),
2024-08-09 10:46:24 +00:00
TokenOwned::Slash => Token::Slash.fmt(f),
TokenOwned::Str => Token::Str.fmt(f),
TokenOwned::String(string) => write!(f, "{string}"),
TokenOwned::Struct => Token::Struct.fmt(f),
TokenOwned::ToString => Token::ToString.fmt(f),
2024-08-10 09:23:43 +00:00
TokenOwned::While => Token::While.fmt(f),
TokenOwned::WriteLine => Token::WriteLine.fmt(f),
2024-08-09 00:58:56 +00:00
}
}
}
2024-08-12 14:08:34 +00:00
/// Token representation that holds no data.
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub enum TokenKind {
Eof,
Identifier,
// Hard-coded values
Boolean,
Float,
Integer,
String,
// Keywords
2024-08-14 03:45:17 +00:00
Async,
Bool,
2024-08-12 14:08:34 +00:00
Else,
FloatKeyword,
2024-08-12 14:08:34 +00:00
If,
Int,
2024-08-12 14:08:34 +00:00
IsEven,
IsOdd,
Length,
Let,
2024-08-12 14:08:34 +00:00
ReadLine,
Str,
2024-08-12 14:08:34 +00:00
ToString,
While,
WriteLine,
// Symbols
2024-08-15 01:15:37 +00:00
BangEqual,
2024-08-12 14:08:34 +00:00
Bang,
Colon,
2024-08-12 14:08:34 +00:00
Comma,
Dot,
DoubleAmpersand,
DoubleDot,
DoubleEqual,
DoublePipe,
Equal,
Greater,
GreaterOrEqual,
LeftCurlyBrace,
LeftParenthesis,
LeftSquareBrace,
Less,
LessOrEqual,
Minus,
MinusEqual,
2024-08-14 05:13:43 +00:00
Mut,
2024-08-12 14:08:34 +00:00
Percent,
Plus,
PlusEqual,
RightCurlyBrace,
RightParenthesis,
RightSquareBrace,
Semicolon,
Star,
Struct,
2024-08-12 14:08:34 +00:00
Slash,
}
impl Display for TokenKind {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
2024-08-14 03:45:17 +00:00
TokenKind::Async => Token::Async.fmt(f),
2024-08-12 14:08:34 +00:00
TokenKind::Bang => Token::Bang.fmt(f),
2024-08-15 01:15:37 +00:00
TokenKind::BangEqual => Token::BangEqual.fmt(f),
TokenKind::Bool => Token::Bool.fmt(f),
TokenKind::Boolean => write!(f, "boolean value"),
TokenKind::Colon => Token::Colon.fmt(f),
2024-08-12 14:08:34 +00:00
TokenKind::Comma => Token::Comma.fmt(f),
TokenKind::Dot => Token::Dot.fmt(f),
TokenKind::DoubleAmpersand => Token::DoubleAmpersand.fmt(f),
TokenKind::DoubleDot => Token::DoubleDot.fmt(f),
TokenKind::DoubleEqual => Token::DoubleEqual.fmt(f),
TokenKind::DoublePipe => Token::DoublePipe.fmt(f),
TokenKind::Else => Token::Else.fmt(f),
TokenKind::Eof => Token::Eof.fmt(f),
TokenKind::Equal => Token::Equal.fmt(f),
TokenKind::Float => write!(f, "float value"),
TokenKind::FloatKeyword => Token::FloatKeyword.fmt(f),
2024-08-12 14:08:34 +00:00
TokenKind::Greater => Token::Greater.fmt(f),
TokenKind::GreaterOrEqual => Token::GreaterEqual.fmt(f),
TokenKind::Identifier => write!(f, "identifier"),
TokenKind::If => Token::If.fmt(f),
TokenKind::Int => Token::Int.fmt(f),
TokenKind::Integer => write!(f, "integer value"),
2024-08-12 14:08:34 +00:00
TokenKind::IsEven => Token::IsEven.fmt(f),
TokenKind::IsOdd => Token::IsOdd.fmt(f),
TokenKind::LeftCurlyBrace => Token::LeftCurlyBrace.fmt(f),
TokenKind::LeftParenthesis => Token::LeftParenthesis.fmt(f),
TokenKind::LeftSquareBrace => Token::LeftSquareBrace.fmt(f),
TokenKind::Length => Token::Length.fmt(f),
TokenKind::Let => Token::Let.fmt(f),
2024-08-12 14:08:34 +00:00
TokenKind::Less => Token::Less.fmt(f),
TokenKind::LessOrEqual => Token::LessEqual.fmt(f),
TokenKind::Minus => Token::Minus.fmt(f),
TokenKind::MinusEqual => Token::MinusEqual.fmt(f),
2024-08-14 05:13:43 +00:00
TokenKind::Mut => Token::Mut.fmt(f),
2024-08-12 14:08:34 +00:00
TokenKind::Percent => Token::Percent.fmt(f),
TokenKind::Plus => Token::Plus.fmt(f),
TokenKind::PlusEqual => Token::PlusEqual.fmt(f),
TokenKind::ReadLine => Token::ReadLine.fmt(f),
TokenKind::RightCurlyBrace => Token::RightCurlyBrace.fmt(f),
TokenKind::RightParenthesis => Token::RightParenthesis.fmt(f),
TokenKind::RightSquareBrace => Token::RightSquareBrace.fmt(f),
TokenKind::Semicolon => Token::Semicolon.fmt(f),
TokenKind::Star => Token::Star.fmt(f),
TokenKind::Str => write!(f, "str"),
2024-08-12 14:08:34 +00:00
TokenKind::Slash => Token::Slash.fmt(f),
TokenKind::String => write!(f, "string value"),
TokenKind::Struct => Token::Struct.fmt(f),
2024-08-12 14:08:34 +00:00
TokenKind::ToString => Token::ToString.fmt(f),
TokenKind::While => Token::While.fmt(f),
TokenKind::WriteLine => Token::WriteLine.fmt(f),
}
}
}
#[cfg(test)]
2024-08-14 01:24:56 +00:00
pub(crate) mod tests {
use super::*;
pub fn all_tokens<'src>() -> [Token<'src>; 51] {
[
Token::Eof,
Token::Identifier("identifier"),
Token::Boolean("true"),
Token::Float("1.0"),
Token::Integer("1"),
Token::String("string"),
2024-08-14 03:45:17 +00:00
Token::Async,
2024-08-14 01:24:56 +00:00
Token::Bool,
Token::Else,
Token::FloatKeyword,
Token::If,
Token::Int,
Token::IsEven,
Token::IsOdd,
Token::Length,
Token::Let,
Token::ReadLine,
Token::Str,
Token::ToString,
Token::While,
Token::WriteLine,
Token::BangEqual,
Token::Bang,
Token::Colon,
Token::Comma,
Token::Dot,
Token::DoubleAmpersand,
Token::DoubleDot,
Token::DoubleEqual,
Token::DoublePipe,
Token::Equal,
Token::Greater,
Token::GreaterEqual,
Token::LeftCurlyBrace,
Token::LeftParenthesis,
Token::LeftSquareBrace,
Token::Less,
Token::LessEqual,
Token::Minus,
2024-08-14 01:24:56 +00:00
Token::MinusEqual,
2024-08-14 05:13:43 +00:00
Token::Mut,
Token::Percent,
Token::Plus,
Token::PlusEqual,
Token::RightCurlyBrace,
Token::RightParenthesis,
Token::RightSquareBrace,
Token::Semicolon,
Token::Star,
Token::Struct,
Token::Slash,
]
}
#[test]
fn token_displays() {
for token in all_tokens().iter() {
let display = token.to_string();
assert_eq!(display, token.to_owned().to_string());
if let Token::Boolean(_)
| Token::Float(_)
| Token::Identifier(_)
| Token::Integer(_)
| Token::String(_) = token
{
continue;
} else {
assert_eq!(display, token.kind().to_string());
}
}
}
}