Comments (3)
BTW, you can make your code more succinct like this:
level_normal = regex(r'[0-4]?[0-9]')
level_specials = regex(r'(66|77|88)')
level_num = (level_specials | level_normal).desc(parse_level_docstring)
# OR:
level_num = alt(level_specials, level_normal).desc(parse_level_docstring)
(That is, for your case you don't need the generate
calls).
from parsy.
Yes, this is correct, and the documented behaviour - http://parsy.readthedocs.io/en/latest/ref/methods_and_combinators.html#parsy.alt - the parsers are done in order, and it moves to the next only if the first fails, therefore alt(A, B) != alt(B, A)
In your case, level_normal
succeeds at parsing (some of) 88
:
>>> re.search(r'[0-4]?[0-9]', '88')
<_sre.SRE_Match object; span=(0, 1), match='8'>
Or:
>>> level_num.parse_partial('88')
('8', '8')
This means that level_specials
is not tried in the first case, and so parsing of the whole thing fails with "EOF expected" if you just call parse
.
There is also a section in the tutorial about alternatives and backtracking - http://parsy.readthedocs.io/en/latest/tutorial.html#alternatives-and-backtracking
I will see if I can make the docs for alt
a bit clearer on this front.
from parsy.
OK, that is clearly documented and explained on http://parsy.readthedocs.io/en/latest/tutorial.html#alternatives-and-backtracking
As I am combining parsers the way I would build a regexp, it's still a bit counter-intuitive to me.
Thank you @spookylukey
from parsy.
Related Issues (20)
- New release? HOT 2
- Weird bind implementation? HOT 7
- Strange peek behaviour/generator exception handling? HOT 4
- Tagged unions/product types for results HOT 11
- >>= 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
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.