diff options
author | ptmcg <ptmcg@austin.rr.com> | 2021-11-10 01:15:28 -0600 |
---|---|---|
committer | ptmcg <ptmcg@austin.rr.com> | 2021-11-10 01:15:28 -0600 |
commit | 22f88473274bdc386edd2a83e3ba35d32d081b1c (patch) | |
tree | 128136caa676587c6bd7e59f848febf510c087b8 /pyparsing/core.py | |
parent | ebd99e2e47a85fe4d7c494d4bda54823c24aaf69 (diff) | |
download | pyparsing-git-22f88473274bdc386edd2a83e3ba35d32d081b1c.tar.gz |
Add debug arg to scan_string, transform_string, search_string
Diffstat (limited to 'pyparsing/core.py')
-rw-r--r-- | pyparsing/core.py | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/pyparsing/core.py b/pyparsing/core.py index 9aa068d..cec7c01 100644 --- a/pyparsing/core.py +++ b/pyparsing/core.py @@ -1117,6 +1117,7 @@ class ParserElement(ABC): max_matches: int = _MAX_INT, overlap: bool = False, *, + debug: bool = False, maxMatches: int = _MAX_INT, ) -> Generator[Tuple[ParseResults, int, int], None, None]: """ @@ -1173,6 +1174,14 @@ class ParserElement(ABC): else: if nextLoc > loc: matches += 1 + if debug: + print( + { + "tokens": tokens.asList(), + "start": preloc, + "end": nextLoc, + } + ) yield tokens, preloc, nextLoc if overlap: nextloc = preparseFn(instring, loc) @@ -1191,7 +1200,7 @@ class ParserElement(ABC): # catch and re-raise exception from here, clears out pyparsing internal stack trace raise exc.with_traceback(None) - def transform_string(self, instring: str) -> str: + def transform_string(self, instring: str, *, debug: bool = False) -> str: """ Extension to :class:`scan_string`, to modify matching text with modified tokens that may be returned from a parse action. To use ``transform_string``, define a grammar and @@ -1217,7 +1226,7 @@ class ParserElement(ABC): # keep string locs straight between transform_string and scan_string self.keepTabs = True try: - for t, s, e in self.scan_string(instring): + for t, s, e in self.scan_string(instring, debug=debug): out.append(instring[lastE:s]) if t: if isinstance(t, ParseResults): @@ -1238,7 +1247,12 @@ class ParserElement(ABC): raise exc.with_traceback(None) def search_string( - self, instring: str, max_matches: int = _MAX_INT, *, maxMatches: int = _MAX_INT + self, + instring: str, + max_matches: int = _MAX_INT, + *, + debug: bool = False, + maxMatches: int = _MAX_INT, ) -> ParseResults: """ Another extension to :class:`scan_string`, simplifying the access to the tokens found @@ -1263,7 +1277,7 @@ class ParserElement(ABC): maxMatches = min(maxMatches, max_matches) try: return ParseResults( - [t for t, s, e in self.scan_string(instring, maxMatches)] + [t for t, s, e in self.scan_string(instring, maxMatches, debug=debug)] ) except ParseBaseException as exc: if ParserElement.verbose_stacktrace: |