-
-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: unary prefix/suffix operator support via PrattParser (#710)
Closes #461 based on #344 Co-authored-by: Tomas Tauber <[email protected]> Co-authored-by: Klas Segeljakt <[email protected]>
- Loading branch information
1 parent
4a4a398
commit 9f7e4f7
Showing
13 changed files
with
592 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[package] | ||
name = "pest_derive" | ||
description = "pest's derive macro" | ||
version = "2.3.1" | ||
version = "2.4.0" | ||
edition = "2018" | ||
authors = ["Dragoș Tiselice <[email protected]>"] | ||
homepage = "https://pest-parser.github.io/" | ||
|
@@ -23,5 +23,5 @@ std = ["pest/std", "pest_generator/std"] | |
|
||
[dependencies] | ||
# for tests, included transitively anyway | ||
pest = { path = "../pest", version = "2.3.1", default-features = false } | ||
pest_generator = { path = "../generator", version = "2.3.1", default-features = false } | ||
pest = { path = "../pest", version = "2.4.0", default-features = false } | ||
pest_generator = { path = "../generator", version = "2.4.0", default-features = false } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
WHITESPACE = _{ " " | "\t" | NEWLINE } | ||
|
||
program = { SOI ~ expr ~ EOI } | ||
expr = { prefix* ~ primary ~ postfix* ~ (infix ~ prefix* ~ primary ~ postfix* )* } | ||
infix = _{ add | sub | mul | div | pow } | ||
add = { "+" } // Addition | ||
sub = { "-" } // Subtraction | ||
mul = { "*" } // Multiplication | ||
div = { "/" } // Division | ||
pow = { "^" } // Exponentiation | ||
prefix = _{ neg } | ||
neg = { "-" } // Negation | ||
postfix = _{ fac } | ||
fac = { "!" } // Factorial | ||
primary = _{ int | "(" ~ expr ~ ")" } | ||
int = @{ (ASCII_NONZERO_DIGIT ~ ASCII_DIGIT+ | ASCII_DIGIT) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
mod parser { | ||
use pest_derive::Parser; | ||
|
||
#[derive(Parser)] | ||
#[grammar = "../examples/calc.pest"] | ||
pub struct Parser; | ||
} | ||
|
||
use parser::Rule; | ||
use pest::{ | ||
iterators::Pairs, | ||
pratt_parser::{Assoc::*, Op, PrattParser}, | ||
Parser, | ||
}; | ||
use std::io::{stdin, stdout, Write}; | ||
|
||
fn parse_to_str(pairs: Pairs<Rule>, pratt: &PrattParser<Rule>) -> String { | ||
pratt | ||
.map_primary(|primary| match primary.as_rule() { | ||
Rule::int => primary.as_str().to_owned(), | ||
Rule::expr => parse_to_str(primary.into_inner(), pratt), | ||
_ => unreachable!(), | ||
}) | ||
.map_prefix(|op, rhs| match op.as_rule() { | ||
Rule::neg => format!("(-{})", rhs), | ||
_ => unreachable!(), | ||
}) | ||
.map_postfix(|lhs, op| match op.as_rule() { | ||
Rule::fac => format!("({}!)", lhs), | ||
_ => unreachable!(), | ||
}) | ||
.map_infix(|lhs, op, rhs| match op.as_rule() { | ||
Rule::add => format!("({}+{})", lhs, rhs), | ||
Rule::sub => format!("({}-{})", lhs, rhs), | ||
Rule::mul => format!("({}*{})", lhs, rhs), | ||
Rule::div => format!("({}/{})", lhs, rhs), | ||
Rule::pow => format!("({}^{})", lhs, rhs), | ||
_ => unreachable!(), | ||
}) | ||
.parse(pairs) | ||
} | ||
|
||
fn parse_to_i32(pairs: Pairs<Rule>, pratt: &PrattParser<Rule>) -> i128 { | ||
pratt | ||
.map_primary(|primary| match primary.as_rule() { | ||
Rule::int => primary.as_str().parse().unwrap(), | ||
Rule::expr => parse_to_i32(primary.into_inner(), pratt), | ||
_ => unreachable!(), | ||
}) | ||
.map_prefix(|op, rhs| match op.as_rule() { | ||
Rule::neg => -rhs, | ||
_ => unreachable!(), | ||
}) | ||
.map_postfix(|lhs, op| match op.as_rule() { | ||
Rule::fac => (1..lhs + 1).product(), | ||
_ => unreachable!(), | ||
}) | ||
.map_infix(|lhs, op, rhs| match op.as_rule() { | ||
Rule::add => lhs + rhs, | ||
Rule::sub => lhs - rhs, | ||
Rule::mul => lhs * rhs, | ||
Rule::div => lhs / rhs, | ||
Rule::pow => (1..rhs + 1).map(|_| lhs).product(), | ||
_ => unreachable!(), | ||
}) | ||
.parse(pairs) | ||
} | ||
|
||
fn main() { | ||
let pratt = PrattParser::new() | ||
.op(Op::infix(Rule::add, Left) | Op::infix(Rule::sub, Left)) | ||
.op(Op::infix(Rule::mul, Left) | Op::infix(Rule::div, Left)) | ||
.op(Op::infix(Rule::pow, Right)) | ||
.op(Op::postfix(Rule::fac)) | ||
.op(Op::prefix(Rule::neg)); | ||
|
||
let stdin = stdin(); | ||
let mut stdout = stdout(); | ||
|
||
loop { | ||
let source = { | ||
print!("> "); | ||
let _ = stdout.flush(); | ||
let mut input = String::new(); | ||
let _ = stdin.read_line(&mut input); | ||
input.trim().to_string() | ||
}; | ||
|
||
let pairs = match parser::Parser::parse(Rule::program, &source) { | ||
Ok(mut parse_tree) => { | ||
parse_tree | ||
.next() | ||
.unwrap() | ||
.into_inner() // inner of program | ||
.next() | ||
.unwrap() | ||
.into_inner() // inner of expr | ||
} | ||
Err(err) => { | ||
println!("Failed parsing input: {:}", err); | ||
continue; | ||
} | ||
}; | ||
|
||
print!("{} => ", source); | ||
print!("{} => ", parse_to_str(pairs.clone(), &pratt)); | ||
println!("{}", parse_to_i32(pairs.clone(), &pratt)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[package] | ||
name = "pest_generator" | ||
description = "pest code generator" | ||
version = "2.3.1" | ||
version = "2.4.0" | ||
edition = "2018" | ||
authors = ["Dragoș Tiselice <[email protected]>"] | ||
homepage = "https://pest-parser.github.io/" | ||
|
@@ -18,8 +18,8 @@ default = ["std"] | |
std = ["pest/std"] | ||
|
||
[dependencies] | ||
pest = { path = "../pest", version = "2.3.1", default-features = false } | ||
pest_meta = { path = "../meta", version = "2.3.1" } | ||
pest = { path = "../pest", version = "2.4.0", default-features = false } | ||
pest_meta = { path = "../meta", version = "2.4.0" } | ||
proc-macro2 = "1.0" | ||
quote = "1.0" | ||
syn = "1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[package] | ||
name = "pest_grammars" | ||
description = "pest popular grammar implementations" | ||
version = "2.3.1" | ||
version = "2.4.0" | ||
edition = "2018" | ||
authors = ["Dragoș Tiselice <[email protected]>"] | ||
homepage = "https://pest-parser.github.io/" | ||
|
@@ -14,8 +14,8 @@ readme = "_README.md" | |
rust-version = "1.56" | ||
|
||
[dependencies] | ||
pest = { path = "../pest", version = "2.3.1" } | ||
pest_derive = { path = "../derive", version = "2.3.1" } | ||
pest = { path = "../pest", version = "2.4.0" } | ||
pest_derive = { path = "../derive", version = "2.4.0" } | ||
|
||
[dev-dependencies] | ||
criterion = "0.3" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[package] | ||
name = "pest_meta" | ||
description = "pest meta language parser and validator" | ||
version = "2.3.1" | ||
version = "2.4.0" | ||
edition = "2018" | ||
authors = ["Dragoș Tiselice <[email protected]>"] | ||
homepage = "https://pest-parser.github.io/" | ||
|
@@ -16,7 +16,7 @@ include = ["Cargo.toml", "src/**/*", "src/grammar.rs", "_README.md", "LICENSE-*" | |
rust-version = "1.56" | ||
|
||
[dependencies] | ||
pest = { path = "../pest", version = "2.3.1" } | ||
pest = { path = "../pest", version = "2.4.0" } | ||
once_cell = "1.8.0" | ||
|
||
[build-dependencies] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[package] | ||
name = "pest" | ||
description = "The Elegant Parser" | ||
version = "2.3.1" | ||
version = "2.4.0" | ||
edition = "2018" | ||
authors = ["Dragoș Tiselice <[email protected]>"] | ||
homepage = "https://pest-parser.github.io/" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.