2 releases
0.1.3 | Nov 16, 2024 |
---|---|
0.1.2 | Nov 15, 2024 |
#499 in Math
233 downloads per month
14KB
89 lines
operations_parser
Links
Operations Parser is a Rust-based library that parses and evaluates arithmetic expressions.
Features
- Supports basic arithmetic operators:
+
,-
,*
,/
, and^
- Handles function calls like
sqrt(16)
andsin(pi)
- Can parse complex, nested expressions like
3 + sqrt(16) * (2^3)
Technical Overview
This parser uses the Pest grammar and parses expressions in multiple stages:
- Expression: The top-level rule.
- Sum: Handles addition and subtraction.
- Product: Handles multiplication and division.
- Power: Handles exponentiation.
- Function Call: Parses functions with expressions as arguments.
Usage
To use the parser, run the following command in the terminal:
cargo run -- parse <file>
To get available commands, just type in the commang:
cargo run -- help
For parsing an expression right from the command run:
cargo run -- "3 + sqrt(16)"
To get credits use:
cargo run -- credits
Grammar Structure
The parser uses a PEG-based grammar defined in grammar.pest
. Below are the rules that govern expression parsing.
- expression: Parses the entire arithmetic expression, starting with
sum
. - sum: Handles addition and subtraction.
- product: Handles multiplication and division.
- power: Handles exponentiation with
^
. - unary: Handles unary operators (
+
and-
). - primary: Recognizes numbers, function calls, and grouped expressions (parentheses).
- function_call: Matches function names followed by an argument in parentheses.
- ident: Matches alphabetic characters used for function names or constants.
Full Grammar
WHITESPACE = _{ " " | "\t" }
expression = _{ sum }
sum = { product ~ (add_op ~ product)* }
product = { power ~ (mul_op ~ power)* }
power = { unary ~ (exp_op ~ unary)* }
unary = { unary_op? ~ primary }
primary = { number | function_call | "(" ~ expression ~ ")" }
number = @{ "-"? ~ ASCII_DIGIT+ ~ ("." ~ ASCII_DIGIT+)? }
add_op = { "+" | "-" }
mul_op = { "*" | "/" }
exp_op = { "^" }
unary_op = { "+" | "-" }
function_call = { ident ~ "(" ~ expression ~ ")" }
ident = @{ ASCII_ALPHA+ }
yaml
Copy code
Dependencies
~2.2–3MB
~58K SLoC