From 203fa36d7ae6b79344e4bf13531b77c09f313793 Mon Sep 17 00:00:00 2001 From: ptmcg Date: Sun, 26 Apr 2020 10:33:12 -0500 Subject: change some lambdas to explicit methods for clarity (see discussion in #207); deleted duplicated examples (commit *all* changes this time) --- examples/stackish.py | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) (limited to 'examples/stackish.py') 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 + """ +) -- cgit v1.2.1