-
-
Notifications
You must be signed in to change notification settings - Fork 160
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
Attempt At Non-Infix Pratt Parsing #728
base: main
Are you sure you want to change the base?
Conversation
Important The PR for now breaks one of the postfix tests - I wanted to open it just in case someone who knows more than me would want to take a look! |
…tional update of `max_power`.
Thanks for the PR, should be able to take a look at this soon. |
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()); |
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.
I think it would be a good idea to check that 1+2*3*5
fails here too. assert!(parser.parse(...).has_errors())
should be sufficient.
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.
Added!
Do you have any idea why the current changes break the postfix test? |
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, | ||
} |
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!
Along the same lines as #600.
Currently, the
chumsky::pratt
parser only supports binary infix operators withleft
orright
associativity, but not non-infix ones. Other libraries and literature might term this asInfixN
or non-associative. (Please correct me if I'm wrong~)For example, comparison operators like
>
and==
are typically non-associative, since expressions like1 == 2 == 3
is invalid. (They cannot be chained.)This PR adds a
non
option for associativity, and tries to follow the same article.