Attempt At Non-Infix Pratt Parsing#728
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.
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.
|
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.
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.
Apologies I should've changed the table - the values in the table don't work either!
There was a problem hiding this comment.
Does this mean the values in the table are correct now? Apologies about not having gotten back to you in a while!
38bb88c to
02a1373
Compare
0f2b61a to
e350fc6
Compare
Along the same lines as #600.
Currently, the
chumsky::prattparser only supports binary infix operators withleftorrightassociativity, but not non-infix ones. Other libraries and literature might term this asInfixNor non-associative. (Please correct me if I'm wrong~)For example, comparison operators like
>and==are typically non-associative, since expressions like1 == 2 == 3is invalid. (They cannot be chained.)This PR adds a
nonoption for associativity, and tries to follow the same article.