math-parser-rs 0.1.0

A simple handwritten dsl for interpreting math.
Documentation
use std::fmt;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TokenType {
    /// A function or variable
    Identifier,
    /// A basic f64 number
    Number,
    /// Operator represents a specific calculation object eg `+`, `-`, `*` or `/`
    Operator,
    /// For multiple function arguments
    Comma,
    /// Open parantheseese
    ParaOpen,
    /// Closing parantheseese
    ParaClose,
    /// Not yet added functionality
    Unimplemented,
}

impl fmt::Display for TokenType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{self:?}")
    }
}

#[derive(Debug, PartialEq)]
pub struct Token {
    pub token_type: TokenType,
    pub text: String,
    pub position: usize,
}

impl fmt::Display for Token {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{{type: {}, text: '{}', pos: {}}}",
            self.token_type, self.text, self.position
        )
    }
}

impl Token {
    pub fn new(token_type: TokenType, text: String, position: usize) -> Self {
        Token {
            token_type,
            text,
            position,
        }
    }
}

#[derive(Debug)]
pub struct LexResult {
    pub rhs: Vec<Token>,
}

impl fmt::Display for LexResult {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "LexResult [")?;
        for token in &self.rhs {
            writeln!(f, "  {token},")?;
        }
        write!(f, "]")
    }
}