Skip to content

Commit

Permalink
Merge pull request #233 from gerlero/files
Browse files Browse the repository at this point in the history
Improve parsing
  • Loading branch information
gerlero authored Oct 17, 2024
2 parents a73cf5b + 455e455 commit a8c43ed
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
25 changes: 21 additions & 4 deletions foamlib/_files/_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,35 @@ def _unpack_binary_field(
return [all]


_IDENTCHARS = identchars + "$"
_IDENTBODYCHARS = (
printables.replace(";", "")
.replace("{", "")
.replace("}", "")
.replace("[", "")
.replace("]", "")
)

_SWITCH = (
Keyword("yes") | Keyword("true") | Keyword("on") | Keyword("y") | Keyword("t")
Keyword("yes", _IDENTBODYCHARS)
| Keyword("true", _IDENTBODYCHARS)
| Keyword("on", _IDENTBODYCHARS)
| Keyword("y", _IDENTBODYCHARS)
| Keyword("t", _IDENTBODYCHARS)
).set_parse_action(lambda: True) | (
Keyword("no") | Keyword("false") | Keyword("off") | Keyword("n") | Keyword("f")
Keyword("no", _IDENTBODYCHARS)
| Keyword("false", _IDENTBODYCHARS)
| Keyword("off", _IDENTBODYCHARS)
| Keyword("n", _IDENTBODYCHARS)
| Keyword("f", _IDENTBODYCHARS)
).set_parse_action(lambda: False)
_DIMENSIONS = (
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, _IDENTBODYCHARS, exclude_chars="()")
+ Opt(Literal("(") + Word(_IDENTBODYCHARS, exclude_chars="()") + Literal(")"))
)
_DIMENSIONED = (Opt(_IDENTIFIER) + _DIMENSIONS + _TENSOR).set_parse_action(
lambda tks: FoamFileBase.Dimensioned(*reversed(tks.as_list()))
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 @@ -71,3 +71,4 @@ def test_parse_value() -> None:
{"(air and water)": {"type": "constant", "sigma": 0.07}}
]
assert Parsed(b"[]")[()] == FoamFile.DimensionSet()
assert Parsed(b"object f.1;")["object"] == "f.1"

0 comments on commit a8c43ed

Please sign in to comment.