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_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 visitForOf(ForOf node); |
16 T visitWhile(While node); | 17 T visitWhile(While node); |
17 T visitDo(Do node); | 18 T visitDo(Do node); |
18 T visitContinue(Continue node); | 19 T visitContinue(Continue node); |
19 T visitBreak(Break node); | 20 T visitBreak(Break node); |
20 T visitReturn(Return node); | 21 T visitReturn(Return node); |
21 T visitThrow(Throw node); | 22 T visitThrow(Throw node); |
22 T visitTry(Try node); | 23 T visitTry(Try node); |
23 T visitCatch(Catch node); | 24 T visitCatch(Catch node); |
24 T visitSwitch(Switch node); | 25 T visitSwitch(Switch node); |
25 T visitCase(Case node); | 26 T visitCase(Case node); |
26 T visitDefault(Default node); | 27 T visitDefault(Default node); |
27 T visitFunctionDeclaration(FunctionDeclaration node); | 28 T visitFunctionDeclaration(FunctionDeclaration node); |
28 T visitLabeledStatement(LabeledStatement node); | 29 T visitLabeledStatement(LabeledStatement node); |
29 T visitLiteralStatement(LiteralStatement node); | 30 T visitLiteralStatement(LiteralStatement node); |
30 T visitDartYield(DartYield node); | 31 T visitDartYield(DartYield node); |
31 | 32 |
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); |
42 | 43 |
43 T visitVariableUse(VariableUse node); | 44 T visitVariableUse(VariableUse node); |
44 T visitThis(This node); | 45 T visitThis(This node); |
| 46 T visitSuper(Super node); |
45 T visitVariableDeclaration(VariableDeclaration node); | 47 T visitVariableDeclaration(VariableDeclaration node); |
46 T visitParameter(Parameter node); | 48 T visitParameter(Parameter node); |
47 T visitAccess(PropertyAccess node); | 49 T visitAccess(PropertyAccess node); |
48 | 50 |
49 T visitNamedFunction(NamedFunction node); | 51 T visitNamedFunction(NamedFunction node); |
50 T visitFun(Fun node); | 52 T visitFun(Fun node); |
| 53 T visitArrowFun(ArrowFun node); |
51 | 54 |
52 T visitLiteralBool(LiteralBool node); | 55 T visitLiteralBool(LiteralBool node); |
53 T visitLiteralString(LiteralString node); | 56 T visitLiteralString(LiteralString node); |
54 T visitLiteralNumber(LiteralNumber node); | 57 T visitLiteralNumber(LiteralNumber node); |
55 T visitLiteralNull(LiteralNull node); | 58 T visitLiteralNull(LiteralNull node); |
56 | 59 |
57 T visitArrayInitializer(ArrayInitializer node); | 60 T visitArrayInitializer(ArrayInitializer node); |
58 T visitArrayHole(ArrayHole node); | 61 T visitArrayHole(ArrayHole node); |
59 T visitObjectInitializer(ObjectInitializer node); | 62 T visitObjectInitializer(ObjectInitializer node); |
60 T visitProperty(Property node); | 63 T visitProperty(Property node); |
61 T visitRegExpLiteral(RegExpLiteral node); | 64 T visitRegExpLiteral(RegExpLiteral node); |
| 65 T visitTemplateString(TemplateString node); |
| 66 T visitTaggedTemplate(TaggedTemplate node); |
62 | 67 |
63 T visitAwait(Await node); | 68 T visitAwait(Await node); |
64 | 69 |
| 70 T visitClassDeclaration(ClassDeclaration node); |
| 71 T visitClassExpression(ClassExpression node); |
| 72 T visitMethod(Method node); |
| 73 T visitPropertyName(PropertyName node); |
| 74 |
65 T visitComment(Comment node); | 75 T visitComment(Comment node); |
| 76 T visitCommentExpression(CommentExpression node); |
66 | 77 |
67 T visitInterpolatedExpression(InterpolatedExpression node); | 78 T visitInterpolatedExpression(InterpolatedExpression node); |
68 T visitInterpolatedLiteral(InterpolatedLiteral node); | 79 T visitInterpolatedLiteral(InterpolatedLiteral node); |
69 T visitInterpolatedParameter(InterpolatedParameter node); | 80 T visitInterpolatedParameter(InterpolatedParameter node); |
70 T visitInterpolatedSelector(InterpolatedSelector node); | 81 T visitInterpolatedSelector(InterpolatedSelector node); |
71 T visitInterpolatedStatement(InterpolatedStatement node); | 82 T visitInterpolatedStatement(InterpolatedStatement node); |
| 83 T visitInterpolatedMethod(InterpolatedMethod node); |
| 84 T visitInterpolatedPropertyName(InterpolatedPropertyName node); |
| 85 T visitInterpolatedVariableDeclaration(InterpolatedVariableDeclaration node); |
72 } | 86 } |
73 | 87 |
74 class BaseVisitor<T> implements NodeVisitor<T> { | 88 class BaseVisitor<T> implements NodeVisitor<T> { |
75 T visitNode(Node node) { | 89 T visitNode(Node node) { |
76 node.visitChildren(this); | 90 node.visitChildren(this); |
77 return null; | 91 return null; |
78 } | 92 } |
79 | 93 |
80 T visitProgram(Program node) => visitNode(node); | 94 T visitProgram(Program node) => visitNode(node); |
81 | 95 |
82 T visitStatement(Statement node) => visitNode(node); | 96 T visitStatement(Statement node) => visitNode(node); |
83 T visitLoop(Loop node) => visitStatement(node); | 97 T visitLoop(Loop node) => visitStatement(node); |
84 T visitJump(Statement node) => visitStatement(node); | 98 T visitJump(Statement node) => visitStatement(node); |
85 | 99 |
86 T visitBlock(Block node) => visitStatement(node); | 100 T visitBlock(Block node) => visitStatement(node); |
87 T visitExpressionStatement(ExpressionStatement node) | 101 T visitExpressionStatement(ExpressionStatement node) |
88 => visitStatement(node); | 102 => visitStatement(node); |
89 T visitEmptyStatement(EmptyStatement node) => visitStatement(node); | 103 T visitEmptyStatement(EmptyStatement node) => visitStatement(node); |
90 T visitIf(If node) => visitStatement(node); | 104 T visitIf(If node) => visitStatement(node); |
91 T visitFor(For node) => visitLoop(node); | 105 T visitFor(For node) => visitLoop(node); |
92 T visitForIn(ForIn node) => visitLoop(node); | 106 T visitForIn(ForIn node) => visitLoop(node); |
| 107 T visitForOf(ForOf node) => visitLoop(node); |
93 T visitWhile(While node) => visitLoop(node); | 108 T visitWhile(While node) => visitLoop(node); |
94 T visitDo(Do node) => visitLoop(node); | 109 T visitDo(Do node) => visitLoop(node); |
95 T visitContinue(Continue node) => visitJump(node); | 110 T visitContinue(Continue node) => visitJump(node); |
96 T visitBreak(Break node) => visitJump(node); | 111 T visitBreak(Break node) => visitJump(node); |
97 T visitReturn(Return node) => visitJump(node); | 112 T visitReturn(Return node) => visitJump(node); |
98 T visitThrow(Throw node) => visitJump(node); | 113 T visitThrow(Throw node) => visitJump(node); |
99 T visitTry(Try node) => visitStatement(node); | 114 T visitTry(Try node) => visitStatement(node); |
100 T visitSwitch(Switch node) => visitStatement(node); | 115 T visitSwitch(Switch node) => visitStatement(node); |
101 T visitFunctionDeclaration(FunctionDeclaration node) | 116 T visitFunctionDeclaration(FunctionDeclaration node) |
102 => visitStatement(node); | 117 => visitStatement(node); |
(...skipping 24 matching lines...) Expand all Loading... |
127 T visitBinary(Binary node) => visitExpression(node); | 142 T visitBinary(Binary node) => visitExpression(node); |
128 T visitPrefix(Prefix node) => visitExpression(node); | 143 T visitPrefix(Prefix node) => visitExpression(node); |
129 T visitPostfix(Postfix node) => visitExpression(node); | 144 T visitPostfix(Postfix node) => visitExpression(node); |
130 T visitAccess(PropertyAccess node) => visitExpression(node); | 145 T visitAccess(PropertyAccess node) => visitExpression(node); |
131 | 146 |
132 T visitVariableUse(VariableUse node) => visitVariableReference(node); | 147 T visitVariableUse(VariableUse node) => visitVariableReference(node); |
133 T visitVariableDeclaration(VariableDeclaration node) | 148 T visitVariableDeclaration(VariableDeclaration node) |
134 => visitVariableReference(node); | 149 => visitVariableReference(node); |
135 T visitParameter(Parameter node) => visitVariableDeclaration(node); | 150 T visitParameter(Parameter node) => visitVariableDeclaration(node); |
136 T visitThis(This node) => visitParameter(node); | 151 T visitThis(This node) => visitParameter(node); |
| 152 T visitSuper(Super node) => visitParameter(node); |
137 | 153 |
138 T visitNamedFunction(NamedFunction node) => visitExpression(node); | 154 T visitNamedFunction(NamedFunction node) => visitExpression(node); |
139 T visitFun(Fun node) => visitExpression(node); | 155 T visitFunctionExpression(FunctionExpression node) => visitExpression(node); |
| 156 T visitFun(Fun node) => visitFunctionExpression(node); |
| 157 T visitArrowFun(ArrowFun node) => visitFunctionExpression(node); |
140 | 158 |
141 T visitLiteral(Literal node) => visitExpression(node); | 159 T visitLiteral(Literal node) => visitExpression(node); |
142 | 160 |
143 T visitLiteralBool(LiteralBool node) => visitLiteral(node); | 161 T visitLiteralBool(LiteralBool node) => visitLiteral(node); |
144 T visitLiteralString(LiteralString node) => visitLiteral(node); | 162 T visitLiteralString(LiteralString node) => visitLiteral(node); |
145 T visitLiteralNumber(LiteralNumber node) => visitLiteral(node); | 163 T visitLiteralNumber(LiteralNumber node) => visitLiteral(node); |
146 T visitLiteralNull(LiteralNull node) => visitLiteral(node); | 164 T visitLiteralNull(LiteralNull node) => visitLiteral(node); |
147 | 165 |
148 T visitArrayInitializer(ArrayInitializer node) => visitExpression(node); | 166 T visitArrayInitializer(ArrayInitializer node) => visitExpression(node); |
149 T visitArrayHole(ArrayHole node) => visitExpression(node); | 167 T visitArrayHole(ArrayHole node) => visitExpression(node); |
150 T visitObjectInitializer(ObjectInitializer node) => visitExpression(node); | 168 T visitObjectInitializer(ObjectInitializer node) => visitExpression(node); |
151 T visitProperty(Property node) => visitNode(node); | 169 T visitProperty(Property node) => visitNode(node); |
152 T visitRegExpLiteral(RegExpLiteral node) => visitExpression(node); | 170 T visitRegExpLiteral(RegExpLiteral node) => visitExpression(node); |
| 171 T visitTemplateString(TemplateString node) => visitExpression(node); |
| 172 T visitTaggedTemplate(TaggedTemplate node) => visitExpression(node); |
| 173 |
| 174 T visitClassDeclaration(ClassDeclaration node) => visitStatement(node); |
| 175 T visitClassExpression(ClassExpression node) => visitExpression(node); |
| 176 T visitMethod(Method node) => visitProperty(node); |
| 177 T visitPropertyName(PropertyName node) => visitExpression(node); |
153 | 178 |
154 T visitInterpolatedNode(InterpolatedNode node) => visitNode(node); | 179 T visitInterpolatedNode(InterpolatedNode node) => visitNode(node); |
155 | 180 |
156 T visitInterpolatedExpression(InterpolatedExpression node) | 181 T visitInterpolatedExpression(InterpolatedExpression node) |
157 => visitInterpolatedNode(node); | 182 => visitInterpolatedNode(node); |
158 T visitInterpolatedLiteral(InterpolatedLiteral node) | 183 T visitInterpolatedLiteral(InterpolatedLiteral node) |
159 => visitInterpolatedNode(node); | 184 => visitInterpolatedNode(node); |
160 T visitInterpolatedParameter(InterpolatedParameter node) | 185 T visitInterpolatedParameter(InterpolatedParameter node) |
161 => visitInterpolatedNode(node); | 186 => visitInterpolatedNode(node); |
162 T visitInterpolatedSelector(InterpolatedSelector node) | 187 T visitInterpolatedSelector(InterpolatedSelector node) |
163 => visitInterpolatedNode(node); | 188 => visitInterpolatedNode(node); |
164 T visitInterpolatedStatement(InterpolatedStatement node) | 189 T visitInterpolatedStatement(InterpolatedStatement node) |
165 => visitInterpolatedNode(node); | 190 => visitInterpolatedNode(node); |
| 191 T visitInterpolatedMethod(InterpolatedMethod node) |
| 192 => visitInterpolatedNode(node); |
| 193 T visitInterpolatedPropertyName(InterpolatedPropertyName node) |
| 194 => visitInterpolatedNode(node); |
| 195 T visitInterpolatedVariableDeclaration(InterpolatedVariableDeclaration node) |
| 196 => visitInterpolatedNode(node); |
166 | 197 |
167 // Ignore comments by default. | 198 // Ignore comments by default. |
168 T visitComment(Comment node) => null; | 199 T visitComment(Comment node) => null; |
| 200 T visitCommentExpression(CommentExpression node) => null; |
169 | 201 |
170 T visitAwait(Await node) => visitExpression(node); | 202 T visitAwait(Await node) => visitExpression(node); |
171 T visitDartYield(DartYield node) => visitStatement(node); | 203 T visitDartYield(DartYield node) => visitStatement(node); |
172 } | 204 } |
173 | 205 |
174 /// This tag interface has no behaviour but must be implemented by any class | 206 /// 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. | 207 /// that is to be stored on a [Node] as source information. |
176 abstract class JavaScriptNodeSourceInformation {} | 208 abstract class JavaScriptNodeSourceInformation {} |
177 | 209 |
178 abstract class Node { | 210 abstract class Node { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 } | 252 } |
221 Program _clone() => new Program(body); | 253 Program _clone() => new Program(body); |
222 } | 254 } |
223 | 255 |
224 abstract class Statement extends Node { | 256 abstract class Statement extends Node { |
225 Statement toStatement() => this; | 257 Statement toStatement() => this; |
226 } | 258 } |
227 | 259 |
228 class Block extends Statement { | 260 class Block extends Statement { |
229 final List<Statement> statements; | 261 final List<Statement> statements; |
230 Block(this.statements); | 262 Block(this.statements) { |
| 263 assert(!statements.any((s) => s is! Statement)); |
| 264 } |
231 Block.empty() : this.statements = <Statement>[]; | 265 Block.empty() : this.statements = <Statement>[]; |
232 | 266 |
233 accept(NodeVisitor visitor) => visitor.visitBlock(this); | 267 accept(NodeVisitor visitor) => visitor.visitBlock(this); |
234 void visitChildren(NodeVisitor visitor) { | 268 void visitChildren(NodeVisitor visitor) { |
235 for (Statement statement in statements) statement.accept(visitor); | 269 for (Statement statement in statements) statement.accept(visitor); |
236 } | 270 } |
237 Block _clone() => new Block(statements); | 271 Block _clone() => new Block(statements); |
238 } | 272 } |
239 | 273 |
240 class ExpressionStatement extends Statement { | 274 class ExpressionStatement extends Statement { |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 | 345 |
312 void visitChildren(NodeVisitor visitor) { | 346 void visitChildren(NodeVisitor visitor) { |
313 leftHandSide.accept(visitor); | 347 leftHandSide.accept(visitor); |
314 object.accept(visitor); | 348 object.accept(visitor); |
315 body.accept(visitor); | 349 body.accept(visitor); |
316 } | 350 } |
317 | 351 |
318 ForIn _clone() => new ForIn(leftHandSide, object, body); | 352 ForIn _clone() => new ForIn(leftHandSide, object, body); |
319 } | 353 } |
320 | 354 |
| 355 class ForOf extends Loop { |
| 356 // Note that [VariableDeclarationList] is a subclass of [Expression]. |
| 357 // Therefore we can type the leftHandSide as [Expression]. |
| 358 final Expression leftHandSide; |
| 359 final Expression iterable; |
| 360 |
| 361 ForOf(this.leftHandSide, this.iterable, Statement body) : super(body); |
| 362 |
| 363 accept(NodeVisitor visitor) => visitor.visitForOf(this); |
| 364 |
| 365 void visitChildren(NodeVisitor visitor) { |
| 366 leftHandSide.accept(visitor); |
| 367 iterable.accept(visitor); |
| 368 body.accept(visitor); |
| 369 } |
| 370 |
| 371 ForIn _clone() => new ForIn(leftHandSide, iterable, body); |
| 372 } |
| 373 |
321 class While extends Loop { | 374 class While extends Loop { |
322 final Node condition; | 375 final Node condition; |
323 | 376 |
324 While(this.condition, Statement body) : super(body); | 377 While(this.condition, Statement body) : super(body); |
325 | 378 |
326 accept(NodeVisitor visitor) => visitor.visitWhile(this); | 379 accept(NodeVisitor visitor) => visitor.visitWhile(this); |
327 | 380 |
328 void visitChildren(NodeVisitor visitor) { | 381 void visitChildren(NodeVisitor visitor) { |
329 condition.accept(visitor); | 382 condition.accept(visitor); |
330 body.accept(visitor); | 383 body.accept(visitor); |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
570 // Code that uses JS must take care of operator precedences, and | 623 // Code that uses JS must take care of operator precedences, and |
571 // put parenthesis if needed. | 624 // put parenthesis if needed. |
572 int get precedenceLevel => PRIMARY; | 625 int get precedenceLevel => PRIMARY; |
573 } | 626 } |
574 | 627 |
575 /** | 628 /** |
576 * [VariableDeclarationList] is a subclass of [Expression] to simplify the | 629 * [VariableDeclarationList] is a subclass of [Expression] to simplify the |
577 * AST. | 630 * AST. |
578 */ | 631 */ |
579 class VariableDeclarationList extends Expression { | 632 class VariableDeclarationList extends Expression { |
| 633 /** The `var` or `let` keyword used for this variable declaration list. */ |
| 634 final String keyword; |
580 final List<VariableInitialization> declarations; | 635 final List<VariableInitialization> declarations; |
581 | 636 |
582 VariableDeclarationList(this.declarations); | 637 VariableDeclarationList(this.keyword, this.declarations); |
583 | 638 |
584 accept(NodeVisitor visitor) => visitor.visitVariableDeclarationList(this); | 639 accept(NodeVisitor visitor) => visitor.visitVariableDeclarationList(this); |
585 | 640 |
586 void visitChildren(NodeVisitor visitor) { | 641 void visitChildren(NodeVisitor visitor) { |
587 for (VariableInitialization declaration in declarations) { | 642 for (VariableInitialization declaration in declarations) { |
588 declaration.accept(visitor); | 643 declaration.accept(visitor); |
589 } | 644 } |
590 } | 645 } |
591 | 646 |
592 VariableDeclarationList _clone() => new VariableDeclarationList(declarations); | 647 VariableDeclarationList _clone() => |
| 648 new VariableDeclarationList(keyword, declarations); |
593 | 649 |
594 int get precedenceLevel => EXPRESSION; | 650 int get precedenceLevel => EXPRESSION; |
595 } | 651 } |
596 | 652 |
597 class Assignment extends Expression { | 653 class Assignment extends Expression { |
598 final Expression leftHandSide; | 654 final Expression leftHandSide; |
599 final String op; // Null, if the assignment is not compound. | 655 final String op; // Null, if the assignment is not compound. |
600 final Expression value; // May be null, for [VariableInitialization]s. | 656 final Expression value; // May be null, for [VariableInitialization]s. |
601 | 657 |
602 Assignment(leftHandSide, value) | 658 Assignment(leftHandSide, value) |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
799 } | 855 } |
800 | 856 |
801 class VariableDeclaration extends VariableReference { | 857 class VariableDeclaration extends VariableReference { |
802 final bool allowRename; | 858 final bool allowRename; |
803 VariableDeclaration(String name, {this.allowRename: true}) : super(name); | 859 VariableDeclaration(String name, {this.allowRename: true}) : super(name); |
804 | 860 |
805 accept(NodeVisitor visitor) => visitor.visitVariableDeclaration(this); | 861 accept(NodeVisitor visitor) => visitor.visitVariableDeclaration(this); |
806 VariableDeclaration _clone() => new VariableDeclaration(name); | 862 VariableDeclaration _clone() => new VariableDeclaration(name); |
807 } | 863 } |
808 | 864 |
| 865 class PropertyName extends Expression { |
| 866 final String name; |
| 867 PropertyName(this.name); |
| 868 |
| 869 accept(NodeVisitor visitor) => visitor.visitPropertyName(this); |
| 870 visitChildren(NodeVisitor visitor) {} |
| 871 int get precedenceLevel => PRIMARY; |
| 872 PropertyName _clone() => new PropertyName(name); |
| 873 } |
| 874 |
809 class Parameter extends VariableDeclaration { | 875 class Parameter extends VariableDeclaration { |
810 Parameter(String name) : super(name); | 876 Parameter(String name) : super(name); |
811 | 877 |
812 accept(NodeVisitor visitor) => visitor.visitParameter(this); | 878 accept(NodeVisitor visitor) => visitor.visitParameter(this); |
813 Parameter _clone() => new Parameter(name); | 879 Parameter _clone() => new Parameter(name); |
814 } | 880 } |
815 | 881 |
816 class This extends Parameter { | 882 class This extends Parameter { |
817 This() : super("this"); | 883 This() : super("this"); |
818 | 884 |
819 accept(NodeVisitor visitor) => visitor.visitThis(this); | 885 accept(NodeVisitor visitor) => visitor.visitThis(this); |
820 This _clone() => new This(); | 886 This _clone() => new This(); |
821 } | 887 } |
822 | 888 |
| 889 // `super` is more restricted in the ES6 spec, but for simplicity we accept |
| 890 // it anywhere that `this` is accepted. |
| 891 class Super extends Parameter { |
| 892 Super() : super("super"); |
| 893 |
| 894 accept(NodeVisitor visitor) => visitor.visitSuper(this); |
| 895 Super _clone() => new Super(); |
| 896 } |
| 897 |
823 class NamedFunction extends Expression { | 898 class NamedFunction extends Expression { |
824 final VariableDeclaration name; | 899 final VariableDeclaration name; |
825 final Fun function; | 900 final Fun function; |
826 | 901 |
827 NamedFunction(this.name, this.function); | 902 NamedFunction(this.name, this.function); |
828 | 903 |
829 accept(NodeVisitor visitor) => visitor.visitNamedFunction(this); | 904 accept(NodeVisitor visitor) => visitor.visitNamedFunction(this); |
830 | 905 |
831 void visitChildren(NodeVisitor visitor) { | 906 void visitChildren(NodeVisitor visitor) { |
832 name.accept(visitor); | 907 name.accept(visitor); |
833 function.accept(visitor); | 908 function.accept(visitor); |
834 } | 909 } |
835 NamedFunction _clone() => new NamedFunction(name, function); | 910 NamedFunction _clone() => new NamedFunction(name, function); |
836 | 911 |
837 int get precedenceLevel => CALL; | 912 int get precedenceLevel => CALL; |
838 } | 913 } |
839 | 914 |
840 class Fun extends Expression { | 915 abstract class FunctionExpression extends Expression { |
| 916 List<Parameter> get params; |
| 917 get body; // Expression or block |
| 918 } |
| 919 |
| 920 class Fun extends FunctionExpression { |
841 final List<Parameter> params; | 921 final List<Parameter> params; |
842 final Block body; | 922 final Block body; |
843 final AsyncModifier asyncModifier; | 923 final AsyncModifier asyncModifier; |
844 | 924 |
845 Fun(this.params, this.body, {this.asyncModifier: const AsyncModifier.sync()}); | 925 Fun(this.params, this.body, {this.asyncModifier: const AsyncModifier.sync()}); |
846 | 926 |
847 accept(NodeVisitor visitor) => visitor.visitFun(this); | 927 accept(NodeVisitor visitor) => visitor.visitFun(this); |
848 | 928 |
849 void visitChildren(NodeVisitor visitor) { | 929 void visitChildren(NodeVisitor visitor) { |
850 for (Parameter param in params) param.accept(visitor); | 930 for (Parameter param in params) param.accept(visitor); |
851 body.accept(visitor); | 931 body.accept(visitor); |
852 } | 932 } |
853 | 933 |
854 Fun _clone() => new Fun(params, body, asyncModifier: asyncModifier); | 934 Fun _clone() => new Fun(params, body, asyncModifier: asyncModifier); |
855 | 935 |
856 int get precedenceLevel => CALL; | 936 int get precedenceLevel => CALL; |
857 } | 937 } |
858 | 938 |
| 939 class ArrowFun extends FunctionExpression { |
| 940 final List<Parameter> params; |
| 941 final body; // Expression or Block |
| 942 |
| 943 ArrowFun(this.params, this.body); |
| 944 |
| 945 accept(NodeVisitor visitor) => visitor.visitArrowFun(this); |
| 946 |
| 947 void visitChildren(NodeVisitor visitor) { |
| 948 for (Parameter param in params) param.accept(visitor); |
| 949 body.accept(visitor); |
| 950 } |
| 951 |
| 952 ArrowFun _clone() => new ArrowFun(params, body); |
| 953 |
| 954 /// Ensure parens always get generated if necessary. |
| 955 // TODO(jmesserly): I'm not sure the printer is handling this correctly for |
| 956 // function() { ... } either. |
| 957 int get precedenceLevel => ASSIGNMENT; |
| 958 } |
| 959 |
859 class AsyncModifier { | 960 class AsyncModifier { |
860 final bool isAsync; | 961 final bool isAsync; |
861 final bool isYielding; | 962 final bool isYielding; |
862 final String description; | 963 final String description; |
863 | 964 |
864 const AsyncModifier.sync() | 965 const AsyncModifier.sync() |
865 : isAsync = false, | 966 : isAsync = false, |
866 isYielding = false, | 967 isYielding = false, |
867 description = "sync"; | 968 description = "sync"; |
868 const AsyncModifier.async() | 969 const AsyncModifier.async() |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
978 | 1079 |
979 void visitChildren(NodeVisitor visitor) {} | 1080 void visitChildren(NodeVisitor visitor) {} |
980 | 1081 |
981 ArrayHole _clone() => new ArrayHole(); | 1082 ArrayHole _clone() => new ArrayHole(); |
982 | 1083 |
983 int get precedenceLevel => PRIMARY; | 1084 int get precedenceLevel => PRIMARY; |
984 } | 1085 } |
985 | 1086 |
986 class ObjectInitializer extends Expression { | 1087 class ObjectInitializer extends Expression { |
987 final List<Property> properties; | 1088 final List<Property> properties; |
988 final bool isOneLiner; | |
989 | 1089 |
990 /** | 1090 /** |
991 * Constructs a new object-initializer containing the given [properties]. | 1091 * 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 */ | 1092 */ |
997 ObjectInitializer(this.properties, {this.isOneLiner: true}); | 1093 ObjectInitializer(this.properties); |
998 | 1094 |
999 accept(NodeVisitor visitor) => visitor.visitObjectInitializer(this); | 1095 accept(NodeVisitor visitor) => visitor.visitObjectInitializer(this); |
1000 | 1096 |
1001 void visitChildren(NodeVisitor visitor) { | 1097 void visitChildren(NodeVisitor visitor) { |
1002 for (Property init in properties) init.accept(visitor); | 1098 for (Property init in properties) init.accept(visitor); |
1003 } | 1099 } |
1004 | 1100 |
1005 ObjectInitializer _clone() => | 1101 ObjectInitializer _clone() => new ObjectInitializer(properties); |
1006 new ObjectInitializer(properties, isOneLiner: isOneLiner); | |
1007 | 1102 |
1008 int get precedenceLevel => PRIMARY; | 1103 int get precedenceLevel => PRIMARY; |
1009 } | 1104 } |
1010 | 1105 |
1011 class Property extends Node { | 1106 class Property extends Node { |
1012 final Literal name; | 1107 final Expression name; |
1013 final Expression value; | 1108 final Expression value; |
1014 | 1109 |
1015 Property(this.name, this.value); | 1110 Property(this.name, this.value) { |
| 1111 assert(name is! VariableDeclaration); |
| 1112 } |
1016 | 1113 |
1017 accept(NodeVisitor visitor) => visitor.visitProperty(this); | 1114 accept(NodeVisitor visitor) => visitor.visitProperty(this); |
1018 | 1115 |
1019 void visitChildren(NodeVisitor visitor) { | 1116 void visitChildren(NodeVisitor visitor) { |
1020 name.accept(visitor); | 1117 name.accept(visitor); |
1021 value.accept(visitor); | 1118 value.accept(visitor); |
1022 } | 1119 } |
1023 | 1120 |
1024 Property _clone() => new Property(name, value); | 1121 Property _clone() => new Property(name, value); |
1025 } | 1122 } |
1026 | 1123 |
| 1124 // TODO(jmesserly): parser does not support this yet. |
| 1125 class TemplateString extends Expression { |
| 1126 /** |
| 1127 * The parts of this template string: a sequence of [String]s and |
| 1128 * [Expression]s. Strings and expressions will alternate, for example: |
| 1129 * |
| 1130 * `foo${1 + 2} bar ${'hi'}` |
| 1131 * |
| 1132 * would be represented by: |
| 1133 * |
| 1134 * ['foo', new JS.Binary('+', js.number(1), js.number(2)), |
| 1135 * ' bar ', new JS.LiteralString("'hi'")] |
| 1136 */ |
| 1137 final List elements; |
| 1138 TemplateString(this.elements); |
| 1139 |
| 1140 accept(NodeVisitor visitor) => visitor.visitTemplateString(this); |
| 1141 |
| 1142 void visitChildren(NodeVisitor visitor) { |
| 1143 for (var element in elements) { |
| 1144 if (element is Expression) element.accept(visitor); |
| 1145 } |
| 1146 } |
| 1147 |
| 1148 TemplateString _clone() => new TemplateString(elements); |
| 1149 |
| 1150 int get precedenceLevel => PRIMARY; |
| 1151 } |
| 1152 |
| 1153 // TODO(jmesserly): parser does not support this yet. |
| 1154 class TaggedTemplate extends Expression { |
| 1155 final Expression tag; |
| 1156 final TemplateString template; |
| 1157 |
| 1158 TaggedTemplate(this.tag, this.template); |
| 1159 |
| 1160 accept(NodeVisitor visitor) => visitor.visitTaggedTemplate(this); |
| 1161 |
| 1162 void visitChildren(NodeVisitor visitor) { |
| 1163 tag.accept(visitor); |
| 1164 template.accept(visitor); |
| 1165 } |
| 1166 |
| 1167 TaggedTemplate _clone() => new TaggedTemplate(tag, template); |
| 1168 |
| 1169 int get precedenceLevel => CALL; |
| 1170 } |
| 1171 |
| 1172 class ClassDeclaration extends Statement { |
| 1173 final ClassExpression classExpr; |
| 1174 |
| 1175 ClassDeclaration(this.classExpr); |
| 1176 |
| 1177 accept(NodeVisitor visitor) => visitor.visitClassDeclaration(this); |
| 1178 visitChildren(NodeVisitor visitor) => classExpr.accept(visitor); |
| 1179 ClassDeclaration _clone() => new ClassDeclaration(classExpr); |
| 1180 } |
| 1181 |
| 1182 class ClassExpression extends Expression { |
| 1183 final VariableDeclaration name; |
| 1184 final Expression heritage; // Can be null. |
| 1185 final List<Method> methods; |
| 1186 |
| 1187 ClassExpression(this.name, this.heritage, this.methods); |
| 1188 |
| 1189 accept(NodeVisitor visitor) => visitor.visitClassExpression(this); |
| 1190 |
| 1191 void visitChildren(NodeVisitor visitor) { |
| 1192 name.accept(visitor); |
| 1193 heritage.accept(visitor); |
| 1194 for (Method element in methods) element.accept(visitor); |
| 1195 } |
| 1196 |
| 1197 ClassExpression _clone() => new ClassExpression(name, heritage, methods); |
| 1198 |
| 1199 int get precedenceLevel => PRIMARY; |
| 1200 } |
| 1201 |
| 1202 class Method extends Property { |
| 1203 final bool isGetter; |
| 1204 final bool isSetter; |
| 1205 final bool isStatic; |
| 1206 |
| 1207 Method(Expression name, Fun function, |
| 1208 {this.isGetter: false, this.isSetter: false, this.isStatic: false}) |
| 1209 : super(name, function); |
| 1210 |
| 1211 Fun get function => super.value; |
| 1212 |
| 1213 accept(NodeVisitor visitor) => visitor.visitMethod(this); |
| 1214 |
| 1215 void visitChildren(NodeVisitor visitor) { |
| 1216 name.accept(visitor); |
| 1217 function.accept(visitor); |
| 1218 } |
| 1219 |
| 1220 Method _clone() => new Method(name, function, |
| 1221 isGetter: isGetter, isSetter: isSetter, isStatic: isStatic); |
| 1222 } |
| 1223 |
1027 /// Tag class for all interpolated positions. | 1224 /// Tag class for all interpolated positions. |
1028 abstract class InterpolatedNode implements Node { | 1225 abstract class InterpolatedNode implements Node { |
1029 get nameOrPosition; | 1226 get nameOrPosition; |
1030 | 1227 |
1031 bool get isNamed => nameOrPosition is String; | 1228 bool get isNamed => nameOrPosition is String; |
1032 bool get isPositional => nameOrPosition is int; | 1229 bool get isPositional => nameOrPosition is int; |
1033 } | 1230 } |
1034 | 1231 |
1035 class InterpolatedExpression extends Expression with InterpolatedNode { | 1232 class InterpolatedExpression extends Expression with InterpolatedNode { |
1036 final nameOrPosition; | 1233 final nameOrPosition; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1086 class InterpolatedStatement extends Statement with InterpolatedNode { | 1283 class InterpolatedStatement extends Statement with InterpolatedNode { |
1087 final nameOrPosition; | 1284 final nameOrPosition; |
1088 | 1285 |
1089 InterpolatedStatement(this.nameOrPosition); | 1286 InterpolatedStatement(this.nameOrPosition); |
1090 | 1287 |
1091 accept(NodeVisitor visitor) => visitor.visitInterpolatedStatement(this); | 1288 accept(NodeVisitor visitor) => visitor.visitInterpolatedStatement(this); |
1092 void visitChildren(NodeVisitor visitor) {} | 1289 void visitChildren(NodeVisitor visitor) {} |
1093 InterpolatedStatement _clone() => new InterpolatedStatement(nameOrPosition); | 1290 InterpolatedStatement _clone() => new InterpolatedStatement(nameOrPosition); |
1094 } | 1291 } |
1095 | 1292 |
| 1293 // TODO(jmesserly): generalize this to InterpolatedProperty? |
| 1294 class InterpolatedMethod extends Expression with InterpolatedNode |
| 1295 implements Method { |
| 1296 final nameOrPosition; |
| 1297 |
| 1298 InterpolatedMethod(this.nameOrPosition); |
| 1299 |
| 1300 accept(NodeVisitor visitor) => visitor.visitInterpolatedMethod(this); |
| 1301 void visitChildren(NodeVisitor visitor) {} |
| 1302 InterpolatedMethod _clone() => new InterpolatedMethod(nameOrPosition); |
| 1303 |
| 1304 int get precedenceLevel => PRIMARY; |
| 1305 Expression get name => _unsupported; |
| 1306 Expression get value => _unsupported; |
| 1307 bool get isGetter => _unsupported; |
| 1308 bool get isSetter => _unsupported; |
| 1309 bool get isStatic => _unsupported; |
| 1310 Fun get function => _unsupported; |
| 1311 get _unsupported => throw '$runtimeType does not support this member.'; |
| 1312 } |
| 1313 |
| 1314 class InterpolatedPropertyName extends Expression with InterpolatedNode |
| 1315 implements PropertyName { |
| 1316 final nameOrPosition; |
| 1317 |
| 1318 InterpolatedPropertyName(this.nameOrPosition); |
| 1319 |
| 1320 accept(NodeVisitor visitor) => visitor.visitInterpolatedPropertyName(this); |
| 1321 void visitChildren(NodeVisitor visitor) {} |
| 1322 InterpolatedPropertyName _clone() => |
| 1323 new InterpolatedPropertyName(nameOrPosition); |
| 1324 |
| 1325 int get precedenceLevel => PRIMARY; |
| 1326 String get name => throw '$runtimeType does not support this member.'; |
| 1327 bool get allowRename => false; |
| 1328 } |
| 1329 |
| 1330 class InterpolatedVariableDeclaration extends Expression with InterpolatedNode |
| 1331 implements VariableDeclaration { |
| 1332 final nameOrPosition; |
| 1333 |
| 1334 InterpolatedVariableDeclaration(this.nameOrPosition); |
| 1335 |
| 1336 accept(NodeVisitor visitor) => |
| 1337 visitor.visitInterpolatedVariableDeclaration(this); |
| 1338 void visitChildren(NodeVisitor visitor) {} |
| 1339 InterpolatedVariableDeclaration _clone() => |
| 1340 new InterpolatedVariableDeclaration(nameOrPosition); |
| 1341 |
| 1342 int get precedenceLevel => PRIMARY; |
| 1343 String get name => throw '$runtimeType does not support this member.'; |
| 1344 bool get allowRename => false; |
| 1345 } |
| 1346 |
1096 /** | 1347 /** |
1097 * [RegExpLiteral]s, despite being called "Literal", do not inherit from | 1348 * [RegExpLiteral]s, despite being called "Literal", do not inherit from |
1098 * [Literal]. Indeed, regular expressions in JavaScript have a side-effect and | 1349 * [Literal]. Indeed, regular expressions in JavaScript have a side-effect and |
1099 * are thus not in the same category as numbers or strings. | 1350 * are thus not in the same category as numbers or strings. |
1100 */ | 1351 */ |
1101 class RegExpLiteral extends Expression { | 1352 class RegExpLiteral extends Expression { |
1102 /** Contains the pattern and the flags.*/ | 1353 /** Contains the pattern and the flags.*/ |
1103 final String pattern; | 1354 final String pattern; |
1104 | 1355 |
1105 RegExpLiteral(this.pattern); | 1356 RegExpLiteral(this.pattern); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1139 class Comment extends Statement { | 1390 class Comment extends Statement { |
1140 final String comment; | 1391 final String comment; |
1141 | 1392 |
1142 Comment(this.comment); | 1393 Comment(this.comment); |
1143 | 1394 |
1144 accept(NodeVisitor visitor) => visitor.visitComment(this); | 1395 accept(NodeVisitor visitor) => visitor.visitComment(this); |
1145 Comment _clone() => new Comment(comment); | 1396 Comment _clone() => new Comment(comment); |
1146 | 1397 |
1147 void visitChildren(NodeVisitor visitor) {} | 1398 void visitChildren(NodeVisitor visitor) {} |
1148 } | 1399 } |
| 1400 |
| 1401 /** |
| 1402 * A comment for expressions. |
| 1403 * |
| 1404 * Extends [Expression] so we can add comments before expressions. |
| 1405 * Has the highest possible precedence, so we don't add parentheses around it. |
| 1406 */ |
| 1407 class CommentExpression extends Expression { |
| 1408 final String comment; |
| 1409 final Expression expression; |
| 1410 |
| 1411 CommentExpression(this.comment, this.expression); |
| 1412 |
| 1413 int get precedenceLevel => PRIMARY; |
| 1414 accept(NodeVisitor visitor) => visitor.visitCommentExpression(this); |
| 1415 CommentExpression _clone() => new CommentExpression(comment, expression); |
| 1416 |
| 1417 void visitChildren(NodeVisitor visitor) => expression.accept(visitor); |
| 1418 } |
OLD | NEW |