| 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 860 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 871 for (int i = 0; i < inputsLength; i++) { | 871 for (int i = 0; i < inputsLength; i++) { |
| 872 visit(inputs[i]); | 872 visit(inputs[i]); |
| 873 out(parts[i + 1]); | 873 out(parts[i + 1]); |
| 874 } | 874 } |
| 875 } | 875 } |
| 876 | 876 |
| 877 visitLiteralStatement(LiteralStatement node) { | 877 visitLiteralStatement(LiteralStatement node) { |
| 878 outLn(node.code); | 878 outLn(node.code); |
| 879 } | 879 } |
| 880 | 880 |
| 881 visitJSExpression(JSExpression node) { | 881 visitInterpolatedNode(InterpolatedNode node) { |
| 882 compiler.internalError(NO_LOCATION_SPANNABLE, | 882 out('#${node.name}'); |
| 883 'JSPrinter should never see a JSExpression.'); | |
| 884 } | 883 } |
| 885 | 884 |
| 886 visitInterpolatedExpression(InterpolatedExpression node) { | 885 visitInterpolatedExpression(InterpolatedExpression node) => |
| 887 visit(node.value); | 886 visitInterpolatedNode(node); |
| 888 } | 887 |
| 888 visitInterpolatedLiteral(InterpolatedLiteral node) => |
| 889 visitInterpolatedNode(node); |
| 890 |
| 891 visitInterpolatedParameter(InterpolatedParameter node) => |
| 892 visitInterpolatedNode(node); |
| 893 |
| 894 visitInterpolatedSelector(InterpolatedSelector node) => |
| 895 visitInterpolatedNode(node); |
| 889 | 896 |
| 890 visitInterpolatedStatement(InterpolatedStatement node) { | 897 visitInterpolatedStatement(InterpolatedStatement node) { |
| 891 visit(node.value); | 898 outLn('#${node.name}'); |
| 892 } | 899 } |
| 893 | 900 |
| 894 void visitComment(Comment node) { | 901 void visitComment(Comment node) { |
| 895 if (shouldCompressOutput) return; | 902 if (shouldCompressOutput) return; |
| 896 String comment = node.comment.trim(); | 903 String comment = node.comment.trim(); |
| 897 if (comment.isEmpty) return; | 904 if (comment.isEmpty) return; |
| 898 for (var line in comment.split('\n')) { | 905 for (var line in comment.split('\n')) { |
| 899 if (comment.startsWith('//')) { | 906 if (comment.startsWith('//')) { |
| 900 outIndentLn(line.trim()); | 907 outIndentLn(line.trim()); |
| 901 } else { | 908 } else { |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1096 } | 1103 } |
| 1097 | 1104 |
| 1098 // Parameters go from a to z and variables go from z to a. This makes each | 1105 // Parameters go from a to z and variables go from z to a. This makes each |
| 1099 // argument list and each top-of-function var declaration look similar and | 1106 // argument list and each top-of-function var declaration look similar and |
| 1100 // helps gzip compress the file. If we have more than 26 arguments and | 1107 // helps gzip compress the file. If we have more than 26 arguments and |
| 1101 // variables then we meet somewhere in the middle of the alphabet. After | 1108 // variables then we meet somewhere in the middle of the alphabet. After |
| 1102 // that we give up trying to be nice to the compression algorithm and just | 1109 // that we give up trying to be nice to the compression algorithm and just |
| 1103 // use the same namespace for arguments and variables, starting with A, and | 1110 // use the same namespace for arguments and variables, starting with A, and |
| 1104 // moving on to a0, a1, etc. | 1111 // moving on to a0, a1, etc. |
| 1105 String declareVariable(String oldName) { | 1112 String declareVariable(String oldName) { |
| 1113 // Variables of this $form$ are used in pattern matching the message of JS |
| 1114 // exceptions, so should not be renamed. |
| 1115 // TODO(sra): Introduce a way for indicating in the JS text which variables |
| 1116 // should not be renamed. |
| 1117 if (oldName.startsWith(r'$') && oldName.endsWith(r'$')) return oldName; |
| 1118 |
| 1106 var newName; | 1119 var newName; |
| 1107 if (variableNumber + parameterNumber < LOWER_CASE_LETTERS) { | 1120 if (variableNumber + parameterNumber < LOWER_CASE_LETTERS) { |
| 1108 // Variables start from z and go backwards, for better gzipability. | 1121 // Variables start from z and go backwards, for better gzipability. |
| 1109 newName = getNameNumber(oldName, LOWER_CASE_LETTERS - 1 - variableNumber); | 1122 newName = getNameNumber(oldName, LOWER_CASE_LETTERS - 1 - variableNumber); |
| 1110 } else { | 1123 } else { |
| 1111 // After 26 variables and parameters we allocate them in the same order. | 1124 // After 26 variables and parameters we allocate them in the same order. |
| 1112 newName = getNameNumber(oldName, variableNumber + parameterNumber); | 1125 newName = getNameNumber(oldName, variableNumber + parameterNumber); |
| 1113 } | 1126 } |
| 1114 variableNumber++; | 1127 variableNumber++; |
| 1115 return newName; | 1128 return newName; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1155 codes.add(nthLetter((n ~/ nameSpaceSize) % LETTERS)); | 1168 codes.add(nthLetter((n ~/ nameSpaceSize) % LETTERS)); |
| 1156 } | 1169 } |
| 1157 codes.add(charCodes.$0 + digit); | 1170 codes.add(charCodes.$0 + digit); |
| 1158 newName = new String.fromCharCodes(codes); | 1171 newName = new String.fromCharCodes(codes); |
| 1159 } | 1172 } |
| 1160 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)); |
| 1161 maps.last[oldName] = newName; | 1174 maps.last[oldName] = newName; |
| 1162 return newName; | 1175 return newName; |
| 1163 } | 1176 } |
| 1164 } | 1177 } |
| OLD | NEW |