-
-
Notifications
You must be signed in to change notification settings - Fork 144
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
[Feature request] Pratt parsing with spans #507
Comments
I did try On that note, why is Edit: I see, it's because getting a |
This is my attempt. https://github.com/zesterer/chumsky/compare/main...stefnotch:pratt-with-op?expand=1 I hope it's roughly the right direction, but one thing that's missing is the ability to write |
I’ll give it a look today to see what I can do to get it to give you the op spanned. A quick and dirty way to do it would be to use the span’s of the expressions to calculate the span of the operator, as the operator’s span is the space between the two expr’s (though whitespace makes that not 100% true) |
I think there's a certain value in having access to the entire parsed operator. For example, when parsing sufficiently complex mathematical expressions, then there is Knuth's up arrow notation. |
Excellent point. I’ll give it a shot tonight and see what I can do! |
So, here is some API questions that I have. How does this look? let parser = atom
.pratt(choice((
left_infix(just('-'), 0, |lhs, op, rhs| Expr::Sub(Box::new(lhs), Box::new(rhs))),
left_infix(just('+'), 0, |lhs, op, rhs| Expr::Add(Box::new(lhs), Box::new(rhs))),
)))
.with_postfix_ops(
// For postfix the `op` is on the right
postfix(just('!'), 0, |lhs, op| Expr::Factorial(Box::new(lhs))),
)
.with_prefix_ops(
// For prefix the `op` is on the left
prefix(just('-'), 0, |op, rhs| Expr::Negate(Box::new(rhs))),
)
.map(|x| x.to_string()); Or do we follow |
That looks like a good API. I'd keep the If it's |
Did you have any luck with a proper implementation so far, or are some parts trickier than expected? Is there anything I could do to help? In my little experiments with extending the Pratt parsing implementation, I never managed to figure out what exactly |
When using the new pratt parsing feature, how would I create a syntax tree which includes spans?
e.g. How does one get spans that include the operators? And how does one get the spans of the operators themselves?
chumsky/src/pratt.rs
Lines 709 to 719 in 01b96cd
The text was updated successfully, but these errors were encountered: