Skip to content
Draft
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
19 changes: 19 additions & 0 deletions autotest/test_codec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from pprint import pprint

import pytest

import modflow_devtools.models as models
from modflow_devtools.codec import make_parser

MODELS = [name for name in models.get_models().keys() if name.startswith("mf6/")]
PARSER = make_parser()


@pytest.mark.parametrize("model_name", MODELS)
def test_parser(model_name, function_tmpdir):
workspace = models.copy_to(function_tmpdir, model_name)
nam_path = next(iter(workspace.glob("*.nam")))
text = nam_path.open().read()
pprint(text)
tree = PARSER.parse(text)
print(tree.pretty())
Empty file added docs/md/codec.md
Empty file.
5 changes: 5 additions & 0 deletions modflow_devtools/codec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from lark import Lark


def make_parser(**kwargs) -> Lark:
return Lark.open("mf6.lark", parser="lalr", rel_to=__file__, **kwargs)
19 changes: 19 additions & 0 deletions modflow_devtools/mf6.lark
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
start: [WS] [_NL*] (block [[WS] _NL*])+ [WS]
block: "begin"i CNAME [_block_index] _NL _content "end"i CNAME [_block_index] _NL+
_block_index: INT
_content: line* [WS]
line: [WS] item* _NL+
item: word | NUMBER
word: /[a-zA-Z0-9._'~,-\\(\\)]+/

%import common.NEWLINE -> _NL
%import common.WS
%import common.WS_INLINE
%import common.CNAME
%import common.WORD
%import common.NUMBER
%import common.INT
%import common.SH_COMMENT
%import common._STRING_INNER
%ignore WS_INLINE
%ignore SH_COMMENT
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ models = [
"tomli",
"tomli-w"
]
codec = [
"lark"
]
dev = ["modflow-devtools[lint,test,docs,dfn,models]"]

[dependency-groups]
Expand Down Expand Up @@ -126,13 +129,17 @@ models = [
"tomli",
"tomli-w"
]
codec = [
"lark"
]
dev = [
{include-group = "build"},
{include-group = "lint"},
{include-group = "test"},
{include-group = "docs"},
{include-group = "dfn"},
{include-group = "models"},
{include-group = "codec"},
]

[project.urls]
Expand Down