1# -*- coding: utf-8 -*-
2# Copyright JS Foundation and other contributors, https://js.foundation/
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are met:
6#
7#   * Redistributions of source code must retain the above copyright
8#     notice, this list of conditions and the following disclaimer.
9#   * Redistributions in binary form must reproduce the above copyright
10#     notice, this list of conditions and the following disclaimer in the
11#     documentation and/or other materials provided with the distribution.
12#
13# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16# ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
17# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24from __future__ import absolute_import, unicode_literals
25
26from .objects import Object
27from .syntax import Syntax
28from .scanner import RegExp
29
30
31class Node(Object):
32    def __dir__(self):
33        return list(self.__dict__.keys())
34
35    def __iter__(self):
36        return self.__iter__
37
38    def keys(self):
39        return self.__dict__.keys()
40
41    def items(self):
42        return self.__dict__.items()
43
44
45class ArrayExpression(Node):
46    def __init__(self, elements):
47        self.type = Syntax.ArrayExpression
48        self.elements = elements
49
50
51class ArrayPattern(Node):
52    def __init__(self, elements):
53        self.type = Syntax.ArrayPattern
54        self.elements = elements
55
56
57class ArrowFunctionExpression(Node):
58    def __init__(self, params, body, expression):
59        self.type = Syntax.ArrowFunctionExpression
60        self.generator = False
61        self.isAsync = False
62        self.params = params
63        self.body = body
64        self.expression = expression
65
66
67class AssignmentExpression(Node):
68    def __init__(self, operator, left, right):
69        self.type = Syntax.AssignmentExpression
70        self.operator = operator
71        self.left = left
72        self.right = right
73
74
75class AssignmentPattern(Node):
76    def __init__(self, left, right):
77        self.type = Syntax.AssignmentPattern
78        self.left = left
79        self.right = right
80
81
82class AsyncArrowFunctionExpression(Node):
83    def __init__(self, params, body, expression):
84        self.type = Syntax.ArrowFunctionExpression
85        self.generator = False
86        self.isAsync = True
87        self.params = params
88        self.body = body
89        self.expression = expression
90
91
92class AsyncFunctionDeclaration(Node):
93    def __init__(self, id, params, body):
94        self.type = Syntax.FunctionDeclaration
95        self.generator = False
96        self.expression = False
97        self.isAsync = True
98        self.id = id
99        self.params = params
100        self.body = body
101
102
103class AsyncFunctionExpression(Node):
104    def __init__(self, id, params, body):
105        self.type = Syntax.FunctionExpression
106        self.generator = False
107        self.expression = False
108        self.isAsync = True
109        self.id = id
110        self.params = params
111        self.body = body
112
113
114class AwaitExpression(Node):
115    def __init__(self, argument):
116        self.type = Syntax.AwaitExpression
117        self.argument = argument
118
119
120class BinaryExpression(Node):
121    def __init__(self, operator, left, right):
122        self.type = Syntax.LogicalExpression if operator in ('||', '&&') else Syntax.BinaryExpression
123        self.operator = operator
124        self.left = left
125        self.right = right
126
127
128class BlockStatement(Node):
129    def __init__(self, body):
130        self.type = Syntax.BlockStatement
131        self.body = body
132
133
134class BreakStatement(Node):
135    def __init__(self, label):
136        self.type = Syntax.BreakStatement
137        self.label = label
138
139
140class CallExpression(Node):
141    def __init__(self, callee, args):
142        self.type = Syntax.CallExpression
143        self.callee = callee
144        self.arguments = args
145
146
147class CatchClause(Node):
148    def __init__(self, param, body):
149        self.type = Syntax.CatchClause
150        self.param = param
151        self.body = body
152
153
154class ClassBody(Node):
155    def __init__(self, body):
156        self.type = Syntax.ClassBody
157        self.body = body
158
159
160class ClassDeclaration(Node):
161    def __init__(self, id, superClass, body):
162        self.type = Syntax.ClassDeclaration
163        self.id = id
164        self.superClass = superClass
165        self.body = body
166
167
168class ClassExpression(Node):
169    def __init__(self, id, superClass, body):
170        self.type = Syntax.ClassExpression
171        self.id = id
172        self.superClass = superClass
173        self.body = body
174
175
176class ComputedMemberExpression(Node):
177    def __init__(self, object, property):
178        self.type = Syntax.MemberExpression
179        self.computed = True
180        self.object = object
181        self.property = property
182
183
184class ConditionalExpression(Node):
185    def __init__(self, test, consequent, alternate):
186        self.type = Syntax.ConditionalExpression
187        self.test = test
188        self.consequent = consequent
189        self.alternate = alternate
190
191
192class ContinueStatement(Node):
193    def __init__(self, label):
194        self.type = Syntax.ContinueStatement
195        self.label = label
196
197
198class DebuggerStatement(Node):
199    def __init__(self):
200        self.type = Syntax.DebuggerStatement
201
202
203class Directive(Node):
204    def __init__(self, expression, directive):
205        self.type = Syntax.ExpressionStatement
206        self.expression = expression
207        self.directive = directive
208
209
210class DoWhileStatement(Node):
211    def __init__(self, body, test):
212        self.type = Syntax.DoWhileStatement
213        self.body = body
214        self.test = test
215
216
217class EmptyStatement(Node):
218    def __init__(self):
219        self.type = Syntax.EmptyStatement
220
221
222class ExportAllDeclaration(Node):
223    def __init__(self, source):
224        self.type = Syntax.ExportAllDeclaration
225        self.source = source
226
227
228class ExportDefaultDeclaration(Node):
229    def __init__(self, declaration):
230        self.type = Syntax.ExportDefaultDeclaration
231        self.declaration = declaration
232
233
234class ExportNamedDeclaration(Node):
235    def __init__(self, declaration, specifiers, source):
236        self.type = Syntax.ExportNamedDeclaration
237        self.declaration = declaration
238        self.specifiers = specifiers
239        self.source = source
240
241
242class ExportSpecifier(Node):
243    def __init__(self, local, exported):
244        self.type = Syntax.ExportSpecifier
245        self.exported = exported
246        self.local = local
247
248
249class ExportDefaultSpecifier(Node):
250    def __init__(self, local):
251        self.type = Syntax.ExportDefaultSpecifier
252        self.local = local
253
254
255class ExpressionStatement(Node):
256    def __init__(self, expression):
257        self.type = Syntax.ExpressionStatement
258        self.expression = expression
259
260
261class ForInStatement(Node):
262    def __init__(self, left, right, body):
263        self.type = Syntax.ForInStatement
264        self.each = False
265        self.left = left
266        self.right = right
267        self.body = body
268
269
270class ForOfStatement(Node):
271    def __init__(self, left, right, body):
272        self.type = Syntax.ForOfStatement
273        self.left = left
274        self.right = right
275        self.body = body
276
277
278class ForStatement(Node):
279    def __init__(self, init, test, update, body):
280        self.type = Syntax.ForStatement
281        self.init = init
282        self.test = test
283        self.update = update
284        self.body = body
285
286
287class FunctionDeclaration(Node):
288    def __init__(self, id, params, body, generator):
289        self.type = Syntax.FunctionDeclaration
290        self.expression = False
291        self.isAsync = False
292        self.id = id
293        self.params = params
294        self.body = body
295        self.generator = generator
296
297
298class FunctionExpression(Node):
299    def __init__(self, id, params, body, generator):
300        self.type = Syntax.FunctionExpression
301        self.expression = False
302        self.isAsync = False
303        self.id = id
304        self.params = params
305        self.body = body
306        self.generator = generator
307
308
309class Identifier(Node):
310    def __init__(self, name):
311        self.type = Syntax.Identifier
312        self.name = name
313
314
315class IfStatement(Node):
316    def __init__(self, test, consequent, alternate):
317        self.type = Syntax.IfStatement
318        self.test = test
319        self.consequent = consequent
320        self.alternate = alternate
321
322
323class Import(Node):
324    def __init__(self):
325        self.type = Syntax.Import
326
327
328class ImportDeclaration(Node):
329    def __init__(self, specifiers, source):
330        self.type = Syntax.ImportDeclaration
331        self.specifiers = specifiers
332        self.source = source
333
334
335class ImportDefaultSpecifier(Node):
336    def __init__(self, local):
337        self.type = Syntax.ImportDefaultSpecifier
338        self.local = local
339
340
341class ImportNamespaceSpecifier(Node):
342    def __init__(self, local):
343        self.type = Syntax.ImportNamespaceSpecifier
344        self.local = local
345
346
347class ImportSpecifier(Node):
348    def __init__(self, local, imported):
349        self.type = Syntax.ImportSpecifier
350        self.local = local
351        self.imported = imported
352
353
354class LabeledStatement(Node):
355    def __init__(self, label, body):
356        self.type = Syntax.LabeledStatement
357        self.label = label
358        self.body = body
359
360
361class Literal(Node):
362    def __init__(self, value, raw):
363        self.type = Syntax.Literal
364        self.value = value
365        self.raw = raw
366
367
368class MetaProperty(Node):
369    def __init__(self, meta, property):
370        self.type = Syntax.MetaProperty
371        self.meta = meta
372        self.property = property
373
374
375class MethodDefinition(Node):
376    def __init__(self, key, computed, value, kind, isStatic):
377        self.type = Syntax.MethodDefinition
378        self.key = key
379        self.computed = computed
380        self.value = value
381        self.kind = kind
382        self.static = isStatic
383
384
385class FieldDefinition(Node):
386    def __init__(self, key, computed, value, kind, isStatic):
387        self.type = Syntax.FieldDefinition
388        self.key = key
389        self.computed = computed
390        self.value = value
391        self.kind = kind
392        self.static = isStatic
393
394
395class Module(Node):
396    def __init__(self, body):
397        self.type = Syntax.Program
398        self.sourceType = 'module'
399        self.body = body
400
401
402class NewExpression(Node):
403    def __init__(self, callee, args):
404        self.type = Syntax.NewExpression
405        self.callee = callee
406        self.arguments = args
407
408
409class ObjectExpression(Node):
410    def __init__(self, properties):
411        self.type = Syntax.ObjectExpression
412        self.properties = properties
413
414
415class ObjectPattern(Node):
416    def __init__(self, properties):
417        self.type = Syntax.ObjectPattern
418        self.properties = properties
419
420
421class Property(Node):
422    def __init__(self, kind, key, computed, value, method, shorthand):
423        self.type = Syntax.Property
424        self.key = key
425        self.computed = computed
426        self.value = value
427        self.kind = kind
428        self.method = method
429        self.shorthand = shorthand
430
431
432class RegexLiteral(Node):
433    def __init__(self, value, raw, pattern, flags):
434        self.type = Syntax.Literal
435        self.value = value
436        self.raw = raw
437        self.regex = RegExp(
438            pattern=pattern,
439            flags=flags,
440        )
441
442
443class RestElement(Node):
444    def __init__(self, argument):
445        self.type = Syntax.RestElement
446        self.argument = argument
447
448
449class ReturnStatement(Node):
450    def __init__(self, argument):
451        self.type = Syntax.ReturnStatement
452        self.argument = argument
453
454
455class Script(Node):
456    def __init__(self, body):
457        self.type = Syntax.Program
458        self.sourceType = 'script'
459        self.body = body
460
461
462class SequenceExpression(Node):
463    def __init__(self, expressions):
464        self.type = Syntax.SequenceExpression
465        self.expressions = expressions
466
467
468class SpreadElement(Node):
469    def __init__(self, argument):
470        self.type = Syntax.SpreadElement
471        self.argument = argument
472
473
474class StaticMemberExpression(Node):
475    def __init__(self, object, property):
476        self.type = Syntax.MemberExpression
477        self.computed = False
478        self.object = object
479        self.property = property
480
481
482class Super(Node):
483    def __init__(self):
484        self.type = Syntax.Super
485
486
487class SwitchCase(Node):
488    def __init__(self, test, consequent):
489        self.type = Syntax.SwitchCase
490        self.test = test
491        self.consequent = consequent
492
493
494class SwitchStatement(Node):
495    def __init__(self, discriminant, cases):
496        self.type = Syntax.SwitchStatement
497        self.discriminant = discriminant
498        self.cases = cases
499
500
501class TaggedTemplateExpression(Node):
502    def __init__(self, tag, quasi):
503        self.type = Syntax.TaggedTemplateExpression
504        self.tag = tag
505        self.quasi = quasi
506
507
508class TemplateElement(Node):
509    class Value(Object):
510        def __init__(self, raw, cooked):
511            self.raw = raw
512            self.cooked = cooked
513
514    def __init__(self, raw, cooked, tail):
515        self.type = Syntax.TemplateElement
516        self.value = TemplateElement.Value(raw, cooked)
517        self.tail = tail
518
519
520class TemplateLiteral(Node):
521    def __init__(self, quasis, expressions):
522        self.type = Syntax.TemplateLiteral
523        self.quasis = quasis
524        self.expressions = expressions
525
526
527class ThisExpression(Node):
528    def __init__(self):
529        self.type = Syntax.ThisExpression
530
531
532class ThrowStatement(Node):
533    def __init__(self, argument):
534        self.type = Syntax.ThrowStatement
535        self.argument = argument
536
537
538class TryStatement(Node):
539    def __init__(self, block, handler, finalizer):
540        self.type = Syntax.TryStatement
541        self.block = block
542        self.handler = handler
543        self.finalizer = finalizer
544
545
546class UnaryExpression(Node):
547    def __init__(self, operator, argument):
548        self.type = Syntax.UnaryExpression
549        self.prefix = True
550        self.operator = operator
551        self.argument = argument
552
553
554class UpdateExpression(Node):
555    def __init__(self, operator, argument, prefix):
556        self.type = Syntax.UpdateExpression
557        self.operator = operator
558        self.argument = argument
559        self.prefix = prefix
560
561
562class VariableDeclaration(Node):
563    def __init__(self, declarations, kind):
564        self.type = Syntax.VariableDeclaration
565        self.declarations = declarations
566        self.kind = kind
567
568
569class VariableDeclarator(Node):
570    def __init__(self, id, init):
571        self.type = Syntax.VariableDeclarator
572        self.id = id
573        self.init = init
574
575
576class WhileStatement(Node):
577    def __init__(self, test, body):
578        self.type = Syntax.WhileStatement
579        self.test = test
580        self.body = body
581
582
583class WithStatement(Node):
584    def __init__(self, object, body):
585        self.type = Syntax.WithStatement
586        self.object = object
587        self.body = body
588
589
590class YieldExpression(Node):
591    def __init__(self, argument, delegate):
592        self.type = Syntax.YieldExpression
593        self.argument = argument
594        self.delegate = delegate
595
596
597class ArrowParameterPlaceHolder(Node):
598    def __init__(self, params):
599        self.type = Syntax.ArrowParameterPlaceHolder
600        self.params = params
601        self.isAsync = False
602
603
604class AsyncArrowParameterPlaceHolder(Node):
605    def __init__(self, params):
606        self.type = Syntax.ArrowParameterPlaceHolder
607        self.params = params
608        self.isAsync = True
609
610
611class BlockComment(Node):
612    def __init__(self, value):
613        self.type = Syntax.BlockComment
614        self.value = value
615
616
617class LineComment(Node):
618    def __init__(self, value):
619        self.type = Syntax.LineComment
620        self.value = value
621