Crates.io | operations_parser |
lib.rs | operations_parser |
version | 0.1.3 |
source | src |
created_at | 2024-11-15 23:50:18.565934 |
updated_at | 2024-11-16 00:00:29.189057 |
description | A Rust-based parser for basic math operations with function support |
homepage | https://github.com/dan-kucherenko/operations_parser_kucherenko |
repository | https://github.com/dan-kucherenko/operations_parser_kucherenko |
max_upload_size | |
id | 1449745 |
size | 25,516 |
Operations Parser is a Rust-based library that parses and evaluates arithmetic expressions.
+
, -
, *
, /
, and ^
sqrt(16)
and sin(pi)
3 + sqrt(16) * (2^3)
This parser uses the Pest grammar and parses expressions in multiple stages:
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
The parser uses a PEG-based grammar defined in grammar.pest
. Below are the rules that govern expression parsing.
sum
.^
.+
and -
).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