diff options
author | Marcin Jaworski <mrjaworski@gmail.com> | 2020-05-03 19:15:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-03 12:15:23 -0500 |
commit | 827f615719ae83800e4252593da5b9d05a97e11a (patch) | |
tree | c43a0088378fb0bf2b0381788394f21461949701 | |
parent | 39934574db9796f27397b9f76a112c2466bd1a69 (diff) | |
download | pyparsing-git-827f615719ae83800e4252593da5b9d05a97e11a.tar.gz |
Pop counter token and return rest instead of dropping all tokens in countedArray (#209)
* Pop counter token and return rest instead of dropping all tokens
* Include only named results from intExpr in countedArray results
* Remove internal Group from countedArray
* Fix operator precedence
* Update countedArray tests
-rw-r--r-- | pyparsing/helpers.py | 5 | ||||
-rw-r--r-- | tests/test_simple_unit.py | 2 | ||||
-rw-r--r-- | tests/test_unit.py | 12 |
3 files changed, 10 insertions, 9 deletions
diff --git a/pyparsing/helpers.py b/pyparsing/helpers.py index d710d78..97e5943 100644 --- a/pyparsing/helpers.py +++ b/pyparsing/helpers.py @@ -55,8 +55,9 @@ def countedArray(expr, intExpr=None): def countFieldParseAction(s, l, t): n = t[0] - arrayExpr << (n and Group(And([expr] * n)) or Group(empty)) - return [] + arrayExpr << (And([expr] * n) if n else empty) + # clear list contents, but keep any named results + del t[:] if intExpr is None: intExpr = Word(nums).setParseAction(lambda t: int(t[0])) diff --git a/tests/test_simple_unit.py b/tests/test_simple_unit.py index 41c4b25..15edc33 100644 --- a/tests/test_simple_unit.py +++ b/tests/test_simple_unit.py @@ -503,7 +503,7 @@ class TestCommonHelperExpressions(PyparsingExpressionTestCase): ), PpTestSpec( desc="A counted array of words", - expr=pp.countedArray(pp.Word("ab"))[...], + expr=pp.Group(pp.countedArray(pp.Word("ab")))[...], text="2 aaa bbb 0 3 abab bbaa abbab", expected_list=[["aaa", "bbb"], [], ["abab", "bbaa", "abbab"]], ), diff --git a/tests/test_unit.py b/tests/test_unit.py index 9f27375..d774fc6 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -2963,14 +2963,14 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase): print("got maximum excursion limit exception") def testCountedArray(self): - from pyparsing import Word, nums, OneOrMore, countedArray + from pyparsing import Word, nums, OneOrMore, Group, countedArray testString = "2 5 7 6 0 1 2 3 4 5 0 3 5 4 3" integer = Word(nums).setParseAction(lambda t: int(t[0])) countedField = countedArray(integer) - r = OneOrMore(countedField).parseString(testString) + r = OneOrMore(Group(countedField)).parseString(testString) print(testString) print(r) @@ -2980,7 +2980,7 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase): # addresses bug raised by Ralf Vosseler def testCountedArrayTest2(self): - from pyparsing import Word, nums, OneOrMore, countedArray + from pyparsing import Word, nums, OneOrMore, Group, countedArray testString = "2 5 7 6 0 1 2 3 4 5 0 3 5 4 3" @@ -2988,7 +2988,7 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase): countedField = countedArray(integer) dummy = Word("A") - r = OneOrMore(dummy ^ countedField).parseString(testString) + r = OneOrMore(Group(dummy ^ countedField)).parseString(testString) print(testString) print(r) @@ -2997,7 +2997,7 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase): ) def testCountedArrayTest3(self): - from pyparsing import Word, nums, OneOrMore, countedArray, alphas + from pyparsing import Word, nums, OneOrMore, Group, countedArray, alphas int_chars = "_" + alphas array_counter = Word(int_chars).setParseAction(lambda t: int_chars.index(t[0])) @@ -3008,7 +3008,7 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase): integer = Word(nums).setParseAction(lambda t: int(t[0])) countedField = countedArray(integer, intExpr=array_counter) - r = OneOrMore(countedField).parseString(testString) + r = OneOrMore(Group(countedField)).parseString(testString) print(testString) print(r) |