chore(deps): Update to pest 2.4's pratt parser.

This commit is contained in:
James Harton 2022-10-02 08:24:09 +13:00
parent 361b73c0bd
commit 63c2d8729f
4 changed files with 34 additions and 32 deletions

16
Cargo.lock generated
View file

@ -296,9 +296,9 @@ checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f"
[[package]]
name = "pest"
version = "2.3.1"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb779fcf4bb850fbbb0edc96ff6cf34fd90c4b1a112ce042653280d9a7364048"
checksum = "dbc7bc69c062e492337d74d59b120c274fd3d261b6bf6d3207d499b4b379c41a"
dependencies = [
"thiserror",
"ucd-trie",
@ -306,9 +306,9 @@ dependencies = [
[[package]]
name = "pest_derive"
version = "2.3.1"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "502b62a6d0245378b04ffe0a7fb4f4419a4815fce813bd8a0ec89a56e07d67b1"
checksum = "60b75706b9642ebcb34dab3bc7750f811609a0eb1dd8b88c2d15bf628c1c65b2"
dependencies = [
"pest",
"pest_generator",
@ -316,9 +316,9 @@ dependencies = [
[[package]]
name = "pest_generator"
version = "2.3.1"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "451e629bf49b750254da26132f1a5a9d11fd8a95a3df51d15c4abd1ba154cb6c"
checksum = "f4f9272122f5979a6511a749af9db9bfc810393f63119970d7085fed1c4ea0db"
dependencies = [
"pest",
"pest_meta",
@ -329,9 +329,9 @@ dependencies = [
[[package]]
name = "pest_meta"
version = "2.3.1"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcec162c71c45e269dfc3fc2916eaeb97feab22993a21bcce4721d08cd7801a6"
checksum = "4c8717927f9b79515e565a64fe46c38b8cd0427e64c40680b14a7365ab09ac8d"
dependencies = [
"once_cell",
"pest",

View file

@ -30,7 +30,7 @@ defuse = { "use" ~ typename ~ defuseproperties }
defuseproperties = { ("," ~ defuseproperty)* }
defuseproperty = { keyword ~ typename }
typeunion = { typename ~ ("+" ~ typename)+ }
typeunion = { typename ~ (plus ~ typename)+ }
typeblock = { "do" ~ typeblockcontents* ~ "end" }
typeblockcontents = _{ documentation | defp | def }

View file

@ -4,33 +4,30 @@ use crate::grammar::{Error, Rule};
use pest::{
iterators::Pair,
prec_climber::{Assoc, Operator, PrecClimber},
pratt_parser::{Assoc, Op, PrattParser},
};
lazy_static! {
pub static ref PREC_CLIMBER: PrecClimber<Rule> = build_precedence_climber();
pub static ref PRATT: PrattParser<Rule> = build_pratt();
}
fn build_precedence_climber() -> PrecClimber<Rule> {
PrecClimber::new(vec![
Operator::new(Rule::logical_or, Assoc::Left),
Operator::new(Rule::logical_and, Assoc::Left),
Operator::new(Rule::equal, Assoc::Right) | Operator::new(Rule::not_equal, Assoc::Right),
Operator::new(Rule::greater_than_or_equal, Assoc::Left)
| Operator::new(Rule::less_than_or_equal, Assoc::Left)
| Operator::new(Rule::greater_than, Assoc::Left)
| Operator::new(Rule::less_than, Assoc::Left),
Operator::new(Rule::bitwise_xor, Assoc::Left)
| Operator::new(Rule::bitwise_or, Assoc::Left),
Operator::new(Rule::bitwise_and, Assoc::Left),
Operator::new(Rule::shift_right, Assoc::Left)
| Operator::new(Rule::shift_left, Assoc::Left),
Operator::new(Rule::plus, Assoc::Left) | Operator::new(Rule::minus, Assoc::Left),
Operator::new(Rule::modulo, Assoc::Left)
| Operator::new(Rule::divide, Assoc::Left)
| Operator::new(Rule::multiply, Assoc::Left),
Operator::new(Rule::exponent, Assoc::Right),
])
fn build_pratt() -> PrattParser<Rule> {
PrattParser::new()
.op(Op::infix(Rule::logical_or, Assoc::Left))
.op(Op::infix(Rule::logical_and, Assoc::Left))
.op(Op::infix(Rule::equal, Assoc::Right) | Op::infix(Rule::not_equal, Assoc::Right))
.op(Op::infix(Rule::greater_than_or_equal, Assoc::Left)
| Op::infix(Rule::less_than_or_equal, Assoc::Left)
| Op::infix(Rule::greater_than, Assoc::Left)
| Op::infix(Rule::less_than, Assoc::Left))
.op(Op::infix(Rule::bitwise_xor, Assoc::Left) | Op::infix(Rule::bitwise_or, Assoc::Left))
.op(Op::infix(Rule::bitwise_and, Assoc::Left))
.op(Op::infix(Rule::shift_right, Assoc::Left) | Op::infix(Rule::shift_left, Assoc::Left))
.op(Op::infix(Rule::plus, Assoc::Left) | Op::infix(Rule::minus, Assoc::Left))
.op(Op::infix(Rule::modulo, Assoc::Left)
| Op::infix(Rule::divide, Assoc::Left)
| Op::infix(Rule::multiply, Assoc::Left))
.op(Op::infix(Rule::exponent, Assoc::Right))
}
fn build_binary(lhs: Node, op: Pair<'_, Rule>, rhs: Node) -> Node {
@ -51,6 +48,10 @@ fn visit(pair: Pair<'_, Rule>) -> Node {
}
pub fn climb(pair: Pair<'_, Rule>) -> Result<Node, Error> {
let node = PREC_CLIMBER.climb(pair.into_inner(), visit, build_binary);
let node = PRATT
.map_primary(visit)
.map_infix(build_binary)
.parse(pair.into_inner());
Ok(node)
}

View file

@ -17,6 +17,7 @@ pub fn init() -> Result<Context, Error> {
let mut context = Context::new();
for path in files.iter() {
println!("Parsing: {}", path.to_str().unwrap());
let source = Source::from_file(path)?;
let source = Arc::new(source);
context = outrun_compiler::compile(context, source)?;