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

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: 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
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..84d2068e79d6a381b6e6483da0a9b230cbee6101
--- /dev/null
+++ b/lib/compiler/implementation/tree/prettyprint.dart
@@ -0,0 +1,284 @@
+// 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 PrettyPrint implements Visitor {
+ StringBuffer sb;
+ int depth;
+
+ void add(SourceString string) {
+ string.printOn(sb);
+ }
+
+ void addInNode(String type, [Map params]) {
+ addCurrentIndent();
+ sb.add("<");
+ addTypeWithParams(type, params);
+ sb.add(">\n");
+ depth++;
+ }
+
+ void addInOutNode(String type, [Map params]) {
+ addCurrentIndent();
+ sb.add("<");
Anton Muhin 2012/05/28 06:07:09 do we really want to emulate XML-like syntax? I p
Roman 2012/05/28 12:32:15 XML-like syntax is the first thing that comes to m
+ addTypeWithParams(type, params);
+ sb.add("/>\n");
+ }
+
+ void addOutNode(String type, [Map params]) {
+ 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.
+ params.forEach((k, v) => sb.add(' $k="$v"'));
+ }
+ }
+
+ void addCurrentIndent() {
+ for (int i = 0; i < depth; i++) {
+ if (i % 4 == 0) {
+ sb.add(". ");
+ } else {
+ sb.add(" ");
+ }
+ }
+ }
+
+ /**
+ * Pretty-prints given node tree into string.
+ */
+ String prettyPrint(Node node) {
+ depth = 0;
Anton Muhin 2012/05/28 06:07:09 please, provide a normal constructor, something li
Roman 2012/05/28 12:32:15 Done.
+ sb = new StringBuffer();
+ 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') | lib/compiler/implementation/tree/tree.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698