Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(95)

Unified Diff: lib/compiler/implementation/tree/prettyprint.dart

Issue 10449021: New Prettyprinter class that converts Node-tree to string. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Change class name from PrettyPrint to PrettyPrinter Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | lib/compiler/implementation/tree/tree.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/compiler/implementation/tree/prettyprint.dart
diff --git a/lib/compiler/implementation/tree/prettyprint.dart b/lib/compiler/implementation/tree/prettyprint.dart
new file mode 100644
index 0000000000000000000000000000000000000000..647868cde30af5845588f64d908da24dc69a3b10
--- /dev/null
+++ b/lib/compiler/implementation/tree/prettyprint.dart
@@ -0,0 +1,286 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+/**
+ * Pretty-prints Node tree in XML-like format.
+ *
+ * TODO(smok): Add main() to run from command-line to print out tree for given
+ * .dart file.
+ */
+class PrettyPrinter implements Visitor {
+ StringBuffer sb;
+ int depth;
+
+ PrettyPrinter() : sb = new StringBuffer(), depth = 0;
+
+ void add(SourceString string) {
+ string.printOn(sb);
+ }
+
+ void addInNode(String type, [Map params]) {
ahe 2012/06/01 12:52:57 What does the method name mean?
Roman 2012/06/05 09:28:54 Changed to "openNode"
+ addCurrentIndent();
+ sb.add("<");
+ addTypeWithParams(type, params);
+ sb.add(">\n");
+ depth++;
+ }
+
+ void addInOutNode(String type, [Map params]) {
ahe 2012/06/01 12:52:57 Ditto.
Roman 2012/06/05 09:28:54 Changed to openAndCloseNode(), not sure if it's th
+ addCurrentIndent();
+ sb.add("<");
+ addTypeWithParams(type, params);
+ sb.add("/>\n");
+ }
+
+ void addOutNode(String type, [Map params]) {
ahe 2012/06/01 12:52:57 Ditto.
Roman 2012/06/05 09:28:54 Changed to "closeNode"
+ depth--;
+ addCurrentIndent();
+ sb.add("</");
+ addTypeWithParams(type, params);
+ sb.add(">\n");
+ }
+
+ void addTypeWithParams(String type, [Map params]) {
+ sb.add("${type}");
+ if (params != null) {
+ // TODO(smok): Escape doublequotes in values.
ahe 2012/06/01 12:52:57 When you get to address this TODO, we have code fo
+ params.forEach((k, v) => sb.add(' $k="$v"'));
+ }
+ }
+
+ void addCurrentIndent() {
+ for (int i = 0; i < depth; i++) {
+ if (i % 4 == 0) {
+ sb.add(". ");
ahe 2012/06/01 12:52:57 I'd prefer if you didn't add the periods. If you m
Roman 2012/06/05 09:28:54 Done.
+ } else {
+ sb.add(" ");
ahe 2012/06/01 12:52:57 Perhaps make this a static constant in the beginni
Roman 2012/06/05 09:28:54 Created static final String _INDENT = " "; Or did
+ }
+ }
+ }
+
+ /**
+ * Pretty-prints given node tree into string.
+ */
+ String prettyPrint(Node node) {
+ depth = 0;
+ sb = new StringBuffer();
ahe 2012/06/01 12:52:57 How do you expect to use this method and class? Ar
Roman 2012/06/05 09:28:54 What do you mean by two StringBuffers? I was think
ahe 2012/06/08 07:37:36 The constructor always creates a string buffer. So
Roman 2012/06/08 09:10:15 Ah, I see now. Yeah, that's dumb. Changed to just
+ node.accept(this);
+ return sb.toString();
+ }
+
+ visitNodeWithChildren(Node node, String type) {
+ addInNode(type);
+ node.visitChildren(this);
+ addOutNode(type);
+ }
+
+ visitBlock(Block node) {
+ visitNodeWithChildren(node, "Block");
+ }
+
+ visitBreakStatement(BreakStatement node) {
+ visitNodeWithChildren(node, "BreakStatement");
+ }
+
+ visitCascade(Cascade node) {
+ visitNodeWithChildren(node, "Cascade");
+ }
+
+ visitCascadeReceiver(CascadeReceiver node) {
+ visitNodeWithChildren(node, "CascadeReceiver");
+ }
+
+ visitCaseMatch(CaseMatch node) {
+ visitNodeWithChildren(node, "CaseMatch");
+ }
+
+ visitCatchBlock(CatchBlock node) {
+ visitNodeWithChildren(node, "CatchBlock");
+ }
+
+ visitClassNode(ClassNode node) {
+ visitNodeWithChildren(node, "ClassNode");
+ }
+
+ visitConditional(Conditional node) {
+ visitNodeWithChildren(node, "Conditional");
+ }
+
+ visitContinueStatement(ContinueStatement node) {
+ visitNodeWithChildren(node, "ContinueStatement");
+ }
+
+ visitDoWhile(DoWhile node) {
+ visitNodeWithChildren(node, "DoWhile");
+ }
+
+ visitEmptyStatement(EmptyStatement node) {
+ visitNodeWithChildren(node, "EmptyStatement");
+ }
+
+ visitExpressionStatement(ExpressionStatement node) {
+ visitNodeWithChildren(node, "ExpressionStatement");
+ }
+
+ visitFor(For node) {
+ visitNodeWithChildren(node, "For");
+ }
+
+ visitForIn(ForIn node) {
+ visitNodeWithChildren(node, "ForIn");
+ }
+
+ visitFunctionDeclaration(FunctionDeclaration node) {
+ visitNodeWithChildren(node, "FunctionDeclaration");
+ }
+
+ visitFunctionExpression(FunctionExpression node) {
+ visitNodeWithChildren(node, "FunctionExpression");
+ }
+
+ visitIdentifier(Identifier node) {
+ addInOutNode("Identifier", {"token" : node.token.slowToString()});
+ }
+
+ visitIf(If node) {
+ visitNodeWithChildren(node, "If");
+ }
+
+ visitLabel(Label node) {
+ visitNodeWithChildren(node, "Label");
+ }
+
+ visitLabeledStatement(LabeledStatement node) {
+ visitNodeWithChildren(node, "LabeledStatement");
+ }
+
+ // Custom.
+ visitLiteral(Literal node, String type) {
+ addInOutNode(type, {"value" : node.value.toString()});
+ }
+
+ visitLiteralBool(LiteralBool node) {
+ visitLiteral(node, "LiteralBool");
+ }
+
+ visitLiteralDouble(LiteralDouble node) {
+ visitLiteral(node, "LiteralDouble");
+ }
+
+ visitLiteralInt(LiteralInt node) {
+ visitLiteral(node, "LiteralInt");
+ }
+
+ visitLiteralList(LiteralList node) {
+ visitNodeWithChildren(node, "LiteralList");
+ }
+
+ visitLiteralMap(LiteralMap node) {
+ visitNodeWithChildren(node, "LiteralMap");
+ }
+
+ visitLiteralMapEntry(LiteralMapEntry node) {
+ visitNodeWithChildren(node, "LiteralMapEntry");
+ }
+
+ visitLiteralNull(LiteralNull node) {
+ visitLiteral(node, "LiteralNull");
+ }
+
+ visitLiteralString(LiteralString node) {
+ addInOutNode("LiteralString", {"value" : node.token.slowToString()});
+ }
+
+ visitModifiers(Modifiers node) {
+ visitNodeWithChildren(node, "Modifiers");
+ }
+
+ visitNamedArgument(NamedArgument node) {
+ visitNodeWithChildren(node, "NamedArgument");
+ }
+
+ visitNewExpression(NewExpression node) {
+ visitNodeWithChildren(node, "NewExpression");
+ }
+
+ visitNodeList(NodeList node) {
+ if (node.nodes.toList().length == 0) {
+ addInOutNode("NodeList");
+ } else {
+ visitNodeWithChildren(node, "NodeList");
+ }
+ }
+
+ visitOperator(Operator node) {
+ addInOutNode("Operator", {"value" : node.token.slowToString()});
+ }
+
+ visitParenthesizedExpression(ParenthesizedExpression node) {
+ visitNodeWithChildren(node, "ParenthesizedExpression");
+ }
+
+ visitReturn(Return node) {
+ visitNodeWithChildren(node, "Return");
+ }
+
+ visitScriptTag(ScriptTag node) {
+ visitNodeWithChildren(node, "ScriptTag");
+ }
+
+ visitSend(Send node) {
+ visitNodeWithChildren(node, "Send");
+ }
+
+ visitSendSet(SendSet node) {
+ visitNodeWithChildren(node, "SendSet");
+ }
+
+ visitStringInterpolation(StringInterpolation node) {
+ visitNodeWithChildren(node, "StringInterpolation");
+ }
+
+ visitStringInterpolationPart(StringInterpolationPart node) {
+ visitNodeWithChildren(node, "StringInterpolationPart");
+ }
+
+ visitStringJuxtaposition(StringJuxtaposition node) {
+ visitNodeWithChildren(node, "StringJuxtaposition");
+ }
+
+ visitSwitchCase(SwitchCase node) {
+ visitNodeWithChildren(node, "SwitchCase");
+ }
+
+ visitSwitchStatement(SwitchStatement node) {
+ visitNodeWithChildren(node, "SwitchStatement");
+ }
+
+ visitThrow(Throw node) {
+ visitNodeWithChildren(node, "Throw");
+ }
+
+ visitTryStatement(TryStatement node) {
+ visitNodeWithChildren(node, "TryStatement");
+ }
+
+ visitTypeAnnotation(TypeAnnotation node) {
+ visitNodeWithChildren(node, "TypeAnnotation");
+ }
+
+ visitTypedef(Typedef node) {
+ visitNodeWithChildren(node, "Typedef");
+ }
+
+ visitTypeVariable(TypeVariable node) {
+ visitNodeWithChildren(node, "TypeVariable");
+ }
+
+ visitVariableDefinitions(VariableDefinitions node) {
+ visitNodeWithChildren(node, "VariableDefinitions");
+ }
+
+ visitWhile(While node) {
+ visitNodeWithChildren(node, "While");
+ }
+}
« no previous file with comments | « no previous file | lib/compiler/implementation/tree/tree.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698