Skip to content

Commit da31e82

Browse files
committed
feat: mf6 input file parser
1 parent e142df7 commit da31e82

6 files changed

Lines changed: 49 additions & 5 deletions

File tree

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
@@ -154,14 +154,16 @@ class Dfn(TypedDict):
154154
MODFLOW 6 input definition. An input definition
155155
specifies a component in an MF6 simulation, e.g.
156156
a model or package, containing input variables.
157+
158+
# TODO: use dataclass, mypy says static methods in a typed dict are invalid
157159
"""
158160

159161
name: str
160-
advanced: bool = False
161-
multi: bool = False
162-
ref: Optional[Ref] = None
163-
sln: Optional[Sln] = None
164-
fkeys: Optional[Dfns] = None
162+
advanced: bool
163+
multi: bool
164+
ref: Optional[Ref]
165+
sln: Optional[Sln]
166+
fkeys: Optional[Dfns]
165167

166168
@staticmethod
167169
def _load_v1_flat(f, common: Optional[dict] = 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
@@ -79,6 +79,9 @@ models = [
7979
"tomli",
8080
"tomli-w"
8181
]
82+
codec = [
83+
"lark"
84+
]
8285
dev = ["modflow-devtools[lint,test,docs,dfn,models]"]
8386

8487
[dependency-groups]
@@ -121,13 +124,17 @@ models = [
121124
"tomli",
122125
"tomli-w"
123126
]
127+
codec = [
128+
"lark"
129+
]
124130
dev = [
125131
{include-group = "build"},
126132
{include-group = "lint"},
127133
{include-group = "test"},
128134
{include-group = "docs"},
129135
{include-group = "dfn"},
130136
{include-group = "models"},
137+
{include-group = "codec"},
131138
]
132139

133140
[project.urls]

0 commit comments

Comments
 (0)