aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python
diff options
context:
space:
mode:
authorrobot-piglet <robot-piglet@yandex-team.com>2024-03-21 08:11:49 +0300
committerrobot-piglet <robot-piglet@yandex-team.com>2024-03-21 08:18:59 +0300
commitdd2d6a2c7b152b15a694144926e4647d20a1af27 (patch)
tree478221fcf51fd79682204afaad555dc08ae6deb6 /contrib/python
parent8825fe7fbe7c3057ed00708e6d0cbd488b0b53f0 (diff)
downloadydb-dd2d6a2c7b152b15a694144926e4647d20a1af27.tar.gz
Intermediate changes
Diffstat (limited to 'contrib/python')
-rw-r--r--contrib/python/pyparsing/py3/.dist-info/METADATA3
-rw-r--r--contrib/python/pyparsing/py3/pyparsing/__init__.py4
-rw-r--r--contrib/python/pyparsing/py3/pyparsing/actions.py21
-rw-r--r--contrib/python/pyparsing/py3/pyparsing/common.py9
-rw-r--r--contrib/python/pyparsing/py3/pyparsing/core.py760
-rw-r--r--contrib/python/pyparsing/py3/pyparsing/diagram/__init__.py2
-rw-r--r--contrib/python/pyparsing/py3/pyparsing/exceptions.py81
-rw-r--r--contrib/python/pyparsing/py3/pyparsing/helpers.py86
-rw-r--r--contrib/python/pyparsing/py3/pyparsing/results.py211
-rw-r--r--contrib/python/pyparsing/py3/pyparsing/testing.py118
-rw-r--r--contrib/python/pyparsing/py3/pyparsing/unicode.py13
-rw-r--r--contrib/python/pyparsing/py3/pyparsing/util.py13
-rw-r--r--contrib/python/pyparsing/py3/ya.make2
13 files changed, 614 insertions, 709 deletions
diff --git a/contrib/python/pyparsing/py3/.dist-info/METADATA b/contrib/python/pyparsing/py3/.dist-info/METADATA
index dbad8472cc..cac4d35d87 100644
--- a/contrib/python/pyparsing/py3/.dist-info/METADATA
+++ b/contrib/python/pyparsing/py3/.dist-info/METADATA
@@ -1,6 +1,6 @@
Metadata-Version: 2.1
Name: pyparsing
-Version: 3.1.1
+Version: 3.1.2
Summary: pyparsing module - Classes and methods to define and execute parsing grammars
Author-email: Paul McGuire <ptmcg.gm+pyparsing@gmail.com>
Requires-Python: >=3.6.8
@@ -19,6 +19,7 @@ Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
+Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
diff --git a/contrib/python/pyparsing/py3/pyparsing/__init__.py b/contrib/python/pyparsing/py3/pyparsing/__init__.py
index 3dbc3cf83b..79d8153ce3 100644
--- a/contrib/python/pyparsing/py3/pyparsing/__init__.py
+++ b/contrib/python/pyparsing/py3/pyparsing/__init__.py
@@ -120,8 +120,8 @@ class version_info(NamedTuple):
return f"{__name__}.{type(self).__name__}({', '.join('{}={!r}'.format(*nv) for nv in zip(self._fields, self))})"
-__version_info__ = version_info(3, 1, 1, "final", 1)
-__version_time__ = "29 Jul 2023 22:27 UTC"
+__version_info__ = version_info(3, 1, 2, "final", 1)
+__version_time__ = "06 Mar 2024 07:08 UTC"
__version__ = __version_info__.__version__
__versionTime__ = __version_time__
__author__ = "Paul McGuire <ptmcg.gm+pyparsing@gmail.com>"
diff --git a/contrib/python/pyparsing/py3/pyparsing/actions.py b/contrib/python/pyparsing/py3/pyparsing/actions.py
index ca6e4c6afb..ce51b3957c 100644
--- a/contrib/python/pyparsing/py3/pyparsing/actions.py
+++ b/contrib/python/pyparsing/py3/pyparsing/actions.py
@@ -111,7 +111,6 @@ def with_attribute(*args, **attr_dict):
<div type="graph">1,3 2,3 1,1</div>
<div>this has no type</div>
</div>
-
'''
div,div_end = make_html_tags("div")
@@ -199,19 +198,9 @@ def with_class(classname, namespace=""):
# pre-PEP8 compatibility symbols
# fmt: off
-@replaced_by_pep8(replace_with)
-def replaceWith(): ...
-
-@replaced_by_pep8(remove_quotes)
-def removeQuotes(): ...
-
-@replaced_by_pep8(with_attribute)
-def withAttribute(): ...
-
-@replaced_by_pep8(with_class)
-def withClass(): ...
-
-@replaced_by_pep8(match_only_at_col)
-def matchOnlyAtCol(): ...
-
+replaceWith = replaced_by_pep8("replaceWith", replace_with)
+removeQuotes = replaced_by_pep8("removeQuotes", remove_quotes)
+withAttribute = replaced_by_pep8("withAttribute", with_attribute)
+withClass = replaced_by_pep8("withClass", with_class)
+matchOnlyAtCol = replaced_by_pep8("matchOnlyAtCol", match_only_at_col)
# fmt: on
diff --git a/contrib/python/pyparsing/py3/pyparsing/common.py b/contrib/python/pyparsing/py3/pyparsing/common.py
index 7a666b276d..74faa46085 100644
--- a/contrib/python/pyparsing/py3/pyparsing/common.py
+++ b/contrib/python/pyparsing/py3/pyparsing/common.py
@@ -206,7 +206,7 @@ class pyparsing_common:
scientific notation and returns a float"""
# streamlining this expression makes the docs nicer-looking
- number = (sci_real | real | signed_integer).setName("number").streamline()
+ number = (sci_real | real | signed_integer).set_name("number").streamline()
"""any numeric expression, returns the corresponding Python type"""
fnumber = (
@@ -216,6 +216,13 @@ class pyparsing_common:
)
"""any int or real number, returned as float"""
+ ieee_float = (
+ Regex(r"(?i)[+-]?((\d+\.?\d*(e[+-]?\d+)?)|nan|inf(inity)?)")
+ .set_name("ieee_float")
+ .set_parse_action(convert_to_float)
+ )
+ """any floating-point literal (int, real number, infinity, or NaN), returned as float"""
+
identifier = Word(identchars, identbodychars).set_name("identifier")
"""typical code identifier (leading alpha or '_', followed by 0 or more alphas, nums, or '_')"""
diff --git a/contrib/python/pyparsing/py3/pyparsing/core.py b/contrib/python/pyparsing/py3/pyparsing/core.py
index 73514ed09d..b19d122160 100644
--- a/contrib/python/pyparsing/py3/pyparsing/core.py
+++ b/contrib/python/pyparsing/py3/pyparsing/core.py
@@ -571,6 +571,7 @@ class ParserElement(ABC):
Example::
+ integer = Word(nums)
date_str = (integer.set_results_name("year") + '/'
+ integer.set_results_name("month") + '/'
+ integer.set_results_name("day"))
@@ -610,9 +611,8 @@ class ParserElement(ABC):
breaker._originalParseMethod = _parseMethod # type: ignore [attr-defined]
self._parse = breaker # type: ignore [assignment]
- else:
- if hasattr(self._parse, "_originalParseMethod"):
- self._parse = self._parse._originalParseMethod # type: ignore [attr-defined, assignment]
+ elif hasattr(self._parse, "_originalParseMethod"):
+ self._parse = self._parse._originalParseMethod # type: ignore [attr-defined, assignment]
return self
def set_parse_action(self, *fns: ParseAction, **kwargs) -> "ParserElement":
@@ -692,13 +692,15 @@ class ParserElement(ABC):
"""
if list(fns) == [None]:
self.parseAction = []
- else:
- if not all(callable(fn) for fn in fns):
- raise TypeError("parse actions must be callable")
- self.parseAction = [_trim_arity(fn) for fn in fns]
- self.callDuringTry = kwargs.get(
- "call_during_try", kwargs.get("callDuringTry", False)
- )
+ return self
+
+ if not all(callable(fn) for fn in fns):
+ raise TypeError("parse actions must be callable")
+ self.parseAction = [_trim_arity(fn) for fn in fns]
+ self.callDuringTry = kwargs.get(
+ "call_during_try", kwargs.get("callDuringTry", False)
+ )
+
return self
def add_parse_action(self, *fns: ParseAction, **kwargs) -> "ParserElement":
@@ -944,11 +946,9 @@ class ParserElement(ABC):
not_in_cache: bool
- def get(self, *args):
- ...
+ def get(self, *args): ...
- def set(self, *args):
- ...
+ def set(self, *args): ...
# argument cache for optimizing repeated calls when backtracking through recursive expressions
packrat_cache = (
@@ -1080,7 +1080,7 @@ class ParserElement(ABC):
elif cache_size_limit > 0:
ParserElement.recursion_memos = _LRUMemo(capacity=cache_size_limit) # type: ignore[assignment]
else:
- raise NotImplementedError("Memo size of %s" % cache_size_limit)
+ raise NotImplementedError(f"Memo size of {cache_size_limit}")
ParserElement._left_recursion_enabled = True
@staticmethod
@@ -1121,13 +1121,16 @@ class ParserElement(ABC):
ParserElement.disable_memoization()
elif ParserElement._left_recursion_enabled:
raise RuntimeError("Packrat and Bounded Recursion are not compatible")
- if not ParserElement._packratEnabled:
- ParserElement._packratEnabled = True
- if cache_size_limit is None:
- ParserElement.packrat_cache = _UnboundedCache()
- else:
- ParserElement.packrat_cache = _FifoCache(cache_size_limit) # type: ignore[assignment]
- ParserElement._parse = ParserElement._parseCache
+
+ if ParserElement._packratEnabled:
+ return
+
+ ParserElement._packratEnabled = True
+ if cache_size_limit is None:
+ ParserElement.packrat_cache = _UnboundedCache()
+ else:
+ ParserElement.packrat_cache = _FifoCache(cache_size_limit) # type: ignore[assignment]
+ ParserElement._parse = ParserElement._parseCache
def parse_string(
self, instring: str, parse_all: bool = False, *, parseAll: bool = False
@@ -1285,9 +1288,9 @@ class ParserElement(ABC):
except ParseBaseException as exc:
if ParserElement.verbose_stacktrace:
raise
- else:
- # catch and re-raise exception from here, clears out pyparsing internal stack trace
- raise exc.with_traceback(None)
+
+ # catch and re-raise exception from here, clears out pyparsing internal stack trace
+ raise exc.with_traceback(None)
def transform_string(self, instring: str, *, debug: bool = False) -> str:
"""
@@ -1317,23 +1320,27 @@ class ParserElement(ABC):
try:
for t, s, e in self.scan_string(instring, debug=debug):
out.append(instring[lastE:s])
- if t:
- if isinstance(t, ParseResults):
- out += t.as_list()
- elif isinstance(t, Iterable) and not isinstance(t, str_type):
- out.extend(t)
- else:
- out.append(t)
lastE = e
+
+ if not t:
+ continue
+
+ if isinstance(t, ParseResults):
+ out += t.as_list()
+ elif isinstance(t, Iterable) and not isinstance(t, str_type):
+ out.extend(t)
+ else:
+ out.append(t)
+
out.append(instring[lastE:])
out = [o for o in out if o]
return "".join([str(s) for s in _flatten(out)])
except ParseBaseException as exc:
if ParserElement.verbose_stacktrace:
raise
- else:
- # catch and re-raise exception from here, clears out pyparsing internal stack trace
- raise exc.with_traceback(None)
+
+ # catch and re-raise exception from here, clears out pyparsing internal stack trace
+ raise exc.with_traceback(None)
def search_string(
self,
@@ -1371,9 +1378,9 @@ class ParserElement(ABC):
except ParseBaseException as exc:
if ParserElement.verbose_stacktrace:
raise
- else:
- # catch and re-raise exception from here, clears out pyparsing internal stack trace
- raise exc.with_traceback(None)
+
+ # catch and re-raise exception from here, clears out pyparsing internal stack trace
+ raise exc.with_traceback(None)
def split(
self,
@@ -1502,9 +1509,12 @@ class ParserElement(ABC):
elif isinstance(other, tuple) and other[:1] == (Ellipsis,):
other = ((0,) + other[1:] + (None,))[:2]
+ if not isinstance(other, (int, tuple)):
+ return NotImplemented
+
if isinstance(other, int):
minElements, optElements = other, 0
- elif isinstance(other, tuple):
+ else:
other = tuple(o if o is not Ellipsis else None for o in other)
other = (other + (None, None))[:2]
if other[0] is None:
@@ -1521,8 +1531,6 @@ class ParserElement(ABC):
optElements -= minElements
else:
return NotImplemented
- else:
- return NotImplemented
if minElements < 0:
raise ValueError("cannot multiply ParserElement by negative value")
@@ -1711,8 +1719,8 @@ class ParserElement(ABC):
"""
if name is not None:
return self._setResultsName(name)
- else:
- return self.copy()
+
+ return self.copy()
def suppress(self) -> "ParserElement":
"""
@@ -1770,7 +1778,7 @@ class ParserElement(ABC):
Example::
- patt = Word(alphas)[1, ...]
+ patt = Word(alphas)[...]
patt.parse_string('ablaj /* comment */ lskjd')
# -> ['ablaj']
@@ -1778,8 +1786,6 @@ class ParserElement(ABC):
patt.parse_string('ablaj /* comment */ lskjd')
# -> ['ablaj', 'lskjd']
"""
- import typing
-
if isinstance(other, str_type):
other = Suppress(other)
@@ -1887,11 +1893,14 @@ class ParserElement(ABC):
Example::
- Word(nums).parse_string("ABC") # -> Exception: Expected W:(0-9) (at char 0), (line:1, col:1)
- Word(nums).set_name("integer").parse_string("ABC") # -> Exception: Expected integer (at char 0), (line:1, col:1)
+ integer = Word(nums)
+ integer.parse_string("ABC") # -> Exception: Expected W:(0-9) (at char 0), (line:1, col:1)
+
+ integer.set_name("integer")
+ integer.parse_string("ABC") # -> Exception: Expected integer (at char 0), (line:1, col:1)
"""
self.customName = name
- self.errmsg = "Expected " + self.name
+ self.errmsg = f"Expected {self.name}"
if __diag__.enable_debug_on_named_expressions:
self.set_debug()
return self
@@ -1957,9 +1966,9 @@ class ParserElement(ABC):
except ParseBaseException as exc:
if ParserElement.verbose_stacktrace:
raise
- else:
- # catch and re-raise exception from here, clears out pyparsing internal stack trace
- raise exc.with_traceback(None)
+
+ # catch and re-raise exception from here, clears out pyparsing internal stack trace
+ raise exc.with_traceback(None)
def __eq__(self, other):
if self is other:
@@ -2137,6 +2146,7 @@ class ParserElement(ABC):
success = True
NL = Literal(r"\n").add_parse_action(replace_with("\n")).ignore(quoted_string)
BOM = "\ufeff"
+ nlstr = "\n"
for t in tests:
if comment_specified and comment.matches(t, False) or comments and not t:
comments.append(
@@ -2146,7 +2156,7 @@ class ParserElement(ABC):
if not t:
continue
out = [
- "\n" + "\n".join(comments) if comments else "",
+ f"{nlstr}{nlstr.join(comments) if comments else ''}",
pyparsing_test.with_line_numbers(t) if with_line_numbers else t,
]
comments = []
@@ -2155,9 +2165,9 @@ class ParserElement(ABC):
t = NL.transform_string(t.lstrip(BOM))
result = self.parse_string(t, parse_all=parseAll)
except ParseBaseException as pe:
- fatal = "(FATAL)" if isinstance(pe, ParseFatalException) else ""
+ fatal = "(FATAL) " if isinstance(pe, ParseFatalException) else ""
out.append(pe.explain())
- out.append("FAIL: " + str(pe))
+ out.append(f"FAIL: {fatal}{pe}")
if ParserElement.verbose_stacktrace:
out.extend(traceback.format_tb(pe.__traceback__))
success = success and failureTests
@@ -2244,91 +2254,42 @@ class ParserElement(ABC):
show_groups=show_groups,
diagram_kwargs=kwargs,
)
- if isinstance(output_html, (str, Path)):
- with open(output_html, "w", encoding="utf-8") as diag_file:
- diag_file.write(railroad_to_html(railroad, embed=embed, **kwargs))
- else:
+ if not isinstance(output_html, (str, Path)):
# we were passed a file-like object, just write to it
output_html.write(railroad_to_html(railroad, embed=embed, **kwargs))
+ return
+
+ with open(output_html, "w", encoding="utf-8") as diag_file:
+ diag_file.write(railroad_to_html(railroad, embed=embed, **kwargs))
# Compatibility synonyms
# fmt: off
- @staticmethod
- @replaced_by_pep8(inline_literals_using)
- def inlineLiteralsUsing(): ...
-
- @staticmethod
- @replaced_by_pep8(set_default_whitespace_chars)
- def setDefaultWhitespaceChars(): ...
-
- @replaced_by_pep8(set_results_name)
- def setResultsName(self): ...
-
- @replaced_by_pep8(set_break)
- def setBreak(self): ...
-
- @replaced_by_pep8(set_parse_action)
- def setParseAction(self): ...
-
- @replaced_by_pep8(add_parse_action)
- def addParseAction(self): ...
-
- @replaced_by_pep8(add_condition)
- def addCondition(self): ...
-
- @replaced_by_pep8(set_fail_action)
- def setFailAction(self): ...
-
- @replaced_by_pep8(try_parse)
- def tryParse(self): ...
-
- @staticmethod
- @replaced_by_pep8(enable_left_recursion)
- def enableLeftRecursion(): ...
-
- @staticmethod
- @replaced_by_pep8(enable_packrat)
- def enablePackrat(): ...
-
- @replaced_by_pep8(parse_string)
- def parseString(self): ...
-
- @replaced_by_pep8(scan_string)
- def scanString(self): ...
-
- @replaced_by_pep8(transform_string)
- def transformString(self): ...
-
- @replaced_by_pep8(search_string)
- def searchString(self): ...
-
- @replaced_by_pep8(ignore_whitespace)
- def ignoreWhitespace(self): ...
-
- @replaced_by_pep8(leave_whitespace)
- def leaveWhitespace(self): ...
-
- @replaced_by_pep8(set_whitespace_chars)
- def setWhitespaceChars(self): ...
-
- @replaced_by_pep8(parse_with_tabs)
- def parseWithTabs(self): ...
-
- @replaced_by_pep8(set_debug_actions)
- def setDebugActions(self): ...
-
- @replaced_by_pep8(set_debug)
- def setDebug(self): ...
-
- @replaced_by_pep8(set_name)
- def setName(self): ...
-
- @replaced_by_pep8(parse_file)
- def parseFile(self): ...
-
- @replaced_by_pep8(run_tests)
- def runTests(self): ...
-
+ inlineLiteralsUsing = replaced_by_pep8("inlineLiteralsUsing", inline_literals_using)
+ setDefaultWhitespaceChars = replaced_by_pep8(
+ "setDefaultWhitespaceChars", set_default_whitespace_chars
+ )
+ setResultsName = replaced_by_pep8("setResultsName", set_results_name)
+ setBreak = replaced_by_pep8("setBreak", set_break)
+ setParseAction = replaced_by_pep8("setParseAction", set_parse_action)
+ addParseAction = replaced_by_pep8("addParseAction", add_parse_action)
+ addCondition = replaced_by_pep8("addCondition", add_condition)
+ setFailAction = replaced_by_pep8("setFailAction", set_fail_action)
+ tryParse = replaced_by_pep8("tryParse", try_parse)
+ enableLeftRecursion = replaced_by_pep8("enableLeftRecursion", enable_left_recursion)
+ enablePackrat = replaced_by_pep8("enablePackrat", enable_packrat)
+ parseString = replaced_by_pep8("parseString", parse_string)
+ scanString = replaced_by_pep8("scanString", scan_string)
+ transformString = replaced_by_pep8("transformString", transform_string)
+ searchString = replaced_by_pep8("searchString", search_string)
+ ignoreWhitespace = replaced_by_pep8("ignoreWhitespace", ignore_whitespace)
+ leaveWhitespace = replaced_by_pep8("leaveWhitespace", leave_whitespace)
+ setWhitespaceChars = replaced_by_pep8("setWhitespaceChars", set_whitespace_chars)
+ parseWithTabs = replaced_by_pep8("parseWithTabs", parse_with_tabs)
+ setDebugActions = replaced_by_pep8("setDebugActions", set_debug_actions)
+ setDebug = replaced_by_pep8("setDebug", set_debug)
+ setName = replaced_by_pep8("setName", set_name)
+ parseFile = replaced_by_pep8("parseFile", parse_file)
+ runTests = replaced_by_pep8("runTests", run_tests)
canParseNext = can_parse_next
resetCache = reset_cache
defaultName = default_name
@@ -2358,7 +2319,7 @@ class _PendingSkip(ParserElement):
def show_skip(t):
if t._skipped.as_list()[-1:] == [""]:
t.pop("_skipped")
- t["_skipped"] = "missing <" + repr(self.anchor) + ">"
+ t["_skipped"] = f"missing <{self.anchor!r}>"
return (
self.anchor + skipper().add_parse_action(must_skip)
@@ -2409,9 +2370,9 @@ class Literal(Token):
Example::
- Literal('blah').parse_string('blah') # -> ['blah']
- Literal('blah').parse_string('blahfooblah') # -> ['blah']
- Literal('blah').parse_string('bla') # -> Exception: Expected "blah"
+ Literal('abc').parse_string('abc') # -> ['abc']
+ Literal('abc').parse_string('abcdef') # -> ['abc']
+ Literal('abc').parse_string('ab') # -> Exception: Expected "abc"
For case-insensitive matching, use :class:`CaselessLiteral`.
@@ -2441,7 +2402,7 @@ class Literal(Token):
self.match = match_string
self.matchLen = len(match_string)
self.firstMatchChar = match_string[:1]
- self.errmsg = "Expected " + self.name
+ self.errmsg = f"Expected {self.name}"
self.mayReturnEmpty = False
self.mayIndexError = False
@@ -2555,40 +2516,37 @@ class Keyword(Token):
or instring[loc + self.matchLen].upper() not in self.identChars
):
return loc + self.matchLen, self.match
- else:
- # followed by keyword char
- errmsg += ", was immediately followed by keyword character"
- errloc = loc + self.matchLen
- else:
- # preceded by keyword char
- errmsg += ", keyword was immediately preceded by keyword character"
- errloc = loc - 1
- # else no match just raise plain exception
- else:
- if (
- instring[loc] == self.firstMatchChar
- and self.matchLen == 1
- or instring.startswith(self.match, loc)
- ):
- if loc == 0 or instring[loc - 1] not in self.identChars:
- if (
- loc >= len(instring) - self.matchLen
- or instring[loc + self.matchLen] not in self.identChars
- ):
- return loc + self.matchLen, self.match
- else:
- # followed by keyword char
- errmsg += (
- ", keyword was immediately followed by keyword character"
- )
- errloc = loc + self.matchLen
+ # followed by keyword char
+ errmsg += ", was immediately followed by keyword character"
+ errloc = loc + self.matchLen
else:
# preceded by keyword char
errmsg += ", keyword was immediately preceded by keyword character"
errloc = loc - 1
# else no match just raise plain exception
+ elif (
+ instring[loc] == self.firstMatchChar
+ and self.matchLen == 1
+ or instring.startswith(self.match, loc)
+ ):
+ if loc == 0 or instring[loc - 1] not in self.identChars:
+ if (
+ loc >= len(instring) - self.matchLen
+ or instring[loc + self.matchLen] not in self.identChars
+ ):
+ return loc + self.matchLen, self.match
+
+ # followed by keyword char
+ errmsg += ", keyword was immediately followed by keyword character"
+ errloc = loc + self.matchLen
+ else:
+ # preceded by keyword char
+ errmsg += ", keyword was immediately preceded by keyword character"
+ errloc = loc - 1
+ # else no match just raise plain exception
+
raise ParseException(instring, errloc, errmsg, self)
@staticmethod
@@ -2620,7 +2578,7 @@ class CaselessLiteral(Literal):
super().__init__(match_string.upper())
# Preserve the defining literal.
self.returnString = match_string
- self.errmsg = "Expected " + self.name
+ self.errmsg = f"Expected {self.name}"
def parseImpl(self, instring, loc, doActions=True):
if instring[loc : loc + self.matchLen].upper() == self.match:
@@ -2795,7 +2753,7 @@ class Word(Token):
integer = Word(nums) # equivalent to Word("0123456789") or Word(srange("0-9"))
# a word with a leading capital, and zero or more lowercase
- capital_word = Word(alphas.upper(), alphas.lower())
+ capitalized_word = Word(alphas.upper(), alphas.lower())
# hostnames are alphanumeric, with leading alpha, and '-'
hostname = Word(alphas, alphanums + '-')
@@ -2872,7 +2830,7 @@ class Word(Token):
self.maxLen = exact
self.minLen = exact
- self.errmsg = "Expected " + self.name
+ self.errmsg = f"Expected {self.name}"
self.mayIndexError = False
self.asKeyword = asKeyword
if self.asKeyword:
@@ -2929,10 +2887,11 @@ class Word(Token):
def charsAsStr(s):
max_repr_len = 16
s = _collapse_string_to_ranges(s, re_escape=False)
+
if len(s) > max_repr_len:
return s[: max_repr_len - 3] + "..."
- else:
- return s
+
+ return s
if self.initChars != self.bodyChars:
base = f"W:({charsAsStr(self.initChars)}, {charsAsStr(self.bodyChars)})"
@@ -2970,14 +2929,11 @@ class Word(Token):
throwException = True
elif self.maxSpecified and loc < instrlen and instring[loc] in bodychars:
throwException = True
- elif self.asKeyword:
- if (
- start > 0
- and instring[start - 1] in bodychars
- or loc < instrlen
- and instring[loc] in bodychars
- ):
- throwException = True
+ elif self.asKeyword and (
+ (start > 0 and instring[start - 1] in bodychars)
+ or (loc < instrlen and instring[loc] in bodychars)
+ ):
+ throwException = True
if throwException:
raise ParseException(instring, loc, self.errmsg, self)
@@ -3077,7 +3033,7 @@ class Regex(Token):
"Regex may only be constructed with a string or a compiled RE object"
)
- self.errmsg = "Expected " + self.name
+ self.errmsg = f"Expected {self.name}"
self.mayIndexError = False
self.asGroupList = asGroupList
self.asMatch = asMatch
@@ -3090,11 +3046,11 @@ class Regex(Token):
def re(self):
if self._re:
return self._re
- else:
- try:
- return re.compile(self.pattern, self.flags)
- except re.error:
- raise ValueError(f"invalid pattern ({self.pattern!r}) passed to Regex")
+
+ try:
+ return re.compile(self.pattern, self.flags)
+ except re.error:
+ raise ValueError(f"invalid pattern ({self.pattern!r}) passed to Regex")
@cached_property
def re_match(self):
@@ -3115,9 +3071,10 @@ class Regex(Token):
loc = result.end()
ret = ParseResults(result.group())
d = result.groupdict()
- if d:
- for k, v in d.items():
- ret[k] = v
+
+ for k, v in d.items():
+ ret[k] = v
+
return loc, ret
def parseImplAsGroupList(self, instring, loc, doActions=True):
@@ -3209,6 +3166,7 @@ class QuotedString(Token):
[['This is the "quote"']]
[['This is the quote with "embedded" quotes']]
"""
+
ws_map = dict(((r"\t", "\t"), (r"\n", "\n"), (r"\f", "\f"), (r"\r", "\r")))
def __init__(
@@ -3328,7 +3286,7 @@ class QuotedString(Token):
except re.error:
raise ValueError(f"invalid pattern {self.pattern!r} passed to Regex")
- self.errmsg = "Expected " + self.name
+ self.errmsg = f"Expected {self.name}"
self.mayIndexError = False
self.mayReturnEmpty = True
@@ -3428,8 +3386,8 @@ class CharsNotIn(Token):
if min < 1:
raise ValueError(
- "cannot specify a minimum length < 1; use "
- "Opt(CharsNotIn()) if zero-length char group is permitted"
+ "cannot specify a minimum length < 1; use"
+ " Opt(CharsNotIn()) if zero-length char group is permitted"
)
self.minLen = min
@@ -3443,7 +3401,7 @@ class CharsNotIn(Token):
self.maxLen = exact
self.minLen = exact
- self.errmsg = "Expected " + self.name
+ self.errmsg = f"Expected {self.name}"
self.mayReturnEmpty = self.minLen == 0
self.mayIndexError = False
@@ -3516,7 +3474,7 @@ class White(Token):
)
# self.leave_whitespace()
self.mayReturnEmpty = True
- self.errmsg = "Expected " + self.name
+ self.errmsg = f"Expected {self.name}"
self.minLen = min
@@ -3565,16 +3523,19 @@ class GoToColumn(PositionToken):
self.col = colno
def preParse(self, instring: str, loc: int) -> int:
- if col(loc, instring) != self.col:
- instrlen = len(instring)
- if self.ignoreExprs:
- loc = self._skipIgnorables(instring, loc)
- while (
- loc < instrlen
- and instring[loc].isspace()
- and col(loc, instring) != self.col
- ):
- loc += 1
+ if col(loc, instring) == self.col:
+ return loc
+
+ instrlen = len(instring)
+ if self.ignoreExprs:
+ loc = self._skipIgnorables(instring, loc)
+ while (
+ loc < instrlen
+ and instring[loc].isspace()
+ and col(loc, instring) != self.col
+ ):
+ loc += 1
+
return loc
def parseImpl(self, instring, loc, doActions=True):
@@ -3620,12 +3581,14 @@ class LineStart(PositionToken):
def preParse(self, instring: str, loc: int) -> int:
if loc == 0:
return loc
- else:
- ret = self.skipper.preParse(instring, loc)
- if "\n" in self.orig_whiteChars:
- while instring[ret : ret + 1] == "\n":
- ret = self.skipper.preParse(instring, ret + 1)
- return ret
+
+ ret = self.skipper.preParse(instring, loc)
+
+ if "\n" in self.orig_whiteChars:
+ while instring[ret : ret + 1] == "\n":
+ ret = self.skipper.preParse(instring, ret + 1)
+
+ return ret
def parseImpl(self, instring, loc, doActions=True):
if col(loc, instring) == 1:
@@ -3666,10 +3629,10 @@ class StringStart(PositionToken):
self.errmsg = "Expected start of text"
def parseImpl(self, instring, loc, doActions=True):
- if loc != 0:
- # see if entire string up to here is just whitespace and ignoreables
- if loc != self.preParse(instring, 0):
- raise ParseException(instring, loc, self.errmsg, self)
+ # see if entire string up to here is just whitespace and ignoreables
+ if loc != 0 and loc != self.preParse(instring, 0):
+ raise ParseException(instring, loc, self.errmsg, self)
+
return loc, []
@@ -3685,12 +3648,12 @@ class StringEnd(PositionToken):
def parseImpl(self, instring, loc, doActions=True):
if loc < len(instring):
raise ParseException(instring, loc, self.errmsg, self)
- elif loc == len(instring):
+ if loc == len(instring):
return loc + 1, []
- elif loc > len(instring):
+ if loc > len(instring):
return loc, []
- else:
- raise ParseException(instring, loc, self.errmsg, self)
+
+ raise ParseException(instring, loc, self.errmsg, self)
class WordStart(PositionToken):
@@ -3823,7 +3786,7 @@ class ParseExpression(ParserElement):
return self
def _generateDefaultName(self) -> str:
- return f"{self.__class__.__name__}:({str(self.exprs)})"
+ return f"{type(self).__name__}:({self.exprs})"
def streamline(self) -> ParserElement:
if self.streamlined:
@@ -3862,7 +3825,7 @@ class ParseExpression(ParserElement):
self.mayReturnEmpty |= other.mayReturnEmpty
self.mayIndexError |= other.mayIndexError
- self.errmsg = "Expected " + str(self)
+ self.errmsg = f"Expected {self}"
return self
@@ -3884,38 +3847,36 @@ class ParseExpression(ParserElement):
return ret
def _setResultsName(self, name, listAllMatches=False):
- if (
+ if not (
__diag__.warn_ungrouped_named_tokens_in_collection
and Diagnostics.warn_ungrouped_named_tokens_in_collection
not in self.suppress_warnings_
):
- for e in self.exprs:
- if (
- isinstance(e, ParserElement)
- and e.resultsName
- and Diagnostics.warn_ungrouped_named_tokens_in_collection
+ return super()._setResultsName(name, listAllMatches)
+
+ for e in self.exprs:
+ if (
+ isinstance(e, ParserElement)
+ and e.resultsName
+ and (
+ Diagnostics.warn_ungrouped_named_tokens_in_collection
not in e.suppress_warnings_
- ):
- warnings.warn(
- "{}: setting results name {!r} on {} expression "
- "collides with {!r} on contained expression".format(
- "warn_ungrouped_named_tokens_in_collection",
- name,
- type(self).__name__,
- e.resultsName,
- ),
- stacklevel=3,
- )
+ )
+ ):
+ warning = (
+ "warn_ungrouped_named_tokens_in_collection:"
+ f" setting results name {name!r} on {type(self).__name__} expression"
+ f" collides with {e.resultsName!r} on contained expression"
+ )
+ warnings.warn(warning, stacklevel=3)
+ break
return super()._setResultsName(name, listAllMatches)
# Compatibility synonyms
# fmt: off
- @replaced_by_pep8(leave_whitespace)
- def leaveWhitespace(self): ...
-
- @replaced_by_pep8(ignore_whitespace)
- def ignoreWhitespace(self): ...
+ leaveWhitespace = replaced_by_pep8("leaveWhitespace", leave_whitespace)
+ ignoreWhitespace = replaced_by_pep8("ignoreWhitespace", ignore_whitespace)
# fmt: on
@@ -3952,18 +3913,18 @@ class And(ParseExpression):
if exprs and Ellipsis in exprs:
tmp = []
for i, expr in enumerate(exprs):
- if expr is Ellipsis:
- if i < len(exprs) - 1:
- skipto_arg: ParserElement = typing.cast(
- ParseExpression, (Empty() + exprs[i + 1])
- ).exprs[-1]
- tmp.append(SkipTo(skipto_arg)("_skipped*"))
- else:
- raise Exception(
- "cannot construct And with sequence ending in ..."
- )
- else:
+ if expr is not Ellipsis:
tmp.append(expr)
+ continue
+
+ if i < len(exprs) - 1:
+ skipto_arg: ParserElement = typing.cast(
+ ParseExpression, (Empty() + exprs[i + 1])
+ ).exprs[-1]
+ tmp.append(SkipTo(skipto_arg)("_skipped*"))
+ continue
+
+ raise Exception("cannot construct And with sequence ending in ...")
exprs[:] = tmp
super().__init__(exprs, savelist)
if self.exprs:
@@ -3982,25 +3943,24 @@ class And(ParseExpression):
def streamline(self) -> ParserElement:
# collapse any _PendingSkip's
- if self.exprs:
- if any(
- isinstance(e, ParseExpression)
- and e.exprs
- and isinstance(e.exprs[-1], _PendingSkip)
- for e in self.exprs[:-1]
- ):
- deleted_expr_marker = NoMatch()
- for i, e in enumerate(self.exprs[:-1]):
- if e is deleted_expr_marker:
- continue
- if (
- isinstance(e, ParseExpression)
- and e.exprs
- and isinstance(e.exprs[-1], _PendingSkip)
- ):
- e.exprs[-1] = e.exprs[-1] + self.exprs[i + 1]
- self.exprs[i + 1] = deleted_expr_marker
- self.exprs = [e for e in self.exprs if e is not deleted_expr_marker]
+ if self.exprs and any(
+ isinstance(e, ParseExpression)
+ and e.exprs
+ and isinstance(e.exprs[-1], _PendingSkip)
+ for e in self.exprs[:-1]
+ ):
+ deleted_expr_marker = NoMatch()
+ for i, e in enumerate(self.exprs[:-1]):
+ if e is deleted_expr_marker:
+ continue
+ if (
+ isinstance(e, ParseExpression)
+ and e.exprs
+ and isinstance(e.exprs[-1], _PendingSkip)
+ ):
+ e.exprs[-1] = e.exprs[-1] + self.exprs[i + 1]
+ self.exprs[i + 1] = deleted_expr_marker
+ self.exprs = [e for e in self.exprs if e is not deleted_expr_marker]
super().streamline()
@@ -4079,7 +4039,7 @@ class And(ParseExpression):
# strip off redundant inner {}'s
while len(inner) > 1 and inner[0 :: len(inner) - 1] == "{}":
inner = inner[1:-1]
- return "{" + inner + "}"
+ return f"{{{inner}}}"
class Or(ParseExpression):
@@ -4200,10 +4160,8 @@ class Or(ParseExpression):
if maxExcLoc == loc:
maxException.msg = self.errmsg
raise maxException
- else:
- raise ParseException(
- instring, loc, "no defined alternatives to match", self
- )
+
+ raise ParseException(instring, loc, "no defined alternatives to match", self)
def __ixor__(self, other):
if isinstance(other, str_type):
@@ -4213,7 +4171,7 @@ class Or(ParseExpression):
return self.append(other) # Or([self, other])
def _generateDefaultName(self) -> str:
- return "{" + " ^ ".join(str(e) for e in self.exprs) + "}"
+ return f"{{{' ^ '.join(str(e) for e in self.exprs)}}}"
def _setResultsName(self, name, listAllMatches=False):
if (
@@ -4227,17 +4185,14 @@ class Or(ParseExpression):
not in e.suppress_warnings_
for e in self.exprs
):
- warnings.warn(
- "{}: setting results name {!r} on {} expression "
- "will return a list of all parsed tokens in an And alternative, "
- "in prior versions only the first token was returned; enclose "
- "contained argument in Group".format(
- "warn_multiple_tokens_in_named_alternation",
- name,
- type(self).__name__,
- ),
- stacklevel=3,
+ warning = (
+ "warn_multiple_tokens_in_named_alternation:"
+ f" setting results name {name!r} on {type(self).__name__} expression"
+ " will return a list of all parsed tokens in an And alternative,"
+ " in prior versions only the first token was returned; enclose"
+ " contained argument in Group"
)
+ warnings.warn(warning, stacklevel=3)
return super()._setResultsName(name, listAllMatches)
@@ -4290,11 +4245,7 @@ class MatchFirst(ParseExpression):
for e in self.exprs:
try:
- return e._parse(
- instring,
- loc,
- doActions,
- )
+ return e._parse(instring, loc, doActions)
except ParseFatalException as pfe:
pfe.__traceback__ = None
pfe.parser_element = e
@@ -4316,10 +4267,8 @@ class MatchFirst(ParseExpression):
if maxExcLoc == loc:
maxException.msg = self.errmsg
raise maxException
- else:
- raise ParseException(
- instring, loc, "no defined alternatives to match", self
- )
+
+ raise ParseException(instring, loc, "no defined alternatives to match", self)
def __ior__(self, other):
if isinstance(other, str_type):
@@ -4329,7 +4278,7 @@ class MatchFirst(ParseExpression):
return self.append(other) # MatchFirst([self, other])
def _generateDefaultName(self) -> str:
- return "{" + " | ".join(str(e) for e in self.exprs) + "}"
+ return f"{{{' | '.join(str(e) for e in self.exprs)}}}"
def _setResultsName(self, name, listAllMatches=False):
if (
@@ -4343,17 +4292,14 @@ class MatchFirst(ParseExpression):
not in e.suppress_warnings_
for e in self.exprs
):
- warnings.warn(
- "{}: setting results name {!r} on {} expression "
- "will return a list of all parsed tokens in an And alternative, "
- "in prior versions only the first token was returned; enclose "
- "contained argument in Group".format(
- "warn_multiple_tokens_in_named_alternation",
- name,
- type(self).__name__,
- ),
- stacklevel=3,
+ warning = (
+ "warn_multiple_tokens_in_named_alternation:"
+ f" setting results name {name!r} on {type(self).__name__} expression"
+ " will return a list of all parsed tokens in an And alternative,"
+ " in prior versions only the first token was returned; enclose"
+ " contained argument in Group"
)
+ warnings.warn(warning, stacklevel=3)
return super()._setResultsName(name, listAllMatches)
@@ -4529,7 +4475,7 @@ class Each(ParseExpression):
return loc, total_results
def _generateDefaultName(self) -> str:
- return "{" + " & ".join(str(e) for e in self.exprs) + "}"
+ return f"{{{' & '.join(str(e) for e in self.exprs)}}}"
class ParseElementEnhance(ParserElement):
@@ -4564,16 +4510,17 @@ class ParseElementEnhance(ParserElement):
return [self.expr] if self.expr is not None else []
def parseImpl(self, instring, loc, doActions=True):
- if self.expr is not None:
- try:
- return self.expr._parse(instring, loc, doActions, callPreParse=False)
- except ParseBaseException as pbe:
- if not isinstance(self, Forward) or self.customName is not None:
- pbe.msg = self.errmsg
- raise
- else:
+ if self.expr is None:
raise ParseException(instring, loc, "No expression defined", self)
+ try:
+ return self.expr._parse(instring, loc, doActions, callPreParse=False)
+ except ParseBaseException as pbe:
+ if not isinstance(self, Forward) or self.customName is not None:
+ if self.errmsg:
+ pbe.msg = self.errmsg
+ raise
+
def leave_whitespace(self, recursive: bool = True) -> ParserElement:
super().leave_whitespace(recursive)
@@ -4593,15 +4540,11 @@ class ParseElementEnhance(ParserElement):
return self
def ignore(self, other) -> ParserElement:
- if isinstance(other, Suppress):
- if other not in self.ignoreExprs:
- super().ignore(other)
- if self.expr is not None:
- self.expr.ignore(self.ignoreExprs[-1])
- else:
+ if not isinstance(other, Suppress) or other not in self.ignoreExprs:
super().ignore(other)
if self.expr is not None:
self.expr.ignore(self.ignoreExprs[-1])
+
return self
def streamline(self) -> ParserElement:
@@ -4631,15 +4574,12 @@ class ParseElementEnhance(ParserElement):
self._checkRecursion([])
def _generateDefaultName(self) -> str:
- return f"{self.__class__.__name__}:({str(self.expr)})"
+ return f"{type(self).__name__}:({self.expr})"
# Compatibility synonyms
# fmt: off
- @replaced_by_pep8(leave_whitespace)
- def leaveWhitespace(self): ...
-
- @replaced_by_pep8(ignore_whitespace)
- def ignoreWhitespace(self): ...
+ leaveWhitespace = replaced_by_pep8("leaveWhitespace", leave_whitespace)
+ ignoreWhitespace = replaced_by_pep8("ignoreWhitespace", ignore_whitespace)
# fmt: on
@@ -4849,7 +4789,7 @@ class PrecededBy(ParseElementEnhance):
retreat = 0
self.exact = True
self.retreat = retreat
- self.errmsg = "not preceded by " + str(expr)
+ self.errmsg = f"not preceded by {expr}"
self.skipWhitespace = False
self.parseAction.append(lambda s, l, t: t.__delitem__(slice(None, None)))
@@ -4859,23 +4799,24 @@ class PrecededBy(ParseElementEnhance):
raise ParseException(instring, loc, self.errmsg)
start = loc - self.retreat
_, ret = self.expr._parse(instring, start)
- else:
- # retreat specified a maximum lookbehind window, iterate
- test_expr = self.expr + StringEnd()
- instring_slice = instring[max(0, loc - self.retreat) : loc]
- last_expr = ParseException(instring, loc, self.errmsg)
- for offset in range(1, min(loc, self.retreat + 1) + 1):
- try:
- # print('trying', offset, instring_slice, repr(instring_slice[loc - offset:]))
- _, ret = test_expr._parse(
- instring_slice, len(instring_slice) - offset
- )
- except ParseBaseException as pbe:
- last_expr = pbe
- else:
- break
+ return loc, ret
+
+ # retreat specified a maximum lookbehind window, iterate
+ test_expr = self.expr + StringEnd()
+ instring_slice = instring[max(0, loc - self.retreat) : loc]
+ last_expr = ParseException(instring, loc, self.errmsg)
+
+ for offset in range(1, min(loc, self.retreat + 1) + 1):
+ try:
+ # print('trying', offset, instring_slice, repr(instring_slice[loc - offset:]))
+ _, ret = test_expr._parse(instring_slice, len(instring_slice) - offset)
+ except ParseBaseException as pbe:
+ last_expr = pbe
else:
- raise last_expr
+ break
+ else:
+ raise last_expr
+
return loc, ret
@@ -4953,7 +4894,7 @@ class NotAny(ParseElementEnhance):
self.skipWhitespace = False
self.mayReturnEmpty = True
- self.errmsg = "Found unwanted token, " + str(self.expr)
+ self.errmsg = f"Found unwanted token, {self.expr}"
def parseImpl(self, instring, loc, doActions=True):
if self.expr.can_parse_next(instring, loc, do_actions=doActions):
@@ -4961,7 +4902,7 @@ class NotAny(ParseElementEnhance):
return loc, []
def _generateDefaultName(self) -> str:
- return "~{" + str(self.expr) + "}"
+ return f"~{{{self.expr}}}"
class _MultipleMatch(ParseElementEnhance):
@@ -5024,19 +4965,18 @@ class _MultipleMatch(ParseElementEnhance):
if (
isinstance(e, ParserElement)
and e.resultsName
- and Diagnostics.warn_ungrouped_named_tokens_in_collection
- not in e.suppress_warnings_
+ and (
+ Diagnostics.warn_ungrouped_named_tokens_in_collection
+ not in e.suppress_warnings_
+ )
):
- warnings.warn(
- "{}: setting results name {!r} on {} expression "
- "collides with {!r} on contained expression".format(
- "warn_ungrouped_named_tokens_in_collection",
- name,
- type(self).__name__,
- e.resultsName,
- ),
- stacklevel=3,
+ warning = (
+ "warn_ungrouped_named_tokens_in_collection:"
+ f" setting results name {name!r} on {type(self).__name__} expression"
+ f" collides with {e.resultsName!r} on contained expression"
)
+ warnings.warn(warning, stacklevel=3)
+ break
return super()._setResultsName(name, listAllMatches)
@@ -5070,7 +5010,7 @@ class OneOrMore(_MultipleMatch):
"""
def _generateDefaultName(self) -> str:
- return "{" + str(self.expr) + "}..."
+ return f"{{{self.expr}}}..."
class ZeroOrMore(_MultipleMatch):
@@ -5104,7 +5044,7 @@ class ZeroOrMore(_MultipleMatch):
return loc, ParseResults([], name=self.resultsName)
def _generateDefaultName(self) -> str:
- return "[" + str(self.expr) + "]..."
+ return f"[{self.expr}]..."
class DelimitedList(ParseElementEnhance):
@@ -5139,12 +5079,11 @@ class DelimitedList(ParseElementEnhance):
expr = ParserElement._literalStringClass(expr)
expr = typing.cast(ParserElement, expr)
- if min is not None:
- if min < 1:
- raise ValueError("min must be greater than 0")
- if max is not None:
- if min is not None and max < min:
- raise ValueError("max must be greater than, or equal to min")
+ if min is not None and min < 1:
+ raise ValueError("min must be greater than 0")
+
+ if max is not None and min is not None and max < min:
+ raise ValueError("max must be greater than, or equal to min")
self.content = expr
self.raw_delim = str(delim)
@@ -5169,7 +5108,8 @@ class DelimitedList(ParseElementEnhance):
super().__init__(delim_list_expr, savelist=True)
def _generateDefaultName(self) -> str:
- return "{0} [{1} {0}]...".format(self.content.streamline(), self.raw_delim)
+ content_expr = self.content.streamline()
+ return f"{content_expr} [{self.raw_delim} {content_expr}]..."
class _NullToken:
@@ -5251,7 +5191,7 @@ class Opt(ParseElementEnhance):
# strip off redundant inner {}'s
while len(inner) > 1 and inner[0 :: len(inner) - 1] == "{}":
inner = inner[1:-1]
- return "[" + inner + "]"
+ return f"[{inner}]"
Optional = Opt
@@ -5586,14 +5526,13 @@ class Forward(ParseElementEnhance):
del memo[peek_key]
return prev_loc, prev_peek.copy()
# the match did get better: see if we can improve further
- else:
- if doActions:
- try:
- memo[act_key] = super().parseImpl(instring, loc, True)
- except ParseException as e:
- memo[peek_key] = memo[act_key] = (new_loc, e)
- raise
- prev_loc, prev_peek = memo[peek_key] = new_loc, new_peek
+ if doActions:
+ try:
+ memo[act_key] = super().parseImpl(instring, loc, True)
+ except ParseException as e:
+ memo[peek_key] = memo[act_key] = (new_loc, e)
+ raise
+ prev_loc, prev_peek = memo[peek_key] = new_loc, new_peek
def leave_whitespace(self, recursive: bool = True) -> ParserElement:
self.skipWhitespace = False
@@ -5637,7 +5576,7 @@ class Forward(ParseElementEnhance):
else:
retString = "None"
finally:
- return self.__class__.__name__ + ": " + retString
+ return f"{type(self).__name__}: {retString}"
def copy(self) -> ParserElement:
if self.expr is not None:
@@ -5648,29 +5587,26 @@ class Forward(ParseElementEnhance):
return ret
def _setResultsName(self, name, list_all_matches=False):
+ # fmt: off
if (
__diag__.warn_name_set_on_empty_Forward
- and Diagnostics.warn_name_set_on_empty_Forward
- not in self.suppress_warnings_
+ and Diagnostics.warn_name_set_on_empty_Forward not in self.suppress_warnings_
+ and self.expr is None
):
- if self.expr is None:
- warnings.warn(
- "{}: setting results name {!r} on {} expression "
- "that has no contained expression".format(
- "warn_name_set_on_empty_Forward", name, type(self).__name__
- ),
- stacklevel=3,
- )
+ warning = (
+ "warn_name_set_on_empty_Forward:"
+ f" setting results name {name!r} on {type(self).__name__} expression"
+ " that has no contained expression"
+ )
+ warnings.warn(warning, stacklevel=3)
+ # fmt: on
return super()._setResultsName(name, list_all_matches)
# Compatibility synonyms
# fmt: off
- @replaced_by_pep8(leave_whitespace)
- def leaveWhitespace(self): ...
-
- @replaced_by_pep8(ignore_whitespace)
- def ignoreWhitespace(self): ...
+ leaveWhitespace = replaced_by_pep8("leaveWhitespace", leave_whitespace)
+ ignoreWhitespace = replaced_by_pep8("ignoreWhitespace", ignore_whitespace)
# fmt: on
@@ -5774,8 +5710,8 @@ class Group(TokenConverter):
if isinstance(tokenlist, ParseResults)
else list(tokenlist)
)
- else:
- return [tokenlist]
+
+ return [tokenlist]
class Dict(TokenConverter):
@@ -5861,8 +5797,8 @@ class Dict(TokenConverter):
if self._asPythonDict:
return [tokenlist.as_dict()] if self.resultsName else tokenlist.as_dict()
- else:
- return [tokenlist] if self.resultsName else tokenlist
+
+ return [tokenlist] if self.resultsName else tokenlist
class Suppress(TokenConverter):
@@ -5904,14 +5840,14 @@ class Suppress(TokenConverter):
def __add__(self, other) -> "ParserElement":
if isinstance(self.expr, _PendingSkip):
return Suppress(SkipTo(other)) + other
- else:
- return super().__add__(other)
+
+ return super().__add__(other)
def __sub__(self, other) -> "ParserElement":
if isinstance(self.expr, _PendingSkip):
return Suppress(SkipTo(other)) - other
- else:
- return super().__sub__(other)
+
+ return super().__sub__(other)
def postParse(self, instring, loc, tokenlist):
return []
@@ -5951,7 +5887,7 @@ def trace_parse_action(f: ParseAction) -> ParseAction:
thisFunc = f.__name__
s, l, t = paArgs[-3:]
if len(paArgs) > 3:
- thisFunc = paArgs[0].__class__.__name__ + "." + thisFunc
+ thisFunc = f"{type(paArgs[0]).__name__}.{thisFunc}"
sys.stderr.write(f">>entering {thisFunc}(line: {line(l, s)!r}, {l}, {t!r})\n")
try:
ret = f(*paArgs)
@@ -6019,8 +5955,8 @@ def srange(s: str) -> str:
- any combination of the above (``'aeiouy'``,
``'a-zA-Z0-9_$'``, etc.)
"""
- _expanded = (
- lambda p: p
+ _expanded = lambda p: (
+ p
if not isinstance(p, ParseResults)
else "".join(chr(c) for c in range(ord(p[0]), ord(p[1]) + 1))
)
@@ -6144,16 +6080,8 @@ lineStart = line_start
lineEnd = line_end
stringStart = string_start
stringEnd = string_end
-
-@replaced_by_pep8(null_debug_action)
-def nullDebugAction(): ...
-
-@replaced_by_pep8(trace_parse_action)
-def traceParseAction(): ...
-
-@replaced_by_pep8(condition_as_parse_action)
-def conditionAsParseAction(): ...
-
-@replaced_by_pep8(token_map)
-def tokenMap(): ...
+nullDebugAction = replaced_by_pep8("nullDebugAction", null_debug_action)
+traceParseAction = replaced_by_pep8("traceParseAction", trace_parse_action)
+conditionAsParseAction = replaced_by_pep8("conditionAsParseAction", condition_as_parse_action)
+tokenMap = replaced_by_pep8("tokenMap", token_map)
# fmt: on
diff --git a/contrib/python/pyparsing/py3/pyparsing/diagram/__init__.py b/contrib/python/pyparsing/py3/pyparsing/diagram/__init__.py
index 267f34474a..700d0b561f 100644
--- a/contrib/python/pyparsing/py3/pyparsing/diagram/__init__.py
+++ b/contrib/python/pyparsing/py3/pyparsing/diagram/__init__.py
@@ -473,7 +473,7 @@ def _to_diagram_element(
:param show_groups: bool flag indicating whether to show groups using bounding box
"""
exprs = element.recurse()
- name = name_hint or element.customName or element.__class__.__name__
+ name = name_hint or element.customName or type(element).__name__
# Python's id() is used to provide a unique identifier for elements
el_id = id(element)
diff --git a/contrib/python/pyparsing/py3/pyparsing/exceptions.py b/contrib/python/pyparsing/py3/pyparsing/exceptions.py
index 12219f124a..1aaea56f54 100644
--- a/contrib/python/pyparsing/py3/pyparsing/exceptions.py
+++ b/contrib/python/pyparsing/py3/pyparsing/exceptions.py
@@ -14,11 +14,13 @@ from .util import (
from .unicode import pyparsing_unicode as ppu
-class ExceptionWordUnicode(ppu.Latin1, ppu.LatinA, ppu.LatinB, ppu.Greek, ppu.Cyrillic):
+class _ExceptionWordUnicodeSet(
+ ppu.Latin1, ppu.LatinA, ppu.LatinB, ppu.Greek, ppu.Cyrillic
+):
pass
-_extract_alphanums = _collapse_string_to_ranges(ExceptionWordUnicode.alphanums)
+_extract_alphanums = _collapse_string_to_ranges(_ExceptionWordUnicodeSet.alphanums)
_exception_word_extractor = re.compile("([" + _extract_alphanums + "]{1,16})|.")
@@ -86,41 +88,39 @@ class ParseBaseException(Exception):
ret.append(" " * (exc.column - 1) + "^")
ret.append(f"{type(exc).__name__}: {exc}")
- if depth > 0:
- callers = inspect.getinnerframes(exc.__traceback__, context=depth)
- seen = set()
- for i, ff in enumerate(callers[-depth:]):
- frm = ff[0]
-
- f_self = frm.f_locals.get("self", None)
- if isinstance(f_self, ParserElement):
- if not frm.f_code.co_name.startswith(
- ("parseImpl", "_parseNoCache")
- ):
- continue
- if id(f_self) in seen:
- continue
- seen.add(id(f_self))
-
- self_type = type(f_self)
- ret.append(
- f"{self_type.__module__}.{self_type.__name__} - {f_self}"
- )
-
- elif f_self is not None:
- self_type = type(f_self)
- ret.append(f"{self_type.__module__}.{self_type.__name__}")
+ if depth <= 0:
+ return "\n".join(ret)
- else:
- code = frm.f_code
- if code.co_name in ("wrapper", "<module>"):
- continue
+ callers = inspect.getinnerframes(exc.__traceback__, context=depth)
+ seen = set()
+ for ff in callers[-depth:]:
+ frm = ff[0]
+
+ f_self = frm.f_locals.get("self", None)
+ if isinstance(f_self, ParserElement):
+ if not frm.f_code.co_name.startswith(("parseImpl", "_parseNoCache")):
+ continue
+ if id(f_self) in seen:
+ continue
+ seen.add(id(f_self))
+
+ self_type = type(f_self)
+ ret.append(f"{self_type.__module__}.{self_type.__name__} - {f_self}")
+
+ elif f_self is not None:
+ self_type = type(f_self)
+ ret.append(f"{self_type.__module__}.{self_type.__name__}")
+
+ else:
+ code = frm.f_code
+ if code.co_name in ("wrapper", "<module>"):
+ continue
- ret.append(code.co_name)
+ ret.append(code.co_name)
- depth -= 1
- if not depth:
- break
+ depth -= 1
+ if not depth:
+ break
return "\n".join(ret)
@@ -220,8 +220,10 @@ class ParseBaseException(Exception):
Example::
+ # an expression to parse 3 integers
expr = pp.Word(pp.nums) * 3
try:
+ # a failing parse - the third integer is prefixed with "A"
expr.parse_string("123 456 A789")
except pp.ParseException as pe:
print(pe.explain(depth=0))
@@ -244,8 +246,7 @@ class ParseBaseException(Exception):
return self.explain_exception(self, depth)
# fmt: off
- @replaced_by_pep8(mark_input_line)
- def markInputline(self): ...
+ markInputline = replaced_by_pep8("markInputline", mark_input_line)
# fmt: on
@@ -255,16 +256,16 @@ class ParseException(ParseBaseException):
Example::
+ integer = Word(nums).set_name("integer")
try:
- Word(nums).set_name("integer").parse_string("ABC")
+ integer.parse_string("ABC")
except ParseException as pe:
print(pe)
- print("column: {}".format(pe.column))
+ print(f"column: {pe.column}")
prints::
- Expected integer (at char 0), (line:1, col:1)
- column: 1
+ Expected integer (at char 0), (line:1, col:1) column: 1
"""
diff --git a/contrib/python/pyparsing/py3/pyparsing/helpers.py b/contrib/python/pyparsing/py3/pyparsing/helpers.py
index 018f0d6ac8..dcfdb8fe4b 100644
--- a/contrib/python/pyparsing/py3/pyparsing/helpers.py
+++ b/contrib/python/pyparsing/py3/pyparsing/helpers.py
@@ -74,7 +74,7 @@ def counted_array(
intExpr = intExpr.copy()
intExpr.set_name("arrayLen")
intExpr.add_parse_action(count_field_parse_action, call_during_try=True)
- return (intExpr + array_expr).set_name("(len) " + str(expr) + "...")
+ return (intExpr + array_expr).set_name(f"(len) {expr}...")
def match_previous_literal(expr: ParserElement) -> ParserElement:
@@ -95,15 +95,17 @@ def match_previous_literal(expr: ParserElement) -> ParserElement:
rep = Forward()
def copy_token_to_repeater(s, l, t):
- if t:
- if len(t) == 1:
- rep << t[0]
- else:
- # flatten t tokens
- tflat = _flatten(t.as_list())
- rep << And(Literal(tt) for tt in tflat)
- else:
+ if not t:
rep << Empty()
+ return
+
+ if len(t) == 1:
+ rep << t[0]
+ return
+
+ # flatten t tokens
+ tflat = _flatten(t.as_list())
+ rep << And(Literal(tt) for tt in tflat)
expr.add_parse_action(copy_token_to_repeater, callDuringTry=True)
rep.set_name("(prev) " + str(expr))
@@ -230,7 +232,7 @@ def one_of(
if isequal(other, cur):
del symbols[i + j + 1]
break
- elif masks(cur, other):
+ if masks(cur, other):
del symbols[i + j + 1]
symbols.insert(i, other)
break
@@ -534,7 +536,9 @@ def nested_expr(
)
else:
ret <<= Group(Suppress(opener) + ZeroOrMore(ret | content) + Suppress(closer))
- ret.set_name("nested %s%s expression" % (opener, closer))
+ ret.set_name(f"nested {opener}{closer} expression")
+ # don't override error message from content expressions
+ ret.errmsg = None
return ret
@@ -580,7 +584,7 @@ def _makeTags(tagStr, xml, suppress_LT=Suppress("<"), suppress_GT=Suppress(">"))
)
closeTag = Combine(Literal("</") + tagStr + ">", adjacent=False)
- openTag.set_name("<%s>" % resname)
+ openTag.set_name(f"<{resname}>")
# add start<tagname> results name in parse action now that ungrouped names are not reported at two levels
openTag.add_parse_action(
lambda t: t.__setitem__(
@@ -589,7 +593,7 @@ def _makeTags(tagStr, xml, suppress_LT=Suppress("<"), suppress_GT=Suppress(">"))
)
closeTag = closeTag(
"end" + "".join(resname.replace(":", " ").title().split())
- ).set_name("</%s>" % resname)
+ ).set_name(f"</{resname}>")
openTag.tag = resname
closeTag.tag = resname
openTag.tag_body = SkipTo(closeTag())
@@ -777,7 +781,7 @@ def infix_notation(
rpar = Suppress(rpar)
# if lpar and rpar are not suppressed, wrap in group
- if not (isinstance(rpar, Suppress) and isinstance(rpar, Suppress)):
+ if not (isinstance(lpar, Suppress) and isinstance(rpar, Suppress)):
lastExpr = base_expr | Group(lpar + ret + rpar)
else:
lastExpr = base_expr | (lpar + ret + rpar)
@@ -787,7 +791,7 @@ def infix_notation(
pa: typing.Optional[ParseAction]
opExpr1: ParserElement
opExpr2: ParserElement
- for i, operDef in enumerate(op_list):
+ for operDef in op_list:
opExpr, arity, rightLeftAssoc, pa = (operDef + (None,))[:4] # type: ignore[assignment]
if isinstance(opExpr, str_type):
opExpr = ParserElement._literalStringClass(opExpr)
@@ -1058,43 +1062,17 @@ dblSlashComment = dbl_slash_comment
cppStyleComment = cpp_style_comment
javaStyleComment = java_style_comment
pythonStyleComment = python_style_comment
-
-@replaced_by_pep8(DelimitedList)
-def delimitedList(): ...
-
-@replaced_by_pep8(DelimitedList)
-def delimited_list(): ...
-
-@replaced_by_pep8(counted_array)
-def countedArray(): ...
-
-@replaced_by_pep8(match_previous_literal)
-def matchPreviousLiteral(): ...
-
-@replaced_by_pep8(match_previous_expr)
-def matchPreviousExpr(): ...
-
-@replaced_by_pep8(one_of)
-def oneOf(): ...
-
-@replaced_by_pep8(dict_of)
-def dictOf(): ...
-
-@replaced_by_pep8(original_text_for)
-def originalTextFor(): ...
-
-@replaced_by_pep8(nested_expr)
-def nestedExpr(): ...
-
-@replaced_by_pep8(make_html_tags)
-def makeHTMLTags(): ...
-
-@replaced_by_pep8(make_xml_tags)
-def makeXMLTags(): ...
-
-@replaced_by_pep8(replace_html_entity)
-def replaceHTMLEntity(): ...
-
-@replaced_by_pep8(infix_notation)
-def infixNotation(): ...
+delimitedList = replaced_by_pep8("delimitedList", DelimitedList)
+delimited_list = replaced_by_pep8("delimited_list", DelimitedList)
+countedArray = replaced_by_pep8("countedArray", counted_array)
+matchPreviousLiteral = replaced_by_pep8("matchPreviousLiteral", match_previous_literal)
+matchPreviousExpr = replaced_by_pep8("matchPreviousExpr", match_previous_expr)
+oneOf = replaced_by_pep8("oneOf", one_of)
+dictOf = replaced_by_pep8("dictOf", dict_of)
+originalTextFor = replaced_by_pep8("originalTextFor", original_text_for)
+nestedExpr = replaced_by_pep8("nestedExpr", nested_expr)
+makeHTMLTags = replaced_by_pep8("makeHTMLTags", make_html_tags)
+makeXMLTags = replaced_by_pep8("makeXMLTags", make_xml_tags)
+replaceHTMLEntity = replaced_by_pep8("replaceHTMLEntity", replace_html_entity)
+infixNotation = replaced_by_pep8("infixNotation", infix_notation)
# fmt: on
diff --git a/contrib/python/pyparsing/py3/pyparsing/results.py b/contrib/python/pyparsing/py3/pyparsing/results.py
index 0313049763..3e5fe2089b 100644
--- a/contrib/python/pyparsing/py3/pyparsing/results.py
+++ b/contrib/python/pyparsing/py3/pyparsing/results.py
@@ -173,42 +173,48 @@ class ParseResults:
):
self._tokdict: Dict[str, _ParseResultsWithOffset]
self._modal = modal
- if name is not None and name != "":
- if isinstance(name, int):
- name = str(name)
- if not modal:
- self._all_names = {name}
- self._name = name
- if toklist not in self._null_values:
- if isinstance(toklist, (str_type, type)):
- toklist = [toklist]
- if asList:
- if isinstance(toklist, ParseResults):
- self[name] = _ParseResultsWithOffset(
- ParseResults(toklist._toklist), 0
- )
- else:
- self[name] = _ParseResultsWithOffset(
- ParseResults(toklist[0]), 0
- )
- self[name]._name = name
- else:
- try:
- self[name] = toklist[0]
- except (KeyError, TypeError, IndexError):
- if toklist is not self:
- self[name] = toklist
- else:
- self._name = name
+
+ if name is None or name == "":
+ return
+
+ if isinstance(name, int):
+ name = str(name)
+
+ if not modal:
+ self._all_names = {name}
+
+ self._name = name
+
+ if toklist in self._null_values:
+ return
+
+ if isinstance(toklist, (str_type, type)):
+ toklist = [toklist]
+
+ if asList:
+ if isinstance(toklist, ParseResults):
+ self[name] = _ParseResultsWithOffset(ParseResults(toklist._toklist), 0)
+ else:
+ self[name] = _ParseResultsWithOffset(ParseResults(toklist[0]), 0)
+ self[name]._name = name
+ return
+
+ try:
+ self[name] = toklist[0]
+ except (KeyError, TypeError, IndexError):
+ if toklist is not self:
+ self[name] = toklist
+ else:
+ self._name = name
def __getitem__(self, i):
if isinstance(i, (int, slice)):
return self._toklist[i]
- else:
- if i not in self._all_names:
- return self._tokdict[i][-1][0]
- else:
- return ParseResults([v[0] for v in self._tokdict[i]])
+
+ if i not in self._all_names:
+ return self._tokdict[i][-1][0]
+
+ return ParseResults([v[0] for v in self._tokdict[i]])
def __setitem__(self, k, v, isinstance=isinstance):
if isinstance(v, _ParseResultsWithOffset):
@@ -226,27 +232,28 @@ class ParseResults:
sub._parent = self
def __delitem__(self, i):
- if isinstance(i, (int, slice)):
- mylen = len(self._toklist)
- del self._toklist[i]
-
- # convert int to slice
- if isinstance(i, int):
- if i < 0:
- i += mylen
- i = slice(i, i + 1)
- # get removed indices
- removed = list(range(*i.indices(mylen)))
- removed.reverse()
- # fixup indices in token dictionary
- for name, occurrences in self._tokdict.items():
- for j in removed:
- for k, (value, position) in enumerate(occurrences):
- occurrences[k] = _ParseResultsWithOffset(
- value, position - (position > j)
- )
- else:
+ if not isinstance(i, (int, slice)):
del self._tokdict[i]
+ return
+
+ mylen = len(self._toklist)
+ del self._toklist[i]
+
+ # convert int to slice
+ if isinstance(i, int):
+ if i < 0:
+ i += mylen
+ i = slice(i, i + 1)
+ # get removed indices
+ removed = list(range(*i.indices(mylen)))
+ removed.reverse()
+ # fixup indices in token dictionary
+ for occurrences in self._tokdict.values():
+ for j in removed:
+ for k, (value, position) in enumerate(occurrences):
+ occurrences[k] = _ParseResultsWithOffset(
+ value, position - (position > j)
+ )
def __contains__(self, k) -> bool:
return k in self._tokdict
@@ -376,7 +383,7 @@ class ParseResults:
"""
self._toklist.insert(index, ins_string)
# fixup indices in token dictionary
- for name, occurrences in self._tokdict.items():
+ for occurrences in self._tokdict.values():
for k, (value, position) in enumerate(occurrences):
occurrences[k] = _ParseResultsWithOffset(
value, position + (position > index)
@@ -652,58 +659,52 @@ class ParseResults:
NL = "\n"
out.append(indent + str(self.as_list()) if include_list else "")
- if full:
- if self.haskeys():
- items = sorted((str(k), v) for k, v in self.items())
- for k, v in items:
- if out:
- out.append(NL)
- out.append(f"{indent}{(' ' * _depth)}- {k}: ")
- if isinstance(v, ParseResults):
- if v:
- out.append(
- v.dump(
- indent=indent,
- full=full,
- include_list=include_list,
- _depth=_depth + 1,
- )
- )
- else:
- out.append(str(v))
- else:
- out.append(repr(v))
- if any(isinstance(vv, ParseResults) for vv in self):
- v = self
- for i, vv in enumerate(v):
- if isinstance(vv, ParseResults):
- out.append(
- "\n{}{}[{}]:\n{}{}{}".format(
- indent,
- (" " * (_depth)),
- i,
- indent,
- (" " * (_depth + 1)),
- vv.dump(
- indent=indent,
- full=full,
- include_list=include_list,
- _depth=_depth + 1,
- ),
- )
- )
- else:
- out.append(
- "\n%s%s[%d]:\n%s%s%s"
- % (
- indent,
- (" " * (_depth)),
- i,
- indent,
- (" " * (_depth + 1)),
- str(vv),
- )
- )
+ if not full:
+ return "".join(out)
+
+ if self.haskeys():
+ items = sorted((str(k), v) for k, v in self.items())
+ for k, v in items:
+ if out:
+ out.append(NL)
+ out.append(f"{indent}{(' ' * _depth)}- {k}: ")
+ if not isinstance(v, ParseResults):
+ out.append(repr(v))
+ continue
+
+ if not v:
+ out.append(str(v))
+ continue
+
+ out.append(
+ v.dump(
+ indent=indent,
+ full=full,
+ include_list=include_list,
+ _depth=_depth + 1,
+ )
+ )
+ if not any(isinstance(vv, ParseResults) for vv in self):
+ return "".join(out)
+
+ v = self
+ incr = " "
+ nl = "\n"
+ for i, vv in enumerate(v):
+ if isinstance(vv, ParseResults):
+ vv_dump = vv.dump(
+ indent=indent,
+ full=full,
+ include_list=include_list,
+ _depth=_depth + 1,
+ )
+ out.append(
+ f"{nl}{indent}{incr * _depth}[{i}]:{nl}{indent}{incr * (_depth + 1)}{vv_dump}"
+ )
+ else:
+ out.append(
+ f"{nl}{indent}{incr * _depth}[{i}]:{nl}{indent}{incr * (_depth + 1)}{vv}"
+ )
return "".join(out)
diff --git a/contrib/python/pyparsing/py3/pyparsing/testing.py b/contrib/python/pyparsing/py3/pyparsing/testing.py
index 6a254c1c5e..5654d47d62 100644
--- a/contrib/python/pyparsing/py3/pyparsing/testing.py
+++ b/contrib/python/pyparsing/py3/pyparsing/testing.py
@@ -1,8 +1,10 @@
# testing.py
from contextlib import contextmanager
+import re
import typing
+
from .core import (
ParserElement,
ParseException,
@@ -49,23 +51,23 @@ class pyparsing_test:
self._save_context["default_whitespace"] = ParserElement.DEFAULT_WHITE_CHARS
self._save_context["default_keyword_chars"] = Keyword.DEFAULT_KEYWORD_CHARS
- self._save_context[
- "literal_string_class"
- ] = ParserElement._literalStringClass
+ self._save_context["literal_string_class"] = (
+ ParserElement._literalStringClass
+ )
self._save_context["verbose_stacktrace"] = ParserElement.verbose_stacktrace
self._save_context["packrat_enabled"] = ParserElement._packratEnabled
if ParserElement._packratEnabled:
- self._save_context[
- "packrat_cache_size"
- ] = ParserElement.packrat_cache.size
+ self._save_context["packrat_cache_size"] = (
+ ParserElement.packrat_cache.size
+ )
else:
self._save_context["packrat_cache_size"] = None
self._save_context["packrat_parse"] = ParserElement._parse
- self._save_context[
- "recursion_enabled"
- ] = ParserElement._left_recursion_enabled
+ self._save_context["recursion_enabled"] = (
+ ParserElement._left_recursion_enabled
+ )
self._save_context["__diag__"] = {
name: getattr(__diag__, name) for name in __diag__._all_names
@@ -180,49 +182,52 @@ class pyparsing_test:
"""
run_test_success, run_test_results = run_tests_report
- if expected_parse_results is not None:
- merged = [
- (*rpt, expected)
- for rpt, expected in zip(run_test_results, expected_parse_results)
- ]
- for test_string, result, expected in merged:
- # expected should be a tuple containing a list and/or a dict or an exception,
- # and optional failure message string
- # an empty tuple will skip any result validation
- fail_msg = next(
- (exp for exp in expected if isinstance(exp, str)), None
+ if expected_parse_results is None:
+ self.assertTrue(
+ run_test_success, msg=msg if msg is not None else "failed runTests"
+ )
+ return
+
+ merged = [
+ (*rpt, expected)
+ for rpt, expected in zip(run_test_results, expected_parse_results)
+ ]
+ for test_string, result, expected in merged:
+ # expected should be a tuple containing a list and/or a dict or an exception,
+ # and optional failure message string
+ # an empty tuple will skip any result validation
+ fail_msg = next((exp for exp in expected if isinstance(exp, str)), None)
+ expected_exception = next(
+ (
+ exp
+ for exp in expected
+ if isinstance(exp, type) and issubclass(exp, Exception)
+ ),
+ None,
+ )
+ if expected_exception is not None:
+ with self.assertRaises(
+ expected_exception=expected_exception, msg=fail_msg or msg
+ ):
+ if isinstance(result, Exception):
+ raise result
+ else:
+ expected_list = next(
+ (exp for exp in expected if isinstance(exp, list)), None
)
- expected_exception = next(
- (
- exp
- for exp in expected
- if isinstance(exp, type) and issubclass(exp, Exception)
- ),
- None,
+ expected_dict = next(
+ (exp for exp in expected if isinstance(exp, dict)), None
)
- if expected_exception is not None:
- with self.assertRaises(
- expected_exception=expected_exception, msg=fail_msg or msg
- ):
- if isinstance(result, Exception):
- raise result
- else:
- expected_list = next(
- (exp for exp in expected if isinstance(exp, list)), None
+ if (expected_list, expected_dict) != (None, None):
+ self.assertParseResultsEquals(
+ result,
+ expected_list=expected_list,
+ expected_dict=expected_dict,
+ msg=fail_msg or msg,
)
- expected_dict = next(
- (exp for exp in expected if isinstance(exp, dict)), None
- )
- if (expected_list, expected_dict) != (None, None):
- self.assertParseResultsEquals(
- result,
- expected_list=expected_list,
- expected_dict=expected_dict,
- msg=fail_msg or msg,
- )
- else:
- # warning here maybe?
- print(f"no validation for {test_string!r}")
+ else:
+ # warning here maybe?
+ print(f"no validation for {test_string!r}")
# do this last, in case some specific test results can be reported instead
self.assertTrue(
@@ -230,9 +235,18 @@ class pyparsing_test:
)
@contextmanager
- def assertRaisesParseException(self, exc_type=ParseException, msg=None):
- with self.assertRaises(exc_type, msg=msg):
- yield
+ def assertRaisesParseException(
+ self, exc_type=ParseException, expected_msg=None, msg=None
+ ):
+ if expected_msg is not None:
+ if isinstance(expected_msg, str):
+ expected_msg = re.escape(expected_msg)
+ with self.assertRaisesRegex(exc_type, expected_msg, msg=msg) as ctx:
+ yield ctx
+
+ else:
+ with self.assertRaises(exc_type, msg=msg) as ctx:
+ yield ctx
@staticmethod
def with_line_numbers(
diff --git a/contrib/python/pyparsing/py3/pyparsing/unicode.py b/contrib/python/pyparsing/py3/pyparsing/unicode.py
index b0a87b235b..426b8b238c 100644
--- a/contrib/python/pyparsing/py3/pyparsing/unicode.py
+++ b/contrib/python/pyparsing/py3/pyparsing/unicode.py
@@ -102,17 +102,10 @@ class unicode_set:
all characters in this range that are valid identifier body characters,
plus the digits 0-9, and · (Unicode MIDDLE DOT)
"""
- return "".join(
- sorted(
- set(
- cls.identchars
- + "0123456789·"
- + "".join(
- [c for c in cls._chars_for_ranges if ("_" + c).isidentifier()]
- )
- )
- )
+ identifier_chars = set(
+ c for c in cls._chars_for_ranges if ("_" + c).isidentifier()
)
+ return "".join(sorted(identifier_chars | set(cls.identchars + "0123456789·")))
@_lazyclassproperty
def identifier(cls):
diff --git a/contrib/python/pyparsing/py3/pyparsing/util.py b/contrib/python/pyparsing/py3/pyparsing/util.py
index d8d3f414cc..4ae018a963 100644
--- a/contrib/python/pyparsing/py3/pyparsing/util.py
+++ b/contrib/python/pyparsing/py3/pyparsing/util.py
@@ -237,7 +237,7 @@ def _flatten(ll: list) -> list:
return ret
-def _make_synonym_function(compat_name: str, fn: C) -> C:
+def replaced_by_pep8(compat_name: str, fn: C) -> C:
# In a future version, uncomment the code in the internal _inner() functions
# to begin emitting DeprecationWarnings.
@@ -251,7 +251,7 @@ def _make_synonym_function(compat_name: str, fn: C) -> C:
@wraps(fn)
def _inner(self, *args, **kwargs):
# warnings.warn(
- # f"Deprecated - use {fn.__name__}", DeprecationWarning, stacklevel=3
+ # f"Deprecated - use {fn.__name__}", DeprecationWarning, stacklevel=2
# )
return fn(self, *args, **kwargs)
@@ -260,7 +260,7 @@ def _make_synonym_function(compat_name: str, fn: C) -> C:
@wraps(fn)
def _inner(*args, **kwargs):
# warnings.warn(
- # f"Deprecated - use {fn.__name__}", DeprecationWarning, stacklevel=3
+ # f"Deprecated - use {fn.__name__}", DeprecationWarning, stacklevel=2
# )
return fn(*args, **kwargs)
@@ -275,10 +275,3 @@ def _make_synonym_function(compat_name: str, fn: C) -> C:
_inner.__kwdefaults__ = None
_inner.__qualname__ = fn.__qualname__
return cast(C, _inner)
-
-
-def replaced_by_pep8(fn: C) -> Callable[[Callable], C]:
- """
- Decorator for pre-PEP8 compatibility synonyms, to link them to the new function.
- """
- return lambda other: _make_synonym_function(other.__name__, fn)
diff --git a/contrib/python/pyparsing/py3/ya.make b/contrib/python/pyparsing/py3/ya.make
index fe1bfb51db..9df0a0f5fa 100644
--- a/contrib/python/pyparsing/py3/ya.make
+++ b/contrib/python/pyparsing/py3/ya.make
@@ -4,7 +4,7 @@ PY3_LIBRARY()
PROVIDES(pyparsing)
-VERSION(3.1.1)
+VERSION(3.1.2)
LICENSE(MIT)