Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 class TemplateManager { | 7 class TemplateManager { |
| 8 Map<String, Template> expressionTemplates = new Map<String, Template>(); | 8 Map<String, Template> expressionTemplates = new Map<String, Template>(); |
| 9 Map<String, Template> statementTemplates = new Map<String, Template>(); | 9 Map<String, Template> statementTemplates = new Map<String, Template>(); |
| 10 | 10 |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 171 | 171 |
| 172 Instantiator visitSplayable(Node node) { | 172 Instantiator visitSplayable(Node node) { |
| 173 // TODO(sra): Process immediate [InterpolatedNode]s, permitting splaying. | 173 // TODO(sra): Process immediate [InterpolatedNode]s, permitting splaying. |
| 174 return visit(node); | 174 return visit(node); |
| 175 } | 175 } |
| 176 | 176 |
| 177 Instantiator visitNode(Node node) { | 177 Instantiator visitNode(Node node) { |
| 178 throw 'Unimplemented InstantiatorGeneratorVisitor for $node'; | 178 throw 'Unimplemented InstantiatorGeneratorVisitor for $node'; |
| 179 } | 179 } |
| 180 | 180 |
| 181 static RegExp identiferRE = new RegExp(r'^[A-Za-z_$][A-Za-z_$0-9]*$'); | |
| 182 | |
| 183 static Expression convertStringToVariableUse(String value) { | |
| 184 assert(identiferRE.hasMatch(value)); | |
|
Jennifer Messerly
2015/03/25 21:29:16
this check already happens in the constructor
| |
| 185 return new VariableUse(value); | |
| 186 } | |
| 187 | |
| 188 Instantiator visitInterpolatedExpression(InterpolatedExpression node) { | 181 Instantiator visitInterpolatedExpression(InterpolatedExpression node) { |
| 189 var nameOrPosition = node.nameOrPosition; | 182 var nameOrPosition = node.nameOrPosition; |
| 190 return (arguments) { | 183 return (arguments) { |
| 191 var value = arguments[nameOrPosition]; | 184 var value = arguments[nameOrPosition]; |
| 192 if (value is Expression) return value; | 185 if (value is Expression) return value; |
| 193 if (value is String) return convertStringToVariableUse(value); | 186 if (value is String) return new Identifier(value); |
| 194 error('Interpolated value #$nameOrPosition is not an Expression: $value'); | 187 error('Interpolated value #$nameOrPosition is not an Expression: $value'); |
| 195 }; | 188 }; |
| 196 } | 189 } |
| 197 | 190 |
| 198 Instantiator visitSplayableExpression(Node node) { | 191 Instantiator visitSplayableExpression(Node node) { |
| 199 if (node is InterpolatedExpression) { | 192 if (node is InterpolatedExpression) { |
| 200 var nameOrPosition = node.nameOrPosition; | 193 var nameOrPosition = node.nameOrPosition; |
| 201 return (arguments) { | 194 return (arguments) { |
| 202 var value = arguments[nameOrPosition]; | 195 var value = arguments[nameOrPosition]; |
| 203 Expression toExpression(item) { | 196 Expression toExpression(item) { |
| 204 if (item is Expression) return item; | 197 if (item is Expression) return item; |
| 205 if (item is String) return convertStringToVariableUse(item); | 198 if (item is String) return new Identifier(item); |
| 206 return error('Interpolated value #$nameOrPosition is not ' | 199 return error('Interpolated value #$nameOrPosition is not ' |
| 207 'an Expression or List of Expressions: $value'); | 200 'an Expression or List of Expressions: $value'); |
| 208 } | 201 } |
| 209 if (value is Iterable) return value.map(toExpression); | 202 if (value is Iterable) return value.map(toExpression); |
| 210 return toExpression(value); | 203 return toExpression(value); |
| 211 }; | 204 }; |
| 212 } | 205 } |
| 213 return visit(node); | 206 return visit(node); |
| 214 } | 207 } |
| 215 | 208 |
| 216 Instantiator visitInterpolatedLiteral(InterpolatedLiteral node) { | 209 Instantiator visitInterpolatedLiteral(InterpolatedLiteral node) { |
| 217 var nameOrPosition = node.nameOrPosition; | 210 var nameOrPosition = node.nameOrPosition; |
| 218 return (arguments) { | 211 return (arguments) { |
| 219 var value = arguments[nameOrPosition]; | 212 var value = arguments[nameOrPosition]; |
| 220 if (value is Literal) return value; | 213 if (value is Literal) return value; |
| 221 error('Interpolated value #$nameOrPosition is not a Literal: $value'); | 214 error('Interpolated value #$nameOrPosition is not a Literal: $value'); |
| 222 }; | 215 }; |
| 223 } | 216 } |
| 224 | 217 |
| 225 Instantiator visitInterpolatedParameter(InterpolatedParameter node) { | 218 Instantiator visitInterpolatedParameter(InterpolatedParameter node) { |
| 226 var nameOrPosition = node.nameOrPosition; | 219 var nameOrPosition = node.nameOrPosition; |
| 227 return (arguments) { | 220 return (arguments) { |
| 228 var value = arguments[nameOrPosition]; | 221 var value = arguments[nameOrPosition]; |
| 229 | 222 |
| 230 Parameter toParameter(item) { | 223 Identifier toIdentifier(item) { |
| 231 if (item is Parameter) return item; | 224 if (item is Identifier) return item; |
| 232 if (item is String) return new Parameter(item); | 225 if (item is String) return new Identifier(item); |
| 233 return error('Interpolated value #$nameOrPosition is not a Parameter or' | 226 return error('Interpolated value #$nameOrPosition is not an Identifier' |
| 234 ' List of Parameters: $value'); | 227 ' or List of Identifiers: $value'); |
| 235 } | 228 } |
| 236 if (value is Iterable) return value.map(toParameter); | 229 if (value is Iterable) return value.map(toIdentifier); |
| 237 return toParameter(value); | 230 return toIdentifier(value); |
| 238 }; | 231 }; |
| 239 } | 232 } |
| 240 | 233 |
| 241 Instantiator visitInterpolatedSelector(InterpolatedSelector node) { | 234 Instantiator visitInterpolatedSelector(InterpolatedSelector node) { |
| 242 // A selector is an expression, as in `a[selector]`. | 235 // A selector is an expression, as in `a[selector]`. |
| 243 // A String argument converted into a LiteralString, so `a.#` with argument | 236 // A String argument converted into a LiteralString, so `a.#` with argument |
| 244 // 'foo' generates `a["foo"]` which prints as `a.foo`. | 237 // 'foo' generates `a["foo"]` which prints as `a.foo`. |
| 245 var nameOrPosition = node.nameOrPosition; | 238 var nameOrPosition = node.nameOrPosition; |
| 246 return (arguments) { | 239 return (arguments) { |
| 247 var value = arguments[nameOrPosition]; | 240 var value = arguments[nameOrPosition]; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 267 Method toMethod(item) { | 260 Method toMethod(item) { |
| 268 if (item is Method) return item; | 261 if (item is Method) return item; |
| 269 return error('Interpolated value #$nameOrPosition is not a Method ' | 262 return error('Interpolated value #$nameOrPosition is not a Method ' |
| 270 'or List of Methods: $value'); | 263 'or List of Methods: $value'); |
| 271 } | 264 } |
| 272 if (value is Iterable) return value.map(toMethod); | 265 if (value is Iterable) return value.map(toMethod); |
| 273 return toMethod(value); | 266 return toMethod(value); |
| 274 }; | 267 }; |
| 275 } | 268 } |
| 276 | 269 |
| 277 Instantiator visitInterpolatedVariableDeclaration( | 270 Instantiator visitInterpolatedIdentifier(InterpolatedIdentifier node) { |
| 278 InterpolatedVariableDeclaration node) { | |
| 279 var nameOrPosition = node.nameOrPosition; | 271 var nameOrPosition = node.nameOrPosition; |
| 280 return (arguments) { | 272 return (arguments) { |
| 281 var item = arguments[nameOrPosition]; | 273 var item = arguments[nameOrPosition]; |
| 282 if (item is VariableDeclaration) return item; | 274 if (item is Identifier) return item; |
| 283 if (item is String) return new VariableDeclaration(item); | 275 if (item is String) return new Identifier(item); |
| 284 return error('Interpolated value #$nameOrPosition is not a ' | 276 return error('Interpolated value #$nameOrPosition is not a ' |
| 285 'VariableDeclaration or String: $item'); | 277 'Identifier or String: $item'); |
| 286 }; | 278 }; |
| 287 } | 279 } |
| 288 | 280 |
| 289 Instantiator visitSplayableStatement(Node node) { | 281 Instantiator visitSplayableStatement(Node node) { |
| 290 if (node is InterpolatedStatement) { | 282 if (node is InterpolatedStatement) { |
| 291 var nameOrPosition = node.nameOrPosition; | 283 var nameOrPosition = node.nameOrPosition; |
| 292 return (arguments) { | 284 return (arguments) { |
| 293 var value = arguments[nameOrPosition]; | 285 var value = arguments[nameOrPosition]; |
| 294 Statement toStatement(item) { | 286 Statement toStatement(item) { |
| 295 if (item is Statement) return item; | 287 if (item is Statement) return item; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 363 } | 355 } |
| 364 | 356 |
| 365 Instantiator visitIfConditionalCompilation(If node) { | 357 Instantiator visitIfConditionalCompilation(If node) { |
| 366 // Special version of visitInterpolatedExpression that permits bools. | 358 // Special version of visitInterpolatedExpression that permits bools. |
| 367 compileCondition(InterpolatedExpression node) { | 359 compileCondition(InterpolatedExpression node) { |
| 368 var nameOrPosition = node.nameOrPosition; | 360 var nameOrPosition = node.nameOrPosition; |
| 369 return (arguments) { | 361 return (arguments) { |
| 370 var value = arguments[nameOrPosition]; | 362 var value = arguments[nameOrPosition]; |
| 371 if (value is bool) return value; | 363 if (value is bool) return value; |
| 372 if (value is Expression) return value; | 364 if (value is Expression) return value; |
| 373 if (value is String) return convertStringToVariableUse(value);; | 365 if (value is String) return new Identifier(value); |
| 374 error('Interpolated value #$nameOrPosition ' | 366 error('Interpolated value #$nameOrPosition ' |
| 375 'is not an Expression: $value'); | 367 'is not an Expression: $value'); |
| 376 }; | 368 }; |
| 377 } | 369 } |
| 378 var makeCondition = compileCondition(node.condition); | 370 var makeCondition = compileCondition(node.condition); |
| 379 Instantiator makeThen = visit(node.then); | 371 Instantiator makeThen = visit(node.then); |
| 380 Instantiator makeOtherwise = visit(node.otherwise); | 372 Instantiator makeOtherwise = visit(node.otherwise); |
| 381 return (arguments) { | 373 return (arguments) { |
| 382 var condition = makeCondition(arguments); | 374 var condition = makeCondition(arguments); |
| 383 if (condition is bool) { | 375 if (condition is bool) { |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 625 String op = node.op; | 617 String op = node.op; |
| 626 return (arguments) => new Prefix(op, makeOperand(arguments)); | 618 return (arguments) => new Prefix(op, makeOperand(arguments)); |
| 627 } | 619 } |
| 628 | 620 |
| 629 Instantiator visitPostfix(Postfix node) { | 621 Instantiator visitPostfix(Postfix node) { |
| 630 Instantiator makeOperand = visit(node.argument); | 622 Instantiator makeOperand = visit(node.argument); |
| 631 String op = node.op; | 623 String op = node.op; |
| 632 return (arguments) => new Postfix(op, makeOperand(arguments)); | 624 return (arguments) => new Postfix(op, makeOperand(arguments)); |
| 633 } | 625 } |
| 634 | 626 |
| 635 Instantiator visitVariableUse(VariableUse node) => | |
| 636 (arguments) => new VariableUse(node.name); | |
| 637 | |
| 638 Instantiator visitThis(This node) => (arguments) => new This(); | 627 Instantiator visitThis(This node) => (arguments) => new This(); |
| 639 Instantiator visitSuper(Super node) => (arguments) => new Super(); | 628 Instantiator visitSuper(Super node) => (arguments) => new Super(); |
| 640 | 629 |
| 641 Instantiator visitVariableDeclaration(VariableDeclaration node) => | 630 Instantiator visitIdentifier(Identifier node) => |
| 642 (arguments) => new VariableDeclaration(node.name); | 631 (arguments) => new Identifier(node.name); |
| 643 | |
| 644 Instantiator visitParameter(Parameter node) => | |
| 645 (arguments) => new Parameter(node.name); | |
| 646 | 632 |
| 647 Instantiator visitAccess(PropertyAccess node) { | 633 Instantiator visitAccess(PropertyAccess node) { |
| 648 Instantiator makeReceiver = visit(node.receiver); | 634 Instantiator makeReceiver = visit(node.receiver); |
| 649 Instantiator makeSelector = visit(node.selector); | 635 Instantiator makeSelector = visit(node.selector); |
| 650 return (arguments) => | 636 return (arguments) => |
| 651 new PropertyAccess(makeReceiver(arguments), makeSelector(arguments)); | 637 new PropertyAccess(makeReceiver(arguments), makeSelector(arguments)); |
| 652 } | 638 } |
| 653 | 639 |
| 654 Instantiator visitNamedFunction(NamedFunction node) { | 640 Instantiator visitNamedFunction(NamedFunction node) { |
| 655 Instantiator makeDeclaration = visit(node.name); | 641 Instantiator makeDeclaration = visit(node.name); |
| 656 Instantiator makeFunction = visit(node.function); | 642 Instantiator makeFunction = visit(node.function); |
| 657 return (arguments) => | 643 return (arguments) => |
| 658 new NamedFunction(makeDeclaration(arguments), makeFunction(arguments)); | 644 new NamedFunction(makeDeclaration(arguments), makeFunction(arguments)); |
| 659 } | 645 } |
| 660 | 646 |
| 661 Instantiator visitFunctionExpression(FunctionExpression node) { | 647 Instantiator visitFunctionExpression(FunctionExpression node) { |
| 662 List<Instantiator> paramMakers = node.params.map(visitSplayable).toList(); | 648 List<Instantiator> paramMakers = node.params.map(visitSplayable).toList(); |
| 663 Instantiator makeBody = visit(node.body); | 649 Instantiator makeBody = visit(node.body); |
| 664 // TODO(sra): Avoid copying params if no interpolation or forced copying. | 650 // TODO(sra): Avoid copying params if no interpolation or forced copying. |
| 665 return (arguments) { | 651 return (arguments) { |
| 666 List<Parameter> params = <Parameter>[]; | 652 List<Identifier> params = <Identifier>[]; |
| 667 for (Instantiator instantiator in paramMakers) { | 653 for (Instantiator instantiator in paramMakers) { |
| 668 var result = instantiator(arguments); | 654 var result = instantiator(arguments); |
| 669 if (result is Iterable) { | 655 if (result is Iterable) { |
| 670 params.addAll(result); | 656 params.addAll(result); |
| 671 } else { | 657 } else { |
| 672 params.add(result); | 658 params.add(result); |
| 673 } | 659 } |
| 674 } | 660 } |
| 675 var body = makeBody(arguments); | 661 var body = makeBody(arguments); |
| 676 if (node is ArrowFun) { | 662 if (node is ArrowFun) { |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 845 if (count != before) containsInterpolatedNode.add(node); | 831 if (count != before) containsInterpolatedNode.add(node); |
| 846 return null; | 832 return null; |
| 847 } | 833 } |
| 848 | 834 |
| 849 visitInterpolatedNode(InterpolatedNode node) { | 835 visitInterpolatedNode(InterpolatedNode node) { |
| 850 containsInterpolatedNode.add(node); | 836 containsInterpolatedNode.add(node); |
| 851 if (node.isNamed) holeNames.add(node.nameOrPosition); | 837 if (node.isNamed) holeNames.add(node.nameOrPosition); |
| 852 ++count; | 838 ++count; |
| 853 } | 839 } |
| 854 } | 840 } |
| OLD | NEW |