summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul McGuire <ptmcg@users.noreply.github.com>2020-07-30 22:05:48 -0500
committerPaul McGuire <ptmcg@users.noreply.github.com>2020-07-30 22:05:48 -0500
commitc1e365bfa036492222837f8c6372a3b818961823 (patch)
treea3340eec27ca72c949c0410bf76a4740577be09d
parentde7c442afb95cff6120a26a4b2c1a43bd84cecbf (diff)
downloadpyparsing-git-c1e365bfa036492222837f8c6372a3b818961823.tar.gz
Add size spec to default Word repr output
-rw-r--r--CHANGES10
-rw-r--r--pyparsing/core.py14
-rw-r--r--tests/test_unit.py40
3 files changed, 50 insertions, 14 deletions
diff --git a/CHANGES b/CHANGES
index 92b679b..d86a76c 100644
--- a/CHANGES
+++ b/CHANGES
@@ -7,6 +7,16 @@ Version 3.0.0b1
- Fixed traceback trimming, and added ParserElement.verbose_traceback
save/restore to reset_pyparsing_context().
+- Default string for Word expressions now also include indications of
+ min and max length specification, if applicable, similar to regex length
+ specifications:
+
+ Word(nums) -> "W:(0-9)"
+ Word(nums, exact=3) -> "W:(0-9){3}"
+ Word(nums, min=2) -> "W:(0-9){2,...}"
+ Word(nums, max=3) -> "W:(0-9){1,3}"
+ Word(nums, min=2, max=3) -> "W:(0-9){2,3}"
+
Version 3.0.0a2 - June, 2020
----------------------------
diff --git a/pyparsing/core.py b/pyparsing/core.py
index 108b1d8..008fb01 100644
--- a/pyparsing/core.py
+++ b/pyparsing/core.py
@@ -2273,11 +2273,21 @@ class Word(Token):
return s
if self.initCharsOrig != self.bodyCharsOrig:
- return "W:({}, {})".format(
+ base = "W:({}, {})".format(
charsAsStr(self.initCharsOrig), charsAsStr(self.bodyCharsOrig)
)
else:
- return "W:({})".format(charsAsStr(self.initCharsOrig))
+ base = "W:({})".format(charsAsStr(self.initCharsOrig))
+
+ # add length specification
+ if self.minLen > 1 or self.maxLen != _MAX_INT:
+ if self.minLen == self.maxLen:
+ return base + "{{{}}}".format(self.minLen)
+ elif self.maxLen == _MAX_INT:
+ return base + "{{{},...}}".format(self.minLen)
+ else:
+ return base + "{{{},{}}}".format(self.minLen, self.maxLen)
+ return base
def parseImpl(self, instring, loc, doActions=True):
if instring[loc] not in self.initChars:
diff --git a/tests/test_unit.py b/tests/test_unit.py
index 0a8e4d3..19258dd 100644
--- a/tests/test_unit.py
+++ b/tests/test_unit.py
@@ -2275,7 +2275,7 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase):
# arity 3 with None opExpr - should raise ValueError
with self.assertRaises(ValueError):
- expr = pp.infixNotation(num, [(None, 3, pp.opAssoc.LEFT),])
+ expr = pp.infixNotation(num, [(None, 3, pp.opAssoc.LEFT)])
# arity 3 with invalid tuple - should raise ValueError
with self.assertRaises(ValueError):
@@ -2310,7 +2310,7 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase):
num = pp.Word(pp.nums).addParseAction(pp.tokenMap(int))
for assoc in (pp.opAssoc.LEFT, pp.opAssoc.RIGHT):
expr = pp.infixNotation(
- num, [("+", 2, pp.opAssoc.LEFT), (("?", ":"), 3, assoc),]
+ num, [("+", 2, pp.opAssoc.LEFT), (("?", ":"), 3, assoc)]
)
self.assertParseAndCheckList(
expr, "3 + 2? 12: 13", [[[3, "+", 2], "?", 12, ":", 13]]
@@ -2519,7 +2519,7 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase):
expected_l = ["spam", "eggs", "suf"]
self.assertParseResultsEquals(
- result, expected_l, msg="issue with ParserElement + str",
+ result, expected_l, msg="issue with ParserElement + str"
)
# str + ParserElement
@@ -2529,7 +2529,7 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase):
expected_l = ["pre", "spam", "eggs"]
self.assertParseResultsEquals(
- result, expected_l, msg="issue with str + ParserElement",
+ result, expected_l, msg="issue with str + ParserElement"
)
# ParserElement + int
@@ -2719,7 +2719,7 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase):
expected = ["pre", "eggs"]
self.assertParseResultsEquals(
- result, expected, msg="issue with str ^ ParserElement",
+ result, expected, msg="issue with str ^ ParserElement"
)
# ParserElement ^ int
@@ -7851,9 +7851,7 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase):
# verify the list of names shown in the explain "stack"
self.assertEqual(
- expected,
- explain_str_lines[-len(expected) :],
- msg="invalid explain str",
+ expected, explain_str_lines[-len(expected) :], msg="invalid explain str"
)
# check type of raised exception matches explain output
@@ -7911,10 +7909,7 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase):
print(depth_none_explain_str)
expr_name = "pyparsing.core._WordRegex - W:(0-9)"
- for expected_function in [
- self_testcase_name,
- expr_name,
- ]:
+ for expected_function in [self_testcase_name, expr_name]:
self.assertTrue(
expected_function in depth_none_explain_str,
"{!r} not found in ParseException.explain()".format(
@@ -7937,6 +7932,27 @@ class Test2_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase):
"{!r} not found in ParseException.explain()".format(expected_function),
)
+ def testExpressionDefaultStrings(self):
+ expr = pp.Word(pp.nums)
+ print(expr)
+ self.assertEqual("W:(0-9)", repr(expr))
+
+ expr = pp.Word(pp.nums, exact=3)
+ print(expr)
+ self.assertEqual("W:(0-9){3}", repr(expr))
+
+ expr = pp.Word(pp.nums, min=2)
+ print(expr)
+ self.assertEqual("W:(0-9){2,...}", repr(expr))
+
+ expr = pp.Word(pp.nums, max=3)
+ print(expr)
+ self.assertEqual("W:(0-9){1,3}", repr(expr))
+
+ expr = pp.Word(pp.nums, min=2, max=3)
+ print(expr)
+ self.assertEqual("W:(0-9){2,3}", repr(expr))
+
class Test3_EnablePackratParsing(TestCase):
def runTest(self):