-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
195 lines (158 loc) · 6.33 KB
/
parse.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from lark import Lark, Transformer
from state import Integer, Decimal, String, List, Set, Object, Empty, Ellipsis, Variable, Expression, AlgebraicType, FunctionType, Function, Statement, MatterStatement, ExpressionStatement
import re
class TreeTransformer(Transformer):
def integer(self, values):
return Integer(int(values[0]))
def decimal(self, values):
return Decimal(float(values[0]))
def string(self, values):
if len(values) > 0:
return String(re.sub('\\\\\\\\', '\\\\', re.sub('\\\\([^\\\\])', '\g<1>', values[0])))
else:
return String('')
def list(self, values):
return List(values)
def set(self, values):
return Set(values)
def object_definition(self, values):
out = {
'name': 'object_definition',
'identifier': None,
'type': None,
'value': None
}
for value in values:
if value['name'] == 'identifier':
out['identifier'] = value
elif value['name'] == 'type_definition':
out['type'] = value['expression']
elif value['name'] == 'assign_definition':
out['value'] = value['expression']
elif value['name'] == 'typeassign_definition':
out['value'] = value['expression']
out['type'] = value['expression']
return out
def object(self, values):
return Object(values)
def empty_value(self, values):
return Empty()
def ellipsis_value(self, values):
return Ellipsis()
def variable(self, values):
return Variable(values)
def name(self, values):
return values[0].value
def identifier(self, values):
return {
'name': 'identifier',
'value': values[0]
}
def algebraic_type(self, values):
return AlgebraicType(values)
def function_type(self, values):
return FunctionType(values)
def function_bind(self, values):
return values[0] if values else None
def function(self, values):
return Function(values[0], values[1:])
def type_definition(self, values):
return {
'name': 'type_definition',
'expression': values[0]
}
def assign_definition(self, values):
return {
'name': 'assign_definition',
'expression': values[0]
}
def typeassign_definition(self, values):
return {
'name': 'typeassign_definition',
'expression': values[0]
}
def expression(self, values):
return Expression(values)
def matter_statement(self, values):
identifier = None
assign = None
type = None
for value in values:
if value['name'] == 'identifier':
identifier = value['value']
elif value['name'] == 'type_definition':
type = value['expression']
elif value['name'] == 'assign_definition':
assign = value['expression']
elif value['name'] == 'typeassign_definition':
assign = value['expression']
type = value['expression']
return MatterStatement(identifier, assign, type)
def expression_statement(self, values):
return ExpressionStatement(values[0])
def start(self, values):
return values
class Parser:
def __init__(self):
self.lark = Lark("""
start: statement+
type_definition : _type expression
assign_definition : _assign expression
typeassign_definition: _typeassign expression
matter_statement : (identifier type_definition assign_definition) | (identifier type_definition) | (identifier assign_definition) | (identifier typeassign_definition)
expression_statement: expression
?statement : (matter_statement | expression_statement) _end_statement
algebraic_type : _base_expression (_algebraic_seperator _base_expression)+
function_type : _base_expression (_function_arrow _base_expression)+
empty_value : _open_paren _close_paren
ellipsis_value : "..."
_base_expression : (empty_value
| ellipsis_value
| variable
| constant
| function
| list
| set
| object
| (_open_paren expression _close_paren))
| (_precidence expression)
expression : (algebraic_type | function_type | _base_expression)+
function : _open_function statement+ _close_function
list : _open_list [expression (_seperator expression)*] _close_list
set : _open_set [expression (_seperator expression)*] _close_set
object_definition : (identifier type_definition assign_definition) | (identifier type_definition) | (identifier assign_definition) | (identifier typeassign_definition)
object : _open_object [object_definition (_seperator object_definition)*] _close_object
?constant : integer | decimal | string
identifier : name
variable: name (_access name)*
function_bind : name?
_open_function : "@" function_bind "{"
_seperator : ","
_type : ":"
_assign : "="
_typeassign : ":="
_access: "."
_precidence: "$"
name : /[_a-zA-Z][_\-a-zA-Z0-9]*/
_function_arrow : "->"
_algebraic_seperator : "|"
integer : /-?\d+/
decimal : /-?\d+\.\d*/
_string_inner: /([^\\\\']|\\\\.)+/
string : "'" _string_inner "'" | "''"
_end_statement : ";"
_close_function : _close_curly_brace
_open_list : "["
_close_list : "]"
_open_set : "<"
_close_set : ">"
_open_paren : "("
_close_paren : ")"
_open_object : "{"
_close_object : _close_curly_brace
_close_curly_brace : "}"
%import common.WS
%ignore WS
""", parser="lalr")
def parse(self, text):
return self.lark.parse(text)