Skip to content

Commit

Permalink
Merge pull request #204 from gerlero/files
Browse files Browse the repository at this point in the history
Improve parsing
  • Loading branch information
gerlero authored Sep 30, 2024
2 parents 754af0c + 0deef19 commit a4489d2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 46 deletions.
92 changes: 46 additions & 46 deletions foamlib/_files/_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,68 +95,68 @@ def _unpack_binary_field(
return [all]


_BINARY_FIELD = (
Keyword("nonuniform").suppress()
+ 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),
Literal("vector").suppress()
+ Literal(">").suppress()
+ common.integer
+ Literal("(").suppress(),
)
| counted_array(
CharsNotIn(exact=8 * 6),
Literal("symmTensor").suppress()
+ Literal(">").suppress()
+ common.integer
+ Literal("(").suppress(),
)
| counted_array(
CharsNotIn(exact=8 * 9),
Literal("tensor").suppress()
+ Literal(">").suppress()
+ common.integer
+ Literal("(").suppress(),
)
)
+ Literal(")").suppress()
).set_parse_action(_unpack_binary_field)


_SWITCH = (
Keyword("yes") | Keyword("true") | Keyword("on") | Keyword("y") | Keyword("t")
).set_parse_action(lambda: True) | (
Keyword("no") | Keyword("false") | Keyword("off") | Keyword("n") | Keyword("f")
).set_parse_action(lambda: False)
_DIMENSIONS = (
Literal("[").suppress() + common.number * 7 + Literal("]").suppress()
Literal("[").suppress() + common.number[0, 7] + Literal("]").suppress()
).set_parse_action(lambda tks: FoamFileBase.DimensionSet(*tks))
_TENSOR = _list_of(common.number) | common.number
_IDENTIFIER = Combine(
Word(identchars + "$", printables, exclude_chars="{(;)}")
+ Opt(Literal("(") + Word(printables, exclude_chars="{(;)}") + Literal(")"))
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()))
)
_FIELD = (
(Keyword("uniform").suppress() + _TENSOR)
| (Keyword("nonuniform").suppress() + _list_of(_TENSOR))
| _BINARY_FIELD
_FIELD = (Keyword("uniform").suppress() + _TENSOR) | (
Keyword("nonuniform").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),
Literal("vector").suppress()
+ Literal(">").suppress()
+ common.integer
+ Literal("(").suppress(),
)
| counted_array(
CharsNotIn(exact=8 * 6),
Literal("symmTensor").suppress()
+ Literal(">").suppress()
+ common.integer
+ Literal("(").suppress(),
)
| counted_array(
CharsNotIn(exact=8 * 9),
Literal("tensor").suppress()
+ Literal(">").suppress()
+ common.integer
+ Literal("(").suppress(),
)
)
+ Literal(")").suppress()
).set_parse_action(_unpack_binary_field)
)
)
_TOKEN = QuotedString('"', unquote_results=False) | _IDENTIFIER
_DATA = Forward()
_KEYWORD = Combine(Literal("(") + Word(identchars + " ") + Literal(")")) | _TOKEN
_KEYWORD = _TOKEN | _list_of(_IDENTIFIER).set_parse_action(
lambda tks: "(" + " ".join(tks[0]) + ")"
)
_KEYWORD_ENTRY = Dict(Group(_keyword_entry_of(_KEYWORD, _DATA)), asdict=True)
_DATA_ENTRY = Forward()
_LIST_ENTRY = _KEYWORD_ENTRY | _DATA_ENTRY
Expand Down
1 change: 1 addition & 0 deletions tests/test_files/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ def test_parse_value() -> None:
assert Parsed(b"((air and water) { type constant; sigma 0.07; })")[()] == [
{"(air and water)": {"type": "constant", "sigma": 0.07}}
]
assert Parsed(b"[]")[()] == FoamFile.DimensionSet()

0 comments on commit a4489d2

Please sign in to comment.