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); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 T visitComment(Comment node); | 81 T visitComment(Comment node); |
82 T visitCommentExpression(CommentExpression node); | 82 T visitCommentExpression(CommentExpression node); |
83 | 83 |
84 T visitInterpolatedExpression(InterpolatedExpression node); | 84 T visitInterpolatedExpression(InterpolatedExpression node); |
85 T visitInterpolatedLiteral(InterpolatedLiteral node); | 85 T visitInterpolatedLiteral(InterpolatedLiteral node); |
86 T visitInterpolatedParameter(InterpolatedParameter node); | 86 T visitInterpolatedParameter(InterpolatedParameter node); |
87 T visitInterpolatedSelector(InterpolatedSelector node); | 87 T visitInterpolatedSelector(InterpolatedSelector node); |
88 T visitInterpolatedStatement(InterpolatedStatement node); | 88 T visitInterpolatedStatement(InterpolatedStatement node); |
89 T visitInterpolatedMethod(InterpolatedMethod node); | 89 T visitInterpolatedMethod(InterpolatedMethod node); |
90 T visitInterpolatedIdentifier(InterpolatedIdentifier node); | 90 T visitInterpolatedIdentifier(InterpolatedIdentifier node); |
| 91 |
| 92 T visitArrayBindingPattern(ArrayBindingPattern node); |
| 93 T visitObjectBindingPattern(ObjectBindingPattern node); |
| 94 T visitDestructuredVariable(DestructuredVariable node); |
91 } | 95 } |
92 | 96 |
93 class BaseVisitor<T> implements NodeVisitor<T> { | 97 class BaseVisitor<T> implements NodeVisitor<T> { |
94 T visitNode(Node node) { | 98 T visitNode(Node node) { |
95 node.visitChildren(this); | 99 node.visitChildren(this); |
96 return null; | 100 return null; |
97 } | 101 } |
98 | 102 |
99 T visitProgram(Program node) => visitNode(node); | 103 T visitProgram(Program node) => visitNode(node); |
100 | 104 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 => visitInterpolatedNode(node); | 207 => visitInterpolatedNode(node); |
204 T visitInterpolatedIdentifier(InterpolatedIdentifier node) | 208 T visitInterpolatedIdentifier(InterpolatedIdentifier node) |
205 => visitInterpolatedNode(node); | 209 => visitInterpolatedNode(node); |
206 | 210 |
207 // Ignore comments by default. | 211 // Ignore comments by default. |
208 T visitComment(Comment node) => null; | 212 T visitComment(Comment node) => null; |
209 T visitCommentExpression(CommentExpression node) => null; | 213 T visitCommentExpression(CommentExpression node) => null; |
210 | 214 |
211 T visitAwait(Await node) => visitExpression(node); | 215 T visitAwait(Await node) => visitExpression(node); |
212 T visitDartYield(DartYield node) => visitStatement(node); | 216 T visitDartYield(DartYield node) => visitStatement(node); |
| 217 |
| 218 T visitBindingPattern(BindingPattern node) => visitNode(node); |
| 219 T visitArrayBindingPattern(ArrayBindingPattern node) |
| 220 => visitBindingPattern(node); |
| 221 T visitObjectBindingPattern(ObjectBindingPattern node) |
| 222 => visitBindingPattern(node); |
| 223 T visitDestructuredVariable(DestructuredVariable node) => visitNode(node); |
213 } | 224 } |
214 | 225 |
215 abstract class Node { | 226 abstract class Node { |
216 /// Sets the source location of this node. For performance reasons, we allow | 227 /// Sets the source location of this node. For performance reasons, we allow |
217 /// setting this after construction. | 228 /// setting this after construction. |
218 Object sourceInformation; | 229 Object sourceInformation; |
219 | 230 |
220 ClosureAnnotation _closureAnnotation; | 231 ClosureAnnotation _closureAnnotation; |
221 /// Closure annotation of this node. | 232 /// Closure annotation of this node. |
222 ClosureAnnotation get closureAnnotation => _closureAnnotation; | 233 ClosureAnnotation get closureAnnotation => _closureAnnotation; |
223 | 234 |
224 accept(NodeVisitor visitor); | 235 accept(NodeVisitor visitor); |
225 void visitChildren(NodeVisitor visitor); | 236 void visitChildren(NodeVisitor visitor); |
226 | 237 |
227 // Shallow clone of node. Does not clone positions since the only use of this | 238 // Shallow clone of node. Does not clone positions since the only use of this |
228 // private method is create a copy with a new position. | 239 // private method is create a copy with a new position. |
229 Node _clone(); | 240 Node _clone(); |
230 | 241 |
231 withClosureAnnotation(ClosureAnnotation closureAnnotation) { | 242 withClosureAnnotation(ClosureAnnotation closureAnnotation) { |
232 if (this.closureAnnotation == closureAnnotation) return this; | 243 if (this.closureAnnotation == closureAnnotation) return this; |
233 | 244 |
234 return _clone() | 245 return _clone() |
235 ..sourceInformation = sourceInformation | 246 ..sourceInformation = sourceInformation |
236 .._closureAnnotation = closureAnnotation; | 247 .._closureAnnotation = closureAnnotation; |
237 } | 248 } |
238 // Returns a node equivalent to [this], but with new source position and end | 249 // Returns a node equivalent to [this], but with new source position and end |
239 // source position. | 250 // source position. |
240 Node withSourceInformation(sourceInformation) { | 251 Node withSourceInformation(sourceInformation) { |
241 if (sourceInformation == this.sourceInformation) { | 252 if (sourceInformation == this.sourceInformation) { |
242 return this; | 253 return this; |
243 } | 254 } |
(...skipping 500 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
744 leftHandSide.accept(visitor); | 755 leftHandSide.accept(visitor); |
745 if (value != null) value.accept(visitor); | 756 if (value != null) value.accept(visitor); |
746 } | 757 } |
747 | 758 |
748 Assignment _clone() => | 759 Assignment _clone() => |
749 new Assignment.compound(leftHandSide, op, value); | 760 new Assignment.compound(leftHandSide, op, value); |
750 } | 761 } |
751 | 762 |
752 class VariableInitialization extends Assignment { | 763 class VariableInitialization extends Assignment { |
753 /** [value] may be null. */ | 764 /** [value] may be null. */ |
754 VariableInitialization(Identifier declaration, Expression value) | 765 VariableInitialization(VariableBinding declaration, Expression value) |
755 : super(declaration, value); | 766 : super(declaration, value); |
756 | 767 |
757 Identifier get declaration => leftHandSide; | 768 VariableBinding get declaration => leftHandSide; |
758 | 769 |
759 accept(NodeVisitor visitor) => visitor.visitVariableInitialization(this); | 770 accept(NodeVisitor visitor) => visitor.visitVariableInitialization(this); |
760 | 771 |
761 VariableInitialization _clone() => | 772 VariableInitialization _clone() => |
762 new VariableInitialization(declaration, value); | 773 new VariableInitialization(declaration, value); |
763 } | 774 } |
764 | 775 |
| 776 abstract class VariableBinding extends Expression { |
| 777 } |
| 778 |
| 779 class DestructuredVariable extends Expression implements Parameter { |
| 780 final Identifier name; |
| 781 final BindingPattern structure; |
| 782 final Expression defaultValue; |
| 783 DestructuredVariable({this.name, this.structure, this.defaultValue}) { |
| 784 assert(name != null || structure != null); |
| 785 } |
| 786 |
| 787 accept(NodeVisitor visitor) => visitor.visitDestructuredVariable(this); |
| 788 void visitChildren(NodeVisitor visitor) { |
| 789 name?.accept(visitor); |
| 790 structure?.accept(visitor); |
| 791 defaultValue?.accept(visitor); |
| 792 } |
| 793 |
| 794 /// Avoid parenthesis when pretty-printing. |
| 795 @override int get precedenceLevel => PRIMARY; |
| 796 @override Node _clone() => |
| 797 new DestructuredVariable( |
| 798 name: name, structure: structure, defaultValue: defaultValue); |
| 799 } |
| 800 |
| 801 abstract class BindingPattern extends Expression implements VariableBinding { |
| 802 final List<DestructuredVariable> variables; |
| 803 BindingPattern(this.variables); |
| 804 |
| 805 void visitChildren(NodeVisitor visitor) { |
| 806 for (DestructuredVariable v in variables) v.accept(visitor); |
| 807 } |
| 808 } |
| 809 |
| 810 class ObjectBindingPattern extends BindingPattern { |
| 811 ObjectBindingPattern(List<DestructuredVariable> variables) |
| 812 : super(variables); |
| 813 accept(NodeVisitor visitor) => visitor.visitObjectBindingPattern(this); |
| 814 |
| 815 /// Avoid parenthesis when pretty-printing. |
| 816 @override int get precedenceLevel => PRIMARY; |
| 817 @override Node _clone() => new ObjectBindingPattern(variables); |
| 818 } |
| 819 |
| 820 class ArrayBindingPattern extends BindingPattern { |
| 821 ArrayBindingPattern(List<DestructuredVariable> variables) |
| 822 : super(variables); |
| 823 accept(NodeVisitor visitor) => visitor.visitArrayBindingPattern(this); |
| 824 |
| 825 /// Avoid parenthesis when pretty-printing. |
| 826 @override int get precedenceLevel => PRIMARY; |
| 827 @override Node _clone() => new ObjectBindingPattern(variables); |
| 828 } |
| 829 |
765 class Conditional extends Expression { | 830 class Conditional extends Expression { |
766 final Expression condition; | 831 final Expression condition; |
767 final Expression then; | 832 final Expression then; |
768 final Expression otherwise; | 833 final Expression otherwise; |
769 | 834 |
770 Conditional(this.condition, this.then, this.otherwise); | 835 Conditional(this.condition, this.then, this.otherwise); |
771 | 836 |
772 accept(NodeVisitor visitor) => visitor.visitConditional(this); | 837 accept(NodeVisitor visitor) => visitor.visitConditional(this); |
773 | 838 |
774 void visitChildren(NodeVisitor visitor) { | 839 void visitChildren(NodeVisitor visitor) { |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
923 Prefix _clone() => new Prefix(op, argument); | 988 Prefix _clone() => new Prefix(op, argument); |
924 | 989 |
925 void visitChildren(NodeVisitor visitor) { | 990 void visitChildren(NodeVisitor visitor) { |
926 argument.accept(visitor); | 991 argument.accept(visitor); |
927 } | 992 } |
928 | 993 |
929 int get precedenceLevel => UNARY; | 994 int get precedenceLevel => UNARY; |
930 } | 995 } |
931 | 996 |
932 // SpreadElement isn't really a prefix expression, as it can only appear in | 997 // SpreadElement isn't really a prefix expression, as it can only appear in |
933 // certain places such as ArgumentList and destructuring, but we pretend | 998 // certain places such as ArgumentList and BindingPattern, but we pretend |
934 // it is for simplicity's sake. | 999 // it is for simplicity's sake. |
935 class Spread extends Prefix { | 1000 class Spread extends Prefix { |
936 Spread(Expression operand) : super('...', operand); | 1001 Spread(Expression operand) : super('...', operand); |
937 int get precedenceLevel => SPREAD; | 1002 int get precedenceLevel => SPREAD; |
938 | 1003 |
939 accept(NodeVisitor visitor) => visitor.visitSpread(this); | 1004 accept(NodeVisitor visitor) => visitor.visitSpread(this); |
940 Spread _clone() => new Spread(argument); | 1005 Spread _clone() => new Spread(argument); |
941 } | 1006 } |
942 | 1007 |
943 class Postfix extends Expression { | 1008 class Postfix extends Expression { |
944 final String op; | 1009 final String op; |
945 final Expression argument; | 1010 final Expression argument; |
946 | 1011 |
947 Postfix(this.op, this.argument); | 1012 Postfix(this.op, this.argument); |
948 | 1013 |
949 accept(NodeVisitor visitor) => visitor.visitPostfix(this); | 1014 accept(NodeVisitor visitor) => visitor.visitPostfix(this); |
950 | 1015 |
951 Postfix _clone() => new Postfix(op, argument); | 1016 Postfix _clone() => new Postfix(op, argument); |
952 | 1017 |
953 void visitChildren(NodeVisitor visitor) { | 1018 void visitChildren(NodeVisitor visitor) { |
954 argument.accept(visitor); | 1019 argument.accept(visitor); |
955 } | 1020 } |
956 | 1021 |
957 int get precedenceLevel => UNARY; | 1022 int get precedenceLevel => UNARY; |
958 } | 1023 } |
959 | 1024 |
960 abstract class Parameter implements Expression {} | 1025 abstract class Parameter implements Expression, VariableBinding {} |
961 | 1026 |
962 class Identifier extends Expression implements Parameter { | 1027 class Identifier extends Expression implements Parameter, VariableBinding { |
963 final String name; | 1028 final String name; |
964 final bool allowRename; | 1029 final bool allowRename; |
965 | 1030 |
966 Identifier(this.name, {this.allowRename: true}) { | 1031 Identifier(this.name, {this.allowRename: true}) { |
967 assert(_identifierRE.hasMatch(name)); | 1032 assert(_identifierRE.hasMatch(name)); |
968 } | 1033 } |
969 static RegExp _identifierRE = new RegExp(r'^[A-Za-z_$][A-Za-z_$0-9]*$'); | 1034 static RegExp _identifierRE = new RegExp(r'^[A-Za-z_$][A-Za-z_$0-9]*$'); |
970 | 1035 |
971 Identifier _clone() => | 1036 Identifier _clone() => |
972 new Identifier(name, allowRename: allowRename); | 1037 new Identifier(name, allowRename: allowRename); |
(...skipping 725 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1698 | 1763 |
1699 final List<ModuleItem> body; | 1764 final List<ModuleItem> body; |
1700 Module(this.body, {this.name}); | 1765 Module(this.body, {this.name}); |
1701 | 1766 |
1702 accept(NodeVisitor visitor) => visitor.visitModule(this); | 1767 accept(NodeVisitor visitor) => visitor.visitModule(this); |
1703 void visitChildren(NodeVisitor visitor) { | 1768 void visitChildren(NodeVisitor visitor) { |
1704 for (ModuleItem item in body) item.accept(visitor); | 1769 for (ModuleItem item in body) item.accept(visitor); |
1705 } | 1770 } |
1706 Module _clone() => new Module(body); | 1771 Module _clone() => new Module(body); |
1707 } | 1772 } |
OLD | NEW |