-
Notifications
You must be signed in to change notification settings - Fork 95
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
Choice with multiple parser types #54
Comments
I am guessing you have some sort of expression type which is represented as an enum like
If so you can just use the map parser to make them return the the Expr type
Unfortunately you will encounter another problem when you do this as each parser needs to have the same type as well as they are stored in an array which necessitates that all elements have the same type. For small number of alternations the or which allows the parsers to have distinct types as long as they all have the same return type. For the choice parser it is however possible to cast each of the parser to a trait object which makes sure that they are all the same type.
You can see exactly how I do it in the parser I made for embed_lang here though there is some extra complexity there as well to inject location in the expression as well. |
Thank you for the detailed reply. As for me, this issue can now be closed. |
It doesn't seem possible to use choice with different types: consider an assignment of a variable:
a = b
.I would express this with
(env.identifier(), env.symbol("="), choice([env.integer(), env.identifier()])
, but the compiler complains that it expects ani64
instead of aString
.How can I express this without writing two separate parsers?
The text was updated successfully, but these errors were encountered: