-
Notifications
You must be signed in to change notification settings - Fork 38
Allow building parsers from GrammarAST with modified start rules.
#585
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
|
Somewhat silly of me not to think of it, but there is no way to get |
This sounds like it could solve my case: 6 production rules which 3 could be used as a top-level rule. Making more than one top-level rule doesn't seem like an I know, I'm asking for a lot! 🤣 Thanks for thinking about this. |
|
Dumb idea: could we make something like |
True, but the mechanism provided here is much more powerful than that, the For instance, you could also delete a rule while leaving productions referencing it in place. |
While perhaps providing a safer abstraction (I think we'd have to return a With either mechanism we could perhaps use a function One of the reasons here we need to bypass validation is because we no longer have source which matches the AST, and it is also likely that spans are messed up so if we did do checking and what not and an error did occur it isn't clear how we'd do error printing (which now even relies on source availability within Both these mechanisms can bypass this problem it seems, (one by being Edit: I guess Edit 2: The impression that I get though is that it might still be better to return a |
|
Ironically all the new changes, because they rely so much on grammar sources being available within This is mostly diagnostic printing, but to some extent also So given that this is still a pretty hacky solution, I'm not sure it makes sense to go through all that effort over actually trying to do something which enables multiple start rules from a single grammar? |
|
I think that perhaps I just have to think about this differently, we'd need a function that takes both an My original attempt was based on the fact that: Anyhow at least in this case (but not arbitrary AST modifications) the original sources should be sufficient. |
This sounds plausible. Would that supersede this PR? |
Yup |
|
It sounds like it's worth giving it a go. Perhaps it will be an "unofficial" API of sorts, at least until we gain more confidence in it, but it is a feature that some people really want, so even unofficial/unstable is better than nothing (and double points if it's not |
Ahh, I had forgot about this part, I know we have some API for unofficial functions which require features to be made pub, and special "Unstable" arguments. This seems like a good opportunity to try and use those. Edit: The CTParserBuilder parts should be done now in 2be58ac For the |
|
Thanks -- I think this is a good intermediate step towards future perfection! Please squash. |
cfgrammar/src/lib/mod.rs
Outdated
| #![allow(clippy::unnecessary_wraps)] | ||
| #![allow(clippy::upper_case_acronyms)] | ||
| #![forbid(unsafe_code)] | ||
| #![deny(unsafe_code)] |
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.
Change no longer needed.
cfgrammar/src/lib/yacc/ast.rs
Outdated
|
|
||
| impl GrammarAST { | ||
| pub fn new() -> GrammarAST { | ||
| pub(crate) fn new() -> GrammarAST { |
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 change of visibility was only included in this patch accidentally, I feel like it is good to consider whether we should do this, but probably doesn't belong in this series.
So I'd rather drop it from this PR.
This attempts to improve the status quo regarding issue softdevteam#484. Previously in order to build a grammar using multiple start rules you needed multiple copies of the grammar in the filesystem, each with a different start rule. This adds a mechanism for modifying the AST by cloning and changing the start rules, then building a parser from each clone. The `CTParserBuilder` methods being unstable and require a build-dependency `feature`.
|
Squashed with the visibility modification, and |
|
I think we can mark this as undraft and merge. |
|
Sorry about that, it's no longer in draft now. |
GrammarASTGrammarAST with modified start rules.
I'm going to mark this as a draft, it isn't something I personally am likely to use,
While thinking through #584 and what uses
GrammarAST::newhas or could have,issue #484 came to mind, the current workaround described in that issue is to create multiple copies of yacc grammars
and modify the start rule.
As such cc'ing @rneswold just to see whether or not this approach is an improvement on that hack,
or if this is something which if we added it might just go unused?
The current hacks have some drawbacks like needing to disable warnings as errors because if you change the start rule, unused warnings can come up from the
top-levelstart rule.The idea of this patch is to provide an alternative hack mechanism, where you build a
GrammarAST, clone itand then modify the
startfield of each clone, in an unsafe manner. Since you only do the actual checking for the first rule, you don't run into running the checks multiple times and subsequent warnings/errors caused by checking a partial grammar with a start rule somewhere in the middle.It doesn't really fix #484 or share parsing tables or anything nice.
This may well be just a solution looking for a problem.
Also, because of the
lrlexdependency I wasn't able to fully test this within cfgrammar (building parsers/lexers and passing input), I should probably be concerned though that the whole&mut self -> &mut GrammarASTseems like it might cause borrow checker issues once we try to actually finish the parser/lexer build process with the modified grammar?