Skip to content

Implement visitor pattern for SyntaxTree::Node #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2022
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
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