nitc :: ASTDump :: defaultinit
# 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
# Display tokens and structural production?
#
# 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
var p = last_parent
add(p, n)
last_parent = n
n.visit_all(self)
last_parent = p
end
redef fun write_line(o, n, p)
do
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
src/parser/parser_nodes.nit:185,1--246,3