Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pratt example #622

Draft
wants to merge 29 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
fed8c90
feat: implement Pratt parser
39555 Nov 10, 2024
ee4459d
commit suggestion
39555 Nov 16, 2024
4b1499d
remove spaces from #[doc(alias = "...")]
39555 Nov 16, 2024
acf4577
remove `UnaryOp` and `BinaryOp` in favor of `Fn`
39555 Nov 16, 2024
a816a1c
remove redundant trait impl
39555 Nov 16, 2024
2a80e65
remove `allow_unused`, move `allow(non_snake_case)` to where it shoul…
39555 Nov 16, 2024
29fe18d
stop dumping pratt into `combinator` namespace
39555 Nov 16, 2024
5a4f4b4
move important things to go first
39555 Nov 16, 2024
919a1cb
strip fancy api for now
39555 Nov 16, 2024
0273a29
remove wrong and long doc for now
39555 Nov 16, 2024
f218911
fix: precedence for associativity, remove `trace()`
39555 Nov 16, 2024
3d7ef41
switch from `&dyn Fn(O) -> O` to `fn(O) -> O`
39555 Nov 17, 2024
a6cbc1a
feat: pass Input into operator closures
39555 Nov 17, 2024
29b64fa
add `trace` for `tests` parser
39555 Nov 17, 2024
b31a3a3
feat: operator closures must return PResult
39555 Nov 18, 2024
33c82f3
feat: allow the user to specify starting power
39555 Nov 18, 2024
040dd85
feat: enum `Assoc` for infix operators. Add `Neither` associativity
39555 Nov 19, 2024
6d88dff
fix: switch to i64, fix precedence checking
39555 Nov 19, 2024
8f18fc2
example: pratt expression parser
39555 Nov 17, 2024
a4ad844
feat: complex postfix operators
39555 Nov 17, 2024
54cb315
pratt_example: operator closures return PResult
39555 Nov 18, 2024
d6da343
test: add tests
39555 Nov 18, 2024
c1a8535
specify the parser start precedence
39555 Nov 18, 2024
a85291b
style: fix indentation
39555 Nov 18, 2024
39cc484
refactor: remove unnecessarily multispace0
39555 Nov 18, 2024
c52c10d
fix: failed tests
39555 Nov 18, 2024
d3c3d0a
use `Assoc` enum. tests for associativity `Neither`
39555 Nov 19, 2024
b7b0629
fix: switch to i64
39555 Nov 19, 2024
5e7fb65
tests ill-formed expressions
39555 Nov 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ required-features = ["alloc"]
name = "string"
required-features = ["alloc"]

[[example]]
name = "pratt"
required-features = ["std"]


[[bench]]
name = "pratt"
path = "examples/pratt/bench.rs"
harness = false
required-features = ["std"]

[[bench]]
name = "arithmetic"
path = "examples/arithmetic/bench.rs"
Expand Down
16 changes: 16 additions & 0 deletions examples/pratt/bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
mod parser;

use criterion::black_box;
use winnow::prelude::*;

fn pratt(c: &mut criterion::Criterion) {
let i =
"a = 2*-2 / ( &**foo.a->p! -+1) + 3^1 / 4 == 1 * (2 - 7 + 567 *12 /2) + 3*(1+2*( 45 /2))";
parser::pratt_parser.parse(i).expect("should parse");
c.bench_function("pratt_parser", |b| {
b.iter(|| black_box(parser::pratt_parser.parse(i).unwrap()));
});
}

criterion::criterion_group!(benches, pratt);
criterion::criterion_main!(benches);
44 changes: 44 additions & 0 deletions examples/pratt/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use winnow::prelude::*;

mod parser;

fn main() -> Result<(), lexopt::Error> {
let args = Args::parse()?;

let input = args.input.as_deref().unwrap_or("1 + 1");
match parser::pratt_parser.parse(input) {
Ok(result) => {
println!("{result}");
}
Err(err) => {
println!("FAILED");
println!("{err}");
}
}

Ok(())
}

#[derive(Default)]
struct Args {
input: Option<String>,
}

impl Args {
fn parse() -> Result<Self, lexopt::Error> {
use lexopt::prelude::*;

let mut res = Args::default();

let mut args = lexopt::Parser::from_env();
while let Some(arg) = args.next()? {
match arg {
Value(input) => {
res.input = Some(input.string()?);
}
_ => return Err(arg.unexpected()),
}
}
Ok(res)
}
}
Loading
Loading