Git Product home page Git Product logo

Comments (3)

spookylukey avatar spookylukey commented on July 18, 2024 1

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.

spookylukey avatar spookylukey commented on July 18, 2024

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.

thlabbe avatar thlabbe commented on July 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.