Class: SyntaxTree::PrettyPrintVisitor

Inherits:
FieldVisitor show all
Defined in:
lib/syntax_tree/pretty_print_visitor.rb

Overview

This visitor pretty-prints the AST into an equivalent s-expression.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BasicVisitor

valid_visit_methods, #visit, #visit_all, #visit_child_nodes, visit_method, visit_methods

Constructor Details

#initialize(q) ⇒ PrettyPrintVisitor

Returns a new instance of PrettyPrintVisitor.



8
9
10
# File 'lib/syntax_tree/pretty_print_visitor.rb', line 8

def initialize(q)
  @q = q
end

Instance Attribute Details

#qObject (readonly)

Returns the value of attribute q.



6
7
8
# File 'lib/syntax_tree/pretty_print_visitor.rb', line 6

def q
  @q
end

Instance Method Details

#visit_binary(node) ⇒ Object

This is here because we need to make sure the operator is cast to a string before we print it out.



14
15
16
17
18
19
20
21
# File 'lib/syntax_tree/pretty_print_visitor.rb', line 14

def visit_binary(node)
  node(node, "binary") do
    field("left", node.left)
    text("operator", node.operator.to_s)
    field("right", node.right)
    comments(node)
  end
end

#visit_label(node) ⇒ Object

This is here to make it a little nicer to look at labels since they typically have their : at the end of the value.



25
26
27
28
29
30
31
32
# File 'lib/syntax_tree/pretty_print_visitor.rb', line 25

def visit_label(node)
  node(node, "label") do
    q.breakable
    q.text(":")
    q.text(node.value[0...-1])
    comments(node)
  end
end