forked from cizra/pycat
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmoo_grammar.py
More file actions
42 lines (31 loc) · 788 Bytes
/
moo_grammar.py
File metadata and controls
42 lines (31 loc) · 788 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from typing import Any
import lark
with open('moo.lark') as f:
lex = lark.Lark(f.read())
class MooList(list):
def __repr__(self) -> str:
s = '{'
first = True
for i in self:
if not first:
s += ', '
s += repr(i)
first = False
s += '}'
return s
class MooObj(str):
def __repr__(self) -> str:
return super().__str__()
class MooStr(str):
def __repr__(self) -> str:
return super().__str__()
class MooToPy(lark.Transformer):
list = MooList
ESCAPED_STRING = MooStr
OBJ_NUM = MooObj
SIGNED_INT = int
def start(self, s):
return s[0]
def parse_value(string: str) -> Any:
tree = lex.parse(string)
return MooToPy().transform(tree)