From a5ef1f7a4b70d65dba3097317ab2ca7fa9ac7d75 Mon Sep 17 00:00:00 2001 From: Gabriel Gerlero Date: Thu, 5 Sep 2024 23:07:50 -0300 Subject: [PATCH] Improve parsing --- foamlib/_files/_parsing.py | 6 +++++- tests/test_files/test_dumpb.py | 2 ++ tests/test_files/test_parsing.py | 2 ++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/foamlib/_files/_parsing.py b/foamlib/_files/_parsing.py index 08fbf72..a6f13b2 100644 --- a/foamlib/_files/_parsing.py +++ b/foamlib/_files/_parsing.py @@ -14,6 +14,7 @@ from pyparsing import ( CharsNotIn, + Combine, Dict, Forward, Group, @@ -134,7 +135,10 @@ def unpack( Literal("[").suppress() + common.number * 7 + Literal("]").suppress() ).set_parse_action(lambda tks: FoamFileBase.DimensionSet(*tks)) _TENSOR = _list_of(common.number) | common.number -_IDENTIFIER = Word(identchars + "$", printables, exclude_chars="{;}") +_IDENTIFIER = Combine( + Word(identchars + "$", printables, exclude_chars="{(;)}") + + Opt(Literal("(") + Word(printables, exclude_chars="{(;)}") + Literal(")")) +) _DIMENSIONED = (Opt(_IDENTIFIER) + _DIMENSIONS + _TENSOR).set_parse_action( lambda tks: FoamFileBase.Dimensioned(*reversed(tks.as_list())) ) diff --git a/tests/test_files/test_dumpb.py b/tests/test_files/test_dumpb.py index 4e19295..b19a7c3 100644 --- a/tests/test_files/test_dumpb.py +++ b/tests/test_files/test_dumpb.py @@ -63,3 +63,5 @@ def test_serialize_data() -> None: assert dumpb([{"a": "b"}, {"c": "d"}]) == b"(a b; c d;)" assert dumpb([{"a": {"b": "c"}}, {"d": {"e": "g"}}]) == b"(a {b c;} d {e g;})" assert dumpb([{"a": [0, 1, 2]}, {"b": {}}]) == b"(a (0 1 2); b {})" + assert dumpb(["water", "oil", "mercury", "air"]) == b"(water oil mercury air)" + assert dumpb("div(phi,U)") == b"div(phi,U)" diff --git a/tests/test_files/test_parsing.py b/tests/test_files/test_parsing.py index e1a9f03..0c06329 100644 --- a/tests/test_files/test_parsing.py +++ b/tests/test_files/test_parsing.py @@ -65,3 +65,5 @@ def test_parse_value() -> None: {"d": {"e": "g"}}, ] assert Parsed(b"(a (0 1 2); b {})")[()] == [{"a": [0, 1, 2]}, {"b": {}}] + assert Parsed(b"(water oil mercury air)")[()] == ["water", "oil", "mercury", "air"] + assert Parsed(b"div(phi,U)")[()] == "div(phi,U)"