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 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$]'); | 18 static final identifierRegexp = new RegExp(r'^[a-zA-Z_0-9$]'); |
19 | 19 |
20 Printer(leg.Compiler compiler, { allowVariableMinification: true }) | 20 Printer(leg.Compiler compiler, { allowVariableMinification: true }) |
21 : shouldCompressOutput = compiler.enableMinification, | 21 : shouldCompressOutput = compiler.enableMinification, |
22 this.compiler = compiler, | 22 this.compiler = compiler, |
23 outBuffer = new leg.CodeBuffer(), | 23 outBuffer = new leg.CodeBuffer(), |
24 danglingElseVisitor = new DanglingElseVisitor(compiler), | 24 danglingElseVisitor = new DanglingElseVisitor(compiler), |
25 localNamer = determineRenamer(compiler.enableMinification, | 25 namer = determineRenamer(compiler.enableMinification, |
26 allowVariableMinification); | 26 allowVariableMinification); |
27 | 27 |
28 static LocalNamer determineRenamer(bool shouldCompressOutput, | 28 static Namer determineRenamer(bool shouldCompressOutput, |
29 bool allowVariableMinification) { | 29 bool allowVariableMinification) { |
30 return (shouldCompressOutput && allowVariableMinification) | 30 return (shouldCompressOutput && allowVariableMinification) |
31 ? new MinifyRenamer() : new IdentityNamer(); | 31 ? new MinifyRenamer() : new IdentityNamer(); |
32 } | 32 } |
33 | 33 |
34 void spaceOut() { | 34 void spaceOut() { |
35 if (!shouldCompressOutput) out(" "); | 35 if (!shouldCompressOutput) out(" "); |
36 } | 36 } |
37 void lineOut() { | 37 void lineOut() { |
38 if (!shouldCompressOutput) out("\n"); | 38 if (!shouldCompressOutput) out("\n"); |
39 } | 39 } |
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 } | 384 } |
385 | 385 |
386 void functionOut(Fun fun, Node name, VarCollector vars) { | 386 void functionOut(Fun fun, Node name, VarCollector vars) { |
387 out("function"); | 387 out("function"); |
388 if (name != null) { | 388 if (name != null) { |
389 out(" "); | 389 out(" "); |
390 // Name must be a [Decl]. Therefore only test for primary expressions. | 390 // Name must be a [Decl]. Therefore only test for primary expressions. |
391 visitNestedExpression(name, PRIMARY, | 391 visitNestedExpression(name, PRIMARY, |
392 newInForInit: false, newAtStatementBegin: false); | 392 newInForInit: false, newAtStatementBegin: false); |
393 } | 393 } |
394 localNamer.enterScope(vars); | 394 namer.enterScope(vars); |
395 out("("); | 395 out("("); |
396 if (fun.params != null) { | 396 if (fun.params != null) { |
397 visitCommaSeparated(fun.params, PRIMARY, | 397 visitCommaSeparated(fun.params, PRIMARY, |
398 newInForInit: false, newAtStatementBegin: false); | 398 newInForInit: false, newAtStatementBegin: false); |
399 } | 399 } |
400 out(")"); | 400 out(")"); |
401 blockBody(fun.body, needsSeparation: false, needsNewline: false); | 401 blockBody(fun.body, needsSeparation: false, needsNewline: false); |
402 localNamer.leaveScope(); | 402 namer.leaveScope(); |
403 } | 403 } |
404 | 404 |
405 visitFunctionDeclaration(FunctionDeclaration declaration) { | 405 visitFunctionDeclaration(FunctionDeclaration declaration) { |
406 VarCollector vars = new VarCollector(); | 406 VarCollector vars = new VarCollector(); |
407 vars.visitFunctionDeclaration(declaration); | 407 vars.visitFunctionDeclaration(declaration); |
408 indent(); | 408 indent(); |
409 functionOut(declaration.function, declaration.name, vars); | 409 functionOut(declaration.function, declaration.name, vars); |
410 lineOut(); | 410 lineOut(); |
411 } | 411 } |
412 | 412 |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
632 } | 632 } |
633 | 633 |
634 visitPostfix(Postfix postfix) { | 634 visitPostfix(Postfix postfix) { |
635 visitNestedExpression(postfix.argument, LEFT_HAND_SIDE, | 635 visitNestedExpression(postfix.argument, LEFT_HAND_SIDE, |
636 newInForInit: inForInit, | 636 newInForInit: inForInit, |
637 newAtStatementBegin: atStatementBegin); | 637 newAtStatementBegin: atStatementBegin); |
638 out(postfix.op); | 638 out(postfix.op); |
639 } | 639 } |
640 | 640 |
641 visitVariableUse(VariableUse ref) { | 641 visitVariableUse(VariableUse ref) { |
642 out(localNamer.getName(ref.name)); | 642 out(namer.getName(ref.name)); |
643 } | 643 } |
644 | 644 |
645 visitThis(This node) { | 645 visitThis(This node) { |
646 out("this"); | 646 out("this"); |
647 } | 647 } |
648 | 648 |
649 visitVariableDeclaration(VariableDeclaration decl) { | 649 visitVariableDeclaration(VariableDeclaration decl) { |
650 out(localNamer.getName(decl.name)); | 650 out(namer.getName(decl.name)); |
651 } | 651 } |
652 | 652 |
653 visitParameter(Parameter param) { | 653 visitParameter(Parameter param) { |
654 out(localNamer.getName(param.name)); | 654 out(namer.getName(param.name)); |
655 } | 655 } |
656 | 656 |
657 bool isDigit(int charCode) { | 657 bool isDigit(int charCode) { |
658 return charCodes.$0 <= charCode && charCode <= charCodes.$9; | 658 return charCodes.$0 <= charCode && charCode <= charCodes.$9; |
659 } | 659 } |
660 | 660 |
661 bool isValidJavaScriptId(String field) { | 661 bool isValidJavaScriptId(String field) { |
662 if (field.length < 3) return false; | 662 if (field.length < 3) return false; |
663 // Ignore the leading and trailing string-delimiter. | 663 // Ignore the leading and trailing string-delimiter. |
664 for (int i = 1; i < field.length - 1; i++) { | 664 for (int i = 1; i < field.length - 1; i++) { |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
937 leg.CodeBuffer prettyPrint(Node node, leg.Compiler compiler, | 937 leg.CodeBuffer prettyPrint(Node node, leg.Compiler compiler, |
938 { allowVariableMinification: true }) { | 938 { allowVariableMinification: true }) { |
939 Printer printer = | 939 Printer printer = |
940 new Printer(compiler, | 940 new Printer(compiler, |
941 allowVariableMinification: allowVariableMinification); | 941 allowVariableMinification: allowVariableMinification); |
942 printer.visit(node); | 942 printer.visit(node); |
943 return printer.outBuffer; | 943 return printer.outBuffer; |
944 } | 944 } |
945 | 945 |
946 | 946 |
947 abstract class LocalNamer { | 947 abstract class Namer { |
948 String getName(String oldName); | 948 String getName(String oldName); |
949 String declareVariable(String oldName); | 949 String declareVariable(String oldName); |
950 String declareParameter(String oldName); | 950 String declareParameter(String oldName); |
951 void enterScope(VarCollector vars); | 951 void enterScope(VarCollector vars); |
952 void leaveScope(); | 952 void leaveScope(); |
953 } | 953 } |
954 | 954 |
955 | 955 |
956 class IdentityNamer implements LocalNamer { | 956 class IdentityNamer implements Namer { |
957 String getName(String oldName) => oldName; | 957 String getName(String oldName) => oldName; |
958 String declareVariable(String oldName) => oldName; | 958 String declareVariable(String oldName) => oldName; |
959 String declareParameter(String oldName) => oldName; | 959 String declareParameter(String oldName) => oldName; |
960 void enterScope(VarCollector vars) {} | 960 void enterScope(VarCollector vars) {} |
961 void leaveScope() {} | 961 void leaveScope() {} |
962 } | 962 } |
963 | 963 |
964 | 964 |
965 class MinifyRenamer implements LocalNamer { | 965 class MinifyRenamer implements Namer { |
966 final List<Map<String, String>> maps = []; | 966 final List<Map<String, String>> maps = []; |
967 final List<int> parameterNumberStack = []; | 967 final List<int> parameterNumberStack = []; |
968 final List<int> variableNumberStack = []; | 968 final List<int> variableNumberStack = []; |
969 int parameterNumber = 0; | 969 int parameterNumber = 0; |
970 int variableNumber = 0; | 970 int variableNumber = 0; |
971 | 971 |
972 MinifyRenamer(); | 972 MinifyRenamer(); |
973 | 973 |
974 void enterScope(VarCollector vars) { | 974 void enterScope(VarCollector vars) { |
975 maps.add(new Map<String, String>()); | 975 maps.add(new Map<String, String>()); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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(new 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 |