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

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

Issue 10837236: Fix dangling else. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add forgotten test. 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 | tests/language/dangling_else_test.dart » ('j') | 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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 } 130 }
131 131
132 visitEmptyStatement(EmptyStatement nop) { 132 visitEmptyStatement(EmptyStatement nop) {
133 outIndentLn(";"); 133 outIndentLn(";");
134 } 134 }
135 135
136 void ifOut(If node, bool shouldIndent) { 136 void ifOut(If node, bool shouldIndent) {
137 Node then = node.then; 137 Node then = node.then;
138 Node elsePart = node.otherwise; 138 Node elsePart = node.otherwise;
139 bool hasElse = node.hasElse; 139 bool hasElse = node.hasElse;
140
140 // Handle dangling elses. 141 // Handle dangling elses.
141 // If the then-branch is an if, which has no else-branch, but we do 142 if (hasElse) {
142 // have one, then we need to put the nested if into braces. 143 NodeVisitor danglingElseVisitor = new DanglingElseVisitor(compiler);
kasperl 2012/08/14 13:42:24 Can this be reused? There's no state in the visito
floitsch 2012/08/14 13:46:48 Done.
143 if (hasElse && then is If) { 144 bool needsBraces = node.then.accept(danglingElseVisitor);
144 Node nested = then; 145 if (needsBraces) {
145 do { 146 then = new Block(<Statement>[then]);
146 If nestedIf = nested; 147 }
147 if (!nestedIf.hasElse) {
148 then = new Block(<Statement>[then]);
149 break;
150 }
151 nested = nestedIf.otherwise;
152 } while (nested is If);
153 }
154 if (then is If && !(then as If).hasElse && hasElse) {
155 then = new Block(<Statement>[then]);
156 } 148 }
157 if (shouldIndent) indent(); 149 if (shouldIndent) indent();
158 out("if"); 150 out("if");
159 spaceOut(); 151 spaceOut();
160 out("("); 152 out("(");
161 visitNestedExpression(node.condition, EXPRESSION, 153 visitNestedExpression(node.condition, EXPRESSION,
162 newInForInit: false, newAtStatementBegin: false); 154 newInForInit: false, newAtStatementBegin: false);
163 out(")"); 155 out(")");
164 bool thenWasBlock = 156 bool thenWasBlock =
165 blockBody(then, needsSeparation: false, needsNewline: !hasElse); 157 blockBody(then, needsSeparation: false, needsNewline: !hasElse);
(...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after
762 out(parts[i + 1]); 754 out(parts[i + 1]);
763 } 755 }
764 out(")"); 756 out(")");
765 } 757 }
766 758
767 visitLiteralStatement(LiteralStatement node) { 759 visitLiteralStatement(LiteralStatement node) {
768 outLn(node.code); 760 outLn(node.code);
769 } 761 }
770 } 762 }
771 763
764 /**
765 * Returns true, if the given node must be wrapped into braces when used
766 * as then-statement in an [If] that has an else branch.
767 */
768 class DanglingElseVisitor extends BaseVisitor<bool> {
769 leg.Compiler compiler;
770
771 DanglingElseVisitor(this.compiler);
772
773 bool visitProgram(Program node) => false;
774
775 bool visitNode(Statement node) {
776 compiler.internalError("Forgot node: $node");
777 }
778
779 bool visitBlock(Block node) => false;
780 bool visitExpressionStatement(ExpressionStatement node) => false;
781 bool visitEmptyStatement(EmptyStatement node) => false;
782 bool visitIf(If node) {
783 if (!node.hasElse) return true;
784 return node.otherwise.accept(this);
785 }
786 bool visitFor(For node) => node.body.accept(this);
787 bool visitForIn(ForIn node) => node.body.accept(this);
788 bool visitWhile(While node) => node.body.accept(this);
789 bool visitDo(Do node) => false;
790 bool visitContinue(Continue node) => false;
791 bool visitBreak(Break node) => false;
792 bool visitReturn(Return node) => false;
793 bool visitThrow(Throw node) => false;
794 bool visitTry(Try node) {
795 if (node.finallyPart != null) {
796 return node.finallyPart.accept(this);
797 } else {
798 return node.catchPart.accept(this);
799 }
800 }
801 bool visitCatch(Catch node) => node.body.accept(this);
802 bool visitSwitch(Switch node) => false;
803 bool visitCase(Case node) => false;
804 bool visitDefault(Default node) => false;
805 bool visitFunctionDeclaration(FunctionDeclaration node) => false;
806 bool visitLabeledStatement(LabeledStatement node)
807 => node.body.accept(this);
808 bool visitLiteralStatement(LiteralStatement node) => true;
809
810 bool visitExpression(Expression node) => false;
811 }
812
813
772 leg.CodeBuffer prettyPrint(Node node, 814 leg.CodeBuffer prettyPrint(Node node,
773 leg.Compiler compiler, 815 leg.Compiler compiler,
774 Dynamic positionElement) { 816 Dynamic positionElement) {
775 Printer printer = new Printer(compiler, positionElement); 817 Printer printer = new Printer(compiler, positionElement);
776 printer.visit(node); 818 printer.visit(node);
777 return printer.outBuffer; 819 return printer.outBuffer;
778 } 820 }
OLDNEW
« no previous file with comments | « no previous file | tests/language/dangling_else_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698