diff options
author | ptmcg <ptmcg@austin.rr.com> | 2020-10-25 14:42:07 -0500 |
---|---|---|
committer | ptmcg <ptmcg@austin.rr.com> | 2020-10-25 14:42:07 -0500 |
commit | 27dc324608a8c83afa47b296c52b7d6c9aa8795e (patch) | |
tree | 39161e2775e938e75a2e81842e4703e57356f246 | |
parent | 7f68a2aa4386e8a075aabc92ca8b6582bcc25a42 (diff) | |
download | pyparsing-git-27dc324608a8c83afa47b296c52b7d6c9aa8795e.tar.gz |
minor perf changes II
-rw-r--r-- | pyparsing/helpers.py | 12 | ||||
-rw-r--r-- | pyparsing/results.py | 4 | ||||
-rw-r--r-- | pyparsing/util.py | 8 |
3 files changed, 14 insertions, 10 deletions
diff --git a/pyparsing/helpers.py b/pyparsing/helpers.py index 00f311b..d2325c9 100644 --- a/pyparsing/helpers.py +++ b/pyparsing/helpers.py @@ -680,6 +680,8 @@ def infixNotation(baseExpr, opList, lpar=Suppress("("), rpar=Suppress(")")): lastExpr = baseExpr | (lpar + ret + rpar) for i, operDef in enumerate(opList): opExpr, arity, rightLeftAssoc, pa = (operDef + (None,))[:4] + if isinstance(opExpr, str_type): + opExpr = ParserElement._literalStringClass(opExpr) if arity == 3: if not isinstance(opExpr, (tuple, list)) or len(opExpr) != 2: raise ValueError( @@ -697,15 +699,15 @@ def infixNotation(baseExpr, opList, lpar=Suppress("("), rpar=Suppress(")")): thisExpr = Forward().setName(termName) if rightLeftAssoc is opAssoc.LEFT: if arity == 1: - matchExpr = _FB(lastExpr + opExpr) + Group(lastExpr + OneOrMore(opExpr)) + matchExpr = _FB(lastExpr + opExpr) + Group(lastExpr + opExpr + opExpr[...]) elif arity == 2: if opExpr is not None: matchExpr = _FB(lastExpr + opExpr + lastExpr) + Group( - lastExpr + OneOrMore(opExpr + lastExpr) + lastExpr + opExpr + lastExpr + (opExpr + lastExpr)[...] ) else: matchExpr = _FB(lastExpr + lastExpr) + Group( - lastExpr + OneOrMore(lastExpr) + lastExpr + lastExpr + lastExpr[...] ) elif arity == 3: matchExpr = _FB( @@ -720,11 +722,11 @@ def infixNotation(baseExpr, opList, lpar=Suppress("("), rpar=Suppress(")")): elif arity == 2: if opExpr is not None: matchExpr = _FB(lastExpr + opExpr + thisExpr) + Group( - lastExpr + OneOrMore(opExpr + thisExpr) + lastExpr + opExpr + thisExpr + (opExpr + thisExpr)[...] ) else: matchExpr = _FB(lastExpr + thisExpr) + Group( - lastExpr + OneOrMore(thisExpr) + lastExpr + thisExpr + thisExpr[...] ) elif arity == 3: matchExpr = _FB( diff --git a/pyparsing/results.py b/pyparsing/results.py index a375b07..300ef92 100644 --- a/pyparsing/results.py +++ b/pyparsing/results.py @@ -141,8 +141,8 @@ class ParseResults: self._all_names = set() if toklist is None: - toklist = [] - if isinstance(toklist, (list, _generator_type)): + self._toklist = [] + elif isinstance(toklist, (list, _generator_type)): self._toklist = [toklist[:]] if isinstance(toklist, ParseResults.List) else list(toklist) else: self._toklist = [toklist] diff --git a/pyparsing/util.py b/pyparsing/util.py index 152316c..3cb69d2 100644 --- a/pyparsing/util.py +++ b/pyparsing/util.py @@ -143,14 +143,16 @@ def _collapseStringToRanges(s, re_escape=True): def escape_re_range_char(c): return "\\" + c if c in r"\^-][" else c + def no_escape_re_range_char(c): + return c + if not re_escape: - escape_re_range_char = lambda c: c + escape_re_range_char = no_escape_re_range_char ret = [] for _, chars in itertools.groupby(sorted(s), key=is_consecutive): first = last = next(chars) - for c in chars: - last = c + last = collections.deque(itertools.chain(iter([last]), chars), maxlen=1).pop() if first == last: ret.append(escape_re_range_char(first)) else: |