-
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.
Merge pull request #33 from wpbonelli/lark
dfn parser working, still need to handle records and lists as nested contexts
- Loading branch information
Showing
10 changed files
with
302 additions
and
58 deletions.
There are no files selected for viewing
Empty file.
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,25 @@ | ||
import numpy as np | ||
|
||
|
||
def parse_word(self, w): | ||
(w,) = w | ||
return str(w) | ||
|
||
|
||
def parse_string(self, s): | ||
return " ".join(s) | ||
|
||
|
||
def parse_int(self, i): | ||
(i,) = i | ||
return int(i) | ||
|
||
|
||
def parse_float(self, f): | ||
(f,) = f | ||
return float(f) | ||
|
||
|
||
def parse_array(self, a): | ||
(a,) = a | ||
return np.array(a) |
File renamed without changes.
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,3 @@ | ||
def make_converter(): | ||
TODO | ||
pass |
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,4 @@ | ||
__all__ = ["make_parser", "DFNTransformer"] | ||
|
||
from flopy4.mf6.io.spec.parser import make_parser | ||
from flopy4.mf6.io.spec.transformer import DFNTransformer |
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,78 @@ | ||
from os import linesep | ||
|
||
from lark import Lark | ||
|
||
ATTRIBUTES = [ | ||
"block", | ||
"name", | ||
"type", | ||
"reader", | ||
"optional", | ||
"true", | ||
"mf6internal", | ||
"longname", | ||
"description", | ||
"layered", | ||
"shape", | ||
"valid", | ||
"tagged", | ||
"in_record", | ||
"preserve_case", | ||
"default_value", | ||
"numeric_index", | ||
"deprecated", | ||
] | ||
|
||
DFN_GRAMMAR = r""" | ||
// dfn | ||
dfn: _NL* (block _NL*)+ _NL* | ||
// block | ||
block: _header parameter* | ||
_header: _hash _dashes _headtext _dashes _NL+ | ||
_headtext: component subcompnt blockname | ||
component: _word | ||
subcompnt: _word | ||
blockname: _word | ||
// parameter | ||
parameter.+1: _paramhead _NL (attribute _NL)* | ||
_paramhead: paramblock _NL paramname | ||
paramblock: "block" _word | ||
paramname: "name" _word | ||
// attribute | ||
attribute.-1: key value | ||
key: ATTRIBUTE | ||
value: string | ||
// string | ||
_word: /[a-zA-z0-9.;\(\)\-\,\\\/]+/ | ||
string: _word+ | ||
// newline | ||
_NL: /(\r?\n[\t ]*)+/ | ||
// comment format | ||
_hash: /\#/ | ||
_dashes: /[\-]+/ | ||
%import common.SH_COMMENT -> COMMENT | ||
%import common.WORD | ||
%import common.WS_INLINE | ||
%ignore WS_INLINE | ||
""" | ||
""" | ||
EBNF description for the MODFLOW 6 definition language. | ||
""" | ||
|
||
|
||
def make_parser(): | ||
""" | ||
Create a parser for the MODFLOW 6 definition language. | ||
""" | ||
|
||
attributes = "|".join(['"' + n + '"i' for n in ATTRIBUTES]) | ||
grammar = linesep.join([DFN_GRAMMAR, f"ATTRIBUTE: ({attributes})"]) | ||
return Lark(grammar, start="dfn") |
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,63 @@ | ||
from lark import Transformer | ||
|
||
from flopy4.io.lark import parse_string | ||
|
||
|
||
class DFNTransformer(Transformer): | ||
""" | ||
Transforms a parse tree for the MODFLOW 6 | ||
specification language into a nested AST | ||
suitable for generating an object model. | ||
Notes | ||
----- | ||
Rather than a flat list of parameters for each component, | ||
which a subsequent step is responsible for turning into a | ||
an object hierarchy, we derive the hierarchical parameter | ||
structure from the DFN file and return a dict of blocks, | ||
each of which is a dict of parameters. | ||
This can be fed to a Jinja template to generate component | ||
modules. | ||
""" | ||
|
||
def key(self, k): | ||
(k,) = k | ||
return str(k).lower() | ||
|
||
def value(self, v): | ||
(v,) = v | ||
return str(v) | ||
|
||
def attribute(self, p): | ||
return str(p[0]), str(p[1]) | ||
|
||
def parameter(self, p): | ||
return dict(p[1:]) | ||
|
||
def paramname(self, n): | ||
(n,) = n | ||
return "name", str(n) | ||
|
||
def paramblock(self, b): | ||
(b,) = b | ||
return "block", str(b) | ||
|
||
def component(self, c): | ||
(c,) = c | ||
return "component", str(c) | ||
|
||
def subcompnt(self, s): | ||
(s,) = s | ||
return "subcomponent", str(s) | ||
|
||
def blockname(self, b): | ||
(b,) = b | ||
return "block", str(b) | ||
|
||
def block(self, b): | ||
params = {p["name"]: p for p in b[6:]} | ||
return b[4][1], params | ||
|
||
string = parse_string | ||
dfn = dict |
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
Oops, something went wrong.