mathengine-parser
A robust parser for mathematical expressions with unit support, built using Pratt parsing for correct operator precedence.
Features
- Pratt Parsing: Handles operator precedence and associativity correctly
- AST Generation: Produces a clean Abstract Syntax Tree
- Unit Support: Parses unit values and conversion expressions
- Error Recovery: Detailed parse error messages with position information
- Type System: Rich type definitions for numbers, units, and values
Usage
use Lexer;
use ;
AST Structure
The parser generates a rich AST with these expression types:
Operator Precedence
The parser correctly handles mathematical precedence:
- Parentheses:
() - Power:
^(right-associative) - Multiplication/Division:
*/ - Addition/Subtraction:
+- - Unit Conversion:
to(highest precedence)
Type System
Includes a comprehensive type system for mathematical values:
use ;
// Pure numbers
let num = from;
// Values with units
let distance = new;
// Unified value type
let result: Value = Number;
Error Handling
Comprehensive error reporting with position information:
use ;
match parser.parse
Integration
Designed to work seamlessly with other mathengine crates:
use Lexer;
use Parser;
use evaluate;
Architecture
Part of the mathengine workspace. This crate bridges the gap between lexical analysis and evaluation, producing a clean AST that can be traversed by evaluators, optimizers, or other analysis tools.
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.