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 1065 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1076 | 1076 |
1077 /** | 1077 /** |
1078 * Use a different precedence level depending on whether the value contains a | 1078 * Use a different precedence level depending on whether the value contains a |
1079 * dot to ensure we generate `(1).toString()` and `1.0.toString()`. | 1079 * dot to ensure we generate `(1).toString()` and `1.0.toString()`. |
1080 */ | 1080 */ |
1081 int get precedenceLevel => value.contains('.') ? PRIMARY : UNARY; | 1081 int get precedenceLevel => value.contains('.') ? PRIMARY : UNARY; |
1082 } | 1082 } |
1083 | 1083 |
1084 class ArrayInitializer extends Expression { | 1084 class ArrayInitializer extends Expression { |
1085 final List<Expression> elements; | 1085 final List<Expression> elements; |
| 1086 final bool multiline; |
1086 | 1087 |
1087 ArrayInitializer(this.elements); | 1088 ArrayInitializer(this.elements, {this.multiline: false}); |
1088 | 1089 |
1089 accept(NodeVisitor visitor) => visitor.visitArrayInitializer(this); | 1090 accept(NodeVisitor visitor) => visitor.visitArrayInitializer(this); |
1090 | 1091 |
1091 void visitChildren(NodeVisitor visitor) { | 1092 void visitChildren(NodeVisitor visitor) { |
1092 for (Expression element in elements) element.accept(visitor); | 1093 for (Expression element in elements) element.accept(visitor); |
1093 } | 1094 } |
1094 | 1095 |
1095 ArrayInitializer _clone() => new ArrayInitializer(elements); | 1096 ArrayInitializer _clone() => new ArrayInitializer(elements); |
1096 | 1097 |
1097 int get precedenceLevel => PRIMARY; | 1098 int get precedenceLevel => PRIMARY; |
1098 } | 1099 } |
1099 | 1100 |
1100 /** | 1101 /** |
1101 * An empty place in an [ArrayInitializer]. | 1102 * An empty place in an [ArrayInitializer]. |
1102 * For example the list [1, , , 2] would contain two holes. | 1103 * For example the list [1, , , 2] would contain two holes. |
1103 */ | 1104 */ |
1104 class ArrayHole extends Expression { | 1105 class ArrayHole extends Expression { |
1105 accept(NodeVisitor visitor) => visitor.visitArrayHole(this); | 1106 accept(NodeVisitor visitor) => visitor.visitArrayHole(this); |
1106 | 1107 |
1107 void visitChildren(NodeVisitor visitor) {} | 1108 void visitChildren(NodeVisitor visitor) {} |
1108 | 1109 |
1109 ArrayHole _clone() => new ArrayHole(); | 1110 ArrayHole _clone() => new ArrayHole(); |
1110 | 1111 |
1111 int get precedenceLevel => PRIMARY; | 1112 int get precedenceLevel => PRIMARY; |
1112 } | 1113 } |
1113 | 1114 |
1114 class ObjectInitializer extends Expression { | 1115 class ObjectInitializer extends Expression { |
1115 final List<Property> properties; | 1116 final List<Property> properties; |
1116 /** | 1117 final bool _multiline; |
1117 * If set to true, forces a vertical layout when using the [Printer]. | |
1118 * Otherwise, layout will be vertical if and only if any [properties] | |
1119 * are [FunctionExpression]s. | |
1120 */ | |
1121 final bool _vertical; | |
1122 bool get vertical { | |
1123 return _vertical || properties.any((p) => p.value is FunctionExpression); | |
1124 } | |
1125 | 1118 |
1126 /** | 1119 /** |
1127 * Constructs a new object-initializer containing the given [properties]. | 1120 * Constructs a new object-initializer containing the given [properties]. |
1128 */ | 1121 */ |
1129 ObjectInitializer(this.properties, {vertical: false}) : _vertical = vertical; | 1122 ObjectInitializer(this.properties, {multiline: false}) |
| 1123 : _multiline = multiline; |
1130 | 1124 |
1131 accept(NodeVisitor visitor) => visitor.visitObjectInitializer(this); | 1125 accept(NodeVisitor visitor) => visitor.visitObjectInitializer(this); |
1132 | 1126 |
1133 void visitChildren(NodeVisitor visitor) { | 1127 void visitChildren(NodeVisitor visitor) { |
1134 for (Property init in properties) init.accept(visitor); | 1128 for (Property init in properties) init.accept(visitor); |
1135 } | 1129 } |
1136 | 1130 |
1137 ObjectInitializer _clone() => new ObjectInitializer(properties); | 1131 ObjectInitializer _clone() => new ObjectInitializer(properties); |
1138 | 1132 |
1139 int get precedenceLevel => PRIMARY; | 1133 int get precedenceLevel => PRIMARY; |
| 1134 /** |
| 1135 * If set to true, forces a vertical layout when using the [Printer]. |
| 1136 * Otherwise, layout will be vertical if and only if any [properties] |
| 1137 * are [FunctionExpression]s. |
| 1138 */ |
| 1139 bool get multiline { |
| 1140 return _multiline || properties.any((p) => p.value is FunctionExpression); |
| 1141 } |
1140 } | 1142 } |
1141 | 1143 |
1142 class Property extends Node { | 1144 class Property extends Node { |
1143 final Expression name; | 1145 final Expression name; |
1144 final Expression value; | 1146 final Expression value; |
1145 | 1147 |
1146 Property(this.name, this.value); | 1148 Property(this.name, this.value); |
1147 | 1149 |
1148 accept(NodeVisitor visitor) => visitor.visitProperty(this); | 1150 accept(NodeVisitor visitor) => visitor.visitProperty(this); |
1149 | 1151 |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1428 final Expression expression; | 1430 final Expression expression; |
1429 | 1431 |
1430 CommentExpression(this.comment, this.expression); | 1432 CommentExpression(this.comment, this.expression); |
1431 | 1433 |
1432 int get precedenceLevel => PRIMARY; | 1434 int get precedenceLevel => PRIMARY; |
1433 accept(NodeVisitor visitor) => visitor.visitCommentExpression(this); | 1435 accept(NodeVisitor visitor) => visitor.visitCommentExpression(this); |
1434 CommentExpression _clone() => new CommentExpression(comment, expression); | 1436 CommentExpression _clone() => new CommentExpression(comment, expression); |
1435 | 1437 |
1436 void visitChildren(NodeVisitor visitor) => expression.accept(visitor); | 1438 void visitChildren(NodeVisitor visitor) => expression.accept(visitor); |
1437 } | 1439 } |
OLD | NEW |