diff options
author | ptmcg <ptmcg@austin.rr.com> | 2020-04-26 10:33:12 -0500 |
---|---|---|
committer | ptmcg <ptmcg@austin.rr.com> | 2020-04-26 10:33:12 -0500 |
commit | 203fa36d7ae6b79344e4bf13531b77c09f313793 (patch) | |
tree | 443459f498f38b97618344c6f707eeaa117cf670 /examples/stackish.py | |
parent | 813ba3bed433a96e02d82cad2e2940a6850d96a5 (diff) | |
download | pyparsing-git-203fa36d7ae6b79344e4bf13531b77c09f313793.tar.gz |
change some lambdas to explicit methods for clarity (see discussion in #207); deleted duplicated examples (commit *all* changes this time)
Diffstat (limited to 'examples/stackish.py')
-rw-r--r-- | examples/stackish.py | 35 |
1 files changed, 15 insertions, 20 deletions
diff --git a/examples/stackish.py b/examples/stackish.py index f02baf3..436a9ea 100644 --- a/examples/stackish.py +++ b/examples/stackish.py @@ -42,15 +42,14 @@ from pyparsing import ( Group,
ZeroOrMore,
srange,
+ pyparsing_common as ppc,
)
MARK, UNMARK, AT, COLON, QUOTE = map(Suppress, "[]@:'")
-NUMBER = Word(nums)
-NUMBER.setParseAction(lambda t: int(t[0]))
-FLOAT = Combine(oneOf("+ -") + Word(nums) + "." + Optional(Word(nums)))
-FLOAT.setParseAction(lambda t: float(t[0]))
-STRING = QuotedString('"', multiline=True)
+NUMBER = ppc.integer()
+FLOAT = ppc.real()
+STRING = QuotedString('"', multiline=True) | QuotedString("'", multiline=True)
WORD = Word(alphas, alphanums + "_:")
ATTRIBUTE = Combine(AT + WORD)
@@ -87,18 +86,14 @@ GROUP = ( )
+ (WORD("name") | UNMARK)
).setParseAction(assignUsing("name"))
-item << (NUMBER | FLOAT | STRING | BLOB | GROUP)
-
-tests = """\
-[ '10:1234567890' @name 25 @age +0.45 @percentage person:zed
-[ [ "hello" 1 child root
-[ "child" [ 200 '4:like' "I" "hello" things root
-[ [ "data" [ 2 1 ] @numbers child root
-[ [ 1 2 3 ] @test 4 5 6 root
-""".splitlines()
-
-for test in tests:
- if test:
- print(test)
- print(item.parseString(test).dump())
- print()
+item <<= FLOAT | NUMBER | STRING | BLOB | GROUP
+
+item.runTests(
+ """\
+ [ '10:1234567890' @name 25 @age +0.45 @percentage person:zed
+ [ [ "hello" 1 child root
+ [ "child" [ 200 '4:like' "I" "hello" things root
+ [ [ "data" [ 2 1 ] @numbers child root
+ [ [ 1 2 3 ] @test 4 5 6 root
+ """
+)
|