| 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; | 5 part of js; |
| 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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 generator.compile(ast); | 81 generator.compile(ast); |
| 82 return generator.analysis.count == 0; | 82 return generator.analysis.count == 0; |
| 83 } | 83 } |
| 84 | 84 |
| 85 void _compile() { | 85 void _compile() { |
| 86 InstantiatorGeneratorVisitor generator = | 86 InstantiatorGeneratorVisitor generator = |
| 87 new InstantiatorGeneratorVisitor(forceCopy); | 87 new InstantiatorGeneratorVisitor(forceCopy); |
| 88 instantiator = generator.compile(ast); | 88 instantiator = generator.compile(ast); |
| 89 positionalArgumentCount = generator.analysis.count; | 89 positionalArgumentCount = generator.analysis.count; |
| 90 Set<String> names = generator.analysis.holeNames; | 90 Set<String> names = generator.analysis.holeNames; |
| 91 holeNames = names.isEmpty ? null : names.toList(growable:false); | 91 holeNames = names.toList(growable:false); |
| 92 } | 92 } |
| 93 | 93 |
| 94 /// Instantiates the template with the given [arguments]. | 94 /// Instantiates the template with the given [arguments]. |
| 95 /// | 95 /// |
| 96 /// This method fills in the holes with the given arguments. The [arguments] | 96 /// This method fills in the holes with the given arguments. The [arguments] |
| 97 /// must be either a [List] or a [Map]. | 97 /// must be either a [List] or a [Map]. |
| 98 Node instantiate(var arguments) { | 98 Node instantiate(var arguments) { |
| 99 if (arguments is List) { | 99 if (arguments is List) { |
| 100 if (arguments.length != positionalArgumentCount) { | 100 if (arguments.length != positionalArgumentCount) { |
| 101 throw 'Wrong number of template arguments, given ${arguments.length}, ' | 101 throw 'Wrong number of template arguments, given ${arguments.length}, ' |
| (...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 makeLeftHandSide(arguments), | 401 makeLeftHandSide(arguments), |
| 402 makeObject(arguments), | 402 makeObject(arguments), |
| 403 makeBody(arguments)); | 403 makeBody(arguments)); |
| 404 }; | 404 }; |
| 405 } | 405 } |
| 406 | 406 |
| 407 TODO(String name) { | 407 TODO(String name) { |
| 408 throw new UnimplementedError('$this.$name'); | 408 throw new UnimplementedError('$this.$name'); |
| 409 } | 409 } |
| 410 | 410 |
| 411 Instantiator visitWhile(While node) => TODO('visitWhile'); | 411 Instantiator visitWhile(While node) { |
| 412 Instantiator visitDo(Do node) => TODO('visitDo'); | 412 Instantiator makeCondition = visit(node.condition); |
| 413 Instantiator makeBody = visit(node.body); |
| 414 return (arguments) { |
| 415 return new While(makeCondition(arguments), makeBody(arguments)); |
| 416 }; |
| 417 } |
| 418 |
| 419 Instantiator visitDo(Do node) { |
| 420 Instantiator makeBody = visit(node.body); |
| 421 Instantiator makeCondition = visit(node.condition); |
| 422 return (arguments) { |
| 423 return new Do(makeBody(arguments), makeCondition(arguments)); |
| 424 }; |
| 425 } |
| 413 | 426 |
| 414 Instantiator visitContinue(Continue node) => | 427 Instantiator visitContinue(Continue node) => |
| 415 (arguments) => new Continue(node.targetLabel); | 428 (arguments) => new Continue(node.targetLabel); |
| 416 | 429 |
| 417 Instantiator visitBreak(Break node) => | 430 Instantiator visitBreak(Break node) => |
| 418 (arguments) => new Break(node.targetLabel); | 431 (arguments) => new Break(node.targetLabel); |
| 419 | 432 |
| 420 Instantiator visitReturn(Return node) { | 433 Instantiator visitReturn(Return node) { |
| 421 Instantiator makeExpression = visitNullable(node.value); | 434 Instantiator makeExpression = visitNullable(node.value); |
| 422 return (arguments) => new Return(makeExpression(arguments)); | 435 return (arguments) => new Return(makeExpression(arguments)); |
| 423 } | 436 } |
| 424 | 437 |
| 425 Instantiator visitThrow(Throw node) { | 438 Instantiator visitThrow(Throw node) { |
| 426 Instantiator makeExpression = visit(node.expression); | 439 Instantiator makeExpression = visit(node.expression); |
| 427 return (arguments) => new Throw(makeExpression(arguments)); | 440 return (arguments) => new Throw(makeExpression(arguments)); |
| 428 } | 441 } |
| 429 | 442 |
| 430 | |
| 431 Instantiator visitTry(Try node) { | 443 Instantiator visitTry(Try node) { |
| 432 Instantiator makeBody = visit(node.body); | 444 Instantiator makeBody = visit(node.body); |
| 433 Instantiator makeCatch = visitNullable(node.catchPart); | 445 Instantiator makeCatch = visitNullable(node.catchPart); |
| 434 Instantiator makeFinally = visitNullable(node.finallyPart); | 446 Instantiator makeFinally = visitNullable(node.finallyPart); |
| 435 return (arguments) => new Try( | 447 return (arguments) => new Try( |
| 436 makeBody(arguments), makeCatch(arguments), makeFinally(arguments)); | 448 makeBody(arguments), makeCatch(arguments), makeFinally(arguments)); |
| 437 } | 449 } |
| 438 | 450 |
| 439 Instantiator visitCatch(Catch node) { | 451 Instantiator visitCatch(Catch node) { |
| 440 Instantiator makeDeclaration = visit(node.declaration); | 452 Instantiator makeDeclaration = visit(node.declaration); |
| 441 Instantiator makeBody = visit(node.body); | 453 Instantiator makeBody = visit(node.body); |
| 442 return (arguments) => new Catch( | 454 return (arguments) => new Catch( |
| 443 makeDeclaration(arguments), makeBody(arguments)); | 455 makeDeclaration(arguments), makeBody(arguments)); |
| 444 } | 456 } |
| 445 | 457 |
| 446 Instantiator visitSwitch(Switch node) => TODO('visitSwitch'); | 458 Instantiator visitSwitch(Switch node) { |
| 447 Instantiator visitCase(Case node) => TODO('visitCase'); | 459 Instantiator makeKey = visit(node.key); |
| 448 Instantiator visitDefault(Default node) => TODO('visitDefault'); | 460 Iterable<Instantiator> makeCases = node.cases.map(visit); |
| 461 return (arguments) { |
| 462 return new Switch(makeKey(arguments), |
| 463 makeCases.map((Instantiator makeCase) => makeCase(arguments)) |
| 464 .toList()); |
| 465 }; |
| 466 } |
| 467 |
| 468 Instantiator visitCase(Case node) { |
| 469 Instantiator makeExpression = visit(node.expression); |
| 470 Instantiator makeBody = visit(node.body); |
| 471 return (arguments) { |
| 472 return new Case(makeExpression(arguments), makeBody(arguments)); |
| 473 }; |
| 474 } |
| 475 |
| 476 Instantiator visitDefault(Default node) { |
| 477 Instantiator makeBody = visit(node.body); |
| 478 return (arguments) { |
| 479 return new Default(makeBody(arguments)); |
| 480 }; |
| 481 } |
| 449 | 482 |
| 450 Instantiator visitFunctionDeclaration(FunctionDeclaration node) { | 483 Instantiator visitFunctionDeclaration(FunctionDeclaration node) { |
| 451 Instantiator makeName = visit(node.name); | 484 Instantiator makeName = visit(node.name); |
| 452 Instantiator makeFunction = visit(node.function); | 485 Instantiator makeFunction = visit(node.function); |
| 453 return (arguments) => | 486 return (arguments) => |
| 454 new FunctionDeclaration(makeName(arguments), makeFunction(arguments)); | 487 new FunctionDeclaration(makeName(arguments), makeFunction(arguments)); |
| 455 } | 488 } |
| 456 | 489 |
| 457 Instantiator visitLabeledStatement(LabeledStatement node) => | 490 Instantiator visitLabeledStatement(LabeledStatement node) => |
| 458 TODO('visitLabeledStatement'); | 491 TODO('visitLabeledStatement'); |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 651 Instantiator makeValue = visit(node.value); | 684 Instantiator makeValue = visit(node.value); |
| 652 return (arguments) { | 685 return (arguments) { |
| 653 return new Property(makeName(arguments), makeValue(arguments)); | 686 return new Property(makeName(arguments), makeValue(arguments)); |
| 654 }; | 687 }; |
| 655 } | 688 } |
| 656 | 689 |
| 657 Instantiator visitRegExpLiteral(RegExpLiteral node) => | 690 Instantiator visitRegExpLiteral(RegExpLiteral node) => |
| 658 (arguments) => new RegExpLiteral(node.pattern); | 691 (arguments) => new RegExpLiteral(node.pattern); |
| 659 | 692 |
| 660 Instantiator visitComment(Comment node) => TODO('visitComment'); | 693 Instantiator visitComment(Comment node) => TODO('visitComment'); |
| 694 |
| 695 Instantiator visitAwait(Await node) { |
| 696 Instantiator makeExpression = visit(node.expression); |
| 697 return (arguments) { |
| 698 return new Await(makeExpression(arguments)); |
| 699 }; |
| 700 } |
| 661 } | 701 } |
| 662 | 702 |
| 663 /** | 703 /** |
| 664 * InterpolatedNodeAnalysis extract [InterpolatedNode]s from AST. | 704 * InterpolatedNodeAnalysis extract [InterpolatedNode]s from AST. |
| 665 */ | 705 */ |
| 666 class InterpolatedNodeAnalysis extends BaseVisitor { | 706 class InterpolatedNodeAnalysis extends BaseVisitor { |
| 667 final Setlet<Node> containsInterpolatedNode = new Setlet<Node>(); | 707 final Setlet<Node> containsInterpolatedNode = new Setlet<Node>(); |
| 668 final List<InterpolatedNode> interpolatedNodes = <InterpolatedNode>[]; | 708 final List<InterpolatedNode> interpolatedNodes = <InterpolatedNode>[]; |
| 669 final Setlet<String> holeNames = new Setlet<String>(); | 709 final Setlet<String> holeNames = new Setlet<String>(); |
| 670 int count = 0; | 710 int count = 0; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 685 return null; | 725 return null; |
| 686 } | 726 } |
| 687 | 727 |
| 688 visitInterpolatedNode(InterpolatedNode node) { | 728 visitInterpolatedNode(InterpolatedNode node) { |
| 689 interpolatedNodes.add(node); | 729 interpolatedNodes.add(node); |
| 690 containsInterpolatedNode.add(node); | 730 containsInterpolatedNode.add(node); |
| 691 if (node.isNamed) holeNames.add(node.nameOrPosition); | 731 if (node.isNamed) holeNames.add(node.nameOrPosition); |
| 692 ++count; | 732 ++count; |
| 693 } | 733 } |
| 694 } | 734 } |
| OLD | NEW |