| 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 extends Indentation implements NodeVisitor { | 7 class Printer extends Indentation implements NodeVisitor { |
| 8 final bool shouldCompressOutput; | 8 final bool shouldCompressOutput; |
| 9 leg.Compiler compiler; | 9 leg.Compiler compiler; |
| 10 leg.CodeBuffer outBuffer; | 10 CodeBuffer outBuffer; |
| 11 bool inForInit = false; | 11 bool inForInit = false; |
| 12 bool atStatementBegin = false; | 12 bool atStatementBegin = false; |
| 13 final DanglingElseVisitor danglingElseVisitor; | 13 final DanglingElseVisitor danglingElseVisitor; |
| 14 final LocalNamer localNamer; | 14 final LocalNamer localNamer; |
| 15 bool pendingSemicolon = false; | 15 bool pendingSemicolon = false; |
| 16 bool pendingSpace = false; | 16 bool pendingSpace = false; |
| 17 DumpInfoTask monitor = null; | 17 DumpInfoTask monitor = null; |
| 18 | 18 |
| 19 static final identifierCharacterRegExp = new RegExp(r'^[a-zA-Z_0-9$]'); | 19 static final identifierCharacterRegExp = new RegExp(r'^[a-zA-Z_0-9$]'); |
| 20 static final expressionContinuationRegExp = new RegExp(r'^[-+([]'); | 20 static final expressionContinuationRegExp = new RegExp(r'^[-+([]'); |
| 21 | 21 |
| 22 Printer(leg.Compiler compiler, DumpInfoTask monitor, | 22 Printer(leg.Compiler compiler, DumpInfoTask monitor, |
| 23 { allowVariableMinification: true }) | 23 { allowVariableMinification: true }) |
| 24 : shouldCompressOutput = compiler.enableMinification, | 24 : shouldCompressOutput = compiler.enableMinification, |
| 25 monitor = monitor, | 25 monitor = monitor, |
| 26 this.compiler = compiler, | 26 this.compiler = compiler, |
| 27 outBuffer = new leg.CodeBuffer(), | 27 outBuffer = new CodeBuffer(), |
| 28 danglingElseVisitor = new DanglingElseVisitor(compiler), | 28 danglingElseVisitor = new DanglingElseVisitor(compiler), |
| 29 localNamer = determineRenamer(compiler.enableMinification, | 29 localNamer = determineRenamer(compiler.enableMinification, |
| 30 allowVariableMinification); | 30 allowVariableMinification); |
| 31 | 31 |
| 32 static LocalNamer determineRenamer(bool shouldCompressOutput, | 32 static LocalNamer determineRenamer(bool shouldCompressOutput, |
| 33 bool allowVariableMinification) { | 33 bool allowVariableMinification) { |
| 34 return (shouldCompressOutput && allowVariableMinification) | 34 return (shouldCompressOutput && allowVariableMinification) |
| 35 ? new MinifyRenamer() : new IdentityNamer(); | 35 ? new MinifyRenamer() : new IdentityNamer(); |
| 36 } | 36 } |
| 37 | 37 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 51 int get lastCharCode { | 51 int get lastCharCode { |
| 52 if (lastAddedString == null) return 0; | 52 if (lastAddedString == null) return 0; |
| 53 assert(lastAddedString.length != ""); | 53 assert(lastAddedString.length != ""); |
| 54 return lastAddedString.codeUnitAt(lastAddedString.length - 1); | 54 return lastAddedString.codeUnitAt(lastAddedString.length - 1); |
| 55 } | 55 } |
| 56 | 56 |
| 57 void out(String str) { | 57 void out(String str) { |
| 58 if (str != "") { | 58 if (str != "") { |
| 59 if (pendingSemicolon) { | 59 if (pendingSemicolon) { |
| 60 if (!shouldCompressOutput) { | 60 if (!shouldCompressOutput) { |
| 61 outBuffer.add(";"); | 61 outBuffer.write(";"); |
| 62 } else if (str != "}") { | 62 } else if (str != "}") { |
| 63 // We want to output newline instead of semicolon because it makes | 63 // 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- | 64 // the raw stack traces much easier to read and it also makes line- |
| 65 // based tools like diff work much better. JavaScript will | 65 // based tools like diff work much better. JavaScript will |
| 66 // automatically insert the semicolon at the newline if it means a | 66 // 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 | 67 // 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 | 68 // next line is not something that can be glued onto a valid |
| 69 // expression to make a new valid expression. | 69 // expression to make a new valid expression. |
| 70 | 70 |
| 71 // 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 |
| 72 // 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 |
| 73 // than newlines because the former doesn't need escaping. | 73 // than newlines because the former doesn't need escaping. |
| 74 if (USE_NEW_EMITTER || expressionContinuationRegExp.hasMatch(str)) { | 74 if (USE_NEW_EMITTER || expressionContinuationRegExp.hasMatch(str)) { |
| 75 outBuffer.add(";"); | 75 outBuffer.write(";"); |
| 76 } else { | 76 } else { |
| 77 outBuffer.add("\n"); | 77 outBuffer.write("\n"); |
| 78 } | 78 } |
| 79 } | 79 } |
| 80 } | 80 } |
| 81 if (pendingSpace && | 81 if (pendingSpace && |
| 82 (!shouldCompressOutput || identifierCharacterRegExp.hasMatch(str))) { | 82 (!shouldCompressOutput || identifierCharacterRegExp.hasMatch(str))) { |
| 83 outBuffer.add(" "); | 83 outBuffer.write(" "); |
| 84 } | 84 } |
| 85 pendingSpace = false; | 85 pendingSpace = false; |
| 86 pendingSemicolon = false; | 86 pendingSemicolon = false; |
| 87 outBuffer.add(str); | 87 outBuffer.write(str); |
| 88 lastAddedString = str; | 88 lastAddedString = str; |
| 89 } | 89 } |
| 90 } | 90 } |
| 91 | 91 |
| 92 void outLn(String str) { | 92 void outLn(String str) { |
| 93 out(str); | 93 out(str); |
| 94 lineOut(); | 94 lineOut(); |
| 95 } | 95 } |
| 96 | 96 |
| 97 void outSemicolonLn() { | 97 void outSemicolonLn() { |
| (...skipping 921 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1019 bool visitDefault(Default node) => false; | 1019 bool visitDefault(Default node) => false; |
| 1020 bool visitFunctionDeclaration(FunctionDeclaration node) => false; | 1020 bool visitFunctionDeclaration(FunctionDeclaration node) => false; |
| 1021 bool visitLabeledStatement(LabeledStatement node) | 1021 bool visitLabeledStatement(LabeledStatement node) |
| 1022 => node.body.accept(this); | 1022 => node.body.accept(this); |
| 1023 bool visitLiteralStatement(LiteralStatement node) => true; | 1023 bool visitLiteralStatement(LiteralStatement node) => true; |
| 1024 | 1024 |
| 1025 bool visitExpression(Expression node) => false; | 1025 bool visitExpression(Expression node) => false; |
| 1026 } | 1026 } |
| 1027 | 1027 |
| 1028 | 1028 |
| 1029 leg.CodeBuffer prettyPrint(Node node, leg.Compiler compiler, | 1029 CodeBuffer prettyPrint(Node node, leg.Compiler compiler, |
| 1030 {DumpInfoTask monitor, | 1030 {DumpInfoTask monitor, |
| 1031 allowVariableMinification: true}) { | 1031 bool allowVariableMinification: true}) { |
| 1032 Printer printer = | 1032 Printer printer = |
| 1033 new Printer(compiler, monitor, | 1033 new Printer(compiler, monitor, |
| 1034 allowVariableMinification: allowVariableMinification); | 1034 allowVariableMinification: allowVariableMinification); |
| 1035 printer.visit(node); | 1035 printer.visit(node); |
| 1036 return printer.outBuffer; | 1036 return printer.outBuffer; |
| 1037 } | 1037 } |
| 1038 | 1038 |
| 1039 | 1039 |
| 1040 abstract class LocalNamer { | 1040 abstract class LocalNamer { |
| 1041 String getName(String oldName); | 1041 String getName(String oldName); |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1168 codes.add(nthLetter((n ~/ nameSpaceSize) % LETTERS)); | 1168 codes.add(nthLetter((n ~/ nameSpaceSize) % LETTERS)); |
| 1169 } | 1169 } |
| 1170 codes.add(charCodes.$0 + digit); | 1170 codes.add(charCodes.$0 + digit); |
| 1171 newName = new String.fromCharCodes(codes); | 1171 newName = new String.fromCharCodes(codes); |
| 1172 } | 1172 } |
| 1173 assert(new RegExp(r'[a-zA-Z][a-zA-Z0-9]*').hasMatch(newName)); | 1173 assert(new RegExp(r'[a-zA-Z][a-zA-Z0-9]*').hasMatch(newName)); |
| 1174 maps.last[oldName] = newName; | 1174 maps.last[oldName] = newName; |
| 1175 return newName; | 1175 return newName; |
| 1176 } | 1176 } |
| 1177 } | 1177 } |
| OLD | NEW |