summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorptmcg <ptmcg@austin.rr.com>2022-06-24 03:22:57 -0500
committerptmcg <ptmcg@austin.rr.com>2022-06-24 03:22:57 -0500
commitd6a9a77401f8402a6aad790b3b98f12eb2dd0a9d (patch)
tree7ea7b3a8cf11226ab16cbb5214ea978df51c8cdf
parent18abf0727deb9b1a4f93f2747345d4e9ea0e8d69 (diff)
downloadpyparsing-git-d6a9a77401f8402a6aad790b3b98f12eb2dd0a9d.tar.gz
Remove assignment to __class__ in Word, remove internal _WordRegex class
-rw-r--r--pyparsing/core.py15
-rw-r--r--pyparsing/exceptions.py2
-rw-r--r--tests/test_unit.py12
3 files changed, 15 insertions, 14 deletions
diff --git a/pyparsing/core.py b/pyparsing/core.py
index e34cfee..11f7368 100644
--- a/pyparsing/core.py
+++ b/pyparsing/core.py
@@ -2868,7 +2868,7 @@ class Word(Token):
self.re = None
else:
self.re_match = self.re.match
- self.__class__ = _WordRegex
+ self.parseImpl = self.parseImpl_regex
def _generateDefaultName(self) -> str:
def charsAsStr(s):
@@ -2929,9 +2929,7 @@ class Word(Token):
return loc, instring[start:loc]
-
-class _WordRegex(Word):
- def parseImpl(self, instring, loc, doActions=True):
+ def parseImpl_regex(self, instring, loc, doActions=True):
result = self.re_match(instring, loc)
if not result:
raise ParseException(instring, loc, self.errmsg, self)
@@ -2940,7 +2938,7 @@ class _WordRegex(Word):
return loc, result.group()
-class Char(_WordRegex):
+class Char(Word):
"""A short-cut class for defining :class:`Word` ``(characters, exact=1)``,
when defining a match of any single character in a string of
characters.
@@ -2958,13 +2956,8 @@ class Char(_WordRegex):
asKeyword = asKeyword or as_keyword
excludeChars = excludeChars or exclude_chars
super().__init__(
- charset, exact=1, asKeyword=asKeyword, excludeChars=excludeChars
+ charset, exact=1, as_keyword=asKeyword, exclude_chars=excludeChars
)
- self.reString = f"[{_collapse_string_to_ranges(self.initChars)}]"
- if asKeyword:
- self.reString = rf"\b{self.reString}\b"
- self.re = re.compile(self.reString)
- self.re_match = self.re.match
class Regex(Token):
diff --git a/pyparsing/exceptions.py b/pyparsing/exceptions.py
index ed88ad2..b0694c3 100644
--- a/pyparsing/exceptions.py
+++ b/pyparsing/exceptions.py
@@ -80,7 +80,7 @@ class ParseBaseException(Exception):
f_self = frm.f_locals.get("self", None)
if isinstance(f_self, ParserElement):
- if frm.f_code.co_name not in ("parseImpl", "_parseNoCache"):
+ if not frm.f_code.co_name.startswith(("parseImpl", "_parseNoCache")):
continue
if id(f_self) in seen:
continue
diff --git a/tests/test_unit.py b/tests/test_unit.py
index 370c2cf..b7a23d0 100644
--- a/tests/test_unit.py
+++ b/tests/test_unit.py
@@ -4860,6 +4860,10 @@ class Test02_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase):
result2, expected2, msg="issue with Char asKeyword=True parsing 2 chars"
)
+ def testCharRe(self):
+ expr = pp.Char("ABCDEFG")
+ self.assertEqual("[A-G]", expr.reString)
+
def testCharsNotIn(self):
"""test CharsNotIn initialized with various arguments"""
@@ -9229,7 +9233,7 @@ class Test02_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase):
expected = [
self_testcase_name,
- "pyparsing.core._WordRegex - integer",
+ "pyparsing.core.Word - integer",
"tests.test_unit.Modifier",
"pyparsing.results.ParseResults",
]
@@ -9274,8 +9278,12 @@ class Test02_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase):
depth_0_explain_str = pe.explain(depth=0)
depth_1_explain_str = pe.explain(depth=1)
print(depth_none_explain_str)
+ print()
+ print(depth_0_explain_str)
+ print()
+ print(depth_1_explain_str)
- expr_name = "pyparsing.core._WordRegex - W:(0-9)"
+ expr_name = "pyparsing.core.Word - W:(0-9)"
for expected_function in [self_testcase_name, expr_name]:
self.assertTrue(
expected_function in depth_none_explain_str,