fix(userdata): Dropped sloppy JSOn parser in favour of a true JavaScript AST analyzer
[plugin.video.netflix.git] / resources / lib / pyjsparser / std_nodes.py
1 from .pyjsparserdata import *
2
3
4 class BaseNode:
5     def finish(self):
6         pass
7
8     def finishArrayExpression(self, elements):
9         self.type = Syntax.ArrayExpression
10         self.elements = elements
11         self.finish()
12         return self
13
14     def finishArrayPattern(self, elements):
15         self.type = Syntax.ArrayPattern
16         self.elements = elements
17         self.finish()
18         return self
19
20     def finishArrowFunctionExpression(self, params, defaults, body, expression):
21         self.type = Syntax.ArrowFunctionExpression
22         self.id = None
23         self.params = params
24         self.defaults = defaults
25         self.body = body
26         self.generator = False
27         self.expression = expression
28         self.finish()
29         return self
30
31     def finishAssignmentExpression(self, operator, left, right):
32         self.type = Syntax.AssignmentExpression
33         self.operator = operator
34         self.left = left
35         self.right = right
36         self.finish()
37         return self
38
39     def finishAssignmentPattern(self, left, right):
40         self.type = Syntax.AssignmentPattern
41         self.left = left
42         self.right = right
43         self.finish()
44         return self
45
46     def finishBinaryExpression(self, operator, left, right):
47         self.type = Syntax.LogicalExpression if (operator == '||' or operator == '&&') else Syntax.BinaryExpression
48         self.operator = operator
49         self.left = left
50         self.right = right
51         self.finish()
52         return self
53
54     def finishBlockStatement(self, body):
55         self.type = Syntax.BlockStatement
56         self.body = body
57         self.finish()
58         return self
59
60     def finishBreakStatement(self, label):
61         self.type = Syntax.BreakStatement
62         self.label = label
63         self.finish()
64         return self
65
66     def finishCallExpression(self, callee, args):
67         self.type = Syntax.CallExpression
68         self.callee = callee
69         self.arguments = args
70         self.finish()
71         return self
72
73     def finishCatchClause(self, param, body):
74         self.type = Syntax.CatchClause
75         self.param = param
76         self.body = body
77         self.finish()
78         return self
79
80     def finishClassBody(self, body):
81         self.type = Syntax.ClassBody
82         self.body = body
83         self.finish()
84         return self
85
86     def finishClassDeclaration(self, id, superClass, body):
87         self.type = Syntax.ClassDeclaration
88         self.id = id
89         self.superClass = superClass
90         self.body = body
91         self.finish()
92         return self
93
94     def finishClassExpression(self, id, superClass, body):
95         self.type = Syntax.ClassExpression
96         self.id = id
97         self.superClass = superClass
98         self.body = body
99         self.finish()
100         return self
101
102     def finishConditionalExpression(self, test, consequent, alternate):
103         self.type = Syntax.ConditionalExpression
104         self.test = test
105         self.consequent = consequent
106         self.alternate = alternate
107         self.finish()
108         return self
109
110     def finishContinueStatement(self, label):
111         self.type = Syntax.ContinueStatement
112         self.label = label
113         self.finish()
114         return self
115
116     def finishDebuggerStatement(self, ):
117         self.type = Syntax.DebuggerStatement
118         self.finish()
119         return self
120
121     def finishDoWhileStatement(self, body, test):
122         self.type = Syntax.DoWhileStatement
123         self.body = body
124         self.test = test
125         self.finish()
126         return self
127
128     def finishEmptyStatement(self, ):
129         self.type = Syntax.EmptyStatement
130         self.finish()
131         return self
132
133     def finishExpressionStatement(self, expression):
134         self.type = Syntax.ExpressionStatement
135         self.expression = expression
136         self.finish()
137         return self
138
139     def finishForStatement(self, init, test, update, body):
140         self.type = Syntax.ForStatement
141         self.init = init
142         self.test = test
143         self.update = update
144         self.body = body
145         self.finish()
146         return self
147
148     def finishForInStatement(self, left, right, body):
149         self.type = Syntax.ForInStatement
150         self.left = left
151         self.right = right
152         self.body = body
153         self.each = False
154         self.finish()
155         return self
156
157     def finishFunctionDeclaration(self, id, params, defaults, body):
158         self.type = Syntax.FunctionDeclaration
159         self.id = id
160         self.params = params
161         self.defaults = defaults
162         self.body = body
163         self.generator = False
164         self.expression = False
165         self.finish()
166         return self
167
168     def finishFunctionExpression(self, id, params, defaults, body):
169         self.type = Syntax.FunctionExpression
170         self.id = id
171         self.params = params
172         self.defaults = defaults
173         self.body = body
174         self.generator = False
175         self.expression = False
176         self.finish()
177         return self
178
179     def finishIdentifier(self, name):
180         self.type = Syntax.Identifier
181         self.name = name
182         self.finish()
183         return self
184
185     def finishIfStatement(self, test, consequent, alternate):
186         self.type = Syntax.IfStatement
187         self.test = test
188         self.consequent = consequent
189         self.alternate = alternate
190         self.finish()
191         return self
192
193     def finishLabeledStatement(self, label, body):
194         self.type = Syntax.LabeledStatement
195         self.label = label
196         self.body = body
197         self.finish()
198         return self
199
200     def finishLiteral(self, token):
201         self.type = Syntax.Literal
202         self.value = token['value']
203         self.raw = None  # todo fix it?
204         if token.get('regex'):
205             self.regex = token['regex']
206         self.finish()
207         return self
208
209     def finishMemberExpression(self, accessor, object, property):
210         self.type = Syntax.MemberExpression
211         self.computed = accessor == '['
212         self.object = object
213         self.property = property
214         self.finish()
215         return self
216
217     def finishNewExpression(self, callee, args):
218         self.type = Syntax.NewExpression
219         self.callee = callee
220         self.arguments = args
221         self.finish()
222         return self
223
224     def finishObjectExpression(self, properties):
225         self.type = Syntax.ObjectExpression
226         self.properties = properties
227         self.finish()
228         return self
229
230     def finishObjectPattern(self, properties):
231         self.type = Syntax.ObjectPattern
232         self.properties = properties
233         self.finish()
234         return self
235
236     def finishPostfixExpression(self, operator, argument):
237         self.type = Syntax.UpdateExpression
238         self.operator = operator
239         self.argument = argument
240         self.prefix = False
241         self.finish()
242         return self
243
244     def finishProgram(self, body):
245         self.type = Syntax.Program
246         self.body = body
247         self.finish()
248         return self
249
250     def finishPyimport(self, imp):
251         self.type = 'PyimportStatement'
252         self.imp = imp
253         self.finish()
254         return self
255
256     def finishProperty(self, kind, key, computed, value, method, shorthand):
257         self.type = Syntax.Property
258         self.key = key
259         self.computed = computed
260         self.value = value
261         self.kind = kind
262         self.method = method
263         self.shorthand = shorthand
264         self.finish()
265         return self
266
267     def finishRestElement(self, argument):
268         self.type = Syntax.RestElement
269         self.argument = argument
270         self.finish()
271         return self
272
273     def finishReturnStatement(self, argument):
274         self.type = Syntax.ReturnStatement
275         self.argument = argument
276         self.finish()
277         return self
278
279     def finishSequenceExpression(self, expressions):
280         self.type = Syntax.SequenceExpression
281         self.expressions = expressions
282         self.finish()
283         return self
284
285     def finishSpreadElement(self, argument):
286         self.type = Syntax.SpreadElement
287         self.argument = argument
288         self.finish()
289         return self
290
291     def finishSwitchCase(self, test, consequent):
292         self.type = Syntax.SwitchCase
293         self.test = test
294         self.consequent = consequent
295         self.finish()
296         return self
297
298     def finishSuper(self, ):
299         self.type = Syntax.Super
300         self.finish()
301         return self
302
303     def finishSwitchStatement(self, discriminant, cases):
304         self.type = Syntax.SwitchStatement
305         self.discriminant = discriminant
306         self.cases = cases
307         self.finish()
308         return self
309
310     def finishTaggedTemplateExpression(self, tag, quasi):
311         self.type = Syntax.TaggedTemplateExpression
312         self.tag = tag
313         self.quasi = quasi
314         self.finish()
315         return self
316
317     def finishTemplateElement(self, value, tail):
318         self.type = Syntax.TemplateElement
319         self.value = value
320         self.tail = tail
321         self.finish()
322         return self
323
324     def finishTemplateLiteral(self, quasis, expressions):
325         self.type = Syntax.TemplateLiteral
326         self.quasis = quasis
327         self.expressions = expressions
328         self.finish()
329         return self
330
331     def finishThisExpression(self, ):
332         self.type = Syntax.ThisExpression
333         self.finish()
334         return self
335
336     def finishThrowStatement(self, argument):
337         self.type = Syntax.ThrowStatement
338         self.argument = argument
339         self.finish()
340         return self
341
342     def finishTryStatement(self, block, handler, finalizer):
343         self.type = Syntax.TryStatement
344         self.block = block
345         self.guardedHandlers = []
346         self.handlers = [handler] if handler else []
347         self.handler = handler
348         self.finalizer = finalizer
349         self.finish()
350         return self
351
352     def finishUnaryExpression(self, operator, argument):
353         self.type = Syntax.UpdateExpression if (operator == '++' or operator == '--') else Syntax.UnaryExpression
354         self.operator = operator
355         self.argument = argument
356         self.prefix = True
357         self.finish()
358         return self
359
360     def finishVariableDeclaration(self, declarations):
361         self.type = Syntax.VariableDeclaration
362         self.declarations = declarations
363         self.kind = 'var'
364         self.finish()
365         return self
366
367     def finishLexicalDeclaration(self, declarations, kind):
368         self.type = Syntax.VariableDeclaration
369         self.declarations = declarations
370         self.kind = kind
371         self.finish()
372         return self
373
374     def finishVariableDeclarator(self, id, init):
375         self.type = Syntax.VariableDeclarator
376         self.id = id
377         self.init = init
378         self.finish()
379         return self
380
381     def finishWhileStatement(self, test, body):
382         self.type = Syntax.WhileStatement
383         self.test = test
384         self.body = body
385         self.finish()
386         return self
387
388     def finishWithStatement(self, object, body):
389         self.type = Syntax.WithStatement
390         self.object = object
391         self.body = body
392         self.finish()
393         return self
394
395     def finishExportSpecifier(self, local, exported):
396         self.type = Syntax.ExportSpecifier
397         self.exported = exported or local
398         self.local = local
399         self.finish()
400         return self
401
402     def finishImportDefaultSpecifier(self, local):
403         self.type = Syntax.ImportDefaultSpecifier
404         self.local = local
405         self.finish()
406         return self
407
408     def finishImportNamespaceSpecifier(self, local):
409         self.type = Syntax.ImportNamespaceSpecifier
410         self.local = local
411         self.finish()
412         return self
413
414     def finishExportNamedDeclaration(self, declaration, specifiers, src):
415         self.type = Syntax.ExportNamedDeclaration
416         self.declaration = declaration
417         self.specifiers = specifiers
418         self.source = src
419         self.finish()
420         return self
421
422     def finishExportDefaultDeclaration(self, declaration):
423         self.type = Syntax.ExportDefaultDeclaration
424         self.declaration = declaration
425         self.finish()
426         return self
427
428     def finishExportAllDeclaration(self, src):
429         self.type = Syntax.ExportAllDeclaration
430         self.source = src
431         self.finish()
432         return self
433
434     def finishImportSpecifier(self, local, imported):
435         self.type = Syntax.ImportSpecifier
436         self.local = local or imported
437         self.imported = imported
438         self.finish()
439         return self
440
441     def finishImportDeclaration(self, specifiers, src):
442         self.type = Syntax.ImportDeclaration
443         self.specifiers = specifiers
444         self.source = src
445         self.finish()
446         return self
447
448     def __getitem__(self, item):
449         return getattr(self, item)
450
451     def __setitem__(self, key, value):
452         setattr(self, key, value)
453
454
455 class Node(BaseNode):
456     pass
457
458
459 class WrappingNode(BaseNode):
460     def __init__(self, startToken=None):
461         pass
462
463
464 def node_to_dict(node):  # extremely important for translation speed
465     if isinstance(node, list):
466         return [node_to_dict(e) for e in node]
467     elif isinstance(node, dict):
468         return dict((k, node_to_dict(v)) for k, v in node.items())
469     elif not isinstance(node, BaseNode):
470         return node
471     return dict((k, node_to_dict(v)) for k, v in node.__dict__.items())