Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commited8ba52

Browse files
committed
core.py - remove extra layers of Suppress wrappers on ignorables; change ignoreExprs from list to set
1 parent80ed92b commited8ba52

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
ifself.copyDefaultWhiteChars:
608608
cpy.whiteChars=set(ParserElement.DEFAULT_WHITE_CHARS)
609609
returncpy
@@ -878,7 +878,7 @@ def _skipIgnorables(self, instring: str, loc: int) -> int:
878878
forignore_fninignore_expr_fns:
879879
try:
880880
while1:
881-
loc,dummy=ignore_fn(instring,loc)
881+
loc,_=ignore_fn(instring,loc)
882882
exprsFound=True
883883
exceptParseException:
884884
pass
@@ -1997,11 +1997,8 @@ def ignore(self, other: ParserElement) -> ParserElement:
19971997
ifisinstance(other,str_type):
19981998
other=Suppress(other)
19991999

2000-
ifisinstance(other,Suppress):
2001-
ifothernotinself.ignoreExprs:
2002-
self.ignoreExprs.append(other)
2003-
else:
2004-
self.ignoreExprs.append(Suppress(other.copy()))
2000+
ifotherisnotself:
2001+
self.ignoreExprs.add(other)
20052002
returnself
20062003

20072004
defset_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-
ifisinstance(other,Suppress):
4361-
ifothernotinself.ignoreExprs:
4362-
super().ignore(other)
4363-
foreinself.exprs:
4364-
e.ignore(self.ignoreExprs[-1])
4365-
else:
4366-
super().ignore(other)
4367-
foreinself.exprs:
4368-
e.ignore(self.ignoreExprs[-1])
4357+
super().ignore(other)
4358+
foreinself.exprs:
4359+
ifothernotine.ignoreExprs:
4360+
e.ignore(other)
43694361
returnself
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
defrecurse(self)->list[ParserElement]:
51485140
return [self.expr]ifself.exprisnotNoneelse []
@@ -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-
ifnotisinstance(other,Suppress)orothernotinself.ignoreExprs:
5200-
super().ignore(other)
5201-
ifself.exprisnotNone:
5202-
self.expr.ignore(self.ignoreExprs[-1])
5191+
super().ignore(other)
5192+
ifself.exprisnotNone:
5193+
self.expr.ignore(other)
52035194

52045195
returnself
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
returnself
61956186

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

64866478
defpostParse(self,instring,loc,tokenlist):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp