Comments (4)
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.
from parsy.
To be clear, help on the above would be appreciated, I'm winging this.
from parsy.
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
from parsy.
I completely forgot I ran into this exact issue and did it again.
I think the problem is that wrapping a yield in a try/except doesn't catch the error because it's not actually "running" in the context where the try/except is. It's returning a value which gets run somewhere else.
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.
from parsy.
Related Issues (20)
- >>= for bind HOT 14
- Missing documentation for eof parser? HOT 1
- Improve debugging: peek show next data in errors HOT 5
- Recompute line number for ParseError passed up from .bind
- Improve debugging ergonomics HOT 3
- Inline (explicit) and implicit tracing
- Missing seq import statement in tutorial HOT 1
- Help with parsing that "hangs" HOT 3
- Bug with backtracking and generate? HOT 4
- Parsy 1.3.0 fails to support 'group' keyword of `regex` function HOT 1
- async/await support in generate() HOT 6
- Release HOT 2
- Interested in a version of parsy with type annotations? HOT 3
- Allow providing a default to optional() HOT 2
- maintaining state while parsing HOT 3
- combine fails when nothing is produced by many HOT 2
- [bug] Parser.desc() causes loss of error information, simple fix HOT 2
- alt doesn't use fallback parsers when initial parser has .many()/.sep_by() HOT 1
- Processing list of tokens HOT 1
- Seeking Guidance on Implementing Parser Autocomplete HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from parsy.