-
Notifications
You must be signed in to change notification settings - Fork 79
feat: implement Pratt parsing #804
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
Conversation
Pull Request Test Coverage Report for Build 19690123416Details
💛 - Coveralls |
|
Thank you for all of your work on this, especially the great summary that made it easier to evaluate this! |
961694e to
8e95be1
Compare
|
A lot of these documentation comments are difficult to get right. I feel like:
I was trying to avoid a comprehensive treatment of Pratt parsing, but I think the documentation questions would be better served by having it more centralized. Maybe the core of this should be a special "topic" page that documents Pratt parsing at a high level, linking out to appropriate resources where necessary. Then the documentation on the individual structs and methods can be a little sparser, linking back to that topic page (or to @epage, what would your opinion be on that? Some specific questions I'm hoping you could answer:
|
8e95be1 to
17a688d
Compare
This will happen to some degree. People will have multiple jump-in points. They may browse straight to We need to balance
Considering the above and this, I suspect focusing the documentation on
Feel free to squash things in. I don't even mind if this PR is one commit as its adding one self-contained piece of functionality and I see docs, tests, and examples as all part of that whole. |
|
As the codebase is now documented (+ my changes), probably we could do a final pass to make sure binding power and precedence aren't mentioned in conflicting or confusing ways and it should be good enough. |
|
@lu-zero to double check, are you saying that this PR is now considered in a "ready" state for reviewing or is this more about changes you all have pending elsewhere? |
|
I sent ssmendon#1 to add some bits of documentation. I'm undecided on the binding power vs precedence though. |
|
@ssmendon if you are busy I can deal with the fmt lint. Regarding binding power vs precedence, what about adding a paragraph like |
I will hopefully have the time to work on this more in about a week. I've had something unexpected come up. (Apologies for the bump). |
There are three repositories (and three branches): * winnow-rs/winnow:main * ssmendon/winnow:ssmendon-pratt-pr (PR winnow-rs#804) * lu-zero/winnow:ssmendon-pratt-pr (newest changes) I'm starting this branch by rebasing lu-zero's work on top of winnow-rs's main. This is just a rote copy of the files explicitly changed (to keep the diff tidy). Co-authored-by: Igor <[email protected]> Co-authored-by: Luca Barbato <[email protected]>
c36a5eb to
cc2769e
Compare
cc2769e to
ea5d44c
Compare
ea5d44c to
33edc03
Compare
There are three repositories (and three branches): * winnow-rs/winnow:main * ssmendon/winnow:ssmendon-pratt-pr (PR winnow-rs#804) * lu-zero/winnow:ssmendon-pratt-pr (newest changes) I'm starting this branch by rebasing lu-zero's work on top of winnow-rs's main. This is just a rote copy of the files explicitly changed (to keep the diff tidy). Co-authored-by: Igor <[email protected]> Co-authored-by: Luca Barbato <[email protected]>
Addresses the following comment: * winnow-rs#804 (comment)
Addresses the following comment: * winnow-rs#804 (comment)
Tries to resolve: * winnow-rs#804 (comment) Alternative resolution for: * #2
33edc03 to
c546899
Compare
|
Changes:
|
755ae89 to
341a078
Compare
Adds a new operator precedence parser. Co-authored-by: Igor <[email protected]> Co-authored-by: Luca Barbato <[email protected]>
341a078 to
9aef8d2
Compare
Closes #131.
Sorry to add yet another PR, but I wanted to address the comments added by @epage to #798. I want this to be upstreamed, even if it's unstable at first, so that I can use the API in some other projects of mine.
I also think it would help resolve a lot of the other related PRs if there's a base PR inside of winnow.
Summary of changes
This PR rebases #614 to the newest version of winnow. It adds:
Most of the work was done by @39555 in the following drafted PRs: #614, #622, and #620. There's also PR #798 (which I did not consult), but it is a more recent attempt to try and have this merged.
Changes from #614
Expression::current_precedence_leveltoprecedence_levelParserforPostfixinstead of for(i64, fn)(in src/combinator/expression.rs#L372-377)PrefixandPostfixcrate::expressiona private module, andpub use expression::*insteadThe reason I renamed
current_precedence_levelwas to remove some boilerplate in the builder-style API. But I am able to change that back. See #614 (comment)The reason I
pub use expression::*in src/combinator/mod.rs is for consistency with the other modules.Changes from #622
c_expressionfrompratt.crate::_topic::languagereferencingc_expressioncrate::_topic::arithmeticreferencingc_expressionThe reason I named it
c_expressioninstead of @epage's suggestion to name itc_expris mostly for consistency with the existings_expressionexample.Related PRs
#798 contains some recent review comments.
— #131 (comment) by @39555
— #131 (comment) by @epage
Discussion summary for #614
Details
TODO: Add an
unstable-prattfeature flagTODO: Overflow checks around operator precedence, which uses
i64Discussed the API of
prefix, infix, postfix. They are both grammar-level constructs and value processing functions. Normally, grammar-level concepts are standalone functions and value processing is put into a trait function.dispatch!discussed instead ofalt()for performance, which led to a slightly different implementation where we dispatch by the input type.The "builder" approach violates the API guidelines discussed earlier, but it's also more convenient.
Discussed trying to implement this without recursion, due to stack overflows.
Operator callbacks return
impl Parserto receive the input, for something like a ternary operator.Provided a sample using
bumpaloandStatefulSome discussion about
start_precedence_levelin feat: implement Pratt parsing #614 (comment)Expressiontype itself changes because the parser's type may be differentDiscussion summary for #618
Details
shunting_yardimplementation without recursionstdDiscussion summary for #620
Details
shunting_yardparserbumpalo— #620 (comment)
Discussion summary for #622
Details
multispace0matchesPrecedencestruct instead of specifying the starting precedence in the parsertracefor the user automaticallyshunting yardalgorithm #618 (comment)Discussion summary for #798
Details
Postfixparser implOpen Questions
unstable-pratt? feat: implement Pratt parsing #614 (comment)expression()? feat: implement Pratt parsing #614 (comment)crate::combinator::expressionhelp guide users to the proper documentation? feat: implement Pratt parsing #614 (comment)precedence_levelbe renamed? feat: implement Pratt parsing #614 (comment)struct Expressionto have some configurable recursion depth? perf: bench forprattandshunting yardparsers #620 (comment)initial_powerin precedence? Pratt example #622 (comment)shunting_yardapproach from PoC: Pratt parsing withshunting yardalgorithm #618 ?shunting_yard: implement a user-provided stack PoC: Pratt parsing withshunting yardalgorithm #618 (comment)