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 Namer namer; | 15 final Namer namer; |
16 bool pendingSemicolon = false; | 16 bool pendingSemicolon = false; |
17 bool pendingSpace = false; | 17 bool pendingSpace = false; |
18 static final identifierRegexp = new RegExp(r'^[a-zA-Z_0-9$]'); | |
19 | 18 |
20 Printer(leg.Compiler compiler, { allowVariableMinification: true }) | 19 Printer(leg.Compiler compiler, { allowVariableMinification: true }) |
21 : shouldCompressOutput = compiler.enableMinification, | 20 : shouldCompressOutput = compiler.enableMinification, |
22 this.compiler = compiler, | 21 this.compiler = compiler, |
23 outBuffer = new leg.CodeBuffer(), | 22 outBuffer = new leg.CodeBuffer(), |
24 danglingElseVisitor = new DanglingElseVisitor(compiler), | 23 danglingElseVisitor = new DanglingElseVisitor(compiler), |
25 namer = determineRenamer(compiler.enableMinification, | 24 namer = determineRenamer(compiler.enableMinification, |
26 allowVariableMinification); | 25 allowVariableMinification); |
27 | 26 |
28 static Namer determineRenamer(bool shouldCompressOutput, | 27 static Namer determineRenamer(bool shouldCompressOutput, |
(...skipping 11 matching lines...) Expand all Loading... |
40 | 39 |
41 String lastAddedString = null; | 40 String lastAddedString = null; |
42 int get lastCharCode { | 41 int get lastCharCode { |
43 if (lastAddedString == null) return 0; | 42 if (lastAddedString == null) return 0; |
44 assert(lastAddedString.length != ""); | 43 assert(lastAddedString.length != ""); |
45 return lastAddedString.charCodeAt(lastAddedString.length - 1); | 44 return lastAddedString.charCodeAt(lastAddedString.length - 1); |
46 } | 45 } |
47 | 46 |
48 void out(String str) { | 47 void out(String str) { |
49 if (str != "") { | 48 if (str != "") { |
| 49 const identifierRegexp = const RegExp(r'^[a-zA-Z_0-9$]'); |
50 if (pendingSemicolon && (!shouldCompressOutput || str != "}")) { | 50 if (pendingSemicolon && (!shouldCompressOutput || str != "}")) { |
51 outBuffer.add(";"); | 51 outBuffer.add(";"); |
52 } | 52 } |
53 if (pendingSpace && | 53 if (pendingSpace && |
54 (!shouldCompressOutput || identifierRegexp.hasMatch(str))) { | 54 (!shouldCompressOutput || identifierRegexp.hasMatch(str))) { |
55 outBuffer.add(" "); | 55 outBuffer.add(" "); |
56 } | 56 } |
57 pendingSpace = false; | 57 pendingSpace = false; |
58 pendingSemicolon = false; | 58 pendingSemicolon = false; |
59 outBuffer.add(str); | 59 outBuffer.add(str); |
(...skipping 1000 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1060 nameSpaceSize *= LETTERS; | 1060 nameSpaceSize *= LETTERS; |
1061 } | 1061 } |
1062 var codes = <int>[]; | 1062 var codes = <int>[]; |
1063 for (var i = 0; i < alphaChars; i++) { | 1063 for (var i = 0; i < alphaChars; i++) { |
1064 nameSpaceSize ~/= LETTERS; | 1064 nameSpaceSize ~/= LETTERS; |
1065 codes.add(nthLetter((n ~/ nameSpaceSize) % LETTERS)); | 1065 codes.add(nthLetter((n ~/ nameSpaceSize) % LETTERS)); |
1066 } | 1066 } |
1067 codes.add(charCodes.$0 + digit); | 1067 codes.add(charCodes.$0 + digit); |
1068 newName = new String.fromCharCodes(codes); | 1068 newName = new String.fromCharCodes(codes); |
1069 } | 1069 } |
1070 assert(new RegExp(r'[a-zA-Z][a-zA-Z0-9]*').hasMatch(newName)); | 1070 assert(const RegExp(r'[a-zA-Z][a-zA-Z0-9]*').hasMatch(newName)); |
1071 maps.last[oldName] = newName; | 1071 maps.last[oldName] = newName; |
1072 return newName; | 1072 return newName; |
1073 } | 1073 } |
1074 } | 1074 } |
OLD | NEW |