-
Notifications
You must be signed in to change notification settings - Fork 131
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
err
Not Working as (I) Expected
#29
Comments
Indeed, It doesn't always matter what See #25 (comment) for more reading. As a workaround, as shown in @dcsobral's answer, you can manually consume whitespace before // just call ws(someParser) to have it handle whitespace
private def ws[T](p: Parser[T]): Parser[T] = new Parser[T] {
def apply(in: Input) = {
val offset = in.offset
val start = handleWhiteSpace(in.source, offset)
p(in.drop (start - offset))
}
}
override def err(msg: String) = ws(super.err(msg))
...
lazy val verb = word("verb") | err("not a verb!") // will now do the right thing |
This overrides `err` in RegexParser to make it consume whitespace just like regex and literal. The original motivation was: object parser extends RegexParsers { def num = "\\d+".r def twoNums = num ~ (num | err("error!")) } // succeeds parser.parseAll(twoNums, "42 721") // fails with a parsing Failure instead of an Error // because err doesn't consume the whitespace but the regex does. parser.parseAll(twoNums, "42 foo") This may change the output of some parsers that failed to parse input (from a Failure to an Error). Fixes scala#29
I'm reopening this since the fix has been reverted. See #41 for more info. |
Hmm... If the fix was reverted because of binary incompatibility, re-applying it for I ask because this seems to be the only issue blocking a |
@gourlaysama Does this require more than a reapplication of the previous patch? |
Moved the milestone for this one, as |
In the following Parser:
It will parse noun verb object.
But, when entering a valid noun, but an invalid verb, why won't the err("not a verb!") return an Error with that particular error message?
credit: Thanks to Travis Brown for explaining the need for the word function here.
@dcsobral provides a thorough explanation here - http://stackoverflow.com/questions/25147833/using-err-in-a-child-parser as to why this behavior is occurring.
I don't completely understand his answer (re-reading it a few more times), but shouldn't
err(...
in my above example result in that particularError
?The text was updated successfully, but these errors were encountered: