Skip to content

Commit

Permalink
Merge pull request #252 from gerlero/files
Browse files Browse the repository at this point in the history
Improve field parsing
  • Loading branch information
gerlero authored Nov 9, 2024
2 parents 05f561c + 524d1be commit 5a133cb
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 26 deletions.
101 changes: 75 additions & 26 deletions foamlib/_files/_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ def _keyword_entry_of(

def _unpack_binary_field(
tks: ParseResults,
*,
elsize: int = 1,
) -> Sequence[Sequence[float] | Sequence[Sequence[float]]]:
elsize = len(tks[0]) // 8
float_size = len(tks[0]) // elsize

arr = array.array("d", "".join(tks).encode("latin-1"))
arr = array.array("f" if float_size == 4 else "d", "".join(tks).encode("latin-1"))

values: Sequence[float] | Sequence[Sequence[float]]

Expand Down Expand Up @@ -130,45 +132,92 @@ def _unpack_binary_field(
_DIMENSIONED = (Opt(_IDENTIFIER) + _DIMENSIONS + _TENSOR).set_parse_action(
lambda tks: FoamFileBase.Dimensioned(*reversed(tks.as_list()))
)
_FIELD = (Keyword("uniform").suppress() + _TENSOR) | (
Keyword("nonuniform").suppress()
_FIELD = (Keyword("uniform", _IDENTBODYCHARS).suppress() + _TENSOR) | (
Keyword("nonuniform", _IDENTBODYCHARS).suppress()
+ (
_list_of(_TENSOR)
| (
Literal("List").suppress()
+ Literal("<").suppress()
+ (
counted_array(
CharsNotIn(exact=8),
(
Literal("scalar").suppress()
+ Literal(">").suppress()
+ common.integer
+ Literal("(").suppress(),
)
| counted_array(
CharsNotIn(exact=8 * 3),
+ (
(
counted_array(
CharsNotIn(exact=8),
common.integer + Literal("(").suppress(),
)
)
| (
counted_array(
CharsNotIn(exact=4),
common.integer + Literal("(").suppress(),
)
)
)
+ Literal(")").suppress()
).set_parse_action(_unpack_binary_field)
| (
Literal("vector").suppress()
+ Literal(">").suppress()
+ common.integer
+ Literal("(").suppress(),
)
| counted_array(
CharsNotIn(exact=8 * 6),
+ (
(
counted_array(
CharsNotIn(exact=8 * 3),
common.integer + Literal("(").suppress(),
)
)
| (
counted_array(
CharsNotIn(exact=4 * 3),
common.integer + Literal("(").suppress(),
)
)
)
+ Literal(")").suppress()
).set_parse_action(lambda tks: _unpack_binary_field(tks, elsize=3))
| (
Literal("symmTensor").suppress()
+ Literal(">").suppress()
+ common.integer
+ Literal("(").suppress(),
)
| counted_array(
CharsNotIn(exact=8 * 9),
+ (
(
counted_array(
CharsNotIn(exact=8 * 6),
common.integer + Literal("(").suppress(),
)
)
| (
counted_array(
CharsNotIn(exact=4 * 6),
common.integer + Literal("(").suppress(),
)
)
)
+ Literal(")").suppress()
).set_parse_action(lambda tks: _unpack_binary_field(tks, elsize=6))
| (
Literal("tensor").suppress()
+ Literal(">").suppress()
+ common.integer
+ Literal("(").suppress(),
)
+ (
(
counted_array(
CharsNotIn(exact=8 * 9),
common.integer + Literal("(").suppress(),
)
)
| (
counted_array(
CharsNotIn(exact=4 * 9),
common.integer + Literal("(").suppress(),
)
)
)
+ Literal(")").suppress()
).set_parse_action(lambda tks: _unpack_binary_field(tks, elsize=9))
)
+ Literal(")").suppress()
).set_parse_action(_unpack_binary_field)
)
)
)
_TOKEN = QuotedString('"', unquote_results=False) | _IDENTIFIER
Expand Down
4 changes: 4 additions & 0 deletions tests/test_files/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def test_parse_value() -> None:
assert Parsed(
b"nonuniform List<vector> 2(\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x10@\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x00\x18@)"
)[()] == [[1, 2, 3], [4, 5, 6]]
assert Parsed(b"nonuniform List<scalar> 2(\x00\x00\x80?\x00\x00\x00@)")[()] == [
1,
2,
]
assert Parsed(b"[1 1 -2 0 0 0 0]")[()] == FoamFile.DimensionSet(
mass=1, length=1, time=-2
)
Expand Down

0 comments on commit 5a133cb

Please sign in to comment.