From c0ebd15deec0a986cc234c40640e142e687676b2 Mon Sep 17 00:00:00 2001 From: Gabriel Gerlero Date: Thu, 28 Mar 2024 16:34:39 -0300 Subject: [PATCH] Update parsing --- foamlib/_dictionaries.py | 60 +++++++++++++++++++++----------------- tests/test_dictionaries.py | 8 +++++ 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/foamlib/_dictionaries.py b/foamlib/_dictionaries.py index eece92b..81ddce9 100644 --- a/foamlib/_dictionaries.py +++ b/foamlib/_dictionaries.py @@ -75,8 +75,7 @@ def _cmd(self, args: Sequence[str], *, key: Optional[str] = None) -> str: ) from None def __getitem__(self, key: str) -> Union["FoamFile.Value", "_FoamDictionary"]: - contents = self._file.path.read_text() - value = _DICTIONARY.parse_string(contents, parse_all=True).as_dict() + value = _DICTIONARY.parse_file(self._file.path, parse_all=True).as_dict() for key in [*self._keywords, key]: value = value[key] @@ -356,8 +355,15 @@ def boundary_field(self) -> "FoamFieldFile.BoundariesDictionary": ) | ( common.integer + Literal("{").suppress() + _ITEM + Literal("}").suppress() - ).set_parse_action(lambda tks: [tks[1]] * tks[0]) + ).set_parse_action(lambda tks: [[tks[1].as_list()] * tks[0]]) +) +_DIMENSIONED = (Opt(common.identifier) + _DIMENSIONS + _ITEM).set_parse_action( + lambda tks: FoamFile.Dimensioned(*reversed(tks.as_list())) +) +_ITEM <<= ( + _LIST | _SUBDICT | _DIMENSIONED | _DIMENSIONS | common.number | _YES | _NO | _TOKEN ) + _FIELD = ( Keyword("uniform").suppress() + ( @@ -371,43 +377,45 @@ def boundary_field(self) -> "FoamFieldFile.BoundariesDictionary": ) | ( Keyword("nonuniform").suppress() + Opt(Literal("List") + Literal("<") + common.identifier + Literal(">")).suppress() - + Opt(common.integer).suppress() + ( - Literal("(").suppress() - + Group( - ( + ( + Opt(common.integer).suppress() + + ( + Literal("(").suppress() + + Group( + ( + common.number + | ( + Literal("(").suppress() + + Group(common.number[3, 9]) + + Literal(")").suppress() + ) + )[...] + ) + + Literal(")").suppress() + ) + ) + | ( + common.integer + + Literal("{").suppress() + + ( common.number | ( Literal("(").suppress() + Group(common.number[3, 9]) + Literal(")").suppress() ) - )[...] - ) - + Literal(")").suppress() + ) + + Literal("}").suppress() + ).set_parse_action(lambda tks: [[tks[1].as_list()] * tks[0]]) ) ) -_DIMENSIONED = (Opt(common.identifier) + _DIMENSIONS + _ITEM).set_parse_action( - lambda tks: FoamFile.Dimensioned(*reversed(tks.as_list())) -) -_ITEM <<= ( - _FIELD - | _LIST - | _SUBDICT - | _DIMENSIONED - | _DIMENSIONS - | common.number - | _YES - | _NO - | _TOKEN -) - _TOKENS = ( QuotedString('"', unquote_results=False) | Word(printables.replace(";", "").replace("{", "").replace("}", "")) )[2, ...].set_parse_action(lambda tks: " ".join(tks)) -_VALUE = (_ITEM ^ _TOKENS).ignore(c_style_comment).ignore(cpp_style_comment) +_VALUE = ((_FIELD | _ITEM) ^ _TOKENS).ignore(c_style_comment).ignore(cpp_style_comment) _KEYWORD = QuotedString('"', unquote_results=False) | Word( identchars + "$(,.)", identbodychars + "$(,.)" diff --git a/tests/test_dictionaries.py b/tests/test_dictionaries.py index aa9ff5e..04e1ed7 100644 --- a/tests/test_dictionaries.py +++ b/tests/test_dictionaries.py @@ -30,12 +30,20 @@ def test_parse_value() -> None: [1, 2, 3], [4, 5, 6], ] + assert _VALUE.parse_string("2{(1 2 3)}").as_list()[0] == [ + [1, 2, 3], + [1, 2, 3], + ] assert _VALUE.parse_string("nonuniform List 2((1 2 3) (4 5 6))").as_list()[ 0 ] == [ [1, 2, 3], [4, 5, 6], ] + assert _VALUE.parse_string("nonuniform List 2{(1 2 3)}").as_list()[0] == [ + [1, 2, 3], + [1, 2, 3], + ] assert _VALUE.parse_string("[1 1 -2 0 0 0 0]").as_list()[ 0 ] == FoamFile.DimensionSet(mass=1, length=1, time=-2)