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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
75 T visitComment(Comment node); | 75 T visitComment(Comment node); |
76 T visitCommentExpression(CommentExpression node); | 76 T visitCommentExpression(CommentExpression node); |
77 | 77 |
78 T visitInterpolatedExpression(InterpolatedExpression node); | 78 T visitInterpolatedExpression(InterpolatedExpression node); |
79 T visitInterpolatedLiteral(InterpolatedLiteral node); | 79 T visitInterpolatedLiteral(InterpolatedLiteral node); |
80 T visitInterpolatedParameter(InterpolatedParameter node); | 80 T visitInterpolatedParameter(InterpolatedParameter node); |
81 T visitInterpolatedSelector(InterpolatedSelector node); | 81 T visitInterpolatedSelector(InterpolatedSelector node); |
82 T visitInterpolatedStatement(InterpolatedStatement node); | 82 T visitInterpolatedStatement(InterpolatedStatement node); |
83 T visitInterpolatedMethod(InterpolatedMethod node); | 83 T visitInterpolatedMethod(InterpolatedMethod node); |
84 T visitInterpolatedIdentifier(InterpolatedIdentifier node); | 84 T visitInterpolatedIdentifier(InterpolatedIdentifier node); |
85 | |
86 T visitArrayDestructuring(ArrayDestructuring node); | |
87 T visitObjectDestructuring(ObjectDestructuring node); | |
88 T visitDestructuredVariable(DestructuredVariable node); | |
85 } | 89 } |
86 | 90 |
87 class BaseVisitor<T> implements NodeVisitor<T> { | 91 class BaseVisitor<T> implements NodeVisitor<T> { |
88 T visitNode(Node node) { | 92 T visitNode(Node node) { |
89 node.visitChildren(this); | 93 node.visitChildren(this); |
90 return null; | 94 return null; |
91 } | 95 } |
92 | 96 |
93 T visitProgram(Program node) => visitNode(node); | 97 T visitProgram(Program node) => visitNode(node); |
94 | 98 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
190 => visitInterpolatedNode(node); | 194 => visitInterpolatedNode(node); |
191 T visitInterpolatedIdentifier(InterpolatedIdentifier node) | 195 T visitInterpolatedIdentifier(InterpolatedIdentifier node) |
192 => visitInterpolatedNode(node); | 196 => visitInterpolatedNode(node); |
193 | 197 |
194 // Ignore comments by default. | 198 // Ignore comments by default. |
195 T visitComment(Comment node) => null; | 199 T visitComment(Comment node) => null; |
196 T visitCommentExpression(CommentExpression node) => null; | 200 T visitCommentExpression(CommentExpression node) => null; |
197 | 201 |
198 T visitAwait(Await node) => visitExpression(node); | 202 T visitAwait(Await node) => visitExpression(node); |
199 T visitDartYield(DartYield node) => visitStatement(node); | 203 T visitDartYield(DartYield node) => visitStatement(node); |
204 | |
205 T visitDestructuring(Destructuring node) => visitNode(node); | |
206 T visitArrayDestructuring(ArrayDestructuring node) | |
207 => visitDestructuring(node); | |
208 T visitObjectDestructuring(ObjectDestructuring node) | |
209 => visitDestructuring(node); | |
210 T visitDestructuredVariable(DestructuredVariable node) => visitNode(node); | |
200 } | 211 } |
201 | 212 |
202 abstract class Node { | 213 abstract class Node { |
203 /// Sets the source location of this node. For performance reasons, we allow | 214 /// Sets the source location of this node. For performance reasons, we allow |
204 /// setting this after construction. | 215 /// setting this after construction. |
205 Object sourceInformation; | 216 Object sourceInformation; |
206 | 217 |
207 ClosureAnnotation _closureAnnotation; | 218 ClosureAnnotation _closureAnnotation; |
208 /// Closure annotation of this node. | 219 /// Closure annotation of this node. |
209 ClosureAnnotation get closureAnnotation => _closureAnnotation; | 220 ClosureAnnotation get closureAnnotation => _closureAnnotation; |
210 | 221 |
211 accept(NodeVisitor visitor); | 222 accept(NodeVisitor visitor); |
212 void visitChildren(NodeVisitor visitor); | 223 void visitChildren(NodeVisitor visitor); |
213 | 224 |
214 // Shallow clone of node. Does not clone positions since the only use of this | 225 // Shallow clone of node. Does not clone positions since the only use of this |
215 // private method is create a copy with a new position. | 226 // private method is create a copy with a new position. |
216 Node _clone(); | 227 Node _clone(); |
217 | 228 |
218 withClosureAnnotation(ClosureAnnotation closureAnnotation) { | 229 withClosureAnnotation(ClosureAnnotation closureAnnotation) { |
219 if (this.closureAnnotation == closureAnnotation) return this; | 230 if (this.closureAnnotation == closureAnnotation) return this; |
220 | 231 |
221 return _clone() | 232 return _clone() |
222 ..sourceInformation = sourceInformation | 233 ..sourceInformation = sourceInformation |
223 .._closureAnnotation = closureAnnotation; | 234 .._closureAnnotation = closureAnnotation; |
224 } | 235 } |
225 // Returns a node equivalent to [this], but with new source position and end | 236 // Returns a node equivalent to [this], but with new source position and end |
226 // source position. | 237 // source position. |
227 Node withSourceInformation(sourceInformation) { | 238 Node withSourceInformation(sourceInformation) { |
228 if (sourceInformation == this.sourceInformation) { | 239 if (sourceInformation == this.sourceInformation) { |
229 return this; | 240 return this; |
230 } | 241 } |
(...skipping 500 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
731 leftHandSide.accept(visitor); | 742 leftHandSide.accept(visitor); |
732 if (value != null) value.accept(visitor); | 743 if (value != null) value.accept(visitor); |
733 } | 744 } |
734 | 745 |
735 Assignment _clone() => | 746 Assignment _clone() => |
736 new Assignment.compound(leftHandSide, op, value); | 747 new Assignment.compound(leftHandSide, op, value); |
737 } | 748 } |
738 | 749 |
739 class VariableInitialization extends Assignment { | 750 class VariableInitialization extends Assignment { |
740 /** [value] may be null. */ | 751 /** [value] may be null. */ |
741 VariableInitialization(Identifier declaration, Expression value) | 752 VariableInitialization(Declarator declaration, Expression value) |
742 : super(declaration, value); | 753 : super(declaration, value); |
743 | 754 |
744 Identifier get declaration => leftHandSide; | 755 Declarator get declaration => leftHandSide; |
745 | 756 |
746 accept(NodeVisitor visitor) => visitor.visitVariableInitialization(this); | 757 accept(NodeVisitor visitor) => visitor.visitVariableInitialization(this); |
747 | 758 |
748 VariableInitialization _clone() => | 759 VariableInitialization _clone() => |
749 new VariableInitialization(declaration, value); | 760 new VariableInitialization(declaration, value); |
750 } | 761 } |
751 | 762 |
763 abstract class Declarator extends Expression { | |
Jennifer Messerly
2015/12/01 02:10:27
The ES6 spec calls this a "Binding", as in Binding
ochafik
2015/12/02 20:05:46
Done (thank you so much for all these links, I fee
| |
764 } | |
765 | |
766 class DestructuredVariable extends Expression implements Parameter { | |
767 final Identifier name; | |
768 final Destructuring structure; | |
769 final Expression defaultValue; | |
770 DestructuredVariable({this.name, this.structure, this.defaultValue}); | |
771 | |
772 accept(NodeVisitor visitor) => visitor.visitDestructuredVariable(this); | |
773 void visitChildren(NodeVisitor visitor) { | |
774 name?.accept(visitor); | |
775 structure?.accept(visitor); | |
776 defaultValue?.accept(visitor); | |
777 } | |
778 | |
779 @override int get precedenceLevel => DESTRUCTURING; | |
780 @override Node _clone() => | |
781 new DestructuredVariable( | |
782 name: name, structure: structure, defaultValue: defaultValue); | |
783 } | |
784 | |
785 abstract class Destructuring extends Expression implements Declarator { | |
786 final List<DestructuredVariable> variables; | |
787 Destructuring(this.variables); | |
788 | |
789 void visitChildren(NodeVisitor visitor) { | |
790 for (DestructuredVariable v in variables) v.accept(visitor); | |
791 } | |
792 } | |
793 | |
794 class ObjectDestructuring extends Destructuring { | |
795 ObjectDestructuring(List<DestructuredVariable> variables) | |
796 : super(variables); | |
797 accept(NodeVisitor visitor) => visitor.visitObjectDestructuring(this); | |
798 | |
799 @override int get precedenceLevel => DESTRUCTURING; | |
800 @override Node _clone() => new ObjectDestructuring(variables); | |
801 } | |
802 | |
803 class ArrayDestructuring extends Destructuring { | |
804 ArrayDestructuring(List<DestructuredVariable> variables) | |
805 : super(variables); | |
806 accept(NodeVisitor visitor) => visitor.visitArrayDestructuring(this); | |
807 | |
808 @override int get precedenceLevel => DESTRUCTURING; | |
809 @override Node _clone() => new ObjectDestructuring(variables); | |
810 } | |
811 | |
752 class Conditional extends Expression { | 812 class Conditional extends Expression { |
753 final Expression condition; | 813 final Expression condition; |
754 final Expression then; | 814 final Expression then; |
755 final Expression otherwise; | 815 final Expression otherwise; |
756 | 816 |
757 Conditional(this.condition, this.then, this.otherwise); | 817 Conditional(this.condition, this.then, this.otherwise); |
758 | 818 |
759 accept(NodeVisitor visitor) => visitor.visitConditional(this); | 819 accept(NodeVisitor visitor) => visitor.visitConditional(this); |
760 | 820 |
761 void visitChildren(NodeVisitor visitor) { | 821 void visitChildren(NodeVisitor visitor) { |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
937 | 997 |
938 Postfix _clone() => new Postfix(op, argument); | 998 Postfix _clone() => new Postfix(op, argument); |
939 | 999 |
940 void visitChildren(NodeVisitor visitor) { | 1000 void visitChildren(NodeVisitor visitor) { |
941 argument.accept(visitor); | 1001 argument.accept(visitor); |
942 } | 1002 } |
943 | 1003 |
944 int get precedenceLevel => UNARY; | 1004 int get precedenceLevel => UNARY; |
945 } | 1005 } |
946 | 1006 |
947 abstract class Parameter implements Expression {} | 1007 abstract class Parameter implements Expression, Declarator {} |
948 | 1008 |
949 class Identifier extends Expression implements Parameter { | 1009 class Identifier extends Expression implements Parameter, Declarator { |
950 final String name; | 1010 final String name; |
951 final bool allowRename; | 1011 final bool allowRename; |
952 | 1012 |
953 Identifier(this.name, {this.allowRename: true}) { | 1013 Identifier(this.name, {this.allowRename: true}) { |
954 assert(_identifierRE.hasMatch(name)); | 1014 assert(_identifierRE.hasMatch(name)); |
955 } | 1015 } |
956 static RegExp _identifierRE = new RegExp(r'^[A-Za-z_$][A-Za-z_$0-9]*$'); | 1016 static RegExp _identifierRE = new RegExp(r'^[A-Za-z_$][A-Za-z_$0-9]*$'); |
957 | 1017 |
958 Identifier _clone() => | 1018 Identifier _clone() => |
959 new Identifier(name, allowRename: allowRename); | 1019 new Identifier(name, allowRename: allowRename); |
(...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1567 final Expression expression; | 1627 final Expression expression; |
1568 | 1628 |
1569 CommentExpression(this.comment, this.expression); | 1629 CommentExpression(this.comment, this.expression); |
1570 | 1630 |
1571 int get precedenceLevel => PRIMARY; | 1631 int get precedenceLevel => PRIMARY; |
1572 accept(NodeVisitor visitor) => visitor.visitCommentExpression(this); | 1632 accept(NodeVisitor visitor) => visitor.visitCommentExpression(this); |
1573 CommentExpression _clone() => new CommentExpression(comment, expression); | 1633 CommentExpression _clone() => new CommentExpression(comment, expression); |
1574 | 1634 |
1575 void visitChildren(NodeVisitor visitor) => expression.accept(visitor); | 1635 void visitChildren(NodeVisitor visitor) => expression.accept(visitor); |
1576 } | 1636 } |
OLD | NEW |