Skip to content

Commit 6c794d1

Browse files
committed
feat: mf6 input file parser
1 parent 7116292 commit 6c794d1

File tree

6 files changed

+49
-5
lines changed

6 files changed

+49
-5
lines changed

autotest/test_codec.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from modflow_devtools.codec import make_parser
2+
3+
4+
def test_parser():
5+
parser = make_parser()
6+
text = """
7+
BEGIN OPTIONS
8+
AN OPTION
9+
ANOTHER OPTION
10+
END OPTIONS
11+
BEGIN PACKAGEDATA
12+
END PACKAGEDATA
13+
"""
14+
tree = parser.parse(text)
15+
print(tree.pretty())

docs/md/codec.md

Whitespace-only changes.

modflow_devtools/codec.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from lark import Lark
2+
3+
4+
def make_parser(**kwargs) -> Lark:
5+
return Lark.open("mf6.lark", parser="lalr", rel_to=__file__, **kwargs)

modflow_devtools/dfn.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,16 @@ class Dfn(TypedDict):
157157
MODFLOW 6 input definition. An input definition
158158
specifies a component in an MF6 simulation, e.g.
159159
a model or package, containing input variables.
160+
161+
# TODO: use dataclass, mypy says static methods in a typed dict are invalid
160162
"""
161163

162164
name: str
163-
advanced: bool = False
164-
multi: bool = False
165-
ref: Ref | None = None
166-
sln: Sln | None = None
167-
fkeys: Dfns | None = None
165+
advanced: bool
166+
multi: bool
167+
ref: Optional[Ref]
168+
sln: Optional[Sln]
169+
fkeys: Optional[Dfns]
168170

169171
@staticmethod
170172
def _load_v1_flat(f, common: dict | None = None) -> tuple[Mapping, list[str]]:

modflow_devtools/mf6.lark

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
start: [WS] block+ [WS]
2+
3+
block: "begin"i CNAME _NL _content "end"i CNAME _NL
4+
_content: line* [WS]
5+
line: [WS] value* _NL
6+
value: WORD | NUMBER
7+
8+
9+
%import common.NEWLINE -> _NL
10+
%import common.WS
11+
%import common.WS_INLINE
12+
%import common.CNAME
13+
%import common.WORD
14+
%import common.NUMBER
15+
%ignore WS_INLINE

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ models = [
8181
"tomli",
8282
"tomli-w"
8383
]
84+
codec = [
85+
"lark"
86+
]
8487
dev = ["modflow-devtools[lint,test,docs,dfn,models]"]
8588

8689
[dependency-groups]
@@ -126,13 +129,17 @@ models = [
126129
"tomli",
127130
"tomli-w"
128131
]
132+
codec = [
133+
"lark"
134+
]
129135
dev = [
130136
{include-group = "build"},
131137
{include-group = "lint"},
132138
{include-group = "test"},
133139
{include-group = "docs"},
134140
{include-group = "dfn"},
135141
{include-group = "models"},
142+
{include-group = "codec"},
136143
]
137144

138145
[project.urls]

0 commit comments

Comments
 (0)