From a6adfd1e29cc44098e47618e6859153f3bacf438 Mon Sep 17 00:00:00 2001 From: wpbonelli Date: Wed, 4 Sep 2024 20:40:04 -0400 Subject: [PATCH] inline grammar --- flopy4/lark/__init__.py | 70 ++++++++++++++++++++++++++++++++++++++--- flopy4/lark/dfn.lark | 10 ------ 2 files changed, 66 insertions(+), 14 deletions(-) delete mode 100644 flopy4/lark/dfn.lark diff --git a/flopy4/lark/__init__.py b/flopy4/lark/__init__.py index 41bba6e..8f81a2c 100644 --- a/flopy4/lark/__init__.py +++ b/flopy4/lark/__init__.py @@ -1,6 +1,68 @@ -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") +MF6_GRAMMAR = r""" +?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 +// and maybe placed in a separate file +KEYS: "K"|"I"|"D"|"S"|"F"|"A" + +%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 +""" + +MF6_PARSER = Lark(MF6_GRAMMAR, start="start") diff --git a/flopy4/lark/dfn.lark b/flopy4/lark/dfn.lark deleted file mode 100644 index f01d79b..0000000 --- a/flopy4/lark/dfn.lark +++ /dev/null @@ -1,10 +0,0 @@ -// TODO: test, probably broken -value: string | NUMBER -string: WORD* -keyval: [string] value -param: (keyval NEWLINE)+ NEWLINE - -%import common.SIGNED_NUMBER -> NUMBER -%import common.NEWLINE -%import common.WORD -%ignore common.WS \ No newline at end of file