use crate::error::ExcelError;
use alloc::format;
use alloc::string::{String, ToString};
use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum TokenKind<'a> {
Number(f64),
Str(&'a str),
Ident(&'a str),
Cell {
col: u32,
row: u32,
col_abs: bool,
row_abs: bool,
},
ErrLit(ExcelError),
LParen,
RParen,
Comma,
Colon,
Plus,
Minus,
Star,
Slash,
Caret,
Amp,
Percent,
Eq,
Ne,
Lt,
Gt,
Le,
Ge,
Eof,
}
impl TokenKind<'_> {
#[must_use]
pub fn describe(&self) -> String {
match *self {
Self::Number(n) => {
if n.is_finite() {
format!("{n}")
} else {
"#NUM! (numeric overflow)".to_string()
}
}
Self::Str(s) => format!("string \"{}\"", s.replace('"', "\"\"")),
Self::Ident(s) => s.to_string(),
Self::Cell { col, row, .. } => crate::ast::CellRef {
col,
row,
col_abs: false,
row_abs: false,
}
.to_string(),
Self::ErrLit(e) => e.literal().to_string(),
Self::LParen => "(".to_string(),
Self::RParen => ")".to_string(),
Self::Comma => ",".to_string(),
Self::Colon => ":".to_string(),
Self::Plus => "+".to_string(),
Self::Minus => "-".to_string(),
Self::Star => "*".to_string(),
Self::Slash => "/".to_string(),
Self::Caret => "^".to_string(),
Self::Amp => "&".to_string(),
Self::Percent => "%".to_string(),
Self::Eq => "=".to_string(),
Self::Ne => "<>".to_string(),
Self::Lt => "<".to_string(),
Self::Gt => ">".to_string(),
Self::Le => "<=".to_string(),
Self::Ge => ">=".to_string(),
Self::Eof => "end of formula".to_string(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Token<'a> {
pub kind: TokenKind<'a>,
pub pos: usize,
}
impl fmt::Display for Token<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.kind.describe())
}
}