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 class Printer implements NodeVisitor { | 5 class Printer implements NodeVisitor { |
6 final bool shouldCompressOutput; | 6 final bool shouldCompressOutput; |
7 leg.Compiler compiler; | 7 leg.Compiler compiler; |
8 leg.CodeBuffer outBuffer; | 8 leg.CodeBuffer outBuffer; |
9 int indentLevel = 0; | 9 int indentLevel = 0; |
10 bool inForInit = false; | 10 bool inForInit = false; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 | 65 |
66 visit(Node node) { | 66 visit(Node node) { |
67 if (node.sourcePosition != null) outBuffer.beginMappedRange(); | 67 if (node.sourcePosition != null) outBuffer.beginMappedRange(); |
68 recordSourcePosition(node.sourcePosition); | 68 recordSourcePosition(node.sourcePosition); |
69 node.accept(this); | 69 node.accept(this); |
70 recordSourcePosition(node.endSourcePosition); | 70 recordSourcePosition(node.endSourcePosition); |
71 if (node.sourcePosition != null) outBuffer.endMappedRange(); | 71 if (node.sourcePosition != null) outBuffer.endMappedRange(); |
72 } | 72 } |
73 | 73 |
74 visitCommaSeparated(List<Node> nodes, int hasRequiredType, | 74 visitCommaSeparated(List<Node> nodes, int hasRequiredType, |
75 [bool newInForInit, bool newAtStatementBegin]) { | 75 {bool newInForInit, bool newAtStatementBegin}) { |
76 for (int i = 0; i < nodes.length; i++) { | 76 for (int i = 0; i < nodes.length; i++) { |
77 if (i != 0) { | 77 if (i != 0) { |
78 atStatementBegin = false; | 78 atStatementBegin = false; |
79 out(","); | 79 out(","); |
80 spaceOut(); | 80 spaceOut(); |
81 } | 81 } |
82 visitNestedExpression(nodes[i], hasRequiredType, | 82 visitNestedExpression(nodes[i], hasRequiredType, |
83 newInForInit, newAtStatementBegin); | 83 newInForInit: newInForInit, |
| 84 newAtStatementBegin: newAtStatementBegin); |
84 } | 85 } |
85 } | 86 } |
86 | 87 |
87 visitAll(List<Node> nodes) { | 88 visitAll(List<Node> nodes) { |
88 nodes.forEach(visit); | 89 nodes.forEach(visit); |
89 } | 90 } |
90 | 91 |
91 visitProgram(Program program) { | 92 visitProgram(Program program) { |
92 visitAll(program.body); | 93 visitAll(program.body); |
93 } | 94 } |
94 | 95 |
95 bool blockBody(Node body, [bool needsSeparation, bool needsNewline]) { | 96 bool blockBody(Node body, {bool needsSeparation, bool needsNewline}) { |
96 if (body is Block) { | 97 if (body is Block) { |
97 spaceOut(); | 98 spaceOut(); |
98 blockOut(body, false, needsNewline); | 99 blockOut(body, false, needsNewline); |
99 return true; | 100 return true; |
100 } | 101 } |
101 if (shouldCompressOutput && needsSeparation) { | 102 if (shouldCompressOutput && needsSeparation) { |
102 // If [shouldCompressOutput] is false, then the 'lineOut' will insert | 103 // If [shouldCompressOutput] is false, then the 'lineOut' will insert |
103 // the separation. | 104 // the separation. |
104 out(" "); | 105 out(" "); |
105 } else { | 106 } else { |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 namer.leaveScope(); | 370 namer.leaveScope(); |
370 } | 371 } |
371 | 372 |
372 visitFunctionDeclaration(FunctionDeclaration declaration) { | 373 visitFunctionDeclaration(FunctionDeclaration declaration) { |
373 indent(); | 374 indent(); |
374 functionOut(declaration.function, declaration.name); | 375 functionOut(declaration.function, declaration.name); |
375 lineOut(); | 376 lineOut(); |
376 } | 377 } |
377 | 378 |
378 visitNestedExpression(Expression node, int requiredPrecedence, | 379 visitNestedExpression(Expression node, int requiredPrecedence, |
379 [bool newInForInit, bool newAtStatementBegin]) { | 380 {bool newInForInit, bool newAtStatementBegin}) { |
380 bool needsParentheses = | 381 bool needsParentheses = |
381 // a - (b + c). | 382 // a - (b + c). |
382 (requiredPrecedence != EXPRESSION && | 383 (requiredPrecedence != EXPRESSION && |
383 node.precedenceLevel < requiredPrecedence) || | 384 node.precedenceLevel < requiredPrecedence) || |
384 // for (a = (x in o); ... ; ... ) { ... } | 385 // for (a = (x in o); ... ; ... ) { ... } |
385 (newInForInit && node is Binary && (node as Binary).op == "in") || | 386 (newInForInit && node is Binary && (node as Binary).op == "in") || |
386 // (function() { ... })(). | 387 // (function() { ... })(). |
387 // ({a: 2, b: 3}.toString()). | 388 // ({a: 2, b: 3}.toString()). |
388 (newAtStatementBegin && (node is NamedFunction || | 389 (newAtStatementBegin && (node is NamedFunction || |
389 node is Fun || | 390 node is Fun || |
(...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
878 } | 879 } |
879 | 880 |
880 String declareName(String oldName) { | 881 String declareName(String oldName) { |
881 if (maps.length == 0) return oldName; | 882 if (maps.length == 0) return oldName; |
882 var newName = "z${nameNumber++}"; | 883 var newName = "z${nameNumber++}"; |
883 maps.last()[oldName] = newName; | 884 maps.last()[oldName] = newName; |
884 return newName; | 885 return newName; |
885 } | 886 } |
886 | 887 |
887 } | 888 } |
OLD | NEW |