This repository has been archived by the owner on Mar 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
normalizer.go
411 lines (386 loc) · 13.2 KB
/
normalizer.go
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package normalizer
import (
"gopkg.in/bblfsh/sdk.v2/uast"
"gopkg.in/bblfsh/sdk.v2/uast/role"
. "gopkg.in/bblfsh/sdk.v2/uast/transformer"
)
var Preprocess = Transformers([][]Transformer{
{Mappings(Preprocessors...)},
}...)
var PreprocessCode = []CodeTransformer{}
var Preprocessors = []Mapping{
ObjectToNode{
InternalTypeKey: "ast_type",
LineKey: "lineno",
ColumnKey: "col_offset",
EndLineKey: "end_lineno",
EndColumnKey: "end_col_offset",
}.Mapping(),
}
var Normalize = Transformers([][]Transformer{
{Mappings(Normalizers...)},
}...)
func funcDefMap(typ string, async bool) Mapping {
return MapSemantic(typ, uast.FunctionGroup{}, MapObj(
Fields{
{Name: "body", Op: Var("body")},
{Name: "name", Op: Var("name")},
// Arguments should be converted by the uast.Arguments normalization
{Name: "args", Op: Obj{
"args": Var("arguments"),
uast.KeyPos: Var("_pos"),
uast.KeyType: Var("_type"),
}},
// Will be filled only if there is a Python3 type annotation. If the field is
// missing or the value is nil it DOESNT mean that the function returns None,
// just that there is no annotation (but the function could still return some
// other type!).
{Name: "returns", Optional: "ret_opt", Op: Cases("ret_case",
Is(nil),
Obj{
uast.KeyType: String("BoxedName"),
"boxed_value": Var("ret_type"),
// No problem dropping this one, it's used by an internal interpreter optimization/cache
// without semantic meaning
"ctx": Any(),
}),
},
{Name: "decorator_list", Op: Var("func_decorators")},
{Name: "noops_previous", Optional: "np_opt", Op: Var("noops_previous")},
{Name: "noops_sameline", Optional: "ns_opt", Op: Var("noops_sameline")},
},
Obj{
"Nodes": Arr(
Fields{
// FIXME: generator=true if it uses yield anywhere in the body
{Name: "async", Op: Bool(async)},
{Name: "decorators", Op: Var("func_decorators")},
{Name: "comments", Op: Fields{
{Name: "noops_previous", Optional: "np_opt", Op: Var("noops_previous")},
{Name: "noops_sameline", Optional: "ns_opt", Op: Var("noops_sameline")},
}},
},
UASTType(uast.Alias{}, Obj{
// FIXME: can't call identifierWithPos because it would take the position of the
// function node that is not exactly the same as the position of the function name
"Name": UASTType(uast.Identifier{}, Obj{
"Name": Var("name"),
}),
"Node": UASTType(uast.Function{}, Obj{
"Type": UASTType(uast.FunctionType{}, Fields{
{Name: "Arguments", Op: Var("arguments")},
{Name: "Returns", Optional: "ret_opt", Op: Cases("ret_case",
// Dont add None here as default, read the comment on the upper
// side of the annotation
Is(nil),
Arr(UASTType(uast.Argument{},
Obj{
"Type": Var("ret_type"),
},
)))},
}),
"Body": UASTType(uast.Block{}, Obj{
"Statements": Var("body"),
}),
}),
}),
),
},
))
}
func identifierWithPos(nameVar string) ObjectOp {
return UASTType(uast.Identifier{}, Obj{
uast.KeyPos: UASTType(uast.Positions{}, Obj{
uast.KeyStart: Var(uast.KeyStart),
uast.KeyEnd: Var(uast.KeyEnd),
}),
"Name": Var(nameVar),
})
}
// mapStr factorizes the common annotation for string types (Byte, Str, StrLiteral)
func mapStr(nativeType string) Mapping {
return Map(
Part("_", Fields{
{Name: uast.KeyType, Op: String(nativeType)},
{Name: uast.KeyPos, Op: Var("pos_")},
{Name: "s", Op: Var("s")},
{Name: "noops_previous", Optional: "np_opt", Op: Var("noops_previous")},
{Name: "noops_sameline", Optional: "ns_opt", Op: Var("noops_sameline")},
}),
Part("_", Fields{
{Name: uast.KeyType, Op: String("Boxed" + nativeType)},
{Name: "boxed_value", Op: UASTType(uast.String{}, Obj{
uast.KeyPos: Var("pos_"),
"Value": Var("s"),
})},
{Name: "noops_previous", Optional: "np_opt", Op: Var("noops_previous")},
{Name: "noops_sameline", Optional: "ns_opt", Op: Var("noops_sameline")},
}),
)
}
var Normalizers = []Mapping{
// Box Names, Strings, Attributes, and Bools into a "BoxedFoo" moving the real node to the
// "value" property and keeping the comments in the parent (if not, comments would be lost
// when promoting the objects).
// For other objects, the comments are dropped.
// See: https://github.com/bblfsh/sdk/issues/361
Map(
Part("_", Fields{
{Name: uast.KeyType, Op: String("Name")},
{Name: uast.KeyPos, Op: Var("pos_")},
{Name: "id", Op: Var("id")},
{Name: "noops_previous", Optional: "np_opt", Op: Var("noops_previous")},
{Name: "noops_sameline", Optional: "ns_opt", Op: Var("noops_sameline")},
}),
Part("_", Fields{
{Name: uast.KeyType, Op: String("BoxedName")},
{Name: "boxed_value", Op: UASTType(uast.Identifier{}, Obj{
uast.KeyPos: Var("pos_"),
"Name": Var("id"),
})},
{Name: "noops_previous", Optional: "np_opt", Op: Var("noops_previous")},
{Name: "noops_sameline", Optional: "ns_opt", Op: Var("noops_sameline")},
}),
),
Map(
Part("_", Fields{
{Name: uast.KeyType, Op: String("BoolLiteral")},
{Name: uast.KeyPos, Op: Var("pos_")},
{Name: "value", Op: Var("lv")},
{Name: "noops_previous", Optional: "np_opt", Op: Var("noops_previous")},
{Name: "noops_sameline", Optional: "ns_opt", Op: Var("noops_sameline")},
}),
Part("_", Fields{
{Name: uast.KeyType, Op: String("BoxedBoolLiteral")},
{Name: "boxed_value", Op: UASTType(uast.Bool{}, Obj{
uast.KeyPos: Var("pos_"),
"Value": Var("lv"),
})},
{Name: "noops_previous", Optional: "np_opt", Op: Var("noops_previous")},
{Name: "noops_sameline", Optional: "ns_opt", Op: Var("noops_sameline")},
}),
),
Map(
Part("_", Fields{
{Name: uast.KeyType, Op: String("Attribute")},
{Name: uast.KeyPos, Op: Var("pos_")},
{Name: "attr", Op: Var("aname")},
// No problem dropping this one, it's used by an internal interpreter optimization
// cache without semantic meaning
// without semantic meaning
{Name: "ctx", Op: Any()},
{Name: "noops_previous", Optional: "np_opt", Op: Var("noops_previous")},
{Name: "noops_sameline", Optional: "ns_opt", Op: Var("noops_sameline")},
}),
Part("_", Fields{
{Name: uast.KeyType, Op: String("BoxedAttribute")},
{Name: "boxed_value", Op: UASTType(uast.Identifier{}, Obj{
uast.KeyPos: Var("pos_"),
"Name": Var("aname"),
})},
{Name: "noops_previous", Optional: "np_opt", Op: Var("noops_previous")},
{Name: "noops_sameline", Optional: "ns_opt", Op: Var("noops_sameline")},
}),
),
mapStr("Bytes"),
mapStr("Str"),
mapStr("StringLiteral"),
MapSemantic("NoopLine", uast.Comment{}, MapObj(
Obj{
"noop_line": CommentTextTrimmed([2]string{"#", ""}, "comm"),
},
CommentNode(false, "comm", nil),
)),
MapSemantic("NoopSameLine", uast.Comment{}, MapObj(
Obj{
"s": CommentText([2]string{"#", ""}, "comm"),
},
CommentNode(false, "comm", nil),
)),
// FIXME: no positions for keywords in the native AST
AnnotateType("keyword", MapObj(
Fields{
{Name: "arg", Op: Var("name")},
// FIXME: change this once we've a way to store other nodes on semantic objects
// See: https://github.com/bblfsh/sdk/issues/361
// See: https://github.com/bblfsh/python-driver/issues/178
{Name: "noops_previous", Optional: "np_opt", Op: Any()},
{Name: "noops_sameline", Optional: "ns_opt", Op: Any()},
},
Fields{
{Name: "arg",
Op: UASTType(uast.Identifier{}, Obj{
"Name": Var("name"),
})},
}),
role.Name),
MapSemantic("arg", uast.Argument{}, MapObj(
Fields{
{Name: uast.KeyToken, Op: Var("name")},
{Name: "default", Optional: "opt_def", Op: Var("init")},
// No problem dropping this one, it's used by an internal interpreter optimization/cache
// without semantic meaning
{Name: "ctx", Optional: "opt_ctx", Op: Any()},
// FIXME: change this once we've a way to store other nodes on semantic objects
// See: https://github.com/bblfsh/sdk/issues/361
// See: https://github.com/bblfsh/python-driver/issues/178
{Name: "noops_previous", Optional: "np_opt", Op: Any()},
{Name: "noops_sameline", Optional: "ns_opt", Op: Any()},
// This one is pesky - they're ignored by the runtime, could have typing from
// mypy, or could have anything else, so we can assign to the semantic type
{Name: "annotation", Optional: "ann_opt", Op: Any()},
},
Fields{
{Name: "Name", Op: identifierWithPos("name")},
{Name: "Init", Optional: "opt_def", Op: Var("init")},
},
)),
MapSemantic("kwonly_arg", uast.Argument{}, MapObj(
Fields{
{Name: uast.KeyToken, Op: Var("name")},
{Name: "default", Op: Var("init")},
// FIXME: change this once we've a way to store other nodes on semantic objects
// See: https://github.com/bblfsh/sdk/issues/361
// See: https://github.com/bblfsh/python-driver/issues/178
{Name: "noops_previous", Optional: "np_opt", Op: Any()},
{Name: "noops_sameline", Optional: "ns_opt", Op: Any()},
// This one is pesky - they're ignored by the runtime, could have typing from
// mypy, or could have anything else, so we can assign to the semantic type
{Name: "annotation", Op: Any()},
},
Obj{
"Init": Var("init"),
"Name": identifierWithPos("name"),
},
)),
MapSemantic("vararg", uast.Argument{}, MapObj(
Fields{
{Name: uast.KeyToken, Op: Var("name")},
// FIXME: change this once we've a way to store other nodes on semantic objects
// See: https://github.com/bblfsh/sdk/issues/361
// See: https://github.com/bblfsh/python-driver/issues/178
{Name: "noops_previous", Optional: "np_opt", Op: Any()},
{Name: "noops_sameline", Optional: "ns_opt", Op: Any()},
// This one is pesky - they're ignored by the runtime, could have typing from
// mypy, or could have anything else, so we can assign to the semantic type
{Name: "annotation", Op: Any()},
},
Obj{
"Name": identifierWithPos("name"),
"Variadic": Bool(true),
},
)),
MapSemantic("kwarg", uast.Argument{}, MapObj(
Fields{
{Name: uast.KeyToken, Op: Var("name")},
// FIXME: change this once we've a way to store other nodes on semantic objects
// See: https://github.com/bblfsh/sdk/issues/361
// See: https://github.com/bblfsh/python-driver/issues/178
{Name: "noops_previous", Optional: "np_opt", Op: Any()},
{Name: "noops_sameline", Optional: "ns_opt", Op: Any()},
// This one is pesky - they're ignored by the runtime, could have typing from
// mypy, or could have anything else, so we can assign to the semantic type
{Name: "annotation", Op: Any()},
},
Obj{
"Name": identifierWithPos("name"),
"MapVariadic": Bool(true),
},
)),
funcDefMap("FunctionDef", false),
funcDefMap("AsyncFunctionDef", true),
AnnotateType("Import", MapObj(
Obj{
"names": Each("vals", Var("name")),
},
Obj{
"names": Each("vals", UASTType(uast.RuntimeImport{},
Obj{
"Path": Var("name"),
})),
},
), role.Import, role.Declaration, role.Statement),
// FIXME: aliases doesn't have a position (can't be currently fixed by the tokenizer
// because they don't even have a line in the native AST)
MapSemantic("alias", uast.Alias{}, MapObj(
Obj{
"name": Var("name"),
"asname": Cases("case_alias",
Check(Is(nil), Var("nilalias")),
Check(Not(Is(nil)), Var("alias")),
),
},
CasesObj("case_alias",
Obj{
"Name": UASTType(uast.Identifier{}, Obj{
"Name": Var("name"),
}),
},
Objs{
{"Node": Obj{}},
{
"Node": UASTType(uast.Identifier{},
Obj{
"Name": Var("alias"),
}),
}},
))),
// Star imports
MapSemantic("ImportFrom", uast.RuntimeImport{}, MapObj(
Fields{
{Name: "names", Op: Arr(
Obj{
uast.KeyType: String("uast:Alias"),
uast.KeyPos: Var("pos"),
"Name": Obj{
uast.KeyType: String("uast:Identifier"),
"Name": String("*"),
},
"Node": Obj{},
},
)},
{Name: "level", Op: Var("level")},
{Name: "module", Op: Var("module")},
// FIXME: change this once we've a way to store other nodes on semantic objects
// See: https://github.com/bblfsh/sdk/issues/361
// See: https://github.com/bblfsh/python-driver/issues/178
{Name: "noops_previous", Optional: "np_opt", Op: Any()},
{Name: "noops_sameline", Optional: "ns_opt", Op: Any()},
},
Obj{
"All": Bool(true),
"Path": UASTType(uast.Identifier{}, Obj{
"Name": OpPrependPath{
// FIXME: no position for the module (path) in the native AST, only when the import starts
numLevel: Var("level"),
path: Var("module"),
joined: Var("joined"),
prefix: "../",
},
}),
},
)),
MapSemantic("ImportFrom", uast.RuntimeImport{}, MapObj(
Fields{
{Name: "names", Op: Var("names")},
{Name: "module", Op: Var("module")},
{Name: "level", Op: Var("level")},
// FIXME: change this once we've a way to store other nodes on semantic objects
// See: https://github.com/bblfsh/sdk/issues/361
// See: https://github.com/bblfsh/python-driver/issues/178
{Name: "noops_previous", Optional: "np_opt", Op: Any()},
{Name: "noops_sameline", Optional: "ns_opt", Op: Any()},
},
Obj{
"Names": Var("names"),
"Path": UASTType(uast.Identifier{}, Obj{
"Name": OpPrependPath{
numLevel: Var("level"),
path: Var("module"),
joined: Var("joined"),
prefix: "../",
},
}),
},
)),
}