-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiq.py
executable file
·244 lines (185 loc) · 6.16 KB
/
piq.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import wrappers
class ObjectProxy(wrappers.ObjectProxy):
def __init__(self, wrapped, loc):
super(ObjectProxy, self).__init__(wrapped)
self._self_loc = loc
@property
def __loc__(self):
return self._self_loc
def __call__(self, *args, **kwargs):
return self.__wrapped__(*args, **kwargs)
def __repr__(self):
return repr(self.__wrapped__)
# called from modified AST
def wrap_object(x, lineno, col_offset):
loc = make_loc((lineno, col_offset))
return ObjectProxy(x, loc)
# remove extra wrapping upon constracting Piq AST nodes
def unwrap_object(x):
if isinstance(x, ObjectProxy):
return unwrap_object(x.__wrapped__)
else:
return x
class Loc(object):
def __init__(self, init_loc):
self.line, self.column = init_loc
def __repr__(self):
return "{}:{}".format(self.line, self.column)
def make_loc(loc):
if loc is None:
return None
elif isinstance(loc, Loc):
return loc
else:
return Loc(loc)
class ParseError(Exception):
def __init__(self, loc, error):
self.error = error
self.loc = loc
def __repr__(self):
return error
def is_piq_node(x):
return isinstance(x, (Name, Named, List, Splice, Scalar))
# make piq AST node
#
# NOTE: this function can be called externally by code that constructs piq ASTs
def make_node(x, is_inside_list=False):
# when called externally, terms won't be wrapped and won't inclue loc
# information
if isinstance(x, ObjectProxy):
loc = x.__loc__
else:
loc = None
if is_piq_node(x):
# already a Piq node
return unwrap_object(x)
elif isinstance(x, (bool, int, float, basestring)):
return Scalar(unwrap_object(x), loc)
elif isinstance(x, list):
# XXX: support iterables?
items = [make_node(item, is_inside_list=True) for item in x]
return List(items, loc)
elif hasattr(x, '__piq__'):
return make_node(x.__piq__())
else:
raise ParseError(
loc,
"value of invalid type '{}': {}".format(type_name(x), x)
)
def type_name(x):
return type(unwrap_object(x)).__name__
# splice Splice'd values into the outer lists
def transform_expand_splices(node):
if isinstance(node, List):
new_items = []
for item in node.items:
if isinstance(item, Splice):
new_items.extend(item.expand())
else:
new_items.append(transform_expand_splices(item))
return List(new_items, node.loc)
elif isinstance(node, Named):
new_value = transform_expand_splices(node.value)
return Named(node.name, node.loc, new_value)
elif isinstance(node, Splice):
# XXX: perform such validation earlier, e.g. in make_node?
raise ParseError(
node.loc,
"splices are only allowed in lists"
)
else:
return node
# transform a.b ... name chain into .a (.b ...) cons
def transform_expand_names(node):
if isinstance(node, List):
new_items = [transform_expand_names(item) for item in node.items]
return List(new_items, node.loc)
elif isinstance(node, Named):
new_value = transform_expand_names(node.value)
node = Named(node.name, node.loc, new_value)
return expand_named(node)
elif isinstance(node, Splice):
assert False # splices should be expanded before names
elif isinstance(node, Name):
return expand_name(node)
elif isinstance(node, Scalar):
return node
else:
assert False
def expand_named(node):
name_parts = split_name(node.name)
if len(name_parts) == 1:
return node
else:
return make_named_chain(name_parts, node.value, node.loc)
def expand_name(node):
name_parts = split_name(node.name)
if len(name_parts) == 1:
return node
else:
value = Name(name_parts[-1], node.loc)
return make_named_chain(name_parts[:-1], value, node.loc)
def make_named_chain(name_parts, value, loc):
node = value
for name in reversed(name_parts):
node = Named(name, loc, node)
return node
def split_name(name):
return name.split('.')
# value of one of the primitive Piq types
class Scalar(object):
def __init__(self, value, loc):
self.value = value
self.loc = loc
def __repr__(self):
return repr(self.value)
# list of nodes
class List(object):
def __init__(self, items, loc):
self.items = items
self.loc = loc
def __repr__(self):
return repr(self.items)
class Name(object):
def __init__(self, name, loc):
self.name = name
self.loc = make_loc(loc)
def __repr__(self):
return '.' + self.name
class Named(object):
def __init__(self, name, loc, value):
self.name = name
self.loc = make_loc(loc)
self.value = make_node(value)
def __repr__(self):
#name_repr = repr(self.name)
name_repr = '.' + self.name
value_repr = repr(self.value)
if isinstance(self.value, Name) or isinstance(self.value, Named):
return name_repr + ' (' + value_repr + ')'
else:
return name_repr + ' ' + value_repr
# list of values to be spliced into the containing list
class Splice(object):
def __init__(self, name, loc, items):
if not isinstance(items, list):
raise ParseError(
items.__loc__,
"{}* must be followed by a list, instead followed by a value of type '{}': {}".format(
self.name, type_name(items), items
)
)
self.name = name
self.loc = make_loc(loc)
self.items = [make_node(x) for x in items]
def __repr__(self):
return '.' + self.name + '* ' + repr(self.items)
def expand(self):
return [Named(self.name, self.loc, x) for x in self.items]
def parse(x, expand_splices=False, expand_names=False):
node = make_node(x)
if expand_splices:
node = transform_expand_splices(node)
if expand_names:
node = transform_expand_names(node)
return node