| 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_ast; | 5 part of js; |
| 6 | 6 |
| 7 | 7 class Printer extends Indentation implements NodeVisitor { |
| 8 class JavaScriptPrintingOptions { | |
| 9 final bool shouldCompressOutput; | 8 final bool shouldCompressOutput; |
| 10 final bool minifyLocalVariables; | 9 leg.DiagnosticListener diagnosticListener; |
| 11 final bool preferSemicolonToNewlineInMinifiedOutput; | 10 CodeBuffer outBuffer; |
| 12 | 11 bool inForInit = false; |
| 13 JavaScriptPrintingOptions( | 12 bool atStatementBegin = false; |
| 14 {this.shouldCompressOutput: false, | |
| 15 this.minifyLocalVariables: false, | |
| 16 this.preferSemicolonToNewlineInMinifiedOutput: false}); | |
| 17 } | |
| 18 | |
| 19 | |
| 20 /// An environment in which JavaScript printing is done. Provides emitting of | |
| 21 /// text and pre- and post-visit callbacks. | |
| 22 abstract class JavaScriptPrintingContext { | |
| 23 /// Signals an error. This should happen only for serious internal errors. | |
| 24 void error(String message) { throw message; } | |
| 25 | |
| 26 /// Adds [string] to the output. | |
| 27 void emit(String string); | |
| 28 | |
| 29 /// Callback immediately before printing [node]. Whitespace may be printed | |
| 30 /// after this callback before the first non-whitespace character for [node]. | |
| 31 void enterNode(Node node) {} | |
| 32 /// Callback after printing the last character representing [node]. | |
| 33 void exitNode(Node node) {} | |
| 34 } | |
| 35 | |
| 36 /// A simple implementation of [JavaScriptPrintingContext] suitable for tests. | |
| 37 class SimpleJavaScriptPrintingContext extends JavaScriptPrintingContext { | |
| 38 final StringBuffer buffer = new StringBuffer(); | |
| 39 | |
| 40 void emit(String string) { | |
| 41 buffer.write(string); | |
| 42 } | |
| 43 | |
| 44 String getText() => buffer.toString(); | |
| 45 } | |
| 46 | |
| 47 | |
| 48 class Printer implements NodeVisitor { | |
| 49 final JavaScriptPrintingOptions options; | |
| 50 final JavaScriptPrintingContext context; | |
| 51 final bool shouldCompressOutput; | |
| 52 final DanglingElseVisitor danglingElseVisitor; | 13 final DanglingElseVisitor danglingElseVisitor; |
| 53 final LocalNamer localNamer; | 14 final LocalNamer localNamer; |
| 54 | |
| 55 bool inForInit = false; | |
| 56 bool atStatementBegin = false; | |
| 57 bool pendingSemicolon = false; | 15 bool pendingSemicolon = false; |
| 58 bool pendingSpace = false; | 16 bool pendingSpace = false; |
| 59 | 17 DumpInfoTask monitor = null; |
| 60 // The current indentation level. | |
| 61 int _indentLevel = 0; | |
| 62 // A cache of all indentation strings used so far. | |
| 63 List<String> _indentList = <String>[""]; | |
| 64 | 18 |
| 65 static final identifierCharacterRegExp = new RegExp(r'^[a-zA-Z_0-9$]'); | 19 static final identifierCharacterRegExp = new RegExp(r'^[a-zA-Z_0-9$]'); |
| 66 static final expressionContinuationRegExp = new RegExp(r'^[-+([]'); | 20 static final expressionContinuationRegExp = new RegExp(r'^[-+([]'); |
| 67 | 21 |
| 68 Printer(JavaScriptPrintingOptions options, | 22 Printer(leg.DiagnosticListener diagnosticListener, DumpInfoTask monitor, |
| 69 JavaScriptPrintingContext context) | 23 { bool enableMinification: false, allowVariableMinification: true }) |
| 70 : options = options, | 24 : shouldCompressOutput = enableMinification, |
| 71 context = context, | 25 monitor = monitor, |
| 72 shouldCompressOutput = options.shouldCompressOutput, | 26 diagnosticListener = diagnosticListener, |
| 73 danglingElseVisitor = new DanglingElseVisitor(context), | 27 outBuffer = new CodeBuffer(), |
| 74 localNamer = determineRenamer(options.shouldCompressOutput, | 28 danglingElseVisitor = new DanglingElseVisitor(diagnosticListener), |
| 75 options.minifyLocalVariables); | 29 localNamer = determineRenamer(enableMinification, |
| 30 allowVariableMinification); |
| 76 | 31 |
| 77 static LocalNamer determineRenamer(bool shouldCompressOutput, | 32 static LocalNamer determineRenamer(bool shouldCompressOutput, |
| 78 bool allowVariableMinification) { | 33 bool allowVariableMinification) { |
| 79 return (shouldCompressOutput && allowVariableMinification) | 34 return (shouldCompressOutput && allowVariableMinification) |
| 80 ? new MinifyRenamer() : new IdentityNamer(); | 35 ? new MinifyRenamer() : new IdentityNamer(); |
| 81 } | 36 } |
| 82 | 37 |
| 83 | |
| 84 // The current indentation string. | |
| 85 String get indentation { | |
| 86 // Lazily add new indentation strings as required. | |
| 87 while (_indentList.length <= _indentLevel) { | |
| 88 _indentList.add(_indentList.last + " "); | |
| 89 } | |
| 90 return _indentList[_indentLevel]; | |
| 91 } | |
| 92 | |
| 93 void indentMore() { | |
| 94 _indentLevel++; | |
| 95 } | |
| 96 | |
| 97 void indentLess() { | |
| 98 _indentLevel--; | |
| 99 } | |
| 100 | |
| 101 | |
| 102 /// Always emit a newline, even under `enableMinification`. | 38 /// Always emit a newline, even under `enableMinification`. |
| 103 void forceLine() { | 39 void forceLine() { |
| 104 out("\n"); | 40 out("\n"); |
| 105 } | 41 } |
| 106 /// Emits a newline for readability. | 42 /// Emits a newline for readability. |
| 107 void lineOut() { | 43 void lineOut() { |
| 108 if (!shouldCompressOutput) forceLine(); | 44 if (!shouldCompressOutput) forceLine(); |
| 109 } | 45 } |
| 110 void spaceOut() { | 46 void spaceOut() { |
| 111 if (!shouldCompressOutput) out(" "); | 47 if (!shouldCompressOutput) out(" "); |
| 112 } | 48 } |
| 113 | 49 |
| 114 String lastAddedString = null; | 50 String lastAddedString = null; |
| 115 int get lastCharCode { | 51 int get lastCharCode { |
| 116 if (lastAddedString == null) return 0; | 52 if (lastAddedString == null) return 0; |
| 117 assert(lastAddedString.length != ""); | 53 assert(lastAddedString.length != ""); |
| 118 return lastAddedString.codeUnitAt(lastAddedString.length - 1); | 54 return lastAddedString.codeUnitAt(lastAddedString.length - 1); |
| 119 } | 55 } |
| 120 | 56 |
| 121 void out(String str) { | 57 void out(String str) { |
| 122 if (str != "") { | 58 if (str != "") { |
| 123 if (pendingSemicolon) { | 59 if (pendingSemicolon) { |
| 124 if (!shouldCompressOutput) { | 60 if (!shouldCompressOutput) { |
| 125 context.emit(";"); | 61 outBuffer.add(";"); |
| 126 } else if (str != "}") { | 62 } else if (str != "}") { |
| 127 // We want to output newline instead of semicolon because it makes | 63 // We want to output newline instead of semicolon because it makes |
| 128 // the raw stack traces much easier to read and it also makes line- | 64 // the raw stack traces much easier to read and it also makes line- |
| 129 // based tools like diff work much better. JavaScript will | 65 // based tools like diff work much better. JavaScript will |
| 130 // automatically insert the semicolon at the newline if it means a | 66 // automatically insert the semicolon at the newline if it means a |
| 131 // parsing error is avoided, so we can only do this trick if the | 67 // parsing error is avoided, so we can only do this trick if the |
| 132 // next line is not something that can be glued onto a valid | 68 // next line is not something that can be glued onto a valid |
| 133 // expression to make a new valid expression. | 69 // expression to make a new valid expression. |
| 134 | 70 |
| 135 // If we're using the new emitter where most pretty printed code | 71 // If we're using the new emitter where most pretty printed code |
| 136 // is escaped in strings, it is a lot easier to deal with semicolons | 72 // is escaped in strings, it is a lot easier to deal with semicolons |
| 137 // than newlines because the former doesn't need escaping. | 73 // than newlines because the former doesn't need escaping. |
| 138 if (options.preferSemicolonToNewlineInMinifiedOutput || | 74 if (USE_NEW_EMITTER || expressionContinuationRegExp.hasMatch(str)) { |
| 139 expressionContinuationRegExp.hasMatch(str)) { | 75 outBuffer.add(";"); |
| 140 context.emit(";"); | |
| 141 } else { | 76 } else { |
| 142 context.emit("\n"); | 77 outBuffer.add("\n"); |
| 143 } | 78 } |
| 144 } | 79 } |
| 145 } | 80 } |
| 146 if (pendingSpace && | 81 if (pendingSpace && |
| 147 (!shouldCompressOutput || identifierCharacterRegExp.hasMatch(str))) { | 82 (!shouldCompressOutput || identifierCharacterRegExp.hasMatch(str))) { |
| 148 context.emit(" "); | 83 outBuffer.add(" "); |
| 149 } | 84 } |
| 150 pendingSpace = false; | 85 pendingSpace = false; |
| 151 pendingSemicolon = false; | 86 pendingSemicolon = false; |
| 152 context.emit(str); | 87 outBuffer.add(str); |
| 153 lastAddedString = str; | 88 lastAddedString = str; |
| 154 } | 89 } |
| 155 } | 90 } |
| 156 | 91 |
| 157 void outLn(String str) { | 92 void outLn(String str) { |
| 158 out(str); | 93 out(str); |
| 159 lineOut(); | 94 lineOut(); |
| 160 } | 95 } |
| 161 | 96 |
| 162 void outSemicolonLn() { | 97 void outSemicolonLn() { |
| 163 if (shouldCompressOutput) { | 98 if (shouldCompressOutput) { |
| 164 pendingSemicolon = true; | 99 pendingSemicolon = true; |
| 165 } else { | 100 } else { |
| 166 out(";"); | 101 out(";"); |
| 167 forceLine(); | 102 forceLine(); |
| 168 } | 103 } |
| 169 } | 104 } |
| 170 | 105 |
| 171 void outIndent(String str) { indent(); out(str); } | 106 void outIndent(String str) { indent(); out(str); } |
| 172 void outIndentLn(String str) { indent(); outLn(str); } | 107 void outIndentLn(String str) { indent(); outLn(str); } |
| 173 void indent() { | 108 void indent() { |
| 174 if (!shouldCompressOutput) { | 109 if (!shouldCompressOutput) { |
| 175 out(indentation); | 110 out(indentation); |
| 176 } | 111 } |
| 177 } | 112 } |
| 178 | 113 |
| 114 void beginSourceRange(Node node) { |
| 115 if (node.sourceInformation != null) { |
| 116 node.sourceInformation.beginMapping(outBuffer); |
| 117 } |
| 118 } |
| 119 |
| 120 void endSourceRange(Node node) { |
| 121 if (node.sourceInformation != null) { |
| 122 node.sourceInformation.endMapping(outBuffer); |
| 123 } |
| 124 } |
| 125 |
| 179 visit(Node node) { | 126 visit(Node node) { |
| 180 context.enterNode(node); | 127 beginSourceRange(node); |
| 128 if (monitor != null) monitor.enteringAst(node, outBuffer.length); |
| 129 |
| 181 node.accept(this); | 130 node.accept(this); |
| 182 context.exitNode(node); | 131 |
| 132 if (monitor != null) monitor.exitingAst(node, outBuffer.length); |
| 133 endSourceRange(node); |
| 183 } | 134 } |
| 184 | 135 |
| 185 visitCommaSeparated(List<Node> nodes, int hasRequiredType, | 136 visitCommaSeparated(List<Node> nodes, int hasRequiredType, |
| 186 {bool newInForInit, bool newAtStatementBegin}) { | 137 {bool newInForInit, bool newAtStatementBegin}) { |
| 187 for (int i = 0; i < nodes.length; i++) { | 138 for (int i = 0; i < nodes.length; i++) { |
| 188 if (i != 0) { | 139 if (i != 0) { |
| 189 atStatementBegin = false; | 140 atStatementBegin = false; |
| 190 out(","); | 141 out(","); |
| 191 spaceOut(); | 142 spaceOut(); |
| 192 } | 143 } |
| 193 visitNestedExpression(nodes[i], hasRequiredType, | 144 visitNestedExpression(nodes[i], hasRequiredType, |
| 194 newInForInit: newInForInit, | 145 newInForInit: newInForInit, |
| 195 newAtStatementBegin: newAtStatementBegin); | 146 newAtStatementBegin: newAtStatementBegin); |
| 196 } | 147 } |
| 197 } | 148 } |
| 198 | 149 |
| 199 visitAll(List<Node> nodes) { | 150 visitAll(List<Node> nodes) { |
| 200 nodes.forEach(visit); | 151 nodes.forEach(visit); |
| 201 } | 152 } |
| 202 | 153 |
| 203 visitProgram(Program program) { | 154 visitProgram(Program program) { |
| 204 visitAll(program.body); | 155 visitAll(program.body); |
| 205 } | 156 } |
| 206 | 157 |
| 158 visitBlob(Blob node) { |
| 159 outBuffer.addBuffer(node.buffer); |
| 160 } |
| 161 |
| 207 bool blockBody(Node body, {bool needsSeparation, bool needsNewline}) { | 162 bool blockBody(Node body, {bool needsSeparation, bool needsNewline}) { |
| 208 if (body is Block) { | 163 if (body is Block) { |
| 209 spaceOut(); | 164 spaceOut(); |
| 210 blockOut(body, false, needsNewline); | 165 blockOut(body, false, needsNewline); |
| 211 return true; | 166 return true; |
| 212 } | 167 } |
| 213 if (shouldCompressOutput && needsSeparation) { | 168 if (shouldCompressOutput && needsSeparation) { |
| 214 // If [shouldCompressOutput] is false, then the 'lineOut' will insert | 169 // If [shouldCompressOutput] is false, then the 'lineOut' will insert |
| 215 // the separation. | 170 // the separation. |
| 216 out(" "); | 171 out(" "); |
| 217 } else { | 172 } else { |
| 218 lineOut(); | 173 lineOut(); |
| 219 } | 174 } |
| 220 indentMore(); | 175 indentBlock(() => visit(body)); |
| 221 visit(body); | |
| 222 indentLess(); | |
| 223 return false; | 176 return false; |
| 224 } | 177 } |
| 225 | 178 |
| 226 void blockOutWithoutBraces(Node node) { | 179 void blockOutWithoutBraces(Node node) { |
| 227 if (node is Block) { | 180 if (node is Block) { |
| 228 context.enterNode(node); | 181 beginSourceRange(node); |
| 229 Block block = node; | 182 Block block = node; |
| 230 block.statements.forEach(blockOutWithoutBraces); | 183 block.statements.forEach(blockOutWithoutBraces); |
| 231 context.exitNode(node); | 184 endSourceRange(node); |
| 232 } else { | 185 } else { |
| 233 visit(node); | 186 visit(node); |
| 234 } | 187 } |
| 235 } | 188 } |
| 236 | 189 |
| 237 void blockOut(Block node, bool shouldIndent, bool needsNewline) { | 190 void blockOut(Block node, bool shouldIndent, bool needsNewline) { |
| 238 if (shouldIndent) indent(); | 191 if (shouldIndent) indent(); |
| 239 context.enterNode(node); | 192 beginSourceRange(node); |
| 240 out("{"); | 193 out("{"); |
| 241 lineOut(); | 194 lineOut(); |
| 242 indentMore(); | 195 indentBlock(() => node.statements.forEach(blockOutWithoutBraces)); |
| 243 node.statements.forEach(blockOutWithoutBraces); | |
| 244 indentLess(); | |
| 245 indent(); | 196 indent(); |
| 246 out("}"); | 197 out("}"); |
| 247 context.exitNode(node); | 198 endSourceRange(node); |
| 248 if (needsNewline) lineOut(); | 199 if (needsNewline) lineOut(); |
| 249 } | 200 } |
| 250 | 201 |
| 251 visitBlock(Block block) { | 202 visitBlock(Block block) { |
| 252 blockOut(block, true, true); | 203 blockOut(block, true, true); |
| 253 } | 204 } |
| 254 | 205 |
| 255 visitExpressionStatement(ExpressionStatement expressionStatement) { | 206 visitExpressionStatement(ExpressionStatement expressionStatement) { |
| 256 indent(); | 207 indent(); |
| 257 visitNestedExpression(expressionStatement.expression, EXPRESSION, | 208 visitNestedExpression(expressionStatement.expression, EXPRESSION, |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 | 400 |
| 450 visitSwitch(Switch node) { | 401 visitSwitch(Switch node) { |
| 451 outIndent("switch"); | 402 outIndent("switch"); |
| 452 spaceOut(); | 403 spaceOut(); |
| 453 out("("); | 404 out("("); |
| 454 visitNestedExpression(node.key, EXPRESSION, | 405 visitNestedExpression(node.key, EXPRESSION, |
| 455 newInForInit: false, newAtStatementBegin: false); | 406 newInForInit: false, newAtStatementBegin: false); |
| 456 out(")"); | 407 out(")"); |
| 457 spaceOut(); | 408 spaceOut(); |
| 458 outLn("{"); | 409 outLn("{"); |
| 459 indentMore(); | 410 indentBlock(() => visitAll(node.cases)); |
| 460 visitAll(node.cases); | |
| 461 indentLess(); | |
| 462 outIndentLn("}"); | 411 outIndentLn("}"); |
| 463 } | 412 } |
| 464 | 413 |
| 465 visitCase(Case node) { | 414 visitCase(Case node) { |
| 466 outIndent("case"); | 415 outIndent("case"); |
| 467 pendingSpace = true; | 416 pendingSpace = true; |
| 468 visitNestedExpression(node.expression, EXPRESSION, | 417 visitNestedExpression(node.expression, EXPRESSION, |
| 469 newInForInit: false, newAtStatementBegin: false); | 418 newInForInit: false, newAtStatementBegin: false); |
| 470 outLn(":"); | 419 outLn(":"); |
| 471 if (!node.body.statements.isEmpty) { | 420 if (!node.body.statements.isEmpty) { |
| 472 indentMore(); | 421 indentBlock(() => blockOutWithoutBraces(node.body)); |
| 473 blockOutWithoutBraces(node.body); | |
| 474 indentLess(); | |
| 475 } | 422 } |
| 476 } | 423 } |
| 477 | 424 |
| 478 visitDefault(Default node) { | 425 visitDefault(Default node) { |
| 479 outIndentLn("default:"); | 426 outIndentLn("default:"); |
| 480 if (!node.body.statements.isEmpty) { | 427 if (!node.body.statements.isEmpty) { |
| 481 indentMore(); | 428 indentBlock(() => blockOutWithoutBraces(node.body)); |
| 482 blockOutWithoutBraces(node.body); | |
| 483 indentLess(); | |
| 484 } | 429 } |
| 485 } | 430 } |
| 486 | 431 |
| 487 visitLabeledStatement(LabeledStatement node) { | 432 visitLabeledStatement(LabeledStatement node) { |
| 488 outIndent("${node.label}:"); | 433 outIndent("${node.label}:"); |
| 489 blockBody(node.body, needsSeparation: false, needsNewline: true); | 434 blockBody(node.body, needsSeparation: false, needsNewline: true); |
| 490 } | 435 } |
| 491 | 436 |
| 492 void functionOut(Fun fun, Node name, VarCollector vars) { | 437 void functionOut(Fun fun, Node name, VarCollector vars) { |
| 493 out("function"); | 438 out("function"); |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 690 rightPrecedenceRequirement = MULTIPLICATIVE; | 635 rightPrecedenceRequirement = MULTIPLICATIVE; |
| 691 break; | 636 break; |
| 692 case "*": | 637 case "*": |
| 693 case "/": | 638 case "/": |
| 694 case "%": | 639 case "%": |
| 695 leftPrecedenceRequirement = MULTIPLICATIVE; | 640 leftPrecedenceRequirement = MULTIPLICATIVE; |
| 696 // We cannot remove parenthesis for "*" because of precision issues. | 641 // We cannot remove parenthesis for "*" because of precision issues. |
| 697 rightPrecedenceRequirement = UNARY; | 642 rightPrecedenceRequirement = UNARY; |
| 698 break; | 643 break; |
| 699 default: | 644 default: |
| 700 context.error("Forgot operator: $op"); | 645 diagnosticListener |
| 646 .internalError(NO_LOCATION_SPANNABLE, "Forgot operator: $op"); |
| 701 } | 647 } |
| 702 | 648 |
| 703 visitNestedExpression(left, leftPrecedenceRequirement, | 649 visitNestedExpression(left, leftPrecedenceRequirement, |
| 704 newInForInit: inForInit, | 650 newInForInit: inForInit, |
| 705 newAtStatementBegin: atStatementBegin); | 651 newAtStatementBegin: atStatementBegin); |
| 706 | 652 |
| 707 if (op == "in" || op == "instanceof") { | 653 if (op == "in" || op == "instanceof") { |
| 708 // There are cases where the space is not required but without further | 654 // There are cases where the space is not required but without further |
| 709 // analysis we cannot know. | 655 // analysis we cannot know. |
| 710 out(" "); | 656 out(" "); |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 927 out(node.pattern); | 873 out(node.pattern); |
| 928 } | 874 } |
| 929 | 875 |
| 930 visitLiteralExpression(LiteralExpression node) { | 876 visitLiteralExpression(LiteralExpression node) { |
| 931 String template = node.template; | 877 String template = node.template; |
| 932 List<Expression> inputs = node.inputs; | 878 List<Expression> inputs = node.inputs; |
| 933 | 879 |
| 934 List<String> parts = template.split('#'); | 880 List<String> parts = template.split('#'); |
| 935 int inputsLength = inputs == null ? 0 : inputs.length; | 881 int inputsLength = inputs == null ? 0 : inputs.length; |
| 936 if (parts.length != inputsLength + 1) { | 882 if (parts.length != inputsLength + 1) { |
| 937 context.error('Wrong number of arguments for JS: $template'); | 883 diagnosticListener.internalError(NO_LOCATION_SPANNABLE, |
| 884 'Wrong number of arguments for JS: $template'); |
| 938 } | 885 } |
| 939 // Code that uses JS must take care of operator precedences, and | 886 // Code that uses JS must take care of operator precedences, and |
| 940 // put parenthesis if needed. | 887 // put parenthesis if needed. |
| 941 out(parts[0]); | 888 out(parts[0]); |
| 942 for (int i = 0; i < inputsLength; i++) { | 889 for (int i = 0; i < inputsLength; i++) { |
| 943 visit(inputs[i]); | 890 visit(inputs[i]); |
| 944 out(parts[i + 1]); | 891 out(parts[i + 1]); |
| 945 } | 892 } |
| 946 } | 893 } |
| 947 | 894 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1054 if (decl.allowRename) vars.add(decl.name); | 1001 if (decl.allowRename) vars.add(decl.name); |
| 1055 } | 1002 } |
| 1056 } | 1003 } |
| 1057 | 1004 |
| 1058 | 1005 |
| 1059 /** | 1006 /** |
| 1060 * Returns true, if the given node must be wrapped into braces when used | 1007 * Returns true, if the given node must be wrapped into braces when used |
| 1061 * as then-statement in an [If] that has an else branch. | 1008 * as then-statement in an [If] that has an else branch. |
| 1062 */ | 1009 */ |
| 1063 class DanglingElseVisitor extends BaseVisitor<bool> { | 1010 class DanglingElseVisitor extends BaseVisitor<bool> { |
| 1064 JavaScriptPrintingContext context; | 1011 leg.DiagnosticListener diagnosticListener; |
| 1065 | 1012 |
| 1066 DanglingElseVisitor(this.context); | 1013 DanglingElseVisitor(this.diagnosticListener); |
| 1067 | 1014 |
| 1068 bool visitProgram(Program node) => false; | 1015 bool visitProgram(Program node) => false; |
| 1069 | 1016 |
| 1070 bool visitNode(Statement node) { | 1017 bool visitNode(Statement node) { |
| 1071 context.error("Forgot node: $node"); | 1018 diagnosticListener |
| 1019 .internalError(NO_LOCATION_SPANNABLE, "Forgot node: $node"); |
| 1072 return null; | 1020 return null; |
| 1073 } | 1021 } |
| 1074 | 1022 |
| 1075 bool visitBlock(Block node) => false; | 1023 bool visitBlock(Block node) => false; |
| 1076 bool visitExpressionStatement(ExpressionStatement node) => false; | 1024 bool visitExpressionStatement(ExpressionStatement node) => false; |
| 1077 bool visitEmptyStatement(EmptyStatement node) => false; | 1025 bool visitEmptyStatement(EmptyStatement node) => false; |
| 1078 bool visitIf(If node) { | 1026 bool visitIf(If node) { |
| 1079 if (!node.hasElse) return true; | 1027 if (!node.hasElse) return true; |
| 1080 return node.otherwise.accept(this); | 1028 return node.otherwise.accept(this); |
| 1081 } | 1029 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 1100 bool visitDefault(Default node) => false; | 1048 bool visitDefault(Default node) => false; |
| 1101 bool visitFunctionDeclaration(FunctionDeclaration node) => false; | 1049 bool visitFunctionDeclaration(FunctionDeclaration node) => false; |
| 1102 bool visitLabeledStatement(LabeledStatement node) | 1050 bool visitLabeledStatement(LabeledStatement node) |
| 1103 => node.body.accept(this); | 1051 => node.body.accept(this); |
| 1104 bool visitLiteralStatement(LiteralStatement node) => true; | 1052 bool visitLiteralStatement(LiteralStatement node) => true; |
| 1105 | 1053 |
| 1106 bool visitExpression(Expression node) => false; | 1054 bool visitExpression(Expression node) => false; |
| 1107 } | 1055 } |
| 1108 | 1056 |
| 1109 | 1057 |
| 1058 CodeBuffer prettyPrint(Node node, leg.Compiler compiler, |
| 1059 {DumpInfoTask monitor, |
| 1060 bool allowVariableMinification: true}) { |
| 1061 Printer printer = |
| 1062 new Printer(compiler, monitor, |
| 1063 enableMinification: compiler.enableMinification, |
| 1064 allowVariableMinification: allowVariableMinification); |
| 1065 printer.visit(node); |
| 1066 return printer.outBuffer; |
| 1067 } |
| 1068 |
| 1069 |
| 1110 abstract class LocalNamer { | 1070 abstract class LocalNamer { |
| 1111 String getName(String oldName); | 1071 String getName(String oldName); |
| 1112 String declareVariable(String oldName); | 1072 String declareVariable(String oldName); |
| 1113 String declareParameter(String oldName); | 1073 String declareParameter(String oldName); |
| 1114 void enterScope(VarCollector vars); | 1074 void enterScope(VarCollector vars); |
| 1115 void leaveScope(); | 1075 void leaveScope(); |
| 1116 } | 1076 } |
| 1117 | 1077 |
| 1118 | 1078 |
| 1119 class IdentityNamer implements LocalNamer { | 1079 class IdentityNamer implements LocalNamer { |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1238 codes.add(nthLetter((n ~/ nameSpaceSize) % LETTERS)); | 1198 codes.add(nthLetter((n ~/ nameSpaceSize) % LETTERS)); |
| 1239 } | 1199 } |
| 1240 codes.add(charCodes.$0 + digit); | 1200 codes.add(charCodes.$0 + digit); |
| 1241 newName = new String.fromCharCodes(codes); | 1201 newName = new String.fromCharCodes(codes); |
| 1242 } | 1202 } |
| 1243 assert(new RegExp(r'[a-zA-Z][a-zA-Z0-9]*').hasMatch(newName)); | 1203 assert(new RegExp(r'[a-zA-Z][a-zA-Z0-9]*').hasMatch(newName)); |
| 1244 maps.last[oldName] = newName; | 1204 maps.last[oldName] = newName; |
| 1245 return newName; | 1205 return newName; |
| 1246 } | 1206 } |
| 1247 } | 1207 } |
| OLD | NEW |