-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
112 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from pathlib import Path | ||
|
||
from lark import Lark | ||
|
||
MF6_GRAMMAR_PATH = Path(__file__).parent / "mf6.lark" | ||
MF6_PARSER = Lark.open(MF6_GRAMMAR_PATH, rel_to=__file__, start="start") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
// TODO: test, probably broken | ||
value: string | NUMBER | ||
string: WORD* | ||
keyval: [string] value | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
KEYS: "K"|"I"|"D"|"S"|"F"|"A" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
?start: _NL* _item* | ||
_item: (block | COMMENT) _NL+ | ||
|
||
// block | ||
block: _begin _NL params _end | ||
_begin: _BEGIN name [index] | ||
_end: _END name | ||
name: WORD | ||
index: INT | ||
_BEGIN: "begin"i | ||
_END: "end"i | ||
|
||
// parameter | ||
params: (param _NL)* | ||
param: _key [_value] | ||
_key: KEYS | ||
_value: NUMBER | path | string | array | list | ||
|
||
// string | ||
string: WORD+ | ||
|
||
// file path | ||
path: INOUT PATH | ||
PATH: [_PATHSEP] (NON_SEPARATOR_STRING [_PATHSEP]) [NON_SEPARATOR_STRING] | ||
_PATHSEP: "/" | ||
INOUT: "filein"i|"fileout"i | ||
|
||
// array | ||
array: constantarray | internalarray | externalarray | ||
constantarray: "CONSTANT" NUMBER | ||
internalarray: "INTERNAL" [factor] [iprn] (NUMBER* [_NL])* | ||
externalarray: "OPEN/CLOSE" WORD [factor] ["binary"] [iprn] | ||
factor: "FACTOR" NUMBER | ||
iprn: "IPRN" INT | ||
|
||
// list (adapted from https://github.com/lark-parser/lark/blob/master/examples/composition/csv.lark) | ||
list: header _NL row* | ||
header: "#" " "? (WORD _SEPARATOR?)+ | ||
row: (_anything _SEPARATOR?)+ _NL | ||
_anything: INT | WORD | NON_SEPARATOR_STRING | FLOAT | SIGNED_FLOAT | ||
NON_SEPARATOR_STRING: /[a-zA-z.;\\\/]+/ | ||
_SEPARATOR: /[ ]+/ | ||
| "\t" | ||
| "," | ||
|
||
// newline | ||
_NL: /(\r?\n[\t ]*)+/ | ||
|
||
// parameter keys file can be generated | ||
// with the rest of the plugin interface | ||
%import .keys.KEYS -> KEYS | ||
|
||
%import common.SH_COMMENT -> COMMENT | ||
%import common.SIGNED_NUMBER -> NUMBER | ||
%import common.SIGNED_FLOAT | ||
%import common.INT | ||
%import common.FLOAT | ||
%import common.WORD | ||
%import common.WS_INLINE | ||
|
||
%ignore WS_INLINE |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from pprint import pprint | ||
|
||
import pytest | ||
from lark import Transformer | ||
|
||
from flopy4.lark import MF6_PARSER | ||
|
||
TEST_PKG = """ | ||
BEGIN OPTIONS | ||
K | ||
I 1 | ||
D 1.0 | ||
S hello world | ||
F FILEIN some/path | ||
END OPTIONS | ||
BEGIN PACKAGEDATA 1 | ||
A INTERNAL 1.0 2.0 3.0 | ||
END PACKAGEDATA | ||
""" | ||
|
||
|
||
def test_parse_mf6(): | ||
tree = MF6_PARSER.parse(TEST_PKG) | ||
# this is working, check it with: | ||
# pytest test/test_lark.py::test_parse_mf6 -s | ||
print(tree.pretty()) | ||
|
||
|
||
class MF6Transformer(Transformer): | ||
# TODO | ||
pass | ||
|
||
|
||
MF6_TRANSFORMER = MF6Transformer() | ||
|
||
|
||
@pytest.mark.xfail | ||
def test_transform_mf6(): | ||
tree = MF6_PARSER.parse(TEST_PKG) | ||
data = MF6_TRANSFORMER.transform(tree) | ||
pprint(data) |