| 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 part of js; | 5 part of js; |
| 6 | 6 |
| 7 class Printer implements NodeVisitor { | 7 class Printer implements NodeVisitor { |
| 8 final bool shouldCompressOutput; | 8 final bool shouldCompressOutput; |
| 9 leg.Compiler compiler; | 9 leg.Compiler compiler; |
| 10 leg.CodeBuffer outBuffer; | 10 leg.CodeBuffer outBuffer; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 outBuffer.add(str); | 57 outBuffer.add(str); |
| 58 lastAddedString = str; | 58 lastAddedString = str; |
| 59 } | 59 } |
| 60 } | 60 } |
| 61 | 61 |
| 62 void outLn(String str) { | 62 void outLn(String str) { |
| 63 out(str); | 63 out(str); |
| 64 lineOut(); | 64 lineOut(); |
| 65 } | 65 } |
| 66 | 66 |
| 67 void outSemicolonLn() { |
| 68 if (shouldCompressOutput) { |
| 69 pendingSemicolon = true; |
| 70 } else { |
| 71 out(";\n"); |
| 72 } |
| 73 } |
| 74 |
| 67 void outIndent(String str) { indent(); out(str); } | 75 void outIndent(String str) { indent(); out(str); } |
| 68 void outIndentLn(String str) { indent(); outLn(str); } | 76 void outIndentLn(String str) { indent(); outLn(str); } |
| 69 void indent() { | 77 void indent() { |
| 70 if (!shouldCompressOutput) { | 78 if (!shouldCompressOutput) { |
| 71 for (int i = 0; i < indentLevel; i++) out(" "); | 79 for (int i = 0; i < indentLevel; i++) out(" "); |
| 72 } | 80 } |
| 73 } | 81 } |
| 74 | 82 |
| 75 void recordSourcePosition(var position) { | 83 void recordSourcePosition(var position) { |
| 76 if (position != null) { | 84 if (position != null) { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 } | 156 } |
| 149 | 157 |
| 150 visitBlock(Block block) { | 158 visitBlock(Block block) { |
| 151 blockOut(block, true, true); | 159 blockOut(block, true, true); |
| 152 } | 160 } |
| 153 | 161 |
| 154 visitExpressionStatement(ExpressionStatement expressionStatement) { | 162 visitExpressionStatement(ExpressionStatement expressionStatement) { |
| 155 indent(); | 163 indent(); |
| 156 visitNestedExpression(expressionStatement.expression, EXPRESSION, | 164 visitNestedExpression(expressionStatement.expression, EXPRESSION, |
| 157 newInForInit: false, newAtStatementBegin: true); | 165 newInForInit: false, newAtStatementBegin: true); |
| 158 pendingSemicolon = true; | 166 outSemicolonLn(); |
| 159 } | 167 } |
| 160 | 168 |
| 161 visitEmptyStatement(EmptyStatement nop) { | 169 visitEmptyStatement(EmptyStatement nop) { |
| 162 outIndentLn(";"); | 170 outIndentLn(";"); |
| 163 } | 171 } |
| 164 | 172 |
| 165 void ifOut(If node, bool shouldIndent) { | 173 void ifOut(If node, bool shouldIndent) { |
| 166 Node then = node.then; | 174 Node then = node.then; |
| 167 Node elsePart = node.otherwise; | 175 Node elsePart = node.otherwise; |
| 168 bool hasElse = node.hasElse; | 176 bool hasElse = node.hasElse; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 if (blockBody(loop.body, needsSeparation: true, needsNewline: false)) { | 264 if (blockBody(loop.body, needsSeparation: true, needsNewline: false)) { |
| 257 spaceOut(); | 265 spaceOut(); |
| 258 } else { | 266 } else { |
| 259 indent(); | 267 indent(); |
| 260 } | 268 } |
| 261 out("while"); | 269 out("while"); |
| 262 spaceOut(); | 270 spaceOut(); |
| 263 out("("); | 271 out("("); |
| 264 visitNestedExpression(loop.condition, EXPRESSION, | 272 visitNestedExpression(loop.condition, EXPRESSION, |
| 265 newInForInit: false, newAtStatementBegin: false); | 273 newInForInit: false, newAtStatementBegin: false); |
| 266 outLn(")"); | 274 out(")"); |
| 267 pendingSemicolon = true; | 275 outSemicolonLn(); |
| 268 } | 276 } |
| 269 | 277 |
| 270 visitContinue(Continue node) { | 278 visitContinue(Continue node) { |
| 271 if (node.targetLabel == null) { | 279 if (node.targetLabel == null) { |
| 272 outIndentLn("continue"); | 280 outIndentLn("continue"); |
| 273 } else { | 281 } else { |
| 274 outIndentLn("continue ${node.targetLabel}"); | 282 outIndentLn("continue ${node.targetLabel}"); |
| 275 } | 283 } |
| 276 pendingSemicolon = true; | 284 pendingSemicolon = true; |
| 277 } | 285 } |
| 278 | 286 |
| 279 visitBreak(Break node) { | 287 visitBreak(Break node) { |
| 280 if (node.targetLabel == null) { | 288 if (node.targetLabel == null) { |
| 281 outIndentLn("break"); | 289 outIndentLn("break"); |
| 282 } else { | 290 } else { |
| 283 outIndentLn("break ${node.targetLabel}"); | 291 outIndentLn("break ${node.targetLabel}"); |
| 284 } | 292 } |
| 285 pendingSemicolon = true; | 293 pendingSemicolon = true; |
| 286 } | 294 } |
| 287 | 295 |
| 288 visitReturn(Return node) { | 296 visitReturn(Return node) { |
| 289 if (node.value == null) { | 297 if (node.value == null) { |
| 290 outIndentLn("return"); | 298 outIndentLn("return"); |
| 291 } else { | 299 } else { |
| 292 outIndent("return"); | 300 outIndent("return"); |
| 293 pendingSpace = true; | 301 pendingSpace = true; |
| 294 visitNestedExpression(node.value, EXPRESSION, | 302 visitNestedExpression(node.value, EXPRESSION, |
| 295 newInForInit: false, newAtStatementBegin: false); | 303 newInForInit: false, newAtStatementBegin: false); |
| 296 } | 304 } |
| 297 pendingSemicolon = true; | 305 outSemicolonLn(); |
| 298 } | 306 } |
| 299 | 307 |
| 300 visitThrow(Throw node) { | 308 visitThrow(Throw node) { |
| 301 outIndent("throw"); | 309 outIndent("throw"); |
| 302 pendingSpace = true; | 310 pendingSpace = true; |
| 303 visitNestedExpression(node.expression, EXPRESSION, | 311 visitNestedExpression(node.expression, EXPRESSION, |
| 304 newInForInit: false, newAtStatementBegin: false); | 312 newInForInit: false, newAtStatementBegin: false); |
| 305 pendingSemicolon = true; | 313 outSemicolonLn(); |
| 306 } | 314 } |
| 307 | 315 |
| 308 visitTry(Try node) { | 316 visitTry(Try node) { |
| 309 outIndent("try"); | 317 outIndent("try"); |
| 310 blockBody(node.body, needsSeparation: true, needsNewline: false); | 318 blockBody(node.body, needsSeparation: true, needsNewline: false); |
| 311 if (node.catchPart != null) { | 319 if (node.catchPart != null) { |
| 312 visit(node.catchPart); | 320 visit(node.catchPart); |
| 313 } | 321 } |
| 314 if (node.finallyPart != null) { | 322 if (node.finallyPart != null) { |
| 315 spaceOut(); | 323 spaceOut(); |
| (...skipping 680 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 996 } | 1004 } |
| 997 codes.add(charCodes.$0 + digit); | 1005 codes.add(charCodes.$0 + digit); |
| 998 newName = new String.fromCharCodes(codes); | 1006 newName = new String.fromCharCodes(codes); |
| 999 } | 1007 } |
| 1000 assert(const RegExp(r'[a-zA-Z][a-zA-Z0-9]*').hasMatch(newName)); | 1008 assert(const RegExp(r'[a-zA-Z][a-zA-Z0-9]*').hasMatch(newName)); |
| 1001 nameNumber++; | 1009 nameNumber++; |
| 1002 maps.last[oldName] = newName; | 1010 maps.last[oldName] = newName; |
| 1003 return newName; | 1011 return newName; |
| 1004 } | 1012 } |
| 1005 } | 1013 } |
| OLD | NEW |