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; |
11 int indentLevel = 0; | 11 int indentLevel = 0; |
12 bool inForInit = false; | 12 bool inForInit = false; |
13 bool atStatementBegin = false; | 13 bool atStatementBegin = false; |
14 final DanglingElseVisitor danglingElseVisitor; | 14 final DanglingElseVisitor danglingElseVisitor; |
15 final LocalNamer localNamer; | 15 final LocalNamer localNamer; |
16 bool pendingSemicolon = false; | 16 bool pendingSemicolon = false; |
17 bool pendingSpace = false; | 17 bool pendingSpace = false; |
18 static final identifierCharacterRegExp = new RegExp(r'^[a-zA-Z_0-9$]'); | 18 static final identifierCharacterRegExp = new RegExp(r'^[a-zA-Z_0-9$]'); |
| 19 static final expressionContinuationRegExp = new RegExp(r'^[-+([]'); |
19 | 20 |
20 Printer(leg.Compiler compiler, { allowVariableMinification: true }) | 21 Printer(leg.Compiler compiler, { allowVariableMinification: true }) |
21 : shouldCompressOutput = compiler.enableMinification, | 22 : shouldCompressOutput = compiler.enableMinification, |
22 this.compiler = compiler, | 23 this.compiler = compiler, |
23 outBuffer = new leg.CodeBuffer(), | 24 outBuffer = new leg.CodeBuffer(), |
24 danglingElseVisitor = new DanglingElseVisitor(compiler), | 25 danglingElseVisitor = new DanglingElseVisitor(compiler), |
25 localNamer = determineRenamer(compiler.enableMinification, | 26 localNamer = determineRenamer(compiler.enableMinification, |
26 allowVariableMinification); | 27 allowVariableMinification); |
27 | 28 |
28 static LocalNamer determineRenamer(bool shouldCompressOutput, | 29 static LocalNamer determineRenamer(bool shouldCompressOutput, |
(...skipping 16 matching lines...) Expand all Loading... |
45 | 46 |
46 String lastAddedString = null; | 47 String lastAddedString = null; |
47 int get lastCharCode { | 48 int get lastCharCode { |
48 if (lastAddedString == null) return 0; | 49 if (lastAddedString == null) return 0; |
49 assert(lastAddedString.length != ""); | 50 assert(lastAddedString.length != ""); |
50 return lastAddedString.charCodeAt(lastAddedString.length - 1); | 51 return lastAddedString.charCodeAt(lastAddedString.length - 1); |
51 } | 52 } |
52 | 53 |
53 void out(String str) { | 54 void out(String str) { |
54 if (str != "") { | 55 if (str != "") { |
55 if (pendingSemicolon && (!shouldCompressOutput || str != "}")) { | 56 if (pendingSemicolon) { |
56 outBuffer.add(";"); | 57 if (!shouldCompressOutput) { |
| 58 outBuffer.add(";"); |
| 59 } else if (str != "}") { |
| 60 // We want to output newline instead of semicolon because it makes |
| 61 // the raw stack traces much easier to read and it also makes line- |
| 62 // based tools like diff work much better. JavaScript will |
| 63 // automatically insert the semicolon at the newline if it means a |
| 64 // parsing error is avoided, so we can only do this trick if the |
| 65 // next line is not something that can be glued onto a valid |
| 66 // expression to make a new valid expression. |
| 67 if (expressionContinuationRegExp.hasMatch(str)) { |
| 68 outBuffer.add(";"); |
| 69 } else { |
| 70 outBuffer.add("\n"); |
| 71 } |
| 72 } |
57 } | 73 } |
58 if (pendingSpace && | 74 if (pendingSpace && |
59 (!shouldCompressOutput || identifierCharacterRegExp.hasMatch(str))) { | 75 (!shouldCompressOutput || identifierCharacterRegExp.hasMatch(str))) { |
60 outBuffer.add(" "); | 76 outBuffer.add(" "); |
61 } | 77 } |
62 pendingSpace = false; | 78 pendingSpace = false; |
63 pendingSemicolon = false; | 79 pendingSemicolon = false; |
64 outBuffer.add(str); | 80 outBuffer.add(str); |
65 lastAddedString = str; | 81 lastAddedString = str; |
66 } | 82 } |
(...skipping 1019 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1086 codes.add(nthLetter((n ~/ nameSpaceSize) % LETTERS)); | 1102 codes.add(nthLetter((n ~/ nameSpaceSize) % LETTERS)); |
1087 } | 1103 } |
1088 codes.add(charCodes.$0 + digit); | 1104 codes.add(charCodes.$0 + digit); |
1089 newName = new String.fromCharCodes(codes); | 1105 newName = new String.fromCharCodes(codes); |
1090 } | 1106 } |
1091 assert(new RegExp(r'[a-zA-Z][a-zA-Z0-9]*').hasMatch(newName)); | 1107 assert(new RegExp(r'[a-zA-Z][a-zA-Z0-9]*').hasMatch(newName)); |
1092 maps.last[oldName] = newName; | 1108 maps.last[oldName] = newName; |
1093 return newName; | 1109 return newName; |
1094 } | 1110 } |
1095 } | 1111 } |
OLD | NEW |