This commit is contained in:
Michael Zhang 2021-09-30 16:07:36 -05:00
parent 18df271749
commit 61bfd16b81
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
3 changed files with 236 additions and 216 deletions

View file

@ -1,6 +1,9 @@
watch: watch:
watchexec -ce py,lark,ag -i gen 'just run' watchexec -ce py,lark,ag -i gen 'just run'
fmt:
pipenv run black .
run: run:
mypy *.py mypy *.py
python agmain.py python agmain.py

View file

@ -18,6 +18,14 @@ class GenResult:
self.parse_rules: defaultdict[str, List[str]] = defaultdict(list) self.parse_rules: defaultdict[str, List[str]] = defaultdict(list)
self.starts: Set[str] = set() self.starts: Set[str] = set()
self.extra = ex 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 @property
def parser_data(self) -> str: def parser_data(self) -> str:

View file

@ -25,6 +25,8 @@ if __name__ == "__main__":
with open("gen/arith.py", "w") as f: with open("gen/arith.py", "w") as f:
fmt_str = textwrap.dedent( fmt_str = textwrap.dedent(
""" """
# This documented generated by agtest.
__all__ = ["parse"] __all__ = ["parse"]
from typing import Generic, TypeVar, Optional, Callable, Dict, Any from typing import Generic, TypeVar, Optional, Callable, Dict, Any
from lark import Lark, Transformer from lark import Lark, Transformer
@ -43,15 +45,22 @@ if __name__ == "__main__":
self.value = self.func() self.value = self.func()
return self.value return self.value
parser = Lark('''{pd}''', parser='lalr', start={starts}, debug=True) parser = Lark('''{pd}''', parser='lalr', start={starts}, debug=True)
class Trans(Transformer[None]): class Trans(Transformer[None]): {transdef}
pass
{ex} {ex}
def parse(input: str, start: Optional[str] = None) -> Any: 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( 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") mod = importlib.import_module("gen.arith")