Skip to content

Commit

Permalink
Some code cleanup, replacing map() calls with list comprehensions; be…
Browse files Browse the repository at this point in the history
…tter typing for debug actions using typing.NamedTuple; using list comps in "".join calls
  • Loading branch information
ptmcg committed Jan 2, 2022
1 parent 5771459 commit 30bd0f3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 34 deletions.
2 changes: 1 addition & 1 deletion pyparsing/__init__.py
Expand Up @@ -126,7 +126,7 @@ def __repr__(self):


__version_info__ = version_info(3, 0, 7, "final", 0)
__version_time__ = "18 Dec 2021 23:16 UTC"
__version_time__ = "02 Jan 2022 22:04 UTC"
__version__ = __version_info__.__version__
__versionTime__ = __version_time__
__author__ = "Paul McGuire <ptmcg.gm+pyparsing@gmail.com>"
Expand Down
71 changes: 41 additions & 30 deletions pyparsing/core.py
Expand Up @@ -5,6 +5,7 @@
from typing import (
Optional as OptionalType,
Iterable as IterableType,
NamedTuple,
Union,
Callable,
Any,
Expand Down Expand Up @@ -243,7 +244,7 @@ def _should_enable_warnings(
nums = "0123456789"
hexnums = nums + "ABCDEFabcdef"
alphanums = alphas + nums
printables = "".join(c for c in string.printable if c not in string.whitespace)
printables = "".join([c for c in string.printable if c not in string.whitespace])

_trim_arity_call_line = None

Expand Down Expand Up @@ -438,6 +439,11 @@ def inline_literals_using(cls: type) -> None:
"""
ParserElement._literalStringClass = cls

class DebugActions(NamedTuple):
debug_try: OptionalType[DebugStartAction]
debug_match: OptionalType[DebugSuccessAction]
debug_fail: OptionalType[DebugExceptionAction]

def __init__(self, savelist: bool = False):
self.parseAction: List[ParseAction] = list()
self.failAction: OptionalType[ParseFailAction] = None
Expand All @@ -460,16 +466,12 @@ def __init__(self, savelist: bool = False):
# mark results names as modal (report only last) or cumulative (list all)
self.modalResults = True
# custom debug actions
self.debugActions: Tuple[
OptionalType[DebugStartAction],
OptionalType[DebugSuccessAction],
OptionalType[DebugExceptionAction],
] = (None, None, None)
self.debugActions = self.DebugActions(None, None, None)
self.re = None
# avoid redundant calls to preParse
self.callPreparse = True
self.callDuringTry = False
self.suppress_warnings_ = []
self.suppress_warnings_: List[Diagnostics] = []

def suppress_warning(self, warning_type: Diagnostics) -> "ParserElement":
"""
Expand Down Expand Up @@ -663,7 +665,7 @@ def is_valid_date(toks):
else:
if not all(callable(fn) for fn in fns):
raise TypeError("parse actions must be callable")
self.parseAction = list(map(_trim_arity, list(fns)))
self.parseAction = [_trim_arity(fn) for fn in fns]
self.callDuringTry = kwargs.get(
"call_during_try", kwargs.get("callDuringTry", False)
)
Expand All @@ -675,7 +677,7 @@ def add_parse_action(self, *fns: ParseAction, **kwargs) -> "ParserElement":
See examples in :class:`copy`.
"""
self.parseAction += list(map(_trim_arity, list(fns)))
self.parseAction += [_trim_arity(fn) for fn in fns]
self.callDuringTry = self.callDuringTry or kwargs.get(
"call_during_try", kwargs.get("callDuringTry", False)
)
Expand Down Expand Up @@ -779,8 +781,8 @@ def _parseNoCache(
else:
pre_loc = loc
tokens_start = pre_loc
if self.debugActions[TRY]:
self.debugActions[TRY](instring, tokens_start, self)
if self.debugActions.debug_try:
self.debugActions.debug_try(instring, tokens_start, self, False)
if self.mayIndexError or pre_loc >= len_instring:
try:
loc, tokens = self.parseImpl(instring, pre_loc, doActions)
Expand All @@ -790,8 +792,10 @@ def _parseNoCache(
loc, tokens = self.parseImpl(instring, pre_loc, doActions)
except Exception as err:
# print("Exception raised:", err)
if self.debugActions[FAIL]:
self.debugActions[FAIL](instring, tokens_start, self, err)
if self.debugActions.debug_fail:
self.debugActions.debug_fail(
instring, tokens_start, self, err, False
)
if self.failAction:
self.failAction(instring, tokens_start, self, err)
raise
Expand Down Expand Up @@ -834,8 +838,10 @@ def _parseNoCache(
)
except Exception as err:
# print "Exception raised in user parse action:", err
if self.debugActions[FAIL]:
self.debugActions[FAIL](instring, tokens_start, self, err)
if self.debugActions.debug_fail:
self.debugActions.debug_fail(
instring, tokens_start, self, err, False
)
raise
else:
for fn in self.parseAction:
Expand All @@ -855,8 +861,10 @@ def _parseNoCache(
)
if debugging:
# print("Matched", self, "->", ret_tokens.as_list())
if self.debugActions[MATCH]:
self.debugActions[MATCH](instring, tokens_start, loc, self, ret_tokens)
if self.debugActions.debug_match:
self.debugActions.debug_match(
instring, tokens_start, loc, self, ret_tokens, False
)

return loc, ret_tokens

Expand Down Expand Up @@ -913,25 +921,25 @@ def _parseCache(
return value
else:
ParserElement.packrat_cache_stats[HIT] += 1
if self.debug and self.debugActions[TRY]:
if self.debug and self.debugActions.debug_try:
try:
self.debugActions[TRY](instring, loc, self, cache_hit=True)
self.debugActions.debug_try(instring, loc, self, cache_hit=True)
except TypeError:
pass
if isinstance(value, Exception):
if self.debug and self.debugActions[FAIL]:
if self.debug and self.debugActions.debug_fail:
try:
self.debugActions[FAIL](
self.debugActions.debug_fail(
instring, loc, self, value, cache_hit=True
)
except TypeError:
pass
raise value

loc_, result, endloc = value[0], value[1].copy(), value[2]
if self.debug and self.debugActions[MATCH]:
if self.debug and self.debugActions.debug_match:
try:
self.debugActions[MATCH](
self.debugActions.debug_match(
instring, loc_, endloc, self, result, cache_hit=True
)
except TypeError:
Expand Down Expand Up @@ -1236,7 +1244,7 @@ def transform_string(self, instring: str, *, debug: bool = False) -> str:
Now Is The Winter Of Our Discontent Made Glorious Summer By This Sun Of York.
"""
out = []
out: List[str] = []
lastE = 0
# force preservation of <TAB>s, to minimize unwanted transformation of string, and to
# keep string locs straight between transform_string and scan_string
Expand All @@ -1248,13 +1256,13 @@ def transform_string(self, instring: str, *, debug: bool = False) -> str:
if isinstance(t, ParseResults):
out += t.as_list()
elif isinstance(t, Iterable) and not isinstance(t, str_type):
out += list(t)
out.extend(t)
else:
out.append(t)
lastE = e
out.append(instring[lastE:])
out = [o for o in out if o]
return "".join(map(str, _flatten(out)))
return "".join([str(s) for s in _flatten(out)])
except ParseBaseException as exc:
if ParserElement.verbose_stacktrace:
raise
Expand Down Expand Up @@ -1759,7 +1767,7 @@ def set_debug_actions(
- ``exception_action`` - method to be called when expression fails to parse;
should have the signature ``fn(input_string: str, location: int, expression: ParserElement, exception: Exception, cache_hit: bool)``
"""
self.debugActions = (
self.debugActions = self.DebugActions(
start_action or _default_start_debug_action,
success_action or _default_success_debug_action,
exception_action or _default_exception_debug_action,
Expand Down Expand Up @@ -2052,7 +2060,8 @@ def run_tests(
failureTests = failureTests or failure_tests
postParse = postParse or post_parse
if isinstance(tests, str_type):
tests = list(map(type(tests).strip, tests.rstrip().splitlines()))
line_strip = type(tests).strip
tests = [line_strip(test_line) for test_line in tests.rstrip().splitlines()]
if isinstance(comment, str_type):
comment = Literal(comment)
if file is None:
Expand All @@ -2067,7 +2076,9 @@ def run_tests(
BOM = "\ufeff"
for t in tests:
if comment is not None and comment.matches(t, False) or comments and not t:
comments.append(pyparsing_test.with_line_numbers(t) if with_line_numbers else t)
comments.append(
pyparsing_test.with_line_numbers(t) if with_line_numbers else t
)
continue
if not t:
continue
Expand Down Expand Up @@ -4279,7 +4290,7 @@ def parseImpl(self, instring, loc, doActions=True):
raise max_fatal

if tmpReqd:
missing = ", ".join(str(e) for e in tmpReqd)
missing = ", ".join([str(e) for e in tmpReqd])
raise ParseException(
instring,
loc,
Expand Down
6 changes: 4 additions & 2 deletions pyparsing/results.py
Expand Up @@ -462,8 +462,10 @@ def __str__(self) -> str:
return (
"["
+ ", ".join(
str(i) if isinstance(i, ParseResults) else repr(i)
for i in self._toklist
[
str(i) if isinstance(i, ParseResults) else repr(i)
for i in self._toklist
]
)
+ "]"
)
Expand Down
2 changes: 1 addition & 1 deletion pyparsing/unicode.py
Expand Up @@ -108,7 +108,7 @@ def identbodychars(cls):
cls.identchars
+ "0123456789"
+ "".join(
c for c in cls._chars_for_ranges if ("_" + c).isidentifier()
[c for c in cls._chars_for_ranges if ("_" + c).isidentifier()]
)
)
)
Expand Down

0 comments on commit 30bd0f3

Please sign in to comment.