Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Implement visitor pattern to SyntaxTree::Node
This pattern will expand our horizons and allow us to do fun stuff with the nodes through the SyntaxTree::Visitor class.

We've chosen to require the Visitor class in node.rb to make it clear that the file requires it. Otherwise, we'd have to add it to lib/syntax_tree.rb in an order-dependent way, which is undesirable.

Co-authored-by: Kaan Ozkan <kaan.ozkan@shopify.com>
Co-authored-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
Co-authored-by: Vinicius Stock <vinicius.stock@shopify.com>
Co-authored-by: Emily Giurleo <emily.giurleo@shopify.com>
Co-authored-by: Rafael Franca <rafael.franca@shopify.com>
  • Loading branch information
6 people committed Mar 31, 2022
commit 45ebf0b15f6bdef12e32a6023bb0d4a19cc1ec4c
25 changes: 25 additions & 0 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "syntax_tree/visitor"

module SyntaxTree
# Represents the location of a node in the tree from the source code.
class Location
Expand Down Expand Up @@ -56,6 +58,29 @@ class Node
# [Location] the location of this node
attr_reader :location

def self.inherited(child)
child.class_eval(<<~EOS, __FILE__, __LINE__ + 1)
def accept(visitor)
visitor.#{child.visit_method_name}(self)
end
EOS

Visitor.class_eval(<<~EOS, __FILE__, __LINE__ + 1)
def #{child.visit_method_name}(node)
visit_all(node.child_nodes)
end
EOS
end

def self.visit_method_name
method_suffix = name.split("::").last
method_suffix.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
method_suffix.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
method_suffix.tr!("-", "_")
method_suffix.downcase!
"visit_#{method_suffix}"
end

def child_nodes
raise NotImplementedError
end
Expand Down
15 changes: 15 additions & 0 deletions lib/syntax_tree/visitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module SyntaxTree
class Visitor
def visit_all(nodes)
nodes.each do |node|
visit(node)
end
end

def visit(node)
node&.accept(self)
end
end
end
54 changes: 54 additions & 0 deletions test/visitor_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

require_relative "test_helper"
require "objspace"

class VisitorTest < Minitest::Test
def test_can_visit_all_nodes
visitor = SyntaxTree::Visitor.new

ObjectSpace.each_object(SyntaxTree::Node.singleton_class)
.reject { |node| node.singleton_class? || node == SyntaxTree::Node }
.each { |node| assert_respond_to(visitor, node.visit_method_name) }
end

def test_node_visit_method_name
assert_equal("visit_t_string_end", SyntaxTree::TStringEnd.visit_method_name)
end

def test_visit_tree
parsed_tree = SyntaxTree.parse(<<~RUBY)
class Foo
def foo; end

class Bar
def bar; end
end
end

def baz; end
RUBY

visitor = DummyVisitor.new
visitor.visit(parsed_tree)
assert_equal(["Foo", "foo", "Bar", "bar", "baz"], visitor.visited_nodes)
end

class DummyVisitor < SyntaxTree::Visitor
attr_reader :visited_nodes

def initialize
super
@visited_nodes = []
end

def visit_class_declaration(node)
@visited_nodes << node.constant.constant.value
super
end

def visit_def(node)
@visited_nodes << node.name.value
end
end
end