-
Notifications
You must be signed in to change notification settings - Fork 51
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
Add expect
combinator
#51
Comments
So one option is to use label to wrap the whole construct and you should
get better error messages.
If that doesn’t work, I believe expect can be implemented with
optional(lookahead_not(combinator) |>
post_traverse({mfa_that_returns_error_reason}).
My suggestion would be to add an error(reason) combinator, which should be
quite straightforward. A PR is appreciated!
--
*José Valim*
www.plataformatec.com.br
Skype: jv.ptec
Founder and Director of R&D
|
To clarify, the error combinator is just a shortcut to
post_traverse(mfa_that_fails_with_error_reason).
--
*José Valim*
www.plataformatec.com.br
Skype: jv.ptec
Founder and Director of R&D
|
Isn't that the |
I think we don’t have a fail combinator?
But I see your point, returning error will just cause the whole choice to
fail.
Maybe you can implement it like this:
choice([lookahead(combinator)]) |> label(...)
This what if the lookahead fails, the whole choice will fail with the given
label.
--
*José Valimwww.plataformatec.com.br
<http://www.plataformatec.com.br/>Founder and Director of R&D*
|
Closing this for now. Please send a PR if it is still desired! |
I'd like to re-open this issue (although I still don't have time to fully understand the nimble_parsec internals and write it myself): I have written a sigil to handle units of length. It should parse (among other things) sums of numbers follow by valid units. For example, it should parse
The above is great! The user gets proper feedback on which units to use thanks to the label combinator. However, if the expression is more complicated:
The above error message is not good. Nimble parsec tries a number of combinators in turn, and the last thing it attempts to match is a number followed by a valid unit followed by the end of the string. But what I really wanted was for the error to be at after the |
As discussed here, NimbleParsec should have an
expect
combinator for better error reporting. I suggest something like this:expect(previous \\ [], expected)
. This combinator would raise an error isexpected
didn't match.My use case: I'm currently working on a parser for the ICU Message Format. Currently, NimbleParsec backtracks so much that I get rather useless error messages. Suppose we have something like this:
The above is invalid, because
abcd
is not a supported option for theplural
argument type. I already know it won't be supported as soon as I parse{variable, plural,
, and I'd like to emit an error as soon as I find theabcd
option. But currently there is no way to do this... NimbleParsec will just backtrack and fail with an error message that doesn't really help anyone (it actually says it expects an end of string on character 0...).An
expect
combinator would allow me to emit the correct error message.The text was updated successfully, but these errors were encountered: