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