Skip to content

Commit

Permalink
Improve parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
gerlero committed Mar 28, 2024
1 parent c9e4bbc commit a6abe49
Showing 1 changed file with 41 additions and 18 deletions.
59 changes: 41 additions & 18 deletions foamlib/_dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ def __getitem__(self, key: str) -> Union["FoamFile.Value", "_FoamDictionary"]:
if isinstance(value, dict):
return _FoamDictionary(self._file, [*self._keywords, key])
else:
start, end = value
return _VALUE.parse_string(contents[start:end], parse_all=True).as_list()[0]
return value

def _setitem(
self,
Expand Down Expand Up @@ -347,6 +346,8 @@ def boundary_field(self) -> "FoamFieldFile.BoundariesDictionary":
identchars + "$", identbodychars
)
_ITEM = Forward()
_DICTIONARY = Forward()
_SUBDICT = Literal("{").suppress() + _DICTIONARY + Literal("}").suppress()
_LIST = Opt(
Literal("List") + Literal("<") + common.identifier + Literal(">")
).suppress() + (
Expand All @@ -360,14 +361,48 @@ def boundary_field(self) -> "FoamFieldFile.BoundariesDictionary":
common.integer + Literal("{").suppress() + _ITEM + Literal("}").suppress()
).set_parse_action(lambda tks: [tks[1]] * tks[0])
)
_FIELD = (Keyword("uniform").suppress() + _ITEM) | (
Keyword("nonuniform").suppress() + _LIST
_FIELD = (
Keyword("uniform").suppress()
+ (
common.number
| (
Literal("(").suppress()
+ Group(common.number[3, 9])
+ Literal(")").suppress()
)
)
) | (
Keyword("nonuniform").suppress()
+ Opt(Literal("List") + Literal("<") + common.identifier + Literal(">")).suppress()
+ Opt(common.integer).suppress()
+ (
Literal("(").suppress()
+ Group(
(
common.number
| (
Literal("(").suppress()
+ Group(common.number[3, 9])
+ Literal(")").suppress()
)
)[...]
)
+ Literal(")").suppress()
)
)
_DIMENSIONED = (Opt(common.identifier) + _DIMENSIONS + _ITEM).set_parse_action(
lambda tks: FoamFile.Dimensioned(*reversed(tks.as_list()))
)
_ITEM <<= (
_FIELD | _LIST | _DIMENSIONED | _DIMENSIONS | common.number | _YES | _NO | _TOKEN
_FIELD
| _LIST
| _SUBDICT
| _DIMENSIONED
| _DIMENSIONS
| common.number
| _YES
| _NO
| _TOKEN
)

_TOKENS = (
Expand All @@ -377,22 +412,10 @@ def boundary_field(self) -> "FoamFieldFile.BoundariesDictionary":

_VALUE = (_ITEM ^ _TOKENS).ignore(c_style_comment).ignore(cpp_style_comment)


_UNPARSED_VALUE = (
QuotedString('"', unquote_results=False)
| Word(printables.replace(";", "").replace("{", "").replace("}", ""))
)[...]
_KEYWORD = QuotedString('"', unquote_results=False) | Word(
identchars + "$(,.)", identbodychars + "$(,.)"
)
_DICTIONARY = Forward()
_ENTRY = _KEYWORD + (
(
Located(_UNPARSED_VALUE).set_parse_action(lambda tks: (tks[0], tks[2]))
+ Literal(";").suppress()
)
| (Literal("{").suppress() + _DICTIONARY + Literal("}").suppress())
)
_ENTRY = _KEYWORD + ((_VALUE + Literal(";").suppress()) | _SUBDICT)
_DICTIONARY <<= (
Dict(Group(_ENTRY)[...])
.set_parse_action(lambda tks: {} if not tks else tks)
Expand Down

0 comments on commit a6abe49

Please sign in to comment.