1 from .pyjsparserdata import *
8 def finishArrayExpression(self, elements):
9 self.type = Syntax.ArrayExpression
10 self.elements = elements
14 def finishArrayPattern(self, elements):
15 self.type = Syntax.ArrayPattern
16 self.elements = elements
20 def finishArrowFunctionExpression(self, params, defaults, body, expression):
21 self.type = Syntax.ArrowFunctionExpression
24 self.defaults = defaults
26 self.generator = False
27 self.expression = expression
31 def finishAssignmentExpression(self, operator, left, right):
32 self.type = Syntax.AssignmentExpression
33 self.operator = operator
39 def finishAssignmentPattern(self, left, right):
40 self.type = Syntax.AssignmentPattern
46 def finishBinaryExpression(self, operator, left, right):
47 self.type = Syntax.LogicalExpression if (operator == '||' or operator == '&&') else Syntax.BinaryExpression
48 self.operator = operator
54 def finishBlockStatement(self, body):
55 self.type = Syntax.BlockStatement
60 def finishBreakStatement(self, label):
61 self.type = Syntax.BreakStatement
66 def finishCallExpression(self, callee, args):
67 self.type = Syntax.CallExpression
73 def finishCatchClause(self, param, body):
74 self.type = Syntax.CatchClause
80 def finishClassBody(self, body):
81 self.type = Syntax.ClassBody
86 def finishClassDeclaration(self, id, superClass, body):
87 self.type = Syntax.ClassDeclaration
89 self.superClass = superClass
94 def finishClassExpression(self, id, superClass, body):
95 self.type = Syntax.ClassExpression
97 self.superClass = superClass
102 def finishConditionalExpression(self, test, consequent, alternate):
103 self.type = Syntax.ConditionalExpression
105 self.consequent = consequent
106 self.alternate = alternate
110 def finishContinueStatement(self, label):
111 self.type = Syntax.ContinueStatement
116 def finishDebuggerStatement(self, ):
117 self.type = Syntax.DebuggerStatement
121 def finishDoWhileStatement(self, body, test):
122 self.type = Syntax.DoWhileStatement
128 def finishEmptyStatement(self, ):
129 self.type = Syntax.EmptyStatement
133 def finishExpressionStatement(self, expression):
134 self.type = Syntax.ExpressionStatement
135 self.expression = expression
139 def finishForStatement(self, init, test, update, body):
140 self.type = Syntax.ForStatement
148 def finishForInStatement(self, left, right, body):
149 self.type = Syntax.ForInStatement
157 def finishFunctionDeclaration(self, id, params, defaults, body):
158 self.type = Syntax.FunctionDeclaration
161 self.defaults = defaults
163 self.generator = False
164 self.expression = False
168 def finishFunctionExpression(self, id, params, defaults, body):
169 self.type = Syntax.FunctionExpression
172 self.defaults = defaults
174 self.generator = False
175 self.expression = False
179 def finishIdentifier(self, name):
180 self.type = Syntax.Identifier
185 def finishIfStatement(self, test, consequent, alternate):
186 self.type = Syntax.IfStatement
188 self.consequent = consequent
189 self.alternate = alternate
193 def finishLabeledStatement(self, label, body):
194 self.type = Syntax.LabeledStatement
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']
209 def finishMemberExpression(self, accessor, object, property):
210 self.type = Syntax.MemberExpression
211 self.computed = accessor == '['
213 self.property = property
217 def finishNewExpression(self, callee, args):
218 self.type = Syntax.NewExpression
220 self.arguments = args
224 def finishObjectExpression(self, properties):
225 self.type = Syntax.ObjectExpression
226 self.properties = properties
230 def finishObjectPattern(self, properties):
231 self.type = Syntax.ObjectPattern
232 self.properties = properties
236 def finishPostfixExpression(self, operator, argument):
237 self.type = Syntax.UpdateExpression
238 self.operator = operator
239 self.argument = argument
244 def finishProgram(self, body):
245 self.type = Syntax.Program
250 def finishPyimport(self, imp):
251 self.type = 'PyimportStatement'
256 def finishProperty(self, kind, key, computed, value, method, shorthand):
257 self.type = Syntax.Property
259 self.computed = computed
263 self.shorthand = shorthand
267 def finishRestElement(self, argument):
268 self.type = Syntax.RestElement
269 self.argument = argument
273 def finishReturnStatement(self, argument):
274 self.type = Syntax.ReturnStatement
275 self.argument = argument
279 def finishSequenceExpression(self, expressions):
280 self.type = Syntax.SequenceExpression
281 self.expressions = expressions
285 def finishSpreadElement(self, argument):
286 self.type = Syntax.SpreadElement
287 self.argument = argument
291 def finishSwitchCase(self, test, consequent):
292 self.type = Syntax.SwitchCase
294 self.consequent = consequent
298 def finishSuper(self, ):
299 self.type = Syntax.Super
303 def finishSwitchStatement(self, discriminant, cases):
304 self.type = Syntax.SwitchStatement
305 self.discriminant = discriminant
310 def finishTaggedTemplateExpression(self, tag, quasi):
311 self.type = Syntax.TaggedTemplateExpression
317 def finishTemplateElement(self, value, tail):
318 self.type = Syntax.TemplateElement
324 def finishTemplateLiteral(self, quasis, expressions):
325 self.type = Syntax.TemplateLiteral
327 self.expressions = expressions
331 def finishThisExpression(self, ):
332 self.type = Syntax.ThisExpression
336 def finishThrowStatement(self, argument):
337 self.type = Syntax.ThrowStatement
338 self.argument = argument
342 def finishTryStatement(self, block, handler, finalizer):
343 self.type = Syntax.TryStatement
345 self.guardedHandlers = []
346 self.handlers = [handler] if handler else []
347 self.handler = handler
348 self.finalizer = finalizer
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
360 def finishVariableDeclaration(self, declarations):
361 self.type = Syntax.VariableDeclaration
362 self.declarations = declarations
367 def finishLexicalDeclaration(self, declarations, kind):
368 self.type = Syntax.VariableDeclaration
369 self.declarations = declarations
374 def finishVariableDeclarator(self, id, init):
375 self.type = Syntax.VariableDeclarator
381 def finishWhileStatement(self, test, body):
382 self.type = Syntax.WhileStatement
388 def finishWithStatement(self, object, body):
389 self.type = Syntax.WithStatement
395 def finishExportSpecifier(self, local, exported):
396 self.type = Syntax.ExportSpecifier
397 self.exported = exported or local
402 def finishImportDefaultSpecifier(self, local):
403 self.type = Syntax.ImportDefaultSpecifier
408 def finishImportNamespaceSpecifier(self, local):
409 self.type = Syntax.ImportNamespaceSpecifier
414 def finishExportNamedDeclaration(self, declaration, specifiers, src):
415 self.type = Syntax.ExportNamedDeclaration
416 self.declaration = declaration
417 self.specifiers = specifiers
422 def finishExportDefaultDeclaration(self, declaration):
423 self.type = Syntax.ExportDefaultDeclaration
424 self.declaration = declaration
428 def finishExportAllDeclaration(self, src):
429 self.type = Syntax.ExportAllDeclaration
434 def finishImportSpecifier(self, local, imported):
435 self.type = Syntax.ImportSpecifier
436 self.local = local or imported
437 self.imported = imported
441 def finishImportDeclaration(self, specifiers, src):
442 self.type = Syntax.ImportDeclaration
443 self.specifiers = specifiers
448 def __getitem__(self, item):
449 return getattr(self, item)
451 def __setitem__(self, key, value):
452 setattr(self, key, value)
455 class Node(BaseNode):
459 class WrappingNode(BaseNode):
460 def __init__(self, startToken=None):
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):
471 return dict((k, node_to_dict(v)) for k, v in node.__dict__.items())