-
Notifications
You must be signed in to change notification settings - Fork 33
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: All Rules are "Top-Level" Functions #484
Comments
Right now, grmtools doesn't generate functions in this way. I guess it's possible to do so and to call "into the middle of" the LR statetable. I haven't thought about that before and it might need a bit of thought about exactly what it means. In the interim, I think there is a (horrible) hack one can do: you can duplicate the grammar (including in a |
Comes to mind that with the horrible hack, is seems undoubtedly likely to produce the 'unused rule' and 'unused token' warnings/errors. You'll likely need to set at least |
@ratmice Definitely! It would be good to do something nicer here, though an interesting question is what "unused" means if you have multiple start rules. |
What if I created a grammar for each portion and then a grammar that called the subsets? I'd have to know where the previous parsing ended to feed the next one. I'd be nicer to have it all in one module, but this might be doable... |
I would delete the unused rules in the "horrible hack". However, I was hoping to use the same lex file, so the "unused token" warnings would be a problem. |
Interesting question, my inclination would be to define I'm assuming this is considering some sort of feature that lifts the (current) restriction that there is a single start rule, So for the purposes of checking unused rules the
I don't think it would be hard to to modify the |
This is nice because I really don't need every rule to be top-level. Out of the 1/2 dozen sections of my string, I really only need 3 field parsers. But it might be too complicated to have some rules be top-level callable and others purely internal. Also, each of these |
Ahh, yeah there are definitely complexities with this multiple start rules idea, that is one I hadn't considered. IIRC anyways, though I couldn't remember exactly where to look off-hand to verify this 0 index rule in the moment. |
Is there a way to import .y files in other .y files? Nvm I saw #110 |
Composition in a general sense is a very hard problem. This issue (relative to my memory of #110) is more limited: it's asking to subset an existing grammar. My intuition is that subsets are always OK -- we just don't happen to support taking advantage of that right now. For example, if we generated Rust code directly, specifically one Rust function per rule, I suspect this would fall out of the hat. Doing so isn't rocket science (I think it's what e.g. LALRPOP does), but someone has to put in the hard yards. |
I think what I'll do is use an enumeration. Something like:
The grammar can recognize when a subset is specified and return the smaller-scoped enum values. Then I can make some simple wrapper functions:
Thanks for the discussion! |
This is a great project and I've used it in a handful of Rust applications. Thanks!
There is one feature from the OCaml tools (
ocamllex
andocamlyacc
) have that I found convenient. In the code thatocamlyacc
generates, all the grammar rules create functions you can call (if you need to parse a subset of the grammar.)For instance, I'm working on a project that uses a string to describe data acquisition. There's a "name" field, optional range specification, option field names, and a field for the event on which to sample. All of that is straightforward to implement. However, we also have an API which would like just the event portion and another API that just wants the device name.
We could have a struct with a bunch of
Option
fields, but I'd rather be able to haveEvent
as a parameter to a function.If this feature would be too disruptive, I wonder how others would solve this with the current tools.
The text was updated successfully, but these errors were encountered: