agtest/agruntime.tmpl.py

40 lines
977 B
Python
Raw Normal View History

2021-09-30 22:53:47 +00:00
# This document is generated by agtest.
# type: ignore
__all__ = ["parse"]
import re
from typing import Generic, TypeVar, Optional, Callable, Dict, Any
from lark import Lark, Transformer
T = TypeVar('T')
builtins: Dict[str, Any] = {{
"parseInt": lambda s: int(s)
}}
class Thunk(Generic[T]):
''' A thunk represents a value that may be computed lazily. '''
value: Optional[T]
def __init__(self, func: Callable[[], T]):
self.func = func
self.value = None
def get(self) -> T:
if self.value is None:
self.value = self.func()
return self.value
parser = Lark('''
{pd}
''', parser='lalr', start={starts}, debug=True)
{ex}
class Trans(Transformer[None]): {transdef}
__agNonterminals = {ntmap}
def parse(input: str, start: Optional[str] = None) -> Any:
if start is not None: start = __agNonterminals[start]
tree = parser.parse(input, start)
trans = Trans()
res = trans.transform(tree)
return res