Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve parsing #43

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 41 additions & 19 deletions foamlib/_dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
Keyword,
LineEnd,
Literal,
Located,
Opt,
QuotedString,
Word,
Expand Down Expand Up @@ -85,8 +84,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 +345,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 +360,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 +411,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
Loading