From 02377aab0865e107ab5bea4ad8963199033d52a3 Mon Sep 17 00:00:00 2001 From: Gabriel Gerlero Date: Sat, 16 Nov 2024 19:46:54 -0300 Subject: [PATCH] Update parsing --- foamlib/_files/_parsing.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/foamlib/_files/_parsing.py b/foamlib/_files/_parsing.py index 1cdcdfa..ed4a74a 100644 --- a/foamlib/_files/_parsing.py +++ b/foamlib/_files/_parsing.py @@ -59,18 +59,20 @@ def _list_of(entry: ParserElement) -> ParserElement: ) -def _counted_tensor_list(*, size: int, ignore: Regex) -> ParserElement: +def _counted_tensor_list( + *, elsize: int = 1, ignore: Regex | None = None +) -> ParserElement: float_pattern = r"(?i:[+-]?((\d+\.?\d*(e[+-]?\d+)?)|nan|inf(inity)?))" - ignore_pattern = rf"(?:\s|{ignore.re.pattern})+" + ignore_pattern = rf"(?:\s|{ignore.re.pattern})+" if ignore is not None else r"\s+" - if size == 1: + if elsize == 1: tensor_pattern = float_pattern tensor = common.ieee_float else: - tensor_pattern = rf"\((?:{ignore_pattern})?(?:{float_pattern}{ignore_pattern}){{{size - 1}}}{float_pattern}(?:{ignore_pattern})?\)" + tensor_pattern = rf"\((?:{ignore_pattern})?(?:{float_pattern}{ignore_pattern}){{{elsize - 1}}}{float_pattern}(?:{ignore_pattern})?\)" tensor = ( Literal("(").suppress() - + Group(common.ieee_float[size], aslist=True) + + Group(common.ieee_float[elsize], aslist=True) + Literal(")").suppress() ) @@ -92,16 +94,16 @@ def list_parse_action( ) -> list[list[float]] | list[list[list[float]]]: values = [ float(v) - for v in re.sub(ignore.re, " ", tks[0]) + for v in (re.sub(ignore.re, " ", tks[0]) if ignore is not None else tks[0]) .replace("(", " ") .replace(")", " ") .split() ] - if size == 1: + if elsize == 1: return [values] - return [[values[i : i + size] for i in range(0, len(values), size)]] + return [[values[i : i + elsize] for i in range(0, len(values), elsize)]] list_.add_parse_action(list_parse_action) @@ -202,7 +204,7 @@ def _unpack_binary_field( Literal("scalar").suppress() + Literal(">").suppress() + ( - _counted_tensor_list(size=1, ignore=_COMMENT) + _counted_tensor_list(elsize=1, ignore=_COMMENT) | ( ( ( @@ -226,7 +228,7 @@ def _unpack_binary_field( Literal("vector").suppress() + Literal(">").suppress() + ( - _counted_tensor_list(size=3, ignore=_COMMENT) + _counted_tensor_list(elsize=3, ignore=_COMMENT) | ( ( ( @@ -250,7 +252,7 @@ def _unpack_binary_field( Literal("symmTensor").suppress() + Literal(">").suppress() + ( - _counted_tensor_list(size=6, ignore=_COMMENT) + _counted_tensor_list(elsize=6, ignore=_COMMENT) | ( ( ( @@ -274,7 +276,7 @@ def _unpack_binary_field( Literal("tensor").suppress() + Literal(">").suppress() + ( - _counted_tensor_list(size=9, ignore=_COMMENT) + _counted_tensor_list(elsize=9, ignore=_COMMENT) | ( ( (