1 个不稳定版本
0.1.0 | 2023年11月26日 |
---|
#13 in #pest-grammar
27KB
448 行
pest_extra
pest_extra 为 Pest 解析器提供附加功能
功能
语法
请见下文了解如何将此语法编译成Pest语法。
变量
变量是您可以在规则中使用的一个字符串
variable = "value" // easier than variable = { "value" }
在语法中包含其他语法
想法: https://github.com/pest-parser/pest/pull/759
包括从一个语法到另一个语法的所有规则。
示例
calculator.pest
include!("grammar2.pest")
integer = @{ ASCII_DIGIT+ }
primary = _{ integer | "(" ~ expr ~ ")" }
atom = _{ unary_minus? ~ primary }
expr = { atom ~ (bin_op ~ atom)* }
equation = _{ SOI ~ expr ~ EOI }
grammar2.pest
WHITESPACE = _{ " " }
unary_minus = { "-" }
bin_op = _{ add | subtract | multiply | divide | modulo }
add = { "+" }
subtract = { "-" }
multiply = { "*" }
divide = { "/" }
modulo = { "%" }
compiled.pest
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" ] }
格式化
使用 pest-fmt。
使用方法
use pest_extra::formatter;
fn main() {
println!("{}", formatter::format(include_str!("./grammar.pest").to_string()));
}
压缩
压缩Pest语法。
使用方法
use pest_extra::formatter;
fn main() {
println!("{}", formatter::minify(include_str!("./grammar.pest").to_string()));
}
比较
calculator.pest
大小:556字节
// 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 = _{ " " }
大小:272字节(节省了48.92%)
minified-calculator.pest
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=_{" "}
将语法编译成Pest语法
安装
[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虚拟机
pest_extra 内置了 pest_vm,可以从您的语法创建解析器。
安装
[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
依赖
~2–11MB
~110K SLoC