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 22 matching lines...) Expand all Loading... |
33 T visitLiteralExpression(LiteralExpression node); | 33 T visitLiteralExpression(LiteralExpression node); |
34 T visitVariableDeclarationList(VariableDeclarationList node); | 34 T visitVariableDeclarationList(VariableDeclarationList node); |
35 T visitAssignment(Assignment node); | 35 T visitAssignment(Assignment node); |
36 T visitVariableInitialization(VariableInitialization node); | 36 T visitVariableInitialization(VariableInitialization node); |
37 T visitConditional(Conditional cond); | 37 T visitConditional(Conditional cond); |
38 T visitNew(New node); | 38 T visitNew(New node); |
39 T visitCall(Call node); | 39 T visitCall(Call node); |
40 T visitBinary(Binary node); | 40 T visitBinary(Binary node); |
41 T visitPrefix(Prefix node); | 41 T visitPrefix(Prefix node); |
42 T visitPostfix(Postfix node); | 42 T visitPostfix(Postfix node); |
| 43 T visitSpread(Spread node); |
43 | 44 |
44 T visitIdentifier(Identifier node); | 45 T visitIdentifier(Identifier node); |
45 T visitThis(This node); | 46 T visitThis(This node); |
46 T visitSuper(Super node); | 47 T visitSuper(Super node); |
47 T visitAccess(PropertyAccess node); | 48 T visitAccess(PropertyAccess node); |
| 49 T visitRestParameter(RestParameter node); |
48 | 50 |
49 T visitNamedFunction(NamedFunction node); | 51 T visitNamedFunction(NamedFunction node); |
50 T visitFun(Fun node); | 52 T visitFun(Fun node); |
51 T visitArrowFun(ArrowFun node); | 53 T visitArrowFun(ArrowFun node); |
52 | 54 |
53 T visitLiteralBool(LiteralBool node); | 55 T visitLiteralBool(LiteralBool node); |
54 T visitLiteralString(LiteralString node); | 56 T visitLiteralString(LiteralString node); |
55 T visitLiteralNumber(LiteralNumber node); | 57 T visitLiteralNumber(LiteralNumber node); |
56 T visitLiteralNull(LiteralNull node); | 58 T visitLiteralNull(LiteralNull node); |
57 | 59 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 } else { | 132 } else { |
131 return visitExpression(node); | 133 return visitExpression(node); |
132 } | 134 } |
133 } | 135 } |
134 T visitConditional(Conditional node) => visitExpression(node); | 136 T visitConditional(Conditional node) => visitExpression(node); |
135 T visitNew(New node) => visitExpression(node); | 137 T visitNew(New node) => visitExpression(node); |
136 T visitCall(Call node) => visitExpression(node); | 138 T visitCall(Call node) => visitExpression(node); |
137 T visitBinary(Binary node) => visitExpression(node); | 139 T visitBinary(Binary node) => visitExpression(node); |
138 T visitPrefix(Prefix node) => visitExpression(node); | 140 T visitPrefix(Prefix node) => visitExpression(node); |
139 T visitPostfix(Postfix node) => visitExpression(node); | 141 T visitPostfix(Postfix node) => visitExpression(node); |
| 142 T visitSpread(Spread node) => visitPrefix(node); |
140 T visitAccess(PropertyAccess node) => visitExpression(node); | 143 T visitAccess(PropertyAccess node) => visitExpression(node); |
141 | 144 |
142 T visitIdentifier(Identifier node) => visitExpression(node); | 145 T visitIdentifier(Identifier node) => visitExpression(node); |
143 T visitThis(This node) => visitExpression(node); | 146 T visitThis(This node) => visitExpression(node); |
144 T visitSuper(Super node) => visitExpression(node); | 147 T visitSuper(Super node) => visitExpression(node); |
145 | 148 |
| 149 T visitRestParameter(RestParameter node) => visitNode(node); |
| 150 |
146 T visitNamedFunction(NamedFunction node) => visitExpression(node); | 151 T visitNamedFunction(NamedFunction node) => visitExpression(node); |
147 T visitFunctionExpression(FunctionExpression node) => visitExpression(node); | 152 T visitFunctionExpression(FunctionExpression node) => visitExpression(node); |
148 T visitFun(Fun node) => visitFunctionExpression(node); | 153 T visitFun(Fun node) => visitFunctionExpression(node); |
149 T visitArrowFun(ArrowFun node) => visitFunctionExpression(node); | 154 T visitArrowFun(ArrowFun node) => visitFunctionExpression(node); |
150 | 155 |
151 T visitLiteral(Literal node) => visitExpression(node); | 156 T visitLiteral(Literal node) => visitExpression(node); |
152 | 157 |
153 T visitLiteralBool(LiteralBool node) => visitLiteral(node); | 158 T visitLiteralBool(LiteralBool node) => visitLiteral(node); |
154 T visitLiteralString(LiteralString node) => visitLiteral(node); | 159 T visitLiteralString(LiteralString node) => visitLiteral(node); |
155 T visitLiteralNumber(LiteralNumber node) => visitLiteral(node); | 160 T visitLiteralNumber(LiteralNumber node) => visitLiteral(node); |
(...skipping 701 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
857 | 862 |
858 Prefix _clone() => new Prefix(op, argument); | 863 Prefix _clone() => new Prefix(op, argument); |
859 | 864 |
860 void visitChildren(NodeVisitor visitor) { | 865 void visitChildren(NodeVisitor visitor) { |
861 argument.accept(visitor); | 866 argument.accept(visitor); |
862 } | 867 } |
863 | 868 |
864 int get precedenceLevel => UNARY; | 869 int get precedenceLevel => UNARY; |
865 } | 870 } |
866 | 871 |
| 872 // SpreadElement isn't really a prefix expression, as it can only appear in |
| 873 // certain places such as ArgumentList and destructuring, but we pretend |
| 874 // it is for simplicity's sake. |
| 875 class Spread extends Prefix { |
| 876 Spread(Expression operand) : super('...', operand); |
| 877 int get precedenceLevel => SPREAD; |
| 878 |
| 879 accept(NodeVisitor visitor) => visitor.visitSpread(this); |
| 880 Spread _clone() => new Spread(argument); |
| 881 } |
| 882 |
867 class Postfix extends Expression { | 883 class Postfix extends Expression { |
868 final String op; | 884 final String op; |
869 final Expression argument; | 885 final Expression argument; |
870 | 886 |
871 Postfix(this.op, this.argument); | 887 Postfix(this.op, this.argument); |
872 | 888 |
873 accept(NodeVisitor visitor) => visitor.visitPostfix(this); | 889 accept(NodeVisitor visitor) => visitor.visitPostfix(this); |
874 | 890 |
875 Postfix _clone() => new Postfix(op, argument); | 891 Postfix _clone() => new Postfix(op, argument); |
876 | 892 |
877 void visitChildren(NodeVisitor visitor) { | 893 void visitChildren(NodeVisitor visitor) { |
878 argument.accept(visitor); | 894 argument.accept(visitor); |
879 } | 895 } |
880 | 896 |
881 int get precedenceLevel => UNARY; | 897 int get precedenceLevel => UNARY; |
882 } | 898 } |
883 | 899 |
884 class Identifier extends Expression { | 900 abstract class Parameter implements Expression {} |
| 901 |
| 902 class Identifier extends Expression implements Parameter { |
885 final String name; | 903 final String name; |
886 final bool allowRename; | 904 final bool allowRename; |
887 | 905 |
888 Identifier(this.name, {this.allowRename: true}) { | 906 Identifier(this.name, {this.allowRename: true}) { |
889 assert(_identifierRE.hasMatch(name)); | 907 assert(_identifierRE.hasMatch(name)); |
890 } | 908 } |
891 static RegExp _identifierRE = new RegExp(r'^[A-Za-z_$][A-Za-z_$0-9]*$'); | 909 static RegExp _identifierRE = new RegExp(r'^[A-Za-z_$][A-Za-z_$0-9]*$'); |
892 | 910 |
893 Identifier _clone() => | 911 Identifier _clone() => |
894 new Identifier(name, allowRename: allowRename); | 912 new Identifier(name, allowRename: allowRename); |
895 accept(NodeVisitor visitor) => visitor.visitIdentifier(this); | 913 accept(NodeVisitor visitor) => visitor.visitIdentifier(this); |
896 int get precedenceLevel => PRIMARY; | 914 int get precedenceLevel => PRIMARY; |
897 void visitChildren(NodeVisitor visitor) {} | 915 void visitChildren(NodeVisitor visitor) {} |
898 } | 916 } |
899 | 917 |
| 918 // This is an expression for convenience in the AST. |
| 919 class RestParameter extends Expression implements Parameter { |
| 920 final Identifier parameter; |
| 921 |
| 922 RestParameter(this.parameter); |
| 923 |
| 924 RestParameter _clone() => new RestParameter(parameter); |
| 925 accept(NodeVisitor visitor) => visitor.visitRestParameter(this); |
| 926 void visitChildren(NodeVisitor visitor) { |
| 927 parameter.accept(visitor); |
| 928 } |
| 929 int get precedenceLevel => SPREAD; |
| 930 } |
| 931 |
900 class This extends Expression { | 932 class This extends Expression { |
901 accept(NodeVisitor visitor) => visitor.visitThis(this); | 933 accept(NodeVisitor visitor) => visitor.visitThis(this); |
902 This _clone() => new This(); | 934 This _clone() => new This(); |
903 int get precedenceLevel => PRIMARY; | 935 int get precedenceLevel => PRIMARY; |
904 void visitChildren(NodeVisitor visitor) {} | 936 void visitChildren(NodeVisitor visitor) {} |
905 } | 937 } |
906 | 938 |
907 // `super` is more restricted in the ES6 spec, but for simplicity we accept | 939 // `super` is more restricted in the ES6 spec, but for simplicity we accept |
908 // it anywhere that `this` is accepted. | 940 // it anywhere that `this` is accepted. |
909 class Super extends Expression { | 941 class Super extends Expression { |
(...skipping 14 matching lines...) Expand all Loading... |
924 void visitChildren(NodeVisitor visitor) { | 956 void visitChildren(NodeVisitor visitor) { |
925 name.accept(visitor); | 957 name.accept(visitor); |
926 function.accept(visitor); | 958 function.accept(visitor); |
927 } | 959 } |
928 NamedFunction _clone() => new NamedFunction(name, function); | 960 NamedFunction _clone() => new NamedFunction(name, function); |
929 | 961 |
930 int get precedenceLevel => CALL; | 962 int get precedenceLevel => CALL; |
931 } | 963 } |
932 | 964 |
933 abstract class FunctionExpression extends Expression { | 965 abstract class FunctionExpression extends Expression { |
934 List<Identifier> get params; | 966 List<Parameter> get params; |
935 get body; // Expression or block | 967 get body; // Expression or block |
936 } | 968 } |
937 | 969 |
938 class Fun extends FunctionExpression { | 970 class Fun extends FunctionExpression { |
939 final List<Identifier> params; | 971 final List<Parameter> params; |
940 final Block body; | 972 final Block body; |
941 final AsyncModifier asyncModifier; | 973 final AsyncModifier asyncModifier; |
942 | 974 |
943 Fun(this.params, this.body, {this.asyncModifier: const AsyncModifier.sync()}); | 975 Fun(this.params, this.body, {this.asyncModifier: const AsyncModifier.sync()}); |
944 | 976 |
945 accept(NodeVisitor visitor) => visitor.visitFun(this); | 977 accept(NodeVisitor visitor) => visitor.visitFun(this); |
946 | 978 |
947 void visitChildren(NodeVisitor visitor) { | 979 void visitChildren(NodeVisitor visitor) { |
948 for (Identifier param in params) param.accept(visitor); | 980 for (Parameter param in params) param.accept(visitor); |
949 body.accept(visitor); | 981 body.accept(visitor); |
950 } | 982 } |
951 | 983 |
952 Fun _clone() => new Fun(params, body, asyncModifier: asyncModifier); | 984 Fun _clone() => new Fun(params, body, asyncModifier: asyncModifier); |
953 | 985 |
954 int get precedenceLevel => CALL; | 986 int get precedenceLevel => CALL; |
955 } | 987 } |
956 | 988 |
957 class ArrowFun extends FunctionExpression { | 989 class ArrowFun extends FunctionExpression { |
958 final List<Identifier> params; | 990 final List<Parameter> params; |
959 final body; // Expression or Block | 991 final body; // Expression or Block |
960 | 992 |
961 ArrowFun(this.params, this.body); | 993 ArrowFun(this.params, this.body); |
962 | 994 |
963 accept(NodeVisitor visitor) => visitor.visitArrowFun(this); | 995 accept(NodeVisitor visitor) => visitor.visitArrowFun(this); |
964 | 996 |
965 void visitChildren(NodeVisitor visitor) { | 997 void visitChildren(NodeVisitor visitor) { |
966 for (Identifier param in params) param.accept(visitor); | 998 for (Parameter param in params) param.accept(visitor); |
967 body.accept(visitor); | 999 body.accept(visitor); |
968 } | 1000 } |
969 | 1001 |
970 ArrowFun _clone() => new ArrowFun(params, body); | 1002 ArrowFun _clone() => new ArrowFun(params, body); |
971 | 1003 |
972 /// Ensure parens always get generated if necessary. | 1004 /// Ensure parens always get generated if necessary. |
973 // TODO(jmesserly): I'm not sure the printer is handling this correctly for | 1005 // TODO(jmesserly): I'm not sure the printer is handling this correctly for |
974 // function() { ... } either. | 1006 // function() { ... } either. |
975 int get precedenceLevel => ASSIGNMENT; | 1007 int get precedenceLevel => ASSIGNMENT; |
976 } | 1008 } |
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1430 final Expression expression; | 1462 final Expression expression; |
1431 | 1463 |
1432 CommentExpression(this.comment, this.expression); | 1464 CommentExpression(this.comment, this.expression); |
1433 | 1465 |
1434 int get precedenceLevel => PRIMARY; | 1466 int get precedenceLevel => PRIMARY; |
1435 accept(NodeVisitor visitor) => visitor.visitCommentExpression(this); | 1467 accept(NodeVisitor visitor) => visitor.visitCommentExpression(this); |
1436 CommentExpression _clone() => new CommentExpression(comment, expression); | 1468 CommentExpression _clone() => new CommentExpression(comment, expression); |
1437 | 1469 |
1438 void visitChildren(NodeVisitor visitor) => expression.accept(visitor); | 1470 void visitChildren(NodeVisitor visitor) => expression.accept(visitor); |
1439 } | 1471 } |
OLD | NEW |