This repository has been archived by the owner on Jan 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
86 lines (68 loc) · 1.87 KB
/
parser.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from lark import Lark
from lark.lexer import Token
game_parser = Lark(r"""
start: line+
line: debug
| define
| setting
name : CNAME
value: CNAME
| NUMBER
| ESCAPED_STRING
?expr: value
| "!" expr -> not
| expr "==" expr -> eq
| expr "!=" expr -> neq
| expr "<" expr -> lt
| expr "<=" expr -> let
| expr ">" expr -> gt
| expr ">=" expr -> get
| expr "+" expr -> add
| "-" expr -> minus
| value "+" -> plus
| "Random" "(" NUMBER "%" ")" -> random
debug : "debug" "{" [ name ("," name)*] "}"
define : "define" name "{" [def ("," def)*] "}"
def: name ("," fcall)?
| WS
setting: "setting" name "{" statements "}"
statements: statement*
statement: fcall ";"
| goto ";"
| ifelse
| if
goto: "goto" expr
if: "if" "(" expr ")" ("{" statements "}" | statement)
ifelse: "if" "(" expr ")" ("{" statements "}" | statement) "else" ("{" statements "}" | statement )
fcall: name "(" [param ("," param)*] ")"
param: expr
| fcall
%import common.CNAME
%import common.NUMBER
%import common.ESCAPED_STRING
%import common.WS
%ignore /\/\/[^\n]*/
%ignore WS
""", start='start')
def get_cname(c):
assert(c.data in ["name", "value"])
return str(c.children[0])
def get_statements(s):
assert(s.data == "statements")
if len(s.children) == 0:
return []
return s.children
def get_param(p):
assert(p.data == "param")
return p.children[0]
def get_value(p):
assert(p.data == "value")
v = p.children[0]
v = str(v)
if (v.isnumeric()):
return int(v)
#print("v:", str(v), repr(v))
return v
def get_expr(p):
assert(p.data == "expr")
return p.children[0]