nitc: dump_tree can display the lines of the source-code
[nit.git] / src / parser / parser_nodes.nit
index 1d68c6f..d8dc9eb 100644 (file)
@@ -18,6 +18,7 @@ module parser_nodes
 
 import location
 import ordered_tree
+private import console
 
 # Root of the AST class-hierarchy
 abstract class ANode
@@ -38,14 +39,24 @@ abstract class ANode
        fun is_structural: Bool do return false
 
        # Write the subtree on stdout.
-       # See `ASTDump`
-       fun dump_tree(display_structural: nullable Bool)
+       #
+       # Visit the subtree and display it with additional and useful information.
+       #
+       # By default, this displays all kind of nodes and the corresponding lines of codes.
+       #
+       # See `ASTDump` for details.
+       fun dump_tree(display_structural, display_line: nullable Bool)
        do
-               var d = new ASTDump(display_structural or else true)
+               var d = new ASTDump(display_structural or else true, display_line or else true)
                d.enter_visit(self)
                d.write_to(sys.stdout)
        end
 
+       # Information to display on a node
+       #
+       # Refine this method to add additional information on each node type.
+       fun dump_info(v: ASTDump): String do return ""
+
        # Parent of the node in the AST
        var parent: nullable ANode = null
 
@@ -185,6 +196,14 @@ class ASTDump
        # Should tokens (and structural production like AQId) be displayed?
        var display_structural: Bool
 
+       # Display lines?
+       #
+       # Should each new line be displayed (numbered and in gray)?
+       var display_line: Bool
+
+       # Current line number (when printing lines)
+       private var line = 0
+
        redef fun visit(n)
        do
                if not display_structural and n.is_structural then return
@@ -195,14 +214,35 @@ class ASTDump
                last_parent = p
        end
 
-       redef fun display(n)
+       redef fun write_line(o, n, p)
        do
-               if n isa Token then
-                       return "{n.class_name} \"{n.text.escape_to_c}\" @{n.location}"
-               else
-                       return "{n.class_name} @{n.location}"
+               if display_line then
+                       var ls = n.location.line_start
+                       var file = n.location.file
+                       var line = self.line
+                       if ls > line and file != null then
+                               if line == 0 then line = ls - 1
+                               while line < ls do
+                                       line += 1
+                                       o.write "{line}\t{file.get_line(line)}\n".light_gray
+                               end
+                               self.line = ls
+                       end
                end
+
+               super
+       end
+
+       redef fun display(n)
+       do
+               return "{n.class_name} {n.dump_info(self)} @{n.location}"
        end
+
+       # `s` as yellow
+       fun yellow(s: String): String do return s.yellow
+
+       # `s` as red
+       fun red(s: String): String do return s.red
 end
 
 # A sequence of nodes
@@ -342,6 +382,8 @@ abstract class Token
 
        redef fun is_structural do return true
 
+       redef fun dump_info(v) do return " {text.escape_to_c}"
+
        # Loose tokens that precede `self`.
        #
        # These tokens start the line or belong to a line with only loose tokens.