Skip to content

Commit

Permalink
Improve parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
gerlero committed Sep 6, 2024
1 parent 1d9abb2 commit a5ef1f7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion foamlib/_files/_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from pyparsing import (
CharsNotIn,
Combine,
Dict,
Forward,
Group,
Expand Down Expand Up @@ -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()))
)
Expand Down
2 changes: 2 additions & 0 deletions tests/test_files/test_dumpb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
2 changes: 2 additions & 0 deletions tests/test_files/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)"

0 comments on commit a5ef1f7

Please sign in to comment.