-
Notifications
You must be signed in to change notification settings - Fork 38
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
Strange peek behaviour/generator exception handling? #34
Comments
Edit: So to restate this in a clearer manner: As far as I can tell, python doesn't let you catch exceptions raised from inside a generator, because they are converted into stopping the generator. All the stackoverflow posts I've looked at so far suggest yielding an exception object from a generator instead of having it raised, which I don't really like, and might require rearchitecting parsy? I'm going to try to see if I can write a wrapper function that will catch exceptions and return them in this form, so at least it can be done on a case by case basis, but will look less nice. |
To be clear, help on the above would be appreciated, I'm winging this. |
If the above is true I'm not quite sure I get how the normal case works, but being unable to figure out how to implement something better, I found a workaround: I think I've temporarily worked around this by modifying the peek implementation to signal the end of the stream with None (I should probably create a more descriptive symbol instead); def peek_with_none(parser): #To work around the cant raise exception from yield issue
@Parser
def peek_parser(stream, index):
if index == len(stream):
#bail early instead of raising a ParseError, or something
return Result.success(index, None)
result = parser(stream, index)
if result.status:
return Result.success(index, result.value)
else:
return result
return peek_parser |
I completely forgot I ran into this exact issue and did it again. I haven't actually figured out how to make this work but it probably involves writing a @parser. I worked around my issue temporarily by using a peek that wouldnt fail and .map()-ing a conditional function over the result to check if continuing is ok. |
I've been staring at this for two days and haven't been able to figure it out:I'm pretty sure at this point the problem is as described in the later paragraphs, and I rubber-ducked myself.Disregarding my weird function naming, why does
block_lex_transformer
keep trying to parse past the end of the input? I figured peek() would yield a ParseError if I tried to do that, but it doesn't seem to be the case?peek(any_char).parse_partial("")
gives a ParseError.I don't know much about how coroutines/generators work, my suspicions are that this isn't working because a GeneratorExit exception seems to come from
yield peek(seq(sideToken, line(id)))
, but I don't know how to deal with it.I think that happens because googling suggests an exception in a generator will stop the generator, I didn't immediately see anything explaining how to have it continue and catch the exception?
The text was updated successfully, but these errors were encountered: