OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 /// Helper for debug JS nodes. | 5 /// Helper for debug JS nodes. |
6 | 6 |
7 library js.debug; | 7 library js.debug; |
8 | 8 |
9 import 'package:js_ast/js_ast.dart'; | 9 import 'package:js_ast/js_ast.dart'; |
10 import '../util/util.dart' show Indentation, Tagging; | 10 |
| 11 import '../io/code_output.dart' show |
| 12 BufferedCodeOutput; |
| 13 import '../util/util.dart' show |
| 14 Indentation, |
| 15 Tagging; |
11 | 16 |
12 /// Unparse the JavaScript [node]. | 17 /// Unparse the JavaScript [node]. |
13 String nodeToString(Node node) { | 18 String nodeToString(Node node) { |
14 JavaScriptPrintingOptions options = new JavaScriptPrintingOptions( | 19 JavaScriptPrintingOptions options = new JavaScriptPrintingOptions( |
15 shouldCompressOutput: true, | 20 shouldCompressOutput: true, |
16 preferSemicolonToNewlineInMinifiedOutput: true); | 21 preferSemicolonToNewlineInMinifiedOutput: true); |
17 LenientPrintingContext printingContext = | 22 LenientPrintingContext printingContext = |
18 new LenientPrintingContext(); | 23 new LenientPrintingContext(); |
19 new Printer(options, printingContext).visit(node); | 24 new Printer(options, printingContext).visit(node); |
20 return printingContext.getText(); | 25 return printingContext.getText(); |
21 } | 26 } |
22 | 27 |
23 /// Visitor that creates an XML-like representation of the structure of a | 28 /// Visitor that creates an XML-like representation of the structure of a |
24 /// JavaScript [Node]. | 29 /// JavaScript [Node]. |
25 class DebugPrinter extends BaseVisitor with Indentation, Tagging<Node> { | 30 class DebugPrinter extends BaseVisitor with Indentation, Tagging<Node> { |
26 StringBuffer sb = new StringBuffer(); | 31 StringBuffer sb = new StringBuffer(); |
27 | 32 |
28 void visitNodeWithChildren(Node node, String type) { | 33 void visitNodeWithChildren(Node node, String type, [Map params]) { |
29 openNode(node, type); | 34 openNode(node, type, params); |
30 node.visitChildren(this); | 35 node.visitChildren(this); |
31 closeNode(); | 36 closeNode(); |
32 } | 37 } |
33 | 38 |
34 @override | 39 @override |
35 void visitNode(Node node) { | 40 void visitNode(Node node) { |
36 visitNodeWithChildren(node, '${node.runtimeType}'); | 41 visitNodeWithChildren(node, '${node.runtimeType}'); |
37 } | 42 } |
38 | 43 |
39 @override | 44 @override |
40 void visitName(Name node) { | 45 void visitName(Name node) { |
41 openAndCloseNode(node, '${node.runtimeType}', {'name': node.name}); | 46 openAndCloseNode(node, '${node.runtimeType}', {'name': node.name}); |
42 } | 47 } |
43 | 48 |
44 @override | 49 @override |
| 50 void visitBinary(Binary node) { |
| 51 visitNodeWithChildren(node, '${node.runtimeType}', {'op': node.op}); |
| 52 } |
| 53 |
| 54 @override |
45 void visitLiteralString(LiteralString node) { | 55 void visitLiteralString(LiteralString node) { |
46 openAndCloseNode(node, '${node.runtimeType}', {'value': node.value}); | 56 openAndCloseNode(node, '${node.runtimeType}', {'value': node.value}); |
47 } | 57 } |
48 | 58 |
49 /** | 59 /** |
50 * Pretty-prints given node tree into string. | 60 * Pretty-prints given node tree into string. |
51 */ | 61 */ |
52 static String prettyPrint(Node node) { | 62 static String prettyPrint(Node node) { |
53 var p = new DebugPrinter(); | 63 var p = new DebugPrinter(); |
54 node.accept(p); | 64 node.accept(p); |
55 return p.sb.toString(); | 65 return p.sb.toString(); |
56 } | 66 } |
57 } | 67 } |
58 | 68 |
59 /// Simple printing context that doesn't throw on errors. | 69 /// Simple printing context that doesn't throw on errors. |
60 class LenientPrintingContext extends SimpleJavaScriptPrintingContext { | 70 class LenientPrintingContext extends SimpleJavaScriptPrintingContext |
| 71 implements BufferedCodeOutput { |
61 @override | 72 @override |
62 void error(String message) { | 73 void error(String message) { |
63 buffer.write('>>$message<<'); | 74 buffer.write('>>$message<<'); |
64 } | 75 } |
65 } | 76 } |
OLD | NEW |