Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(329)

Side by Side Diff: lib/compiler/implementation/js/printer.dart

Issue 10831365: Don't put parenthesis to LiteralExpression (ie foreign expressions) when the expression starts a st… (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
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 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698