Skip to content

Commit a81bb20

Browse files
authored
Merge pull request #587 from gerlero/files
Update custom ParserElements
2 parents 512f4e9 + d433b16 commit a81bb20

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

foamlib/_files/_parsing/_elements.py

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
ParserElement,
1717
ParseResults,
1818
Regex,
19+
Suppress,
1920
common,
2021
counted_array,
2122
)
@@ -57,15 +58,11 @@ def _generateDefaultName(self) -> str:
5758
def parseImpl(
5859
self, instring: str, loc: int, doActions: bool = True
5960
) -> tuple[int, ParseResults]:
60-
ignore_pattern = (
61-
"|".join(
62-
rf"(?:{ignore_expr.expr.re.pattern})" # type: ignore [attr-defined]
63-
for ignore_expr in ignore_exprs
64-
)
65-
if (ignore_exprs := self.ignoreExprs)
66-
else ""
67-
)
68-
spacing_pattern = rf"\s|(?:{ignore_pattern})" if ignore_pattern else r"\s"
61+
spacing_pattern = "|".join(re.escape(c) for c in self.whiteChars)
62+
for ignore_expr in self.ignoreExprs:
63+
assert isinstance(ignore_expr, Suppress)
64+
assert isinstance(ignore_expr.expr, Regex)
65+
spacing_pattern += f"|(?:{ignore_expr.expr.re.pattern})"
6966

7067
if np.issubdtype(self._dtype, np.floating):
7168
base_pattern = self._FLOAT_PATTERN
@@ -89,8 +86,7 @@ def parseImpl(
8986
if match := regular_pattern.match(instring, pos=loc):
9087
count = int(c) if (c := match.group(1)) else None
9188
contents = match.group(2)
92-
if ignore_pattern:
93-
contents = re.sub(ignore_pattern, " ", contents)
89+
contents = re.sub(spacing_pattern, " ", contents)
9490
if self._elshape:
9591
contents = contents.replace("(", " ").replace(")", " ")
9692

@@ -127,8 +123,7 @@ def parseImpl(
127123
if match := repeated_pattern.match(instring, pos=loc):
128124
count = int(match.group(1))
129125
contents = match.group(2)
130-
if ignore_pattern:
131-
contents = re.sub(ignore_pattern, " ", contents)
126+
contents = re.sub(spacing_pattern, " ", contents)
132127
if self._elshape:
133128
contents = contents.replace("(", " ").replace(")", " ")
134129

@@ -210,15 +205,11 @@ def _generateDefaultName(self) -> str:
210205
def parseImpl(
211206
self, instring: str, loc: int, doActions: bool = True
212207
) -> tuple[int, ParseResults]:
213-
ignore_pattern = (
214-
"|".join(
215-
rf"(?:{ignore_expr.expr.re.pattern})" # type: ignore [attr-defined]
216-
for ignore_expr in ignore_exprs
217-
)
218-
if (ignore_exprs := self.ignoreExprs)
219-
else ""
220-
)
221-
spacing_pattern = rf"\s|(?:{ignore_pattern})" if ignore_pattern else r"\s"
208+
spacing_pattern = "|".join(re.escape(c) for c in self.whiteChars)
209+
for ignore_expr in self.ignoreExprs:
210+
assert isinstance(ignore_expr, Suppress)
211+
assert isinstance(ignore_expr.expr, Regex)
212+
spacing_pattern += f"|(?:{ignore_expr.expr.re.pattern})"
222213

223214
three_face_pattern = rf"3(?:{spacing_pattern})*\((?:{spacing_pattern})*(?:{self._INT_PATTERN}(?:{spacing_pattern})*){{3}}\)"
224215
four_face_pattern = rf"4(?:{spacing_pattern})*\((?:{spacing_pattern})*(?:{self._INT_PATTERN}(?:{spacing_pattern})*){{4}}\)"
@@ -233,8 +224,7 @@ def parseImpl(
233224
if match := face_list_pattern.match(instring, pos=loc):
234225
count = int(c) if (c := match.group(1)) else None
235226
contents = match.group(2)
236-
if ignore_pattern:
237-
contents = re.sub(ignore_pattern, " ", contents)
227+
contents = re.sub(spacing_pattern, " ", contents)
238228
contents = contents.replace("(", " ").replace(")", " ")
239229

240230
raw = np.fromstring(contents, sep=" ", dtype=int)

0 commit comments

Comments
 (0)