Created
May 5, 2010 12:00
-
-
Save messa/390687 to your computer and use it in GitHub Desktop.
lepl (Python parsing module) example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.6 | |
from lepl import * | |
retezec = String() | String("'") | |
cislo = Digit()[1:] > (lambda chars: int("".join(chars))) | |
slovnik = Delayed() | |
klicSlovniku = retezec | cislo | |
hodnotaSlovniku = retezec | cislo | slovnik | |
polozkaSlovniku = klicSlovniku / ~Literal(":") / ~Whitespace() / hodnotaSlovniku > (lambda item: item) | |
obsahSlovniku = polozkaSlovniku / ZeroOrMore(Literal(",") / polozkaSlovniku) | |
slovnik += ~Literal("{") / Optional(obsahSlovniku) / ~Literal("}") > (lambda items: dict(items)) | |
dokument = slovnik / Eos() | |
assert dokument.parse("""{"foo": "bar"}""")[0] == {"foo": "bar"} | |
assert dokument.parse("""{'foo': {'bar': 42}}""")[0] == {"foo": {"bar": 42}} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment