diff --git a/pyparsing/__init__.py b/pyparsing/__init__.py index 0474e8d1..2d45cfa1 100644 --- a/pyparsing/__init__.py +++ b/pyparsing/__init__.py @@ -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 " diff --git a/pyparsing/core.py b/pyparsing/core.py index 77510353..53cca4c5 100644 --- a/pyparsing/core.py +++ b/pyparsing/core.py @@ -5,6 +5,7 @@ from typing import ( Optional as OptionalType, Iterable as IterableType, + NamedTuple, Union, Callable, Any, @@ -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 @@ -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 @@ -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": """ @@ -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) ) @@ -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) ) @@ -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) @@ -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 @@ -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: @@ -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 @@ -913,15 +921,15 @@ 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: @@ -929,9 +937,9 @@ def _parseCache( 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: @@ -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 s, to minimize unwanted transformation of string, and to # keep string locs straight between transform_string and scan_string @@ -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 @@ -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, @@ -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: @@ -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 @@ -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, diff --git a/pyparsing/results.py b/pyparsing/results.py index 842d16b3..9676f45b 100644 --- a/pyparsing/results.py +++ b/pyparsing/results.py @@ -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 + ] ) + "]" ) diff --git a/pyparsing/unicode.py b/pyparsing/unicode.py index caa3306d..92261487 100644 --- a/pyparsing/unicode.py +++ b/pyparsing/unicode.py @@ -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()] ) ) )