From: Jean Privat Date: Tue, 24 Feb 2015 03:59:46 +0000 (+0700) Subject: Merge: parser: add `ASTDump` and `ANode::dump_tree` X-Git-Tag: v0.7.2~9 X-Git-Url: http://nitlanguage.org?hp=b74de4ee90d3831dcea37435afbbce951f767fd6 Merge: parser: add `ASTDump` and `ANode::dump_tree` A simple helper to debug AST. I used it to debug auto-serialization in #1159 Pull-Request: #1167 Reviewed-by: Alexandre Terrasa --- diff --git a/src/parser/parser_nodes.nit b/src/parser/parser_nodes.nit index 289ad53..1c0eb21 100644 --- a/src/parser/parser_nodes.nit +++ b/src/parser/parser_nodes.nit @@ -17,6 +17,7 @@ module parser_nodes import location +import ordered_tree # Root of the AST class-hierarchy abstract class ANode @@ -33,6 +34,15 @@ abstract class ANode sys.stderr.write "{hot_location} {self.class_name}: {message}\n{hot_location.colored_line("0;32")}\n" end + # Write the subtree on stdout. + # See `ASTDump` + fun dump_tree + do + var d = new ASTDump + d.enter_visit(self) + d.write_to(sys.stdout) + end + # Parent of the node in the AST var parent: nullable ANode = null @@ -158,6 +168,33 @@ private class CollectAnnotationsByNameVisitor end end +# A helper class to handle (print) Nit AST as an OrderedTree +class ASTDump + super Visitor + super OrderedTree[ANode] + + # Reference to the last parent in the Ordered Tree + # Is used to handle the initial node parent and workaround possible inconsistent `ANode::parent` + private var last_parent: nullable ANode = null + + redef fun visit(n) + do + var p = last_parent + add(p, n) + last_parent = n + n.visit_all(self) + last_parent = p + end + + redef fun display(n) + do + if n isa Token then + return "{n.class_name} \"{n.text.escape_to_c}\" @{n.location}" + else + return "{n.class_name} @{n.location}" + end + end +end # A sequence of nodes # It is a specific class (instead of using a Array) to track the parent/child relation when nodes are added or removed