diff options
author | ptmcg <ptmcg@austin.rr.com> | 2022-05-18 23:59:30 -0500 |
---|---|---|
committer | ptmcg <ptmcg@austin.rr.com> | 2022-05-18 23:59:30 -0500 |
commit | 79fe2b54bc08c791383ca81d4701b5afdd3d7390 (patch) | |
tree | 54086b76e4f3a9b7c9792c683ebea6a2324b7305 | |
parent | dbe71461b5a56967ff0abf79ce7c8b0eddb75a66 (diff) | |
download | pyparsing-git-79fe2b54bc08c791383ca81d4701b5afdd3d7390.tar.gz |
Make expr[:ender] equivalent to expr[...:ender]
-rw-r--r-- | CHANGES | 5 | ||||
-rw-r--r-- | pyparsing/core.py | 6 | ||||
-rw-r--r-- | tests/test_unit.py | 56 |
3 files changed, 54 insertions, 13 deletions
@@ -4,13 +4,14 @@ Change Log Version 3.0.10 - (in development) --------------------------------- -- Extended `expr[]` notation to accept a slice, indicating a `stop_on` +- Extended `expr[]` notation for repetition of expr to accept a + slice, where the slice's stop value indicates a `stop_on` expression: test = "BEGIN aaa bbb ccc END" BEGIN, END = map(Keyword, "BEGIN END".split()) body_word = Word(alphas) - expr = BEGIN + Group(body_word[...: END]) + END + expr = BEGIN + Group(body_word[:END]) + END print(expr.parse_string(test).as_list()) Prints: diff --git a/pyparsing/core.py b/pyparsing/core.py index 4638d19..a806f5b 100644 --- a/pyparsing/core.py +++ b/pyparsing/core.py @@ -1651,15 +1651,17 @@ class ParserElement(ABC): stop_on = NoMatch() if isinstance(key, slice): key, stop_on = key.start, key.stop + if key is None: + key = ... stop_on_defined = True elif isinstance(key, tuple) and isinstance(key[-1], slice): key, stop_on = (key[0], key[1].start), key[1].stop stop_on_defined = True # convert single arg keys to tuples + if isinstance(key, str_type): + key = (key,) try: - if isinstance(key, str_type): - key = (key,) iter(key) except TypeError: key = (key, key) diff --git a/tests/test_unit.py b/tests/test_unit.py index c070c52..b9c8111 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -5729,13 +5729,28 @@ class Test02_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase): ) expr = BEGIN + body_word[1, ...].stopOn(ender) + END - self.assertParseAndCheckList(expr, test, test.split(), "Did not successfully stop on ending expression %r" % ender) + self.assertParseAndCheckList( + expr, + test, + test.split(), + "Did not successfully stop on ending expression %r" % ender, + ) - expr = BEGIN + body_word[1, ...: ender] + END - self.assertParseAndCheckList(expr, test, test.split(), "Did not successfully stop on ending expression %r" % ender) + expr = BEGIN + body_word[1, ...:ender] + END + self.assertParseAndCheckList( + expr, + test, + test.split(), + "Did not successfully stop on ending expression %r" % ender, + ) - expr = BEGIN + body_word[(1, ...): ender] + END - self.assertParseAndCheckList(expr, test, test.split(), "Did not successfully stop on ending expression %r" % ender) + expr = BEGIN + body_word[(1, ...):ender] + END + self.assertParseAndCheckList( + expr, + test, + test.split(), + "Did not successfully stop on ending expression %r" % ender, + ) number = pp.Word(pp.nums + ",.()").setName("number with optional commas") parser = pp.OneOrMore(pp.Word(pp.alphanums + "-/."), stopOn=number)( @@ -5755,13 +5770,36 @@ class Test02_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase): body_word = pp.Word(pp.alphas).setName("word") for ender in (END, "END", pp.CaselessKeyword("END")): expr = BEGIN + pp.ZeroOrMore(body_word, stopOn=ender) + END - self.assertParseAndCheckList(expr, test, test.split(), "Did not successfully stop on ending expression %r" % ender) + self.assertParseAndCheckList( + expr, + test, + test.split(), + "Did not successfully stop on ending expression %r" % ender, + ) expr = BEGIN + body_word[...].stopOn(ender) + END - self.assertParseAndCheckList(expr, test, test.split(), "Did not successfully stop on ending expression %r" % ender) + self.assertParseAndCheckList( + expr, + test, + test.split(), + "Did not successfully stop on ending expression %r" % ender, + ) + + expr = BEGIN + body_word[...:ender] + END + self.assertParseAndCheckList( + expr, + test, + test.split(), + "Did not successfully stop on ending expression %r" % ender, + ) - expr = BEGIN + body_word[...: ender] + END - self.assertParseAndCheckList(expr, test, test.split(), "Did not successfully stop on ending expression %r" % ender) + expr = BEGIN + body_word[:ender] + END + self.assertParseAndCheckList( + expr, + test, + test.split(), + "Did not successfully stop on ending expression %r" % ender, + ) def testNestedAsDict(self): |