-
-
Notifications
You must be signed in to change notification settings - Fork 187
Attempt At Non-Infix Pratt Parsing #728
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
Open
lancylot2004
wants to merge
7
commits into
zesterer:main
Choose a base branch
from
lancylot2004:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2c279be
feat: Adds non-associative option in `Associativity`.
lancylot2004 ae7e2d0
feat: Adds power calculations to `Associativity.`
lancylot2004 dcd687b
feat: Adds consideration for non-infix operators by `max_power`.
lancylot2004 258af36
fix: Updates binding power functions and propagates `max_power` changes.
lancylot2004 9ef2d17
fix: Removes erroneous `next_power` calculation in `do_parse_infix`.
lancylot2004 5759988
fix: Prevents underflow in binding power calculations and tries condi…
lancylot2004 9a01478
fix: Adds extra non-infix as per comment on #728.
lancylot2004 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -162,9 +162,10 @@ macro_rules! op_check_and_emit { | |
>, | ||
lhs: (), | ||
min_power: u32, | ||
max_power: &mut u32, | ||
f: &dyn Fn(&mut InputRef<'src, 'parse, I, E>, u32) -> PResult<Check, O>, | ||
) -> Result<(), ()> { | ||
self.do_parse_infix::<Check>(inp, pre_expr, pre_op, lhs, min_power, &f) | ||
self.do_parse_infix::<Check>(inp, pre_expr, pre_op, lhs, min_power, max_power, &f) | ||
} | ||
#[inline(always)] | ||
fn do_parse_infix_emit<'parse>( | ||
|
@@ -179,9 +180,10 @@ macro_rules! op_check_and_emit { | |
>, | ||
lhs: O, | ||
min_power: u32, | ||
max_power: &mut u32, | ||
f: &dyn Fn(&mut InputRef<'src, 'parse, I, E>, u32) -> PResult<Emit, O>, | ||
) -> Result<O, O> { | ||
self.do_parse_infix::<Emit>(inp, pre_expr, pre_op, lhs, min_power, &f) | ||
self.do_parse_infix::<Emit>(inp, pre_expr, pre_op, lhs, min_power, max_power, &f) | ||
} | ||
}; | ||
} | ||
|
@@ -244,6 +246,7 @@ where | |
_pre_op: &input::Checkpoint<'src, 'parse, I, <E::State as Inspector<'src, I>>::Checkpoint>, | ||
lhs: M::Output<O>, | ||
_min_power: u32, | ||
_max_power: &mut u32, | ||
_f: &impl Fn(&mut InputRef<'src, 'parse, I, E>, u32) -> PResult<M, O>, | ||
) -> Result<M::Output<O>, M::Output<O>> | ||
where | ||
|
@@ -292,6 +295,7 @@ where | |
pre_op: &input::Checkpoint<'src, 'parse, I, <E::State as Inspector<'src, I>>::Checkpoint>, | ||
lhs: (), | ||
min_power: u32, | ||
max_power: &mut u32, | ||
f: &dyn Fn(&mut InputRef<'src, 'parse, I, E>, u32) -> PResult<Check, O>, | ||
) -> Result<(), ()>; | ||
#[doc(hidden)] | ||
|
@@ -302,6 +306,7 @@ where | |
pre_op: &input::Checkpoint<'src, 'parse, I, <E::State as Inspector<'src, I>>::Checkpoint>, | ||
lhs: O, | ||
min_power: u32, | ||
max_power: &mut u32, | ||
f: &dyn Fn(&mut InputRef<'src, 'parse, I, E>, u32) -> PResult<Emit, O>, | ||
) -> Result<O, O>; | ||
} | ||
|
@@ -356,12 +361,13 @@ where | |
pre_op: &input::Checkpoint<'src, 'parse, I, <E::State as Inspector<'src, I>>::Checkpoint>, | ||
lhs: M::Output<O>, | ||
min_power: u32, | ||
max_power: &mut u32, | ||
f: &impl Fn(&mut InputRef<'src, 'parse, I, E>, u32) -> PResult<M, O>, | ||
) -> Result<M::Output<O>, M::Output<O>> | ||
where | ||
Self: Sized, | ||
{ | ||
M::invoke_pratt_op_infix(self, inp, pre_expr, pre_op, lhs, min_power, f) | ||
M::invoke_pratt_op_infix(self, inp, pre_expr, pre_op, lhs, min_power, max_power, f) | ||
} | ||
|
||
#[inline(always)] | ||
|
@@ -414,10 +420,11 @@ where | |
pre_op: &input::Checkpoint<'src, 'parse, I, <E::State as Inspector<'src, I>>::Checkpoint>, | ||
lhs: (), | ||
min_power: u32, | ||
max_power: &mut u32, | ||
f: &dyn Fn(&mut InputRef<'src, 'parse, I, E>, u32) -> PResult<Check, O>, | ||
) -> Result<(), ()> { | ||
self.0 | ||
.do_parse_infix_check(inp, pre_expr, pre_op, lhs, min_power, &f) | ||
.do_parse_infix_check(inp, pre_expr, pre_op, lhs, min_power, max_power, &f) | ||
} | ||
#[inline(always)] | ||
fn do_parse_infix_emit<'parse>( | ||
|
@@ -427,23 +434,35 @@ where | |
pre_op: &input::Checkpoint<'src, 'parse, I, <E::State as Inspector<'src, I>>::Checkpoint>, | ||
lhs: O, | ||
min_power: u32, | ||
max_power: &mut u32, | ||
f: &dyn Fn(&mut InputRef<'src, 'parse, I, E>, u32) -> PResult<Emit, O>, | ||
) -> Result<O, O> { | ||
self.0 | ||
.do_parse_infix_emit(inp, pre_expr, pre_op, lhs, min_power, &f) | ||
.do_parse_infix_emit(inp, pre_expr, pre_op, lhs, min_power, max_power, &f) | ||
} | ||
} | ||
|
||
/// Defines the [associativity](https://en.wikipedia.org/wiki/Associative_property) and binding power of an [`infix`] | ||
/// operator (see [`left`] and [`right`]). | ||
/// operator (see [`left`], [`right`], and [`non`]). | ||
/// | ||
/// Higher binding powers should be used for higher precedence operators. | ||
/// | ||
/// The left, right, and next binding powers are used to determine precedence in the parser. They | ||
/// are calculated as follows: | ||
/// | ||
/// | | left power | right power | next power | | ||
/// |-------|------------|-------------|------------| | ||
/// | Left | bp * 3 | bp * 3 + 1 | bp * 3 | | ||
/// | Right | bp * 3 + 1 | bp * 3 | bp * 3 | | ||
/// | Non | bp * 3 | bp * 3 | bp * 3 + 1 | | ||
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] | ||
pub enum Associativity { | ||
/// Specifies that the operator should be left-associative, with the given binding power (see [`left`]). | ||
Left(u16), | ||
/// Specifies that the operator should be right-associative, with the given binding power (see [`right`]). | ||
Right(u16), | ||
/// Specifies that the operator should non-associative, with the given binding power (see [`non`]). | ||
Non(u16), | ||
} | ||
|
||
/// Specifies a left [`Associativity`] with the given binding power. | ||
|
@@ -462,18 +481,32 @@ pub fn right(binding_power: u16) -> Associativity { | |
Associativity::Right(binding_power) | ||
} | ||
|
||
/// Specifies a non-associative [`Associativity`] with the given binding power. | ||
/// | ||
/// Non-associative operators cannot be chained, but otherwise respect binding powers. For example, | ||
/// the expression `a == b == c` is invalid if `==` is a non-associative operator (which in | ||
/// general, all comparison operators are). | ||
pub fn non(binding_power: u16) -> Associativity { | ||
Associativity::Non(binding_power) | ||
} | ||
|
||
impl Associativity { | ||
fn left_power(&self) -> u32 { | ||
let (&Self::Left(x) | &Self::Non(x) | &Self::Right(x)) = self; | ||
x as u32 * 3 + 1 | ||
} | ||
|
||
fn right_power(&self) -> u32 { | ||
match self { | ||
Self::Left(x) => *x as u32 * 2, | ||
Self::Right(x) => *x as u32 * 2 + 1, | ||
&Self::Left(x) | &Self::Non(x) => x as u32 * 3 + 2, | ||
&Self::Right(x) => x as u32 * 3 + 1, | ||
} | ||
} | ||
|
||
fn right_power(&self) -> u32 { | ||
fn next_power(&self) -> u32 { | ||
match self { | ||
Self::Left(x) => *x as u32 * 2 + 1, | ||
Self::Right(x) => *x as u32 * 2, | ||
&Self::Left(x) | &Self::Right(x) => x as u32 * 3 + 1, | ||
&Self::Non(x) => x as u32 * 3, | ||
} | ||
} | ||
} | ||
|
@@ -536,42 +569,50 @@ where | |
A: Parser<'src, I, Op, E>, | ||
F: Fn(O, Op, O, &mut MapExtra<'src, '_, I, E>) -> O, | ||
{ | ||
#[inline] | ||
fn do_parse_infix<'parse, M: Mode>( | ||
&self, | ||
inp: &mut InputRef<'src, 'parse, I, E>, | ||
pre_expr: &input::Cursor<'src, 'parse, I>, | ||
pre_op: &input::Checkpoint<'src, 'parse, I, <E::State as Inspector<'src, I>>::Checkpoint>, | ||
lhs: M::Output<O>, | ||
min_power: u32, | ||
max_power: &mut u32, | ||
f: &impl Fn(&mut InputRef<'src, 'parse, I, E>, u32) -> PResult<M, O>, | ||
) -> Result<M::Output<O>, M::Output<O>> | ||
where | ||
Self: Sized, | ||
{ | ||
if self.associativity.left_power() >= min_power { | ||
match self.op_parser.go::<M>(inp) { | ||
Ok(op) => match f(inp, self.associativity.right_power()) { | ||
Ok(rhs) => Ok(M::combine( | ||
M::combine(lhs, rhs, |lhs, rhs| (lhs, rhs)), | ||
op, | ||
|(lhs, rhs), op| { | ||
(self.fold)(lhs, op, rhs, &mut MapExtra::new(pre_expr, inp)) | ||
}, | ||
775E )), | ||
Err(()) => { | ||
inp.rewind(pre_op.clone()); | ||
Err(lhs) | ||
} | ||
}, | ||
Err(()) => { | ||
inp.rewind(pre_op.clone()); | ||
Err(lhs) | ||
} | ||
let assoc = self.associativity; | ||
if assoc.left_power() < min_power || assoc.left_power() > *max_power { | ||
return Err(lhs); | ||
} | ||
|
||
let op = match self.op_parser.go::<M>(inp) { | ||
Ok(op) => op, | ||
Err(()) => { | ||
inp.rewind(pre_op.clone()); | ||
return Err(lhs); | ||
} | ||
} else { | ||
Err(lhs) | ||
}; | ||
|
||
let rhs = match f(inp, assoc.right_power()) { | ||
Ok(rhs) => rhs, | ||
Err(()) => { | ||
inp.rewind(pre_op.clone()); | ||
return Err(lhs); | ||
} | ||
}; | ||
|
||
let res = M::combine( | ||
M::combine(lhs, rhs, |lhs, rhs| (lhs, rhs)), | ||
op, | ||
|(lhs, rhs), op| { (self.fold)(lhs, op, rhs, &mut MapExtra::new(pre_expr, inp)) }, | ||
); | ||
|
||
if let Associativity::Non(_) | Associativity::Left(_) = assoc { | ||
*max_power = assoc.next_power(); | ||
} | ||
Ok(res) | ||
} | ||
|
||
op_check_and_emit!(); | ||
|
@@ -818,14 +859,15 @@ macro_rules! impl_operator_for_tuple { | |
pre_op: &input::Checkpoint<'src, 'parse, I, <E::State as Inspector<'src, I>>::Checkpoint>, | ||
mut lhs: M::Output<O>, | ||
min_power: u32, | ||
max_power: &mut u32, | ||
f: &impl Fn(&mut InputRef<'src, 'parse, I, E>, u32) -> PResult<M, O>, | ||
) -> Result<M::Output<O>, M::Output<O>> | ||
where | ||
Self: Sized, | ||
{ | ||
let ($($X,)*) = self; | ||
$( | ||
match $X.do_parse_infix::<M>(inp, pre_expr, pre_op, lhs, min_power, f) { | ||
match $X.do_parse_infix::<M>(inp, pre_expr, pre_op, lhs, min_power, max_power, f) { | ||
Ok(out) => return Ok(out), | ||
Err(out) => lhs = out, | ||
} | ||
|
@@ -894,13 +936,14 @@ where | |
pre_op: &input::Checkpoint<'src, 'parse, I, <E::State as Inspector<'src, I>>::Checkpoint>, | ||
mut lhs: M::Output<O>, | ||
min_power: u32, | ||
max_power: &mut u32, | ||
f: &impl Fn(&mut InputRef<'src, 'parse, I, E>, u32) -> PResult<M, O>, | ||
) -> Result<M::Output<O>, M::Output<O>> | ||
where | ||
Self: Sized, | ||
{ | ||
for op in self { | ||
match op.do_parse_infix::<M>(inp, pre_expr, pre_op, lhs, min_power, f) { | ||
match op.do_parse_infix::<M>(inp, pre_expr, pre_op, lhs, min_power, max_power, f) { | ||
Ok(out) => return Ok(out), | ||
Err(out) => lhs = out, | ||
} | ||
|
@@ -918,19 +961,21 @@ impl<'src, Atom, Ops> Pratt<Atom, Ops> { | |
&self, | ||
inp: &mut InputRef<'src, '_, I, E>, | ||
min_power: u32, | ||
max_power: Option<u32>, | ||
) -> PResult<M, O> | ||
where | ||
I: Input<'src>, | ||
E: ParserExtra<'src, I>, | ||
Atom: Parser<'src, I, O, E>, | ||
Ops: Operator<'src, I, O, E>, | ||
{ | ||
let mut max_power: u32 = max_power.unwrap_or(u32::MAX); | ||
let pre_expr = inp.save(); | ||
// Prefix unary operators | ||
let mut lhs = match self | ||
.ops | ||
.do_parse_prefix::<M>(inp, &pre_expr, &|inp, min_power| { | ||
recursive::recurse(|| self.pratt_go::<M, _, _, _>(inp, min_power)) | ||
recursive::recurse(|| self.pratt_go::<M, _, _, _>(inp, min_power, None)) | ||
}) { | ||
Ok(out) => out, | ||
Err(()) => self.atom.go::<M>(inp)?, | ||
|
@@ -958,8 +1003,9 @@ impl<'src, Atom, Ops> Pratt<Atom, Ops> { | |
&pre_op, | ||
lhs, | ||
min_power, | ||
&mut max_power, | ||
&|inp, min_power| { | ||
recursive::recurse(|| self.pratt_go::<M, _, _, _>(inp, min_power)) | ||
recursive::recurse(|| self.pratt_go::<M, _, _, _>(inp, min_power, None)) | ||
}, | ||
) { | ||
Ok(out) => { | ||
|
@@ -986,7 +1032,7 @@ where | |
Ops: Operator<'src, I, O, E>, | ||
{ | ||
fn go<M: Mode>(&self, inp: &mut InputRef<'src, '_, I, E>) -> PResult<M, O> { | ||
self.pratt_go::<M, _, _, _>(inp, 0) | ||
self.pratt_go::<M, _, _, _>(inp, 0, None) | ||
} | ||
|
||
go_extra!(O); | ||
|
@@ -1254,4 +1300,26 @@ mod tests { | |
Ok("(((§(1 + (-(~(2!)))))$) * 3)".to_string()), | ||
) | ||
} | ||
|
||
#[test] | ||
fn with_pre_and_postfix_ops_and_parentheses() { | ||
let atom = text::int::<_, Err<Simple<char>>>(10) | ||
.from_str() | ||
.unwrapped() | ||
.map(Expr::Literal); | ||
|
||
let parser = atom | ||
.pratt(( | ||
// -- Infix | ||
infix(left(1), just('+'), |l, _, r, _| i(Expr::Add, l, r)), | ||
infix(non(2), just('*'), |l, _, r, _| i(Expr::Mul, l, r)), | ||
)) | ||
.map(|x| x.to_string()); | ||
assert_eq!( | ||
parser.parse("1+2*3").into_result(), | ||
Ok("(1 + (2 * 3))".to_string()) | ||
); | ||
assert!(parser.parse("1+2*3*3").has_errors()); | ||
Comment on lines
+1311
to
+1322
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be a good idea to check that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added! |
||
assert!(parser.parse("1+2*3*5").has_errors()); | ||
} | ||
} |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem to match up with the table above it. Perhaps this is the cause of the test failure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies I should've changed the table - the values in the table don't work either!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean the values in the table are correct now? Apologies about not having gotten back to you in a while!