Class: SyntaxTree::MatchVisitor

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

Overview

This visitor transforms the AST into a Ruby pattern matching expression that would match correctly against the AST.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BasicVisitor

valid_visit_methods, #visit_all, #visit_child_nodes, visit_method, visit_methods

Constructor Details

#initialize(q) ⇒ MatchVisitor

Returns a new instance of MatchVisitor.



9
10
11
# File 'lib/syntax_tree/match_visitor.rb', line 9

def initialize(q)
  @q = q
end

Instance Attribute Details

#qObject (readonly)

Returns the value of attribute q.



7
8
9
# File 'lib/syntax_tree/match_visitor.rb', line 7

def q
  @q
end

Instance Method Details

#visit(node) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/syntax_tree/match_visitor.rb', line 13

def visit(node)
  case node
  when Node
    super
  when String
    # pp will split up a string on newlines and concat them together using a
    # "+" operator. This breaks the pattern matching expression. So instead
    # we're going to check here for strings and manually put the entire
    # value into the output buffer.
    q.text(node.inspect)
  else
    node.pretty_print(q)
  end
end