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

Handle #-directives as keywords in OpenFOAM files #315

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion foamlib/_files/_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def __setitem__(self, keywords: str | tuple[str, ...] | None, data: Entry) -> No
+ dumps(keywords[-1])
+ b" "
+ dumps(data, kind=kind)
+ b";"
+ (b";" if not keywords[-1].startswith("#") else b"")
+ after,
)

Expand Down
24 changes: 19 additions & 5 deletions foamlib/_files/_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,19 @@ def count_parse_action(tks: ParseResults) -> None:


def _dict_of(
keyword: ParserElement, data: ParserElement, *, located: bool = False
keyword: ParserElement,
data: ParserElement,
*,
directive: ParserElement | None = None,
located: bool = False,
) -> ParserElement:
dict_ = Forward()

keyword_entry = keyword + (dict_ | (data + Literal(";").suppress()))

if directive is not None:
keyword_entry |= directive + data + LineEnd().suppress() # type: ignore [no-untyped-call]

if located:
keyword_entry = Located(keyword_entry)

Expand All @@ -175,12 +182,17 @@ def _keyword_entry_of(
keyword: ParserElement,
data: ParserElement,
*,
directive: ParserElement | None = None,
located: bool = False,
) -> ParserElement:
keyword_entry = keyword + (
_dict_of(keyword, data, located=located) | (data + Literal(";").suppress())
_dict_of(keyword, data, directive=directive, located=located)
| (data + Literal(";").suppress())
)

if directive is not None:
keyword_entry |= directive + data + LineEnd().suppress() # type: ignore [no-untyped-call]

if located:
keyword_entry = Located(keyword_entry)
else:
Expand Down Expand Up @@ -240,7 +252,8 @@ def _keyword_entry_of(
| _tensor_list(TensorKind.TENSOR, ignore=_COMMENT)
)
)
_TOKEN = dbl_quoted_string | _IDENTIFIER
_DIRECTIVE = Word("#", _IDENTBODYCHARS)
_TOKEN = dbl_quoted_string | _IDENTIFIER | _DIRECTIVE
_DATA = Forward()
_KEYWORD_ENTRY = _keyword_entry_of(_TOKEN | _list_of(_IDENTIFIER), _DATA)
_DICT = _dict_of(_TOKEN, _DATA)
Expand All @@ -263,14 +276,15 @@ def parse_data(s: str) -> Data:


_LOCATED_DICTIONARY = Group(
_keyword_entry_of(_TOKEN, Opt(_DATA, default=""), located=True)
_keyword_entry_of(
_TOKEN, Opt(_DATA, default=""), directive=_DIRECTIVE, located=True
)
)[...]
_LOCATED_DATA = Group(Located(_DATA.copy().add_parse_action(lambda tks: ["", tks[0]])))

_FILE = (
Dict(_LOCATED_DICTIONARY + Opt(_LOCATED_DATA) + _LOCATED_DICTIONARY)
.ignore(_COMMENT)
.ignore(Literal("#include") + ... + LineEnd()) # type: ignore [no-untyped-call]
.parse_with_tabs()
)

Expand Down
8 changes: 8 additions & 0 deletions tests/test_files/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,11 @@ def test_parse_value() -> None:
]
assert Parsed(b"[]")[()] == FoamFile.DimensionSet()
assert Parsed(b"object f.1;")[("object",)] == "f.1"


def test_parse_directive() -> None:
assert Parsed(b'#include "filename"')[("#include",)] == '"filename"'
assert (
Parsed(b"functions\n{\n#includeFunc funcName\n}")[("functions", "#includeFunc")]
== "funcName"
)
Loading