OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 part of js_ast; | |
6 | |
7 abstract class NodeVisitor<T> { | |
8 T visitProgram(Program node); | |
9 | |
10 T visitBlock(Block node); | |
11 T visitExpressionStatement(ExpressionStatement node); | |
12 T visitEmptyStatement(EmptyStatement node); | |
13 T visitIf(If node); | |
14 T visitFor(For node); | |
15 T visitForIn(ForIn node); | |
16 T visitWhile(While node); | |
17 T visitDo(Do node); | |
18 T visitContinue(Continue node); | |
19 T visitBreak(Break node); | |
20 T visitReturn(Return node); | |
21 T visitThrow(Throw node); | |
22 T visitTry(Try node); | |
23 T visitCatch(Catch node); | |
24 T visitSwitch(Switch node); | |
25 T visitCase(Case node); | |
26 T visitDefault(Default node); | |
27 T visitFunctionDeclaration(FunctionDeclaration node); | |
28 T visitLabeledStatement(LabeledStatement node); | |
29 T visitLiteralStatement(LiteralStatement node); | |
30 T visitDartYield(DartYield node); | |
31 | |
32 T visitLiteralExpression(LiteralExpression node); | |
33 T visitVariableDeclarationList(VariableDeclarationList node); | |
34 T visitAssignment(Assignment node); | |
35 T visitVariableInitialization(VariableInitialization node); | |
36 T visitConditional(Conditional cond); | |
37 T visitNew(New node); | |
38 T visitCall(Call node); | |
39 T visitBinary(Binary node); | |
40 T visitPrefix(Prefix node); | |
41 T visitPostfix(Postfix node); | |
42 | |
43 T visitVariableUse(VariableUse node); | |
44 T visitThis(This node); | |
45 T visitVariableDeclaration(VariableDeclaration node); | |
46 T visitParameter(Parameter node); | |
47 T visitAccess(PropertyAccess node); | |
48 | |
49 T visitNamedFunction(NamedFunction node); | |
50 T visitFun(Fun node); | |
51 | |
52 T visitLiteralBool(LiteralBool node); | |
53 T visitLiteralString(LiteralString node); | |
54 T visitLiteralNumber(LiteralNumber node); | |
55 T visitLiteralNull(LiteralNull node); | |
56 | |
57 T visitArrayInitializer(ArrayInitializer node); | |
58 T visitArrayHole(ArrayHole node); | |
59 T visitObjectInitializer(ObjectInitializer node); | |
60 T visitProperty(Property node); | |
61 T visitRegExpLiteral(RegExpLiteral node); | |
62 | |
63 T visitAwait(Await node); | |
64 | |
65 T visitComment(Comment node); | |
66 | |
67 T visitInterpolatedExpression(InterpolatedExpression node); | |
68 T visitInterpolatedLiteral(InterpolatedLiteral node); | |
69 T visitInterpolatedParameter(InterpolatedParameter node); | |
70 T visitInterpolatedSelector(InterpolatedSelector node); | |
71 T visitInterpolatedStatement(InterpolatedStatement node); | |
72 } | |
73 | |
74 class BaseVisitor<T> implements NodeVisitor<T> { | |
75 T visitNode(Node node) { | |
76 node.visitChildren(this); | |
77 return null; | |
78 } | |
79 | |
80 T visitProgram(Program node) => visitNode(node); | |
81 | |
82 T visitStatement(Statement node) => visitNode(node); | |
83 T visitLoop(Loop node) => visitStatement(node); | |
84 T visitJump(Statement node) => visitStatement(node); | |
85 | |
86 T visitBlock(Block node) => visitStatement(node); | |
87 T visitExpressionStatement(ExpressionStatement node) | |
88 => visitStatement(node); | |
89 T visitEmptyStatement(EmptyStatement node) => visitStatement(node); | |
90 T visitIf(If node) => visitStatement(node); | |
91 T visitFor(For node) => visitLoop(node); | |
92 T visitForIn(ForIn node) => visitLoop(node); | |
93 T visitWhile(While node) => visitLoop(node); | |
94 T visitDo(Do node) => visitLoop(node); | |
95 T visitContinue(Continue node) => visitJump(node); | |
96 T visitBreak(Break node) => visitJump(node); | |
97 T visitReturn(Return node) => visitJump(node); | |
98 T visitThrow(Throw node) => visitJump(node); | |
99 T visitTry(Try node) => visitStatement(node); | |
100 T visitSwitch(Switch node) => visitStatement(node); | |
101 T visitFunctionDeclaration(FunctionDeclaration node) | |
102 => visitStatement(node); | |
103 T visitLabeledStatement(LabeledStatement node) => visitStatement(node); | |
104 T visitLiteralStatement(LiteralStatement node) => visitStatement(node); | |
105 | |
106 T visitCatch(Catch node) => visitNode(node); | |
107 T visitCase(Case node) => visitNode(node); | |
108 T visitDefault(Default node) => visitNode(node); | |
109 | |
110 T visitExpression(Expression node) => visitNode(node); | |
111 T visitVariableReference(VariableReference node) => visitExpression(node); | |
112 | |
113 T visitLiteralExpression(LiteralExpression node) => visitExpression(node); | |
114 T visitVariableDeclarationList(VariableDeclarationList node) | |
115 => visitExpression(node); | |
116 T visitAssignment(Assignment node) => visitExpression(node); | |
117 T visitVariableInitialization(VariableInitialization node) { | |
118 if (node.value != null) { | |
119 return visitAssignment(node); | |
120 } else { | |
121 return visitExpression(node); | |
122 } | |
123 } | |
124 T visitConditional(Conditional node) => visitExpression(node); | |
125 T visitNew(New node) => visitExpression(node); | |
126 T visitCall(Call node) => visitExpression(node); | |
127 T visitBinary(Binary node) => visitExpression(node); | |
128 T visitPrefix(Prefix node) => visitExpression(node); | |
129 T visitPostfix(Postfix node) => visitExpression(node); | |
130 T visitAccess(PropertyAccess node) => visitExpression(node); | |
131 | |
132 T visitVariableUse(VariableUse node) => visitVariableReference(node); | |
133 T visitVariableDeclaration(VariableDeclaration node) | |
134 => visitVariableReference(node); | |
135 T visitParameter(Parameter node) => visitVariableDeclaration(node); | |
136 T visitThis(This node) => visitParameter(node); | |
137 | |
138 T visitNamedFunction(NamedFunction node) => visitExpression(node); | |
139 T visitFun(Fun node) => visitExpression(node); | |
140 | |
141 T visitLiteral(Literal node) => visitExpression(node); | |
142 | |
143 T visitLiteralBool(LiteralBool node) => visitLiteral(node); | |
144 T visitLiteralString(LiteralString node) => visitLiteral(node); | |
145 T visitLiteralNumber(LiteralNumber node) => visitLiteral(node); | |
146 T visitLiteralNull(LiteralNull node) => visitLiteral(node); | |
147 | |
148 T visitArrayInitializer(ArrayInitializer node) => visitExpression(node); | |
149 T visitArrayHole(ArrayHole node) => visitExpression(node); | |
150 T visitObjectInitializer(ObjectInitializer node) => visitExpression(node); | |
151 T visitProperty(Property node) => visitNode(node); | |
152 T visitRegExpLiteral(RegExpLiteral node) => visitExpression(node); | |
153 | |
154 T visitInterpolatedNode(InterpolatedNode node) => visitNode(node); | |
155 | |
156 T visitInterpolatedExpression(InterpolatedExpression node) | |
157 => visitInterpolatedNode(node); | |
158 T visitInterpolatedLiteral(InterpolatedLiteral node) | |
159 => visitInterpolatedNode(node); | |
160 T visitInterpolatedParameter(InterpolatedParameter node) | |
161 => visitInterpolatedNode(node); | |
162 T visitInterpolatedSelector(InterpolatedSelector node) | |
163 => visitInterpolatedNode(node); | |
164 T visitInterpolatedStatement(InterpolatedStatement node) | |
165 => visitInterpolatedNode(node); | |
166 | |
167 // Ignore comments by default. | |
168 T visitComment(Comment node) => null; | |
169 | |
170 T visitAwait(Await node) => visitExpression(node); | |
171 T visitDartYield(DartYield node) => visitStatement(node); | |
172 } | |
173 | |
174 /// This tag interface has no behaviour but must be implemented by any class | |
175 /// that is to be stored on a [Node] as source information. | |
176 abstract class JavaScriptNodeSourceInformation {} | |
177 | |
178 abstract class Node { | |
179 JavaScriptNodeSourceInformation get sourceInformation => _sourceInformation; | |
180 | |
181 JavaScriptNodeSourceInformation _sourceInformation; | |
182 | |
183 accept(NodeVisitor visitor); | |
184 void visitChildren(NodeVisitor visitor); | |
185 | |
186 // Shallow clone of node. Does not clone positions since the only use of this | |
187 // private method is create a copy with a new position. | |
188 Node _clone(); | |
189 | |
190 // Returns a node equivalent to [this], but with new source position and end | |
191 // source position. | |
192 Node withSourceInformation( | |
193 JavaScriptNodeSourceInformation sourceInformation) { | |
194 if (sourceInformation == _sourceInformation) { | |
195 return this; | |
196 } | |
197 Node clone = _clone(); | |
198 // TODO(sra): Should existing data be 'sticky' if we try to overwrite with | |
199 // `null`? | |
200 clone._sourceInformation = sourceInformation; | |
201 return clone; | |
202 } | |
203 | |
204 VariableUse asVariableUse() => null; | |
205 | |
206 bool get isCommaOperator => false; | |
207 | |
208 Statement toStatement() { | |
209 throw new UnsupportedError('toStatement'); | |
210 } | |
211 } | |
212 | |
213 class Program extends Node { | |
214 final List<Statement> body; | |
215 Program(this.body); | |
216 | |
217 accept(NodeVisitor visitor) => visitor.visitProgram(this); | |
218 void visitChildren(NodeVisitor visitor) { | |
219 for (Statement statement in body) statement.accept(visitor); | |
220 } | |
221 Program _clone() => new Program(body); | |
222 } | |
223 | |
224 abstract class Statement extends Node { | |
225 Statement toStatement() => this; | |
226 } | |
227 | |
228 class Block extends Statement { | |
229 final List<Statement> statements; | |
230 Block(this.statements); | |
231 Block.empty() : this.statements = <Statement>[]; | |
232 | |
233 accept(NodeVisitor visitor) => visitor.visitBlock(this); | |
234 void visitChildren(NodeVisitor visitor) { | |
235 for (Statement statement in statements) statement.accept(visitor); | |
236 } | |
237 Block _clone() => new Block(statements); | |
238 } | |
239 | |
240 class ExpressionStatement extends Statement { | |
241 final Expression expression; | |
242 ExpressionStatement(this.expression); | |
243 | |
244 accept(NodeVisitor visitor) => visitor.visitExpressionStatement(this); | |
245 void visitChildren(NodeVisitor visitor) { expression.accept(visitor); } | |
246 ExpressionStatement _clone() => new ExpressionStatement(expression); | |
247 } | |
248 | |
249 class EmptyStatement extends Statement { | |
250 EmptyStatement(); | |
251 | |
252 accept(NodeVisitor visitor) => visitor.visitEmptyStatement(this); | |
253 void visitChildren(NodeVisitor visitor) {} | |
254 EmptyStatement _clone() => new EmptyStatement(); | |
255 } | |
256 | |
257 class If extends Statement { | |
258 final Expression condition; | |
259 final Node then; | |
260 final Node otherwise; | |
261 | |
262 If(this.condition, this.then, this.otherwise); | |
263 If.noElse(this.condition, this.then) : this.otherwise = new EmptyStatement(); | |
264 | |
265 bool get hasElse => otherwise is !EmptyStatement; | |
266 | |
267 accept(NodeVisitor visitor) => visitor.visitIf(this); | |
268 | |
269 void visitChildren(NodeVisitor visitor) { | |
270 condition.accept(visitor); | |
271 then.accept(visitor); | |
272 otherwise.accept(visitor); | |
273 } | |
274 | |
275 If _clone() => new If(condition, then, otherwise); | |
276 } | |
277 | |
278 abstract class Loop extends Statement { | |
279 final Statement body; | |
280 Loop(this.body); | |
281 } | |
282 | |
283 class For extends Loop { | |
284 final Expression init; | |
285 final Expression condition; | |
286 final Expression update; | |
287 | |
288 For(this.init, this.condition, this.update, Statement body) : super(body); | |
289 | |
290 accept(NodeVisitor visitor) => visitor.visitFor(this); | |
291 | |
292 void visitChildren(NodeVisitor visitor) { | |
293 if (init != null) init.accept(visitor); | |
294 if (condition != null) condition.accept(visitor); | |
295 if (update != null) update.accept(visitor); | |
296 body.accept(visitor); | |
297 } | |
298 | |
299 For _clone() => new For(init, condition, update, body); | |
300 } | |
301 | |
302 class ForIn extends Loop { | |
303 // Note that [VariableDeclarationList] is a subclass of [Expression]. | |
304 // Therefore we can type the leftHandSide as [Expression]. | |
305 final Expression leftHandSide; | |
306 final Expression object; | |
307 | |
308 ForIn(this.leftHandSide, this.object, Statement body) : super(body); | |
309 | |
310 accept(NodeVisitor visitor) => visitor.visitForIn(this); | |
311 | |
312 void visitChildren(NodeVisitor visitor) { | |
313 leftHandSide.accept(visitor); | |
314 object.accept(visitor); | |
315 body.accept(visitor); | |
316 } | |
317 | |
318 ForIn _clone() => new ForIn(leftHandSide, object, body); | |
319 } | |
320 | |
321 class While extends Loop { | |
322 final Node condition; | |
323 | |
324 While(this.condition, Statement body) : super(body); | |
325 | |
326 accept(NodeVisitor visitor) => visitor.visitWhile(this); | |
327 | |
328 void visitChildren(NodeVisitor visitor) { | |
329 condition.accept(visitor); | |
330 body.accept(visitor); | |
331 } | |
332 | |
333 While _clone() => new While(condition, body); | |
334 } | |
335 | |
336 class Do extends Loop { | |
337 final Expression condition; | |
338 | |
339 Do(Statement body, this.condition) : super(body); | |
340 | |
341 accept(NodeVisitor visitor) => visitor.visitDo(this); | |
342 | |
343 void visitChildren(NodeVisitor visitor) { | |
344 body.accept(visitor); | |
345 condition.accept(visitor); | |
346 } | |
347 | |
348 Do _clone() => new Do(body, condition); | |
349 } | |
350 | |
351 class Continue extends Statement { | |
352 final String targetLabel; // Can be null. | |
353 | |
354 Continue(this.targetLabel); | |
355 | |
356 accept(NodeVisitor visitor) => visitor.visitContinue(this); | |
357 void visitChildren(NodeVisitor visitor) {} | |
358 | |
359 Continue _clone() => new Continue(targetLabel); | |
360 } | |
361 | |
362 class Break extends Statement { | |
363 final String targetLabel; // Can be null. | |
364 | |
365 Break(this.targetLabel); | |
366 | |
367 accept(NodeVisitor visitor) => visitor.visitBreak(this); | |
368 void visitChildren(NodeVisitor visitor) {} | |
369 | |
370 Break _clone() => new Break(targetLabel); | |
371 } | |
372 | |
373 class Return extends Statement { | |
374 final Expression value; // Can be null. | |
375 | |
376 Return([this.value = null]); | |
377 | |
378 accept(NodeVisitor visitor) => visitor.visitReturn(this); | |
379 | |
380 void visitChildren(NodeVisitor visitor) { | |
381 if (value != null) value.accept(visitor); | |
382 } | |
383 | |
384 Return _clone() => new Return(value); | |
385 } | |
386 | |
387 class Throw extends Statement { | |
388 final Expression expression; | |
389 | |
390 Throw(this.expression); | |
391 | |
392 accept(NodeVisitor visitor) => visitor.visitThrow(this); | |
393 | |
394 void visitChildren(NodeVisitor visitor) { | |
395 expression.accept(visitor); | |
396 } | |
397 | |
398 Throw _clone() => new Throw(expression); | |
399 } | |
400 | |
401 class Try extends Statement { | |
402 final Block body; | |
403 final Catch catchPart; // Can be null if [finallyPart] is non-null. | |
404 final Block finallyPart; // Can be null if [catchPart] is non-null. | |
405 | |
406 Try(this.body, this.catchPart, this.finallyPart) { | |
407 assert(catchPart != null || finallyPart != null); | |
408 } | |
409 | |
410 accept(NodeVisitor visitor) => visitor.visitTry(this); | |
411 | |
412 void visitChildren(NodeVisitor visitor) { | |
413 body.accept(visitor); | |
414 if (catchPart != null) catchPart.accept(visitor); | |
415 if (finallyPart != null) finallyPart.accept(visitor); | |
416 } | |
417 | |
418 Try _clone() => new Try(body, catchPart, finallyPart); | |
419 } | |
420 | |
421 class Catch extends Node { | |
422 final VariableDeclaration declaration; | |
423 final Block body; | |
424 | |
425 Catch(this.declaration, this.body); | |
426 | |
427 accept(NodeVisitor visitor) => visitor.visitCatch(this); | |
428 | |
429 void visitChildren(NodeVisitor visitor) { | |
430 declaration.accept(visitor); | |
431 body.accept(visitor); | |
432 } | |
433 | |
434 Catch _clone() => new Catch(declaration, body); | |
435 } | |
436 | |
437 class Switch extends Statement { | |
438 final Expression key; | |
439 final List<SwitchClause> cases; | |
440 | |
441 Switch(this.key, this.cases); | |
442 | |
443 accept(NodeVisitor visitor) => visitor.visitSwitch(this); | |
444 | |
445 void visitChildren(NodeVisitor visitor) { | |
446 key.accept(visitor); | |
447 for (SwitchClause clause in cases) clause.accept(visitor); | |
448 } | |
449 | |
450 Switch _clone() => new Switch(key, cases); | |
451 } | |
452 | |
453 abstract class SwitchClause extends Node { | |
454 final Block body; | |
455 | |
456 SwitchClause(this.body); | |
457 } | |
458 | |
459 class Case extends SwitchClause { | |
460 final Expression expression; | |
461 | |
462 Case(this.expression, Block body) : super(body); | |
463 | |
464 accept(NodeVisitor visitor) => visitor.visitCase(this); | |
465 | |
466 void visitChildren(NodeVisitor visitor) { | |
467 expression.accept(visitor); | |
468 body.accept(visitor); | |
469 } | |
470 | |
471 Case _clone() => new Case(expression, body); | |
472 } | |
473 | |
474 class Default extends SwitchClause { | |
475 Default(Block body) : super(body); | |
476 | |
477 accept(NodeVisitor visitor) => visitor.visitDefault(this); | |
478 | |
479 void visitChildren(NodeVisitor visitor) { | |
480 body.accept(visitor); | |
481 } | |
482 | |
483 Default _clone() => new Default(body); | |
484 } | |
485 | |
486 class FunctionDeclaration extends Statement { | |
487 final VariableDeclaration name; | |
488 final Fun function; | |
489 | |
490 FunctionDeclaration(this.name, this.function); | |
491 | |
492 accept(NodeVisitor visitor) => visitor.visitFunctionDeclaration(this); | |
493 | |
494 void visitChildren(NodeVisitor visitor) { | |
495 name.accept(visitor); | |
496 function.accept(visitor); | |
497 } | |
498 | |
499 FunctionDeclaration _clone() => new FunctionDeclaration(name, function); | |
500 } | |
501 | |
502 class LabeledStatement extends Statement { | |
503 final String label; | |
504 final Statement body; | |
505 | |
506 LabeledStatement(this.label, this.body); | |
507 | |
508 accept(NodeVisitor visitor) => visitor.visitLabeledStatement(this); | |
509 | |
510 void visitChildren(NodeVisitor visitor) { | |
511 body.accept(visitor); | |
512 } | |
513 | |
514 LabeledStatement _clone() => new LabeledStatement(label, body); | |
515 } | |
516 | |
517 class LiteralStatement extends Statement { | |
518 final String code; | |
519 | |
520 LiteralStatement(this.code); | |
521 | |
522 accept(NodeVisitor visitor) => visitor.visitLiteralStatement(this); | |
523 void visitChildren(NodeVisitor visitor) { } | |
524 | |
525 LiteralStatement _clone() => new LiteralStatement(code); | |
526 } | |
527 | |
528 // Not a real JavaScript node, but represents the yield statement from a dart | |
529 // program translated to JavaScript. | |
530 class DartYield extends Statement { | |
531 final Expression expression; | |
532 | |
533 final bool hasStar; | |
534 | |
535 DartYield(this.expression, this.hasStar); | |
536 | |
537 accept(NodeVisitor visitor) => visitor.visitDartYield(this); | |
538 | |
539 void visitChildren(NodeVisitor visitor) { | |
540 expression.accept(visitor); | |
541 } | |
542 | |
543 DartYield _clone() => new DartYield(expression, hasStar); | |
544 } | |
545 | |
546 abstract class Expression extends Node { | |
547 int get precedenceLevel; | |
548 | |
549 Statement toStatement() => new ExpressionStatement(this); | |
550 } | |
551 | |
552 class LiteralExpression extends Expression { | |
553 final String template; | |
554 final List<Expression> inputs; | |
555 | |
556 LiteralExpression(this.template) : inputs = const []; | |
557 LiteralExpression.withData(this.template, this.inputs); | |
558 | |
559 accept(NodeVisitor visitor) => visitor.visitLiteralExpression(this); | |
560 | |
561 void visitChildren(NodeVisitor visitor) { | |
562 if (inputs != null) { | |
563 for (Expression expr in inputs) expr.accept(visitor); | |
564 } | |
565 } | |
566 | |
567 LiteralExpression _clone() => | |
568 new LiteralExpression.withData(template, inputs); | |
569 | |
570 // Code that uses JS must take care of operator precedences, and | |
571 // put parenthesis if needed. | |
572 int get precedenceLevel => PRIMARY; | |
573 } | |
574 | |
575 /** | |
576 * [VariableDeclarationList] is a subclass of [Expression] to simplify the | |
577 * AST. | |
578 */ | |
579 class VariableDeclarationList extends Expression { | |
580 final List<VariableInitialization> declarations; | |
581 | |
582 VariableDeclarationList(this.declarations); | |
583 | |
584 accept(NodeVisitor visitor) => visitor.visitVariableDeclarationList(this); | |
585 | |
586 void visitChildren(NodeVisitor visitor) { | |
587 for (VariableInitialization declaration in declarations) { | |
588 declaration.accept(visitor); | |
589 } | |
590 } | |
591 | |
592 VariableDeclarationList _clone() => new VariableDeclarationList(declarations); | |
593 | |
594 int get precedenceLevel => EXPRESSION; | |
595 } | |
596 | |
597 class Assignment extends Expression { | |
598 final Expression leftHandSide; | |
599 final String op; // Null, if the assignment is not compound. | |
600 final Expression value; // May be null, for [VariableInitialization]s. | |
601 | |
602 Assignment(leftHandSide, value) | |
603 : this.compound(leftHandSide, null, value); | |
604 Assignment.compound(this.leftHandSide, this.op, this.value); | |
605 | |
606 int get precedenceLevel => ASSIGNMENT; | |
607 | |
608 bool get isCompound => op != null; | |
609 | |
610 accept(NodeVisitor visitor) => visitor.visitAssignment(this); | |
611 | |
612 void visitChildren(NodeVisitor visitor) { | |
613 leftHandSide.accept(visitor); | |
614 if (value != null) value.accept(visitor); | |
615 } | |
616 | |
617 Assignment _clone() => | |
618 new Assignment.compound(leftHandSide, op, value); | |
619 } | |
620 | |
621 class VariableInitialization extends Assignment { | |
622 /** [value] may be null. */ | |
623 VariableInitialization(VariableDeclaration declaration, Expression value) | |
624 : super(declaration, value); | |
625 | |
626 VariableDeclaration get declaration => leftHandSide; | |
627 | |
628 accept(NodeVisitor visitor) => visitor.visitVariableInitialization(this); | |
629 | |
630 VariableInitialization _clone() => | |
631 new VariableInitialization(declaration, value); | |
632 } | |
633 | |
634 class Conditional extends Expression { | |
635 final Expression condition; | |
636 final Expression then; | |
637 final Expression otherwise; | |
638 | |
639 Conditional(this.condition, this.then, this.otherwise); | |
640 | |
641 accept(NodeVisitor visitor) => visitor.visitConditional(this); | |
642 | |
643 void visitChildren(NodeVisitor visitor) { | |
644 condition.accept(visitor); | |
645 then.accept(visitor); | |
646 otherwise.accept(visitor); | |
647 } | |
648 | |
649 Conditional _clone() => new Conditional(condition, then, otherwise); | |
650 | |
651 int get precedenceLevel => ASSIGNMENT; | |
652 } | |
653 | |
654 class Call extends Expression { | |
655 Expression target; | |
656 List<Expression> arguments; | |
657 | |
658 Call(this.target, this.arguments); | |
659 | |
660 accept(NodeVisitor visitor) => visitor.visitCall(this); | |
661 | |
662 void visitChildren(NodeVisitor visitor) { | |
663 target.accept(visitor); | |
664 for (Expression arg in arguments) arg.accept(visitor); | |
665 } | |
666 | |
667 Call _clone() => new Call(target, arguments); | |
668 | |
669 int get precedenceLevel => CALL; | |
670 } | |
671 | |
672 class New extends Call { | |
673 New(Expression cls, List<Expression> arguments) : super(cls, arguments); | |
674 | |
675 accept(NodeVisitor visitor) => visitor.visitNew(this); | |
676 | |
677 New _clone() => new New(target, arguments); | |
678 } | |
679 | |
680 class Binary extends Expression { | |
681 final String op; | |
682 final Expression left; | |
683 final Expression right; | |
684 | |
685 Binary(this.op, this.left, this.right); | |
686 | |
687 accept(NodeVisitor visitor) => visitor.visitBinary(this); | |
688 | |
689 Binary _clone() => new Binary(op, left, right); | |
690 | |
691 void visitChildren(NodeVisitor visitor) { | |
692 left.accept(visitor); | |
693 right.accept(visitor); | |
694 } | |
695 | |
696 bool get isCommaOperator => op == ','; | |
697 | |
698 int get precedenceLevel { | |
699 // TODO(floitsch): switch to constant map. | |
700 switch (op) { | |
701 case "*": | |
702 case "/": | |
703 case "%": | |
704 return MULTIPLICATIVE; | |
705 case "+": | |
706 case "-": | |
707 return ADDITIVE; | |
708 case "<<": | |
709 case ">>": | |
710 case ">>>": | |
711 return SHIFT; | |
712 case "<": | |
713 case ">": | |
714 case "<=": | |
715 case ">=": | |
716 case "instanceof": | |
717 case "in": | |
718 return RELATIONAL; | |
719 case "==": | |
720 case "===": | |
721 case "!=": | |
722 case "!==": | |
723 return EQUALITY; | |
724 case "&": | |
725 return BIT_AND; | |
726 case "^": | |
727 return BIT_XOR; | |
728 case "|": | |
729 return BIT_OR; | |
730 case "&&": | |
731 return LOGICAL_AND; | |
732 case "||": | |
733 return LOGICAL_OR; | |
734 case ',': | |
735 return EXPRESSION; | |
736 default: | |
737 throw "Internal Error: Unhandled binary operator: $op"; | |
738 } | |
739 } | |
740 } | |
741 | |
742 class Prefix extends Expression { | |
743 final String op; | |
744 final Expression argument; | |
745 | |
746 Prefix(this.op, this.argument); | |
747 | |
748 accept(NodeVisitor visitor) => visitor.visitPrefix(this); | |
749 | |
750 Prefix _clone() => new Prefix(op, argument); | |
751 | |
752 void visitChildren(NodeVisitor visitor) { | |
753 argument.accept(visitor); | |
754 } | |
755 | |
756 int get precedenceLevel => UNARY; | |
757 } | |
758 | |
759 class Postfix extends Expression { | |
760 final String op; | |
761 final Expression argument; | |
762 | |
763 Postfix(this.op, this.argument); | |
764 | |
765 accept(NodeVisitor visitor) => visitor.visitPostfix(this); | |
766 | |
767 Postfix _clone() => new Postfix(op, argument); | |
768 | |
769 void visitChildren(NodeVisitor visitor) { | |
770 argument.accept(visitor); | |
771 } | |
772 | |
773 | |
774 int get precedenceLevel => UNARY; | |
775 } | |
776 | |
777 abstract class VariableReference extends Expression { | |
778 final String name; | |
779 | |
780 VariableReference(this.name) { | |
781 assert(_identifierRE.hasMatch(name)); | |
782 } | |
783 static RegExp _identifierRE = new RegExp(r'^[A-Za-z_$][A-Za-z_$0-9]*$'); | |
784 | |
785 accept(NodeVisitor visitor); | |
786 int get precedenceLevel => PRIMARY; | |
787 void visitChildren(NodeVisitor visitor) {} | |
788 } | |
789 | |
790 class VariableUse extends VariableReference { | |
791 VariableUse(String name) : super(name); | |
792 | |
793 accept(NodeVisitor visitor) => visitor.visitVariableUse(this); | |
794 VariableUse _clone() => new VariableUse(name); | |
795 | |
796 VariableUse asVariableUse() => this; | |
797 | |
798 toString() => 'VariableUse($name)'; | |
799 } | |
800 | |
801 class VariableDeclaration extends VariableReference { | |
802 final bool allowRename; | |
803 VariableDeclaration(String name, {this.allowRename: true}) : super(name); | |
804 | |
805 accept(NodeVisitor visitor) => visitor.visitVariableDeclaration(this); | |
806 VariableDeclaration _clone() => new VariableDeclaration(name); | |
807 } | |
808 | |
809 class Parameter extends VariableDeclaration { | |
810 Parameter(String name) : super(name); | |
811 | |
812 accept(NodeVisitor visitor) => visitor.visitParameter(this); | |
813 Parameter _clone() => new Parameter(name); | |
814 } | |
815 | |
816 class This extends Parameter { | |
817 This() : super("this"); | |
818 | |
819 accept(NodeVisitor visitor) => visitor.visitThis(this); | |
820 This _clone() => new This(); | |
821 } | |
822 | |
823 class NamedFunction extends Expression { | |
824 final VariableDeclaration name; | |
825 final Fun function; | |
826 | |
827 NamedFunction(this.name, this.function); | |
828 | |
829 accept(NodeVisitor visitor) => visitor.visitNamedFunction(this); | |
830 | |
831 void visitChildren(NodeVisitor visitor) { | |
832 name.accept(visitor); | |
833 function.accept(visitor); | |
834 } | |
835 NamedFunction _clone() => new NamedFunction(name, function); | |
836 | |
837 int get precedenceLevel => CALL; | |
838 } | |
839 | |
840 class Fun extends Expression { | |
841 final List<Parameter> params; | |
842 final Block body; | |
843 final AsyncModifier asyncModifier; | |
844 | |
845 Fun(this.params, this.body, {this.asyncModifier: const AsyncModifier.sync()}); | |
846 | |
847 accept(NodeVisitor visitor) => visitor.visitFun(this); | |
848 | |
849 void visitChildren(NodeVisitor visitor) { | |
850 for (Parameter param in params) param.accept(visitor); | |
851 body.accept(visitor); | |
852 } | |
853 | |
854 Fun _clone() => new Fun(params, body, asyncModifier: asyncModifier); | |
855 | |
856 int get precedenceLevel => CALL; | |
857 } | |
858 | |
859 class AsyncModifier { | |
860 final bool isAsync; | |
861 final bool isYielding; | |
862 final String description; | |
863 | |
864 const AsyncModifier.sync() | |
865 : isAsync = false, | |
866 isYielding = false, | |
867 description = "sync"; | |
868 const AsyncModifier.async() | |
869 : isAsync = true, | |
870 isYielding = false, | |
871 description = "async"; | |
872 const AsyncModifier.asyncStar() | |
873 : isAsync = true, | |
874 isYielding = true, | |
875 description = "async*"; | |
876 const AsyncModifier.syncStar() | |
877 : isAsync = false, | |
878 isYielding = true, | |
879 description = "sync*"; | |
880 toString() => description; | |
881 } | |
882 | |
883 class PropertyAccess extends Expression { | |
884 final Expression receiver; | |
885 final Expression selector; | |
886 | |
887 PropertyAccess(this.receiver, this.selector); | |
888 PropertyAccess.field(this.receiver, String fieldName) | |
889 : selector = new LiteralString('"$fieldName"'); | |
890 PropertyAccess.indexed(this.receiver, int index) | |
891 : selector = new LiteralNumber('$index'); | |
892 | |
893 accept(NodeVisitor visitor) => visitor.visitAccess(this); | |
894 | |
895 void visitChildren(NodeVisitor visitor) { | |
896 receiver.accept(visitor); | |
897 selector.accept(visitor); | |
898 } | |
899 | |
900 PropertyAccess _clone() => new PropertyAccess(receiver, selector); | |
901 | |
902 int get precedenceLevel => CALL; | |
903 } | |
904 | |
905 abstract class Literal extends Expression { | |
906 void visitChildren(NodeVisitor visitor) {} | |
907 | |
908 int get precedenceLevel => PRIMARY; | |
909 } | |
910 | |
911 class LiteralBool extends Literal { | |
912 final bool value; | |
913 | |
914 LiteralBool(this.value); | |
915 | |
916 accept(NodeVisitor visitor) => visitor.visitLiteralBool(this); | |
917 // [visitChildren] inherited from [Literal]. | |
918 LiteralBool _clone() => new LiteralBool(value); | |
919 } | |
920 | |
921 class LiteralNull extends Literal { | |
922 LiteralNull(); | |
923 | |
924 accept(NodeVisitor visitor) => visitor.visitLiteralNull(this); | |
925 LiteralNull _clone() => new LiteralNull(); | |
926 } | |
927 | |
928 class LiteralString extends Literal { | |
929 final String value; | |
930 | |
931 /** | |
932 * Constructs a LiteralString from a string value. | |
933 * | |
934 * The constructor does not add the required quotes. If [value] is not | |
935 * surrounded by quotes and property escaped, the resulting object is invalid | |
936 * as a JS value. | |
937 * | |
938 * TODO(sra): Introduce variants for known valid strings that don't allocate a | |
939 * new string just to add quotes. | |
940 */ | |
941 LiteralString(this.value); | |
942 | |
943 accept(NodeVisitor visitor) => visitor.visitLiteralString(this); | |
944 LiteralString _clone() => new LiteralString(value); | |
945 } | |
946 | |
947 class LiteralNumber extends Literal { | |
948 final String value; // Must be a valid JavaScript number literal. | |
949 | |
950 LiteralNumber(this.value); | |
951 | |
952 accept(NodeVisitor visitor) => visitor.visitLiteralNumber(this); | |
953 LiteralNumber _clone() => new LiteralNumber(value); | |
954 } | |
955 | |
956 class ArrayInitializer extends Expression { | |
957 final List<Expression> elements; | |
958 | |
959 ArrayInitializer(this.elements); | |
960 | |
961 accept(NodeVisitor visitor) => visitor.visitArrayInitializer(this); | |
962 | |
963 void visitChildren(NodeVisitor visitor) { | |
964 for (Expression element in elements) element.accept(visitor); | |
965 } | |
966 | |
967 ArrayInitializer _clone() => new ArrayInitializer(elements); | |
968 | |
969 int get precedenceLevel => PRIMARY; | |
970 } | |
971 | |
972 /** | |
973 * An empty place in an [ArrayInitializer]. | |
974 * For example the list [1, , , 2] would contain two holes. | |
975 */ | |
976 class ArrayHole extends Expression { | |
977 accept(NodeVisitor visitor) => visitor.visitArrayHole(this); | |
978 | |
979 void visitChildren(NodeVisitor visitor) {} | |
980 | |
981 ArrayHole _clone() => new ArrayHole(); | |
982 | |
983 int get precedenceLevel => PRIMARY; | |
984 } | |
985 | |
986 class ObjectInitializer extends Expression { | |
987 final List<Property> properties; | |
988 final bool isOneLiner; | |
989 | |
990 /** | |
991 * Constructs a new object-initializer containing the given [properties]. | |
992 * | |
993 * [isOneLiner] describes the behaviour when pretty-printing (non-minified). | |
994 * If true print all properties on the same line. | |
995 * If false print each property on a seperate line. | |
996 */ | |
997 ObjectInitializer(this.properties, {this.isOneLiner: true}); | |
998 | |
999 accept(NodeVisitor visitor) => visitor.visitObjectInitializer(this); | |
1000 | |
1001 void visitChildren(NodeVisitor visitor) { | |
1002 for (Property init in properties) init.accept(visitor); | |
1003 } | |
1004 | |
1005 ObjectInitializer _clone() => | |
1006 new ObjectInitializer(properties, isOneLiner: isOneLiner); | |
1007 | |
1008 int get precedenceLevel => PRIMARY; | |
1009 } | |
1010 | |
1011 class Property extends Node { | |
1012 final Literal name; | |
1013 final Expression value; | |
1014 | |
1015 Property(this.name, this.value); | |
1016 | |
1017 accept(NodeVisitor visitor) => visitor.visitProperty(this); | |
1018 | |
1019 void visitChildren(NodeVisitor visitor) { | |
1020 name.accept(visitor); | |
1021 value.accept(visitor); | |
1022 } | |
1023 | |
1024 Property _clone() => new Property(name, value); | |
1025 } | |
1026 | |
1027 /// Tag class for all interpolated positions. | |
1028 abstract class InterpolatedNode implements Node { | |
1029 get nameOrPosition; | |
1030 | |
1031 bool get isNamed => nameOrPosition is String; | |
1032 bool get isPositional => nameOrPosition is int; | |
1033 } | |
1034 | |
1035 class InterpolatedExpression extends Expression with InterpolatedNode { | |
1036 final nameOrPosition; | |
1037 | |
1038 InterpolatedExpression(this.nameOrPosition); | |
1039 | |
1040 accept(NodeVisitor visitor) => visitor.visitInterpolatedExpression(this); | |
1041 void visitChildren(NodeVisitor visitor) {} | |
1042 InterpolatedExpression _clone() => | |
1043 new InterpolatedExpression(nameOrPosition); | |
1044 | |
1045 int get precedenceLevel => PRIMARY; | |
1046 } | |
1047 | |
1048 class InterpolatedLiteral extends Literal with InterpolatedNode { | |
1049 final nameOrPosition; | |
1050 | |
1051 InterpolatedLiteral(this.nameOrPosition); | |
1052 | |
1053 accept(NodeVisitor visitor) => visitor.visitInterpolatedLiteral(this); | |
1054 void visitChildren(NodeVisitor visitor) {} | |
1055 InterpolatedLiteral _clone() => new InterpolatedLiteral(nameOrPosition); | |
1056 } | |
1057 | |
1058 class InterpolatedParameter extends Expression with InterpolatedNode | |
1059 implements Parameter { | |
1060 final nameOrPosition; | |
1061 | |
1062 String get name { throw "InterpolatedParameter.name must not be invoked"; } | |
1063 bool get allowRename => false; | |
1064 | |
1065 InterpolatedParameter(this.nameOrPosition); | |
1066 | |
1067 accept(NodeVisitor visitor) => visitor.visitInterpolatedParameter(this); | |
1068 void visitChildren(NodeVisitor visitor) {} | |
1069 InterpolatedParameter _clone() => new InterpolatedParameter(nameOrPosition); | |
1070 | |
1071 int get precedenceLevel => PRIMARY; | |
1072 } | |
1073 | |
1074 class InterpolatedSelector extends Expression with InterpolatedNode { | |
1075 final nameOrPosition; | |
1076 | |
1077 InterpolatedSelector(this.nameOrPosition); | |
1078 | |
1079 accept(NodeVisitor visitor) => visitor.visitInterpolatedSelector(this); | |
1080 void visitChildren(NodeVisitor visitor) {} | |
1081 InterpolatedSelector _clone() => new InterpolatedSelector(nameOrPosition); | |
1082 | |
1083 int get precedenceLevel => PRIMARY; | |
1084 } | |
1085 | |
1086 class InterpolatedStatement extends Statement with InterpolatedNode { | |
1087 final nameOrPosition; | |
1088 | |
1089 InterpolatedStatement(this.nameOrPosition); | |
1090 | |
1091 accept(NodeVisitor visitor) => visitor.visitInterpolatedStatement(this); | |
1092 void visitChildren(NodeVisitor visitor) {} | |
1093 InterpolatedStatement _clone() => new InterpolatedStatement(nameOrPosition); | |
1094 } | |
1095 | |
1096 /** | |
1097 * [RegExpLiteral]s, despite being called "Literal", do not inherit from | |
1098 * [Literal]. Indeed, regular expressions in JavaScript have a side-effect and | |
1099 * are thus not in the same category as numbers or strings. | |
1100 */ | |
1101 class RegExpLiteral extends Expression { | |
1102 /** Contains the pattern and the flags.*/ | |
1103 final String pattern; | |
1104 | |
1105 RegExpLiteral(this.pattern); | |
1106 | |
1107 accept(NodeVisitor visitor) => visitor.visitRegExpLiteral(this); | |
1108 void visitChildren(NodeVisitor visitor) {} | |
1109 RegExpLiteral _clone() => new RegExpLiteral(pattern); | |
1110 | |
1111 int get precedenceLevel => PRIMARY; | |
1112 } | |
1113 | |
1114 /** | |
1115 * An asynchronous await. | |
1116 * | |
1117 * Not part of JavaScript. We desugar this expression before outputting. | |
1118 * Should only occur in a [Fun] with `asyncModifier` async or asyncStar. | |
1119 */ | |
1120 class Await extends Expression { | |
1121 /** The awaited expression. */ | |
1122 final Expression expression; | |
1123 | |
1124 Await(this.expression); | |
1125 | |
1126 int get precedenceLevel => UNARY; | |
1127 accept(NodeVisitor visitor) => visitor.visitAwait(this); | |
1128 void visitChildren(NodeVisitor visitor) => expression.accept(visitor); | |
1129 Await _clone() => new Await(expression); | |
1130 | |
1131 } | |
1132 | |
1133 /** | |
1134 * A comment. | |
1135 * | |
1136 * Extends [Statement] so we can add comments before statements in | |
1137 * [Block] and [Program]. | |
1138 */ | |
1139 class Comment extends Statement { | |
1140 final String comment; | |
1141 | |
1142 Comment(this.comment); | |
1143 | |
1144 accept(NodeVisitor visitor) => visitor.visitComment(this); | |
1145 Comment _clone() => new Comment(comment); | |
1146 | |
1147 void visitChildren(NodeVisitor visitor) {} | |
1148 } | |
OLD | NEW |