a
This commit is contained in:
parent
18df271749
commit
61bfd16b81
3 changed files with 236 additions and 216 deletions
3
Justfile
3
Justfile
|
@ -1,6 +1,9 @@
|
|||
watch:
|
||||
watchexec -ce py,lark,ag -i gen 'just run'
|
||||
|
||||
fmt:
|
||||
pipenv run black .
|
||||
|
||||
run:
|
||||
mypy *.py
|
||||
python agmain.py
|
||||
|
|
8
aggen.py
8
aggen.py
|
@ -18,6 +18,14 @@ class GenResult:
|
|||
self.parse_rules: defaultdict[str, List[str]] = defaultdict(list)
|
||||
self.starts: Set[str] = set()
|
||||
self.extra = ex
|
||||
self.trans: List[str] = list()
|
||||
|
||||
@property
|
||||
def transdef(self) -> str:
|
||||
s = self.trans
|
||||
if not s:
|
||||
s = ["pass"]
|
||||
return "\n" + "\n".join(map(lambda c: f" {c}", s))
|
||||
|
||||
@property
|
||||
def parser_data(self) -> str:
|
||||
|
|
17
agmain.py
17
agmain.py
|
@ -25,6 +25,8 @@ if __name__ == "__main__":
|
|||
with open("gen/arith.py", "w") as f:
|
||||
fmt_str = textwrap.dedent(
|
||||
"""
|
||||
# This documented generated by agtest.
|
||||
|
||||
__all__ = ["parse"]
|
||||
from typing import Generic, TypeVar, Optional, Callable, Dict, Any
|
||||
from lark import Lark, Transformer
|
||||
|
@ -43,15 +45,22 @@ if __name__ == "__main__":
|
|||
self.value = self.func()
|
||||
return self.value
|
||||
parser = Lark('''{pd}''', parser='lalr', start={starts}, debug=True)
|
||||
class Trans(Transformer[None]):
|
||||
pass
|
||||
class Trans(Transformer[None]): {transdef}
|
||||
{ex}
|
||||
def parse(input: str, start: Optional[str] = None) -> Any:
|
||||
return parser.parse(input, start)
|
||||
tree = parser.parse(input, start)
|
||||
trans = Trans()
|
||||
res = trans.transform(tree)
|
||||
return res
|
||||
"""
|
||||
)
|
||||
f.write(
|
||||
fmt_str.format(pd=res.parser_data, ex=res.extra, starts=list(res.starts))
|
||||
fmt_str.format(
|
||||
pd=res.parser_data,
|
||||
ex=res.extra,
|
||||
starts=list(res.starts),
|
||||
transdef=res.transdef,
|
||||
)
|
||||
)
|
||||
|
||||
mod = importlib.import_module("gen.arith")
|
||||
|
|
Loading…
Reference in a new issue