use std::fmt::{Debug, Display, Formatter};
use Token::*;
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum Token<N> {
Number(N),
Variable(String),
Constant(String),
Function(String),
BinaryOperator(String),
UnaryOperator(String),
ArgCount(usize),
GroupingOpen(char),
GroupingClose(char),
Unknown(String),
Comma,
}
impl<N: Display> Display for Token<N> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Number(n) => write!(f, "Number({})", n),
Variable(name) => write!(f, "Variable({})", name),
Constant(name) => write!(f, "Constant({})", name),
Function(name) => write!(f, "Function({})", name),
BinaryOperator(name) => write!(f, "BinaryOperator('{}')", name),
UnaryOperator(name) => write!(f, "UnaryOperator('{}')", name),
ArgCount(n) => write!(f, "ArgCount({})", n),
GroupingOpen(c) => write!(f, "ParenthesisOpen('{}')", c),
GroupingClose(c) => write!(f, "ParenthesisClose('{}')", c),
Unknown(name) => write!(f, "Unknown({})", name),
Comma => write!(f, "Comma"),
}
}
}
impl<N: Debug> Debug for Token<N> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Number(n) => write!(f, "Number({:?})", n),
Variable(name) => write!(f, "Variable({:?})", name),
Constant(name) => write!(f, "Constant({:?})", name),
Function(name) => write!(f, "Function({:?})", name),
BinaryOperator(name) => write!(f, "BinaryOperator('{:?}')", name),
UnaryOperator(name) => write!(f, "UnaryOperator('{:?}')", name),
ArgCount(n) => write!(f, "ArgCount({:?})", n),
GroupingOpen(c) => write!(f, "ParenthesisOpen('{:?}')", c),
GroupingClose(c) => write!(f, "ParenthesisClose('{:?}')", c),
Unknown(name) => write!(f, "Unknown({:?})", name),
Comma => write!(f, "Comma"),
}
}
}
impl<N> Token<N> {
#[inline]
pub fn is_number(&self) -> bool {
matches!(self, Token::Number(_))
}
#[inline]
pub fn is_variable(&self) -> bool {
matches!(self, Token::Variable(_))
}
#[inline]
pub fn is_constant(&self) -> bool {
matches!(self, Token::Constant(_))
}
#[inline]
pub fn is_function(&self) -> bool {
matches!(self, Token::Function(_))
}
#[inline]
pub fn is_unary_operator(&self) -> bool {
matches!(self, Token::UnaryOperator(_))
}
#[inline]
pub fn is_binary_operator(&self) -> bool {
matches!(self, Token::BinaryOperator(_))
}
#[inline]
pub fn is_arg_count(&self) -> bool {
matches!(self, Token::ArgCount(_))
}
#[inline]
pub fn is_grouping_open(&self) -> bool {
matches!(self, Token::GroupingOpen(_))
}
#[inline]
pub fn is_grouping_close(&self) -> bool {
matches!(self, Token::GroupingClose(_))
}
#[inline]
pub fn is_comma(&self) -> bool {
matches!(self, Token::Comma)
}
#[inline]
pub fn is_unknown(&self) -> bool {
matches!(self, Token::Unknown(_))
}
#[inline]
pub fn contains_symbol(&self, name: char) -> bool {
match self {
Token::GroupingClose(c) | Token::GroupingOpen(c) => *c == name,
_ => false,
}
}
}