Skip to content

Commit

Permalink
Update parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
gerlero committed Mar 28, 2024
1 parent e77129f commit 4a11b54
Showing 1 changed file with 36 additions and 66 deletions.
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

0 comments on commit 4a11b54

Please sign in to comment.