| Crates.io | mathengine-lexer |
| lib.rs | mathengine-lexer |
| version | 0.1.0 |
| created_at | 2025-10-06 23:48:40.895497+00 |
| updated_at | 2025-10-06 23:48:40.895497+00 |
| description | High-performance lexical analyzer for mathematical expressions with unit support |
| homepage | https://github.com/javif89/mathengine |
| repository | https://github.com/javif89/mathengine |
| max_upload_size | |
| id | 1871003 |
| size | 10,488 |
A high-performance lexical analyzer for mathematical expressions with unit support.
+, -, *, /, ^ (power)10m, 23.5C)( and )to keyword for conversionsuse mathengine_lexer::{Lexer, Token, Operation};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let lexer = Lexer::new("10m + 2 * (3.5 - 1)");
let tokens = lexer.tokenize()?;
for token in tokens {
println!("{:?}", token);
}
// Output:
// UnitValue { value: 10.0, unit: "m" }
// Operation(Add)
// Number(2.0)
// Operation(Multiply)
// Lparen
// Number(3.5)
// Operation(Subtract)
// Number(1.0)
// Rparen
Ok(())
}
Token::Number(f64) - Numeric literalsToken::UnitValue { value: f64, unit: String } - Numbers with unitsToken::Unit(String) - Standalone unitsToken::Operation(Operation) - Mathematical operatorsToken::Lparen / Token::Rparen - ParenthesesThe lexer provides detailed error information:
use mathengine_lexer::{Lexer, LexError};
let lexer = Lexer::new("10 + @invalid");
match lexer.tokenize() {
Ok(tokens) => println!("Tokens: {:?}", tokens),
Err(LexError::UnexpectedCharacter { char, position }) => {
println!("Unexpected character '{}' at position {}", char, position);
}
Err(e) => println!("Error: {}", e),
}
Part of the mathengine workspace - a modular mathematical expression evaluator. This crate focuses purely on lexical analysis and can be used independently or as part of the complete pipeline.
Licensed under either of Apache License, Version 2.0 or MIT license at your option.