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