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

Update parsing #46

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
102 changes: 36 additions & 66 deletions foamlib/_dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
LineEnd,
Literal,
Opt,
ParserElement,
QuotedString,
Word,
c_style_comment,
Expand Down Expand Up @@ -338,84 +339,53 @@ def boundary_field(self) -> "FoamFieldFile.BoundariesDictionary":
_DIMENSIONS = (
Literal("[").suppress() + common.number * 7 + Literal("]").suppress()
).set_parse_action(lambda tks: FoamFile.DimensionSet(*tks))
_TOKEN = QuotedString('"', unquote_results=False) | Word(
identchars + "$", identbodychars
)
_ITEM = Forward()
_DICTIONARY = Forward()
_SUBDICT = Literal("{").suppress() + _DICTIONARY + Literal("}").suppress()
_LIST = Opt(
Literal("List") + Literal("<") + common.identifier + Literal(">")
).suppress() + (
(
Opt(common.integer).suppress()
+ Literal("(").suppress()
+ Group(_ITEM[...])
+ Literal(")").suppress()
)
| (
common.integer + Literal("{").suppress() + _ITEM + Literal("}").suppress()
).set_parse_action(lambda tks: [[tks[1].as_list()] * tks[0]])
)
_DIMENSIONED = (Opt(common.identifier) + _DIMENSIONS + _ITEM).set_parse_action(
lambda tks: FoamFile.Dimensioned(*reversed(tks.as_list()))
)
_ITEM <<= (
_LIST | _SUBDICT | _DIMENSIONED | _DIMENSIONS | common.number | _YES | _NO | _TOKEN
)

_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()
+ (

def _list_of(elem: ParserElement) -> ParserElement:
return 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()
)
+ (Literal("(").suppress() + Group((elem)[...]) + Literal(")").suppress())
)
| (
common.integer
+ Literal("{").suppress()
+ (
common.number
| (
Literal("(").suppress()
+ Group(common.number[3, 9])
+ Literal(")").suppress()
)
)
+ Literal("}").suppress()
common.integer + Literal("{").suppress() + elem + Literal("}").suppress()
).set_parse_action(lambda tks: [[tks[1].as_list()] * tks[0]])
)


_TENSOR = _list_of(common.number) | common.number
_DIMENSIONED = (Opt(common.identifier) + _DIMENSIONS + _TENSOR).set_parse_action(
lambda tks: FoamFile.Dimensioned(*reversed(tks.as_list()))
)
_FIELD = (Keyword("uniform").suppress() + _TENSOR) | (
Keyword("nonuniform").suppress() + _list_of(_TENSOR)
)
_TOKEN = QuotedString('"', unquote_results=False) | Word(
identchars + "$", identbodychars
)
_DICTIONARY = Forward()
_SUBDICT = Literal("{").suppress() + _DICTIONARY + Literal("}").suppress()
_ITEM = Forward()
_LIST = _list_of(_ITEM)
_ITEM <<= (
_FIELD
| _LIST
| _SUBDICT
| _DIMENSIONED
| _DIMENSIONS
| common.number
| _YES
| _NO
| _TOKEN
)
_TOKENS = (
QuotedString('"', unquote_results=False)
| Word(printables.replace(";", "").replace("{", "").replace("}", ""))
)[2, ...].set_parse_action(lambda tks: " ".join(tks))

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

_KEYWORD = QuotedString('"', unquote_results=False) | Word(
identchars + "$(,.)", identbodychars + "$(,.)"
Expand All @@ -426,7 +396,7 @@ def boundary_field(self) -> "FoamFieldFile.BoundariesDictionary":
.set_parse_action(lambda tks: {} if not tks else tks)
.ignore(c_style_comment)
.ignore(cpp_style_comment)
.ignore(Literal("#include") + ... + LineEnd()) # type: ignore
.ignore(Literal("#include") + ... + LineEnd()) # type: ignore [no-untyped-call]
)


Expand Down
Loading