Crates.io | pest_extra |
lib.rs | pest_extra |
version | 0.1.0 |
source | src |
created_at | 2023-11-26 08:32:40.106161 |
updated_at | 2023-11-26 08:32:40.106161 |
description | Provides additional functionality for the Pest parser |
homepage | https://github.com/zalupa35/pest_extra |
repository | https://github.com/zalupa35/pest_extra |
max_upload_size | |
id | 1048867 |
size | 28,106 |
pest_extra provides additional functionality for the Pest parser
See below to find how to compile this grammar into Pest grammar.
A variable is a string that you can use in rules:
variable = "value" // easier than variable = { "value" }
Idea: https://github.com/pest-parser/pest/pull/759
Includes all the rules from one grammar to another.
include!("grammar2.pest")
integer = @{ ASCII_DIGIT+ }
primary = _{ integer | "(" ~ expr ~ ")" }
atom = _{ unary_minus? ~ primary }
expr = { atom ~ (bin_op ~ atom)* }
equation = _{ SOI ~ expr ~ EOI }
WHITESPACE = _{ " " }
unary_minus = { "-" }
bin_op = _{ add | subtract | multiply | divide | modulo }
add = { "+" }
subtract = { "-" }
multiply = { "*" }
divide = { "/" }
modulo = { "%" }
unary_minus = { "-" }
divide = { "/" }
modulo = { "%" }
WHITESPACE = _{ " " }
equation = _{ SOI ~ expr ~ EOI }
bin_op = _{ add | subtract | multiply | divide | modulo }
atom = _{ unary_minus? ~ primary }
integer = @{ ASCII_DIGIT+ }
primary = _{ integer | "(" ~ expr ~ ")" }
add = { "+" }
expr = { atom ~ (bin_op ~ atom)* }
multiply = { "*" }
subtract = { "-" }
[dependencies]
pest_extra = { version = "0.1.0", features = [ "formatter" ] }
Uses pest-fmt.
Usage:
use pest_extra::formatter;
fn main() {
println!("{}", formatter::format(include_str!("./grammar.pest").to_string()));
}
Minifies Pest grammar.
Usage:
use pest_extra::formatter;
fn main() {
println!("{}", formatter::minify(include_str!("./grammar.pest").to_string()));
}
Comparsion:
Size: 556 bytes
// No whitespace allowed between digits
integer = @{ ASCII_DIGIT+ }
unary_minus = { "-" }
primary = _{ integer | "(" ~ expr ~ ")" }
atom = _{ unary_minus? ~ primary }
bin_op = _{ add | subtract | multiply | divide | modulo }
add = { "+" }
subtract = { "-" }
multiply = { "*" }
divide = { "/" }
modulo = { "%" }
expr = { atom ~ (bin_op ~ atom)* }
// We can't have SOI and EOI on expr directly, because it is used recursively (e.g. with parentheses)
equation = _{ SOI ~ expr ~ EOI }
WHITESPACE = _{ " " }
Size: 272 bytes (saved 48.92%)
integer=@{ASCII_DIGIT+}unary_minus={"-"}primary=_{integer|"("~expr~")"}atom=_{unary_minus?~primary}bin_op=_{add|subtract|multiply|divide|modulo}add={"+"}subtract={"-"}multiply={"*"}divide={"/"}modulo={"%"}expr={atom~(bin_op~atom)*}equation=_{SOI~expr~EOI}WHITESPACE=_{" "}
[dependencies]
pest_extra = "0.1.0"
use pest_extra::{
formatter,
parser::{compile_grammar, types::Grammar},
};
fn main() {
// Compiling grammar
if let Ok(grammar) = compile_grammar(Grammar::from_file("./grammar.pest").unwrap()) {
// Formatting grammar
println!("{}", formatter::format(grammar).unwrap());
}
}
pest_extra has built-in pest_vm to create a parser from your grammar.
[dependencies]
pest_extra = { version = "0.1.0", features = [ "vm" ] }
use pest_extra::{
parser::{compile_grammar, types::Grammar},
vm
};
fn main() {
// Compiling grammar
if let Ok(grammar) = compile_grammar(Grammar::from_file("./grammar.pest").unwrap()) {
// Creating parser
let vm = vm::PestVm::new(&grammar).vm;
let result = vm.parse("rule as &str", "input as &str");
}
}
MIT