| 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 19 matching lines...) Expand all Loading... |
| 30 assert(lastAddedString.length != ""); | 30 assert(lastAddedString.length != ""); |
| 31 return lastAddedString.charCodeAt(lastAddedString.length - 1); | 31 return lastAddedString.charCodeAt(lastAddedString.length - 1); |
| 32 } | 32 } |
| 33 | 33 |
| 34 void out(String str) { | 34 void out(String str) { |
| 35 if (str != "") { | 35 if (str != "") { |
| 36 outBuffer.add(str); | 36 outBuffer.add(str); |
| 37 lastAddedString = str; | 37 lastAddedString = str; |
| 38 } | 38 } |
| 39 } | 39 } |
| 40 |
| 40 void outLn(String str) { | 41 void outLn(String str) { |
| 41 out(str); | 42 out(str); |
| 42 lineOut(); | 43 lineOut(); |
| 43 } | 44 } |
| 45 |
| 44 void outIndent(String str) { indent(); out(str); } | 46 void outIndent(String str) { indent(); out(str); } |
| 45 void outIndentLn(String str) { indent(); outLn(str); } | 47 void outIndentLn(String str) { indent(); outLn(str); } |
| 46 void indent() { | 48 void indent() { |
| 47 if (!shouldCompressOutput) { | 49 if (!shouldCompressOutput) { |
| 48 for (int i = 0; i < indentLevel; i++) out(" "); | 50 for (int i = 0; i < indentLevel; i++) out(" "); |
| 49 } | 51 } |
| 50 } | 52 } |
| 51 | 53 |
| 52 void recordSourcePosition(var position) { | 54 void recordSourcePosition(var position) { |
| 53 if (position != null) { | 55 if (position != null) { |
| (...skipping 691 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 745 } | 747 } |
| 746 | 748 |
| 747 visitLiteralExpression(LiteralExpression node) { | 749 visitLiteralExpression(LiteralExpression node) { |
| 748 String template = node.template; | 750 String template = node.template; |
| 749 List<Expression> inputs = node.inputs; | 751 List<Expression> inputs = node.inputs; |
| 750 | 752 |
| 751 List<String> parts = template.split('#'); | 753 List<String> parts = template.split('#'); |
| 752 if (parts.length != inputs.length + 1) { | 754 if (parts.length != inputs.length + 1) { |
| 753 compiler.internalError('Wrong number of arguments for JS: $template'); | 755 compiler.internalError('Wrong number of arguments for JS: $template'); |
| 754 } | 756 } |
| 755 out("("); | 757 // Save [atStatementBegin] because visiting the parts might change it. |
| 758 bool beginStatement = atStatementBegin; |
| 759 if (!beginStatement) out("("); |
| 756 out(parts[0]); | 760 out(parts[0]); |
| 757 for (int i = 0; i < inputs.length; i++) { | 761 for (int i = 0; i < inputs.length; i++) { |
| 758 visit(inputs[i]); | 762 visit(inputs[i]); |
| 759 out(parts[i + 1]); | 763 out(parts[i + 1]); |
| 760 } | 764 } |
| 761 out(")"); | 765 if (!beginStatement) out(")"); |
| 762 } | 766 } |
| 763 | 767 |
| 764 visitLiteralStatement(LiteralStatement node) { | 768 visitLiteralStatement(LiteralStatement node) { |
| 765 outLn(node.code); | 769 outLn(node.code); |
| 766 } | 770 } |
| 767 } | 771 } |
| 768 | 772 |
| 769 /** | 773 /** |
| 770 * Returns true, if the given node must be wrapped into braces when used | 774 * Returns true, if the given node must be wrapped into braces when used |
| 771 * as then-statement in an [If] that has an else branch. | 775 * as then-statement in an [If] that has an else branch. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 816 } | 820 } |
| 817 | 821 |
| 818 | 822 |
| 819 leg.CodeBuffer prettyPrint(Node node, | 823 leg.CodeBuffer prettyPrint(Node node, |
| 820 leg.Compiler compiler, | 824 leg.Compiler compiler, |
| 821 Dynamic positionElement) { | 825 Dynamic positionElement) { |
| 822 Printer printer = new Printer(compiler, positionElement); | 826 Printer printer = new Printer(compiler, positionElement); |
| 823 printer.visit(node); | 827 printer.visit(node); |
| 824 return printer.outBuffer; | 828 return printer.outBuffer; |
| 825 } | 829 } |
| OLD | NEW |