🌐 AI搜索 & 代理 主页
Skip to content

Commit ed8ba52

Browse files
committed
core.py - remove extra layers of Suppress wrappers on ignorables; change ignoreExprs from list to set
1 parent 80ed92b commit ed8ba52

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

CHANGES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ Version 3.3.0 - under development
9898
(3.1.1 through 3.3.0), aggregating results into `perf_pyparsing.csv` at the repo root.
9999
- See `tests/README.md` for usage instructions.
100100

101+
(added in 3.3.0 final)
102+
103+
- Minor performance enhancement when assigning ignorable expressions using `ParserElement.ignore()`.
104+
101105

102106
Version 3.2.5 - September, 2025
103107
-------------------------------

pyparsing/core.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ def __init__(self, savelist: bool = False) -> None:
495495
# used when checking for left-recursion
496496
self._may_return_empty: bool = False
497497
self.keepTabs: bool = False
498-
self.ignoreExprs: list[ParserElement] = list()
498+
self.ignoreExprs: set[ParserElement] = set()
499499
self.debug: bool = False
500500
self.streamlined: bool = False
501501
# optimize exception handling for subclasses that don't advance parse index
@@ -603,7 +603,7 @@ def copy(self) -> ParserElement:
603603
"""
604604
cpy = copy.copy(self)
605605
cpy.parseAction = self.parseAction[:]
606-
cpy.ignoreExprs = self.ignoreExprs[:]
606+
cpy.ignoreExprs = self.ignoreExprs.copy()
607607
if self.copyDefaultWhiteChars:
608608
cpy.whiteChars = set(ParserElement.DEFAULT_WHITE_CHARS)
609609
return cpy
@@ -878,7 +878,7 @@ def _skipIgnorables(self, instring: str, loc: int) -> int:
878878
for ignore_fn in ignore_expr_fns:
879879
try:
880880
while 1:
881-
loc, dummy = ignore_fn(instring, loc)
881+
loc, _ = ignore_fn(instring, loc)
882882
exprsFound = True
883883
except ParseException:
884884
pass
@@ -1997,11 +1997,8 @@ def ignore(self, other: ParserElement) -> ParserElement:
19971997
if isinstance(other, str_type):
19981998
other = Suppress(other)
19991999

2000-
if isinstance(other, Suppress):
2001-
if other not in self.ignoreExprs:
2002-
self.ignoreExprs.append(other)
2003-
else:
2004-
self.ignoreExprs.append(Suppress(other.copy()))
2000+
if other is not self:
2001+
self.ignoreExprs.add(other)
20052002
return self
20062003

20072004
def set_debug_actions(
@@ -4357,15 +4354,10 @@ def ignore(self, other) -> ParserElement:
43574354
matching; may be called repeatedly, to define multiple comment or other
43584355
ignorable patterns.
43594356
"""
4360-
if isinstance(other, Suppress):
4361-
if other not in self.ignoreExprs:
4362-
super().ignore(other)
4363-
for e in self.exprs:
4364-
e.ignore(self.ignoreExprs[-1])
4365-
else:
4366-
super().ignore(other)
4367-
for e in self.exprs:
4368-
e.ignore(self.ignoreExprs[-1])
4357+
super().ignore(other)
4358+
for e in self.exprs:
4359+
if other not in e.ignoreExprs:
4360+
e.ignore(other)
43694361
return self
43704362

43714363
def _generateDefaultName(self) -> str:
@@ -5142,7 +5134,7 @@ def __init__(self, expr: Union[ParserElement, str], savelist: bool = False) -> N
51425134
self.skipWhitespace = expr.skipWhitespace
51435135
self.saveAsList = expr.saveAsList
51445136
self.callPreparse = expr.callPreparse
5145-
self.ignoreExprs.extend(expr.ignoreExprs)
5137+
self.ignoreExprs |= expr.ignoreExprs
51465138

51475139
def recurse(self) -> list[ParserElement]:
51485140
return [self.expr] if self.expr is not None else []
@@ -5196,10 +5188,9 @@ def ignore(self, other) -> ParserElement:
51965188
matching; may be called repeatedly, to define multiple comment or other
51975189
ignorable patterns.
51985190
"""
5199-
if not isinstance(other, Suppress) or other not in self.ignoreExprs:
5200-
super().ignore(other)
5201-
if self.expr is not None:
5202-
self.expr.ignore(self.ignoreExprs[-1])
5191+
super().ignore(other)
5192+
if self.expr is not None:
5193+
self.expr.ignore(other)
52035194

52045195
return self
52055196

@@ -6189,7 +6180,7 @@ def __lshift__(self, other) -> Forward:
61896180
)
61906181
self.skipWhitespace = self.expr.skipWhitespace
61916182
self.saveAsList = self.expr.saveAsList
6192-
self.ignoreExprs.extend(self.expr.ignoreExprs)
6183+
self.ignoreExprs |= self.expr.ignoreExprs
61936184
self.lshift_line = traceback.extract_stack(limit=2)[-2] # type: ignore[assignment]
61946185
return self
61956186

@@ -6480,7 +6471,8 @@ def ignore(self, other) -> ParserElement:
64806471
if self.adjacent:
64816472
ParserElement.ignore(self, other)
64826473
else:
6483-
super().ignore(other)
6474+
if other is not self:
6475+
super().ignore(other)
64846476
return self
64856477

64866478
def postParse(self, instring, loc, tokenlist):

0 commit comments

Comments
 (0)