| 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 class Printer implements NodeVisitor { | 5 class Printer implements NodeVisitor { |
| 6 final bool shouldCompressOutput = false; | 6 final bool shouldCompressOutput = false; |
| 7 leg.Compiler compiler; | 7 leg.Compiler compiler; |
| 8 var positionElement; | 8 var positionElement; |
| 9 leg.CodeBuffer outBuffer; | 9 leg.CodeBuffer outBuffer; |
| 10 int indentLevel = 0; | 10 int indentLevel = 0; |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 visitThrow(Throw node) { | 271 visitThrow(Throw node) { |
| 272 outIndent("throw "); | 272 outIndent("throw "); |
| 273 visitNestedExpression(node.expression, EXPRESSION, | 273 visitNestedExpression(node.expression, EXPRESSION, |
| 274 newInForInit: false, newAtStatementBegin: false); | 274 newInForInit: false, newAtStatementBegin: false); |
| 275 outLn(";"); | 275 outLn(";"); |
| 276 } | 276 } |
| 277 | 277 |
| 278 visitTry(Try node) { | 278 visitTry(Try node) { |
| 279 outIndent("try"); | 279 outIndent("try"); |
| 280 blockBody(node.body, needsSeparation: true, needsNewline: false); | 280 blockBody(node.body, needsSeparation: true, needsNewline: false); |
| 281 spaceOut(); | |
| 282 if (node.catchPart !== null) { | 281 if (node.catchPart !== null) { |
| 283 visit(node.catchPart); | 282 visit(node.catchPart); |
| 284 } | 283 } |
| 285 if (node.finallyPart !== null) { | 284 if (node.finallyPart !== null) { |
| 286 spaceOut(); | 285 spaceOut(); |
| 287 out("finally"); | 286 out("finally"); |
| 288 blockBody(node.finallyPart, needsSeparation: true, needsNewline: true); | 287 blockBody(node.finallyPart, needsSeparation: true, needsNewline: true); |
| 289 } else { | 288 } else { |
| 290 lineOut(); | 289 lineOut(); |
| 291 } | 290 } |
| (...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 635 | 634 |
| 636 visitAccess(PropertyAccess access) { | 635 visitAccess(PropertyAccess access) { |
| 637 visitNestedExpression(access.receiver, CALL, | 636 visitNestedExpression(access.receiver, CALL, |
| 638 newInForInit: inForInit, | 637 newInForInit: inForInit, |
| 639 newAtStatementBegin: atStatementBegin); | 638 newAtStatementBegin: atStatementBegin); |
| 640 Node selector = access.selector; | 639 Node selector = access.selector; |
| 641 if (selector is LiteralString) { | 640 if (selector is LiteralString) { |
| 642 LiteralString selectorString = selector; | 641 LiteralString selectorString = selector; |
| 643 String fieldWithQuotes = selectorString.value; | 642 String fieldWithQuotes = selectorString.value; |
| 644 if (isValidJavaScriptId(fieldWithQuotes)) { | 643 if (isValidJavaScriptId(fieldWithQuotes)) { |
| 645 if (isDigit(lastCharCode)) out(" "); | 644 if (access.receiver is LiteralNumber) out(" "); |
| 646 out("."); | 645 out("."); |
| 647 out(fieldWithQuotes.substring(1, fieldWithQuotes.length - 1)); | 646 out(fieldWithQuotes.substring(1, fieldWithQuotes.length - 1)); |
| 648 return; | 647 return; |
| 649 } | 648 } |
| 650 } | 649 } |
| 651 out("["); | 650 out("["); |
| 652 visitNestedExpression(selector, EXPRESSION, | 651 visitNestedExpression(selector, EXPRESSION, |
| 653 newInForInit: false, newAtStatementBegin: false); | 652 newInForInit: false, newAtStatementBegin: false); |
| 654 out("]"); | 653 out("]"); |
| 655 } | 654 } |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 747 } | 746 } |
| 748 | 747 |
| 749 visitLiteralExpression(LiteralExpression node) { | 748 visitLiteralExpression(LiteralExpression node) { |
| 750 String template = node.template; | 749 String template = node.template; |
| 751 List<Expression> inputs = node.inputs; | 750 List<Expression> inputs = node.inputs; |
| 752 | 751 |
| 753 List<String> parts = template.split('#'); | 752 List<String> parts = template.split('#'); |
| 754 if (parts.length != inputs.length + 1) { | 753 if (parts.length != inputs.length + 1) { |
| 755 compiler.internalError('Wrong number of arguments for JS: $template'); | 754 compiler.internalError('Wrong number of arguments for JS: $template'); |
| 756 } | 755 } |
| 757 // Save [atStatementBegin] because visiting the parts might change it. | 756 // Code that uses JS must take care of operator precedences, and |
| 758 bool beginStatement = atStatementBegin; | 757 // put parenthesis if needed. |
| 759 if (!beginStatement) out("("); | |
| 760 out(parts[0]); | 758 out(parts[0]); |
| 761 for (int i = 0; i < inputs.length; i++) { | 759 for (int i = 0; i < inputs.length; i++) { |
| 762 visit(inputs[i]); | 760 visit(inputs[i]); |
| 763 out(parts[i + 1]); | 761 out(parts[i + 1]); |
| 764 } | 762 } |
| 765 if (!beginStatement) out(")"); | |
| 766 } | 763 } |
| 767 | 764 |
| 768 visitLiteralStatement(LiteralStatement node) { | 765 visitLiteralStatement(LiteralStatement node) { |
| 769 outLn(node.code); | 766 outLn(node.code); |
| 770 } | 767 } |
| 771 } | 768 } |
| 772 | 769 |
| 773 /** | 770 /** |
| 774 * Returns true, if the given node must be wrapped into braces when used | 771 * Returns true, if the given node must be wrapped into braces when used |
| 775 * as then-statement in an [If] that has an else branch. | 772 * as then-statement in an [If] that has an else branch. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 820 } | 817 } |
| 821 | 818 |
| 822 | 819 |
| 823 leg.CodeBuffer prettyPrint(Node node, | 820 leg.CodeBuffer prettyPrint(Node node, |
| 824 leg.Compiler compiler, | 821 leg.Compiler compiler, |
| 825 Dynamic positionElement) { | 822 Dynamic positionElement) { |
| 826 Printer printer = new Printer(compiler, positionElement); | 823 Printer printer = new Printer(compiler, positionElement); |
| 827 printer.visit(node); | 824 printer.visit(node); |
| 828 return printer.outBuffer; | 825 return printer.outBuffer; |
| 829 } | 826 } |
| OLD | NEW |