OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of js; | 5 part of js_ast; |
6 | 6 |
7 abstract class NodeVisitor<T> { | 7 abstract class NodeVisitor<T> { |
8 T visitProgram(Program node); | 8 T visitProgram(Program node); |
9 | 9 |
10 T visitBlock(Block node); | 10 T visitBlock(Block node); |
11 T visitExpressionStatement(ExpressionStatement node); | 11 T visitExpressionStatement(ExpressionStatement node); |
12 T visitEmptyStatement(EmptyStatement node); | 12 T visitEmptyStatement(EmptyStatement node); |
13 T visitIf(If node); | 13 T visitIf(If node); |
14 T visitFor(For node); | 14 T visitFor(For node); |
15 T visitForIn(ForIn node); | 15 T visitForIn(ForIn node); |
16 T visitWhile(While node); | 16 T visitWhile(While node); |
17 T visitDo(Do node); | 17 T visitDo(Do node); |
18 T visitContinue(Continue node); | 18 T visitContinue(Continue node); |
19 T visitBreak(Break node); | 19 T visitBreak(Break node); |
20 T visitReturn(Return node); | 20 T visitReturn(Return node); |
21 T visitThrow(Throw node); | 21 T visitThrow(Throw node); |
22 T visitTry(Try node); | 22 T visitTry(Try node); |
23 T visitCatch(Catch node); | 23 T visitCatch(Catch node); |
24 T visitSwitch(Switch node); | 24 T visitSwitch(Switch node); |
25 T visitCase(Case node); | 25 T visitCase(Case node); |
26 T visitDefault(Default node); | 26 T visitDefault(Default node); |
27 T visitFunctionDeclaration(FunctionDeclaration node); | 27 T visitFunctionDeclaration(FunctionDeclaration node); |
28 T visitLabeledStatement(LabeledStatement node); | 28 T visitLabeledStatement(LabeledStatement node); |
29 T visitLiteralStatement(LiteralStatement node); | 29 T visitLiteralStatement(LiteralStatement node); |
30 T visitDartYield(DartYield node); | 30 T visitDartYield(DartYield node); |
31 | 31 |
32 T visitBlob(Blob node); | |
33 T visitLiteralExpression(LiteralExpression node); | 32 T visitLiteralExpression(LiteralExpression node); |
34 T visitVariableDeclarationList(VariableDeclarationList node); | 33 T visitVariableDeclarationList(VariableDeclarationList node); |
35 T visitAssignment(Assignment node); | 34 T visitAssignment(Assignment node); |
36 T visitVariableInitialization(VariableInitialization node); | 35 T visitVariableInitialization(VariableInitialization node); |
37 T visitConditional(Conditional cond); | 36 T visitConditional(Conditional cond); |
38 T visitNew(New node); | 37 T visitNew(New node); |
39 T visitCall(Call node); | 38 T visitCall(Call node); |
40 T visitBinary(Binary node); | 39 T visitBinary(Binary node); |
41 T visitPrefix(Prefix node); | 40 T visitPrefix(Prefix node); |
42 T visitPostfix(Postfix node); | 41 T visitPostfix(Postfix node); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 T visitFunctionDeclaration(FunctionDeclaration node) | 101 T visitFunctionDeclaration(FunctionDeclaration node) |
103 => visitStatement(node); | 102 => visitStatement(node); |
104 T visitLabeledStatement(LabeledStatement node) => visitStatement(node); | 103 T visitLabeledStatement(LabeledStatement node) => visitStatement(node); |
105 T visitLiteralStatement(LiteralStatement node) => visitStatement(node); | 104 T visitLiteralStatement(LiteralStatement node) => visitStatement(node); |
106 | 105 |
107 T visitCatch(Catch node) => visitNode(node); | 106 T visitCatch(Catch node) => visitNode(node); |
108 T visitCase(Case node) => visitNode(node); | 107 T visitCase(Case node) => visitNode(node); |
109 T visitDefault(Default node) => visitNode(node); | 108 T visitDefault(Default node) => visitNode(node); |
110 | 109 |
111 T visitExpression(Expression node) => visitNode(node); | 110 T visitExpression(Expression node) => visitNode(node); |
112 T visitBlob(Blob node) => visitExpression(node); | |
113 T visitVariableReference(VariableReference node) => visitExpression(node); | 111 T visitVariableReference(VariableReference node) => visitExpression(node); |
114 | 112 |
115 T visitLiteralExpression(LiteralExpression node) => visitExpression(node); | 113 T visitLiteralExpression(LiteralExpression node) => visitExpression(node); |
116 T visitVariableDeclarationList(VariableDeclarationList node) | 114 T visitVariableDeclarationList(VariableDeclarationList node) |
117 => visitExpression(node); | 115 => visitExpression(node); |
118 T visitAssignment(Assignment node) => visitExpression(node); | 116 T visitAssignment(Assignment node) => visitExpression(node); |
119 T visitVariableInitialization(VariableInitialization node) { | 117 T visitVariableInitialization(VariableInitialization node) { |
120 if (node.value != null) { | 118 if (node.value != null) { |
121 return visitAssignment(node); | 119 return visitAssignment(node); |
122 } else { | 120 } else { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 T visitInterpolatedStatement(InterpolatedStatement node) | 164 T visitInterpolatedStatement(InterpolatedStatement node) |
167 => visitInterpolatedNode(node); | 165 => visitInterpolatedNode(node); |
168 | 166 |
169 // Ignore comments by default. | 167 // Ignore comments by default. |
170 T visitComment(Comment node) => null; | 168 T visitComment(Comment node) => null; |
171 | 169 |
172 T visitAwait(Await node) => visitExpression(node); | 170 T visitAwait(Await node) => visitExpression(node); |
173 T visitDartYield(DartYield node) => visitStatement(node); | 171 T visitDartYield(DartYield node) => visitStatement(node); |
174 } | 172 } |
175 | 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 |
176 abstract class Node { | 178 abstract class Node { |
177 SourceInformation get sourceInformation => _sourceInformation; | 179 JavaScriptNodeSourceInformation get sourceInformation => _sourceInformation; |
178 | 180 |
179 SourceInformation _sourceInformation; | 181 JavaScriptNodeSourceInformation _sourceInformation; |
180 | 182 |
181 accept(NodeVisitor visitor); | 183 accept(NodeVisitor visitor); |
182 void visitChildren(NodeVisitor visitor); | 184 void visitChildren(NodeVisitor visitor); |
183 | 185 |
184 // Shallow clone of node. Does not clone positions since the only use of this | 186 // Shallow clone of node. Does not clone positions since the only use of this |
185 // private method is create a copy with a new position. | 187 // private method is create a copy with a new position. |
186 Node _clone(); | 188 Node _clone(); |
187 | 189 |
188 // Returns a node equivalent to [this], but with new source position and end | 190 // Returns a node equivalent to [this], but with new source position and end |
189 // source position. | 191 // source position. |
190 Node withSourceInformation(SourceInformation sourceInformation) { | 192 Node withSourceInformation( |
| 193 JavaScriptNodeSourceInformation sourceInformation) { |
191 if (sourceInformation == _sourceInformation) { | 194 if (sourceInformation == _sourceInformation) { |
192 return this; | 195 return this; |
193 } | 196 } |
194 Node clone = _clone(); | 197 Node clone = _clone(); |
195 // TODO(sra): Should existing data be 'sticky' if we try to overwrite with | 198 // TODO(sra): Should existing data be 'sticky' if we try to overwrite with |
196 // `null`? | 199 // `null`? |
197 clone._sourceInformation = sourceInformation; | 200 clone._sourceInformation = sourceInformation; |
198 return clone; | 201 return clone; |
199 } | 202 } |
200 | 203 |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 final String code; | 518 final String code; |
516 | 519 |
517 LiteralStatement(this.code); | 520 LiteralStatement(this.code); |
518 | 521 |
519 accept(NodeVisitor visitor) => visitor.visitLiteralStatement(this); | 522 accept(NodeVisitor visitor) => visitor.visitLiteralStatement(this); |
520 void visitChildren(NodeVisitor visitor) { } | 523 void visitChildren(NodeVisitor visitor) { } |
521 | 524 |
522 LiteralStatement _clone() => new LiteralStatement(code); | 525 LiteralStatement _clone() => new LiteralStatement(code); |
523 } | 526 } |
524 | 527 |
525 // Not a real javascript node, but represents the yield statement from a dart | 528 // Not a real JavaScript node, but represents the yield statement from a dart |
526 // program translated to javascript. | 529 // program translated to JavaScript. |
527 class DartYield extends Statement { | 530 class DartYield extends Statement { |
528 final Expression expression; | 531 final Expression expression; |
529 | 532 |
530 final bool hasStar; | 533 final bool hasStar; |
531 | 534 |
532 DartYield(this.expression, this.hasStar); | 535 DartYield(this.expression, this.hasStar); |
533 | 536 |
534 accept(NodeVisitor visitor) => visitor.visitDartYield(this); | 537 accept(NodeVisitor visitor) => visitor.visitDartYield(this); |
535 | 538 |
536 void visitChildren(NodeVisitor visitor) { | 539 void visitChildren(NodeVisitor visitor) { |
537 expression.accept(visitor); | 540 expression.accept(visitor); |
538 } | 541 } |
539 | 542 |
540 DartYield _clone() => new DartYield(expression, hasStar); | 543 DartYield _clone() => new DartYield(expression, hasStar); |
541 } | 544 } |
542 | 545 |
543 abstract class Expression extends Node { | 546 abstract class Expression extends Node { |
544 int get precedenceLevel; | 547 int get precedenceLevel; |
545 | 548 |
546 Statement toStatement() => new ExpressionStatement(this); | 549 Statement toStatement() => new ExpressionStatement(this); |
547 } | 550 } |
548 | 551 |
549 /// Wrap a CodeBuffer as an expression. | |
550 class Blob extends Expression { | |
551 // TODO(ahe): This class is an aid to convert everything to ASTs, remove when | |
552 // not needed anymore. | |
553 | |
554 final CodeBuffer buffer; | |
555 | |
556 Blob(this.buffer); | |
557 | |
558 accept(NodeVisitor visitor) => visitor.visitBlob(this); | |
559 | |
560 void visitChildren(NodeVisitor visitor) {} | |
561 | |
562 Blob _clone() => new Blob(buffer); | |
563 | |
564 int get precedenceLevel => PRIMARY; | |
565 | |
566 } | |
567 | |
568 class LiteralExpression extends Expression { | 552 class LiteralExpression extends Expression { |
569 final String template; | 553 final String template; |
570 final List<Expression> inputs; | 554 final List<Expression> inputs; |
571 | 555 |
572 LiteralExpression(this.template) : inputs = const []; | 556 LiteralExpression(this.template) : inputs = const []; |
573 LiteralExpression.withData(this.template, this.inputs); | 557 LiteralExpression.withData(this.template, this.inputs); |
574 | 558 |
575 accept(NodeVisitor visitor) => visitor.visitLiteralExpression(this); | 559 accept(NodeVisitor visitor) => visitor.visitLiteralExpression(this); |
576 | 560 |
577 void visitChildren(NodeVisitor visitor) { | 561 void visitChildren(NodeVisitor visitor) { |
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
940 accept(NodeVisitor visitor) => visitor.visitLiteralNull(this); | 924 accept(NodeVisitor visitor) => visitor.visitLiteralNull(this); |
941 LiteralNull _clone() => new LiteralNull(); | 925 LiteralNull _clone() => new LiteralNull(); |
942 } | 926 } |
943 | 927 |
944 class LiteralString extends Literal { | 928 class LiteralString extends Literal { |
945 final String value; | 929 final String value; |
946 | 930 |
947 /** | 931 /** |
948 * Constructs a LiteralString from a string value. | 932 * Constructs a LiteralString from a string value. |
949 * | 933 * |
950 * The constructor does not add the required quotes. If [value] is | 934 * The constructor does not add the required quotes. If [value] is not |
951 * not surrounded by quotes, the resulting object is invalid as a JS | 935 * surrounded by quotes and property escaped, the resulting object is invalid |
952 * value. | 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. |
953 */ | 940 */ |
954 LiteralString(this.value); | 941 LiteralString(this.value); |
955 | 942 |
956 accept(NodeVisitor visitor) => visitor.visitLiteralString(this); | 943 accept(NodeVisitor visitor) => visitor.visitLiteralString(this); |
957 LiteralString _clone() => new LiteralString(value); | 944 LiteralString _clone() => new LiteralString(value); |
958 } | 945 } |
959 | 946 |
960 class LiteralNumber extends Literal { | 947 class LiteralNumber extends Literal { |
961 final String value; | 948 final String value; // Must be a valid JavaScript number literal. |
962 | 949 |
963 LiteralNumber(this.value); | 950 LiteralNumber(this.value); |
964 | 951 |
965 accept(NodeVisitor visitor) => visitor.visitLiteralNumber(this); | 952 accept(NodeVisitor visitor) => visitor.visitLiteralNumber(this); |
966 LiteralNumber _clone() => new LiteralNumber(value); | 953 LiteralNumber _clone() => new LiteralNumber(value); |
967 } | 954 } |
968 | 955 |
969 class ArrayInitializer extends Expression { | 956 class ArrayInitializer extends Expression { |
970 final List<Expression> elements; | 957 final List<Expression> elements; |
971 | 958 |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1120 accept(NodeVisitor visitor) => visitor.visitRegExpLiteral(this); | 1107 accept(NodeVisitor visitor) => visitor.visitRegExpLiteral(this); |
1121 void visitChildren(NodeVisitor visitor) {} | 1108 void visitChildren(NodeVisitor visitor) {} |
1122 RegExpLiteral _clone() => new RegExpLiteral(pattern); | 1109 RegExpLiteral _clone() => new RegExpLiteral(pattern); |
1123 | 1110 |
1124 int get precedenceLevel => PRIMARY; | 1111 int get precedenceLevel => PRIMARY; |
1125 } | 1112 } |
1126 | 1113 |
1127 /** | 1114 /** |
1128 * An asynchronous await. | 1115 * An asynchronous await. |
1129 * | 1116 * |
1130 * Not part of javascript. We desugar this expression before outputting. | 1117 * Not part of JavaScript. We desugar this expression before outputting. |
1131 * Should only occur in a [Fun] with `asyncModifier` async or asyncStar. | 1118 * Should only occur in a [Fun] with `asyncModifier` async or asyncStar. |
1132 */ | 1119 */ |
1133 class Await extends Expression { | 1120 class Await extends Expression { |
1134 /** The awaited expression. */ | 1121 /** The awaited expression. */ |
1135 final Expression expression; | 1122 final Expression expression; |
1136 | 1123 |
1137 Await(this.expression); | 1124 Await(this.expression); |
1138 | 1125 |
1139 int get precedenceLevel => UNARY; | 1126 int get precedenceLevel => UNARY; |
1140 accept(NodeVisitor visitor) => visitor.visitAwait(this); | 1127 accept(NodeVisitor visitor) => visitor.visitAwait(this); |
(...skipping 11 matching lines...) Expand all Loading... |
1152 class Comment extends Statement { | 1139 class Comment extends Statement { |
1153 final String comment; | 1140 final String comment; |
1154 | 1141 |
1155 Comment(this.comment); | 1142 Comment(this.comment); |
1156 | 1143 |
1157 accept(NodeVisitor visitor) => visitor.visitComment(this); | 1144 accept(NodeVisitor visitor) => visitor.visitComment(this); |
1158 Comment _clone() => new Comment(comment); | 1145 Comment _clone() => new Comment(comment); |
1159 | 1146 |
1160 void visitChildren(NodeVisitor visitor) {} | 1147 void visitChildren(NodeVisitor visitor) {} |
1161 } | 1148 } |
OLD | NEW |