class Rgr::Searcher
Attributes
search_ast[R]
Public Class Methods
new(search)
click to toggle source
# File lib/rgr/searcher.rb, line 8 def initialize(search) @search_ast = parser_class.parse(search) end
Public Instance Methods
search_file(file)
click to toggle source
# File lib/rgr/searcher.rb, line 12 def search_file(file) if ast = parse_file(file) search_node_rec(ast, search_ast) else [] end end
Private Instance Methods
match?(node, search)
click to toggle source
# File lib/rgr/searcher.rb, line 50 def match?(node, search) return true if wildcard?(search) if node.type == search.type node.to_a.zip(search.to_a).all? { |n, s| if n.is_a?(Parser::AST::Node) && s.is_a?(Parser::AST::Node) match?(n, s) else n == s end } end end
parse_file(file)
click to toggle source
# File lib/rgr/searcher.rb, line 21 def parse_file(file) parser.parse(Parser::Source::Buffer.new(file).read) rescue => e $stderr.puts "Error parsing `#{file}':" $stderr.puts e.message end
parser()
click to toggle source
# File lib/rgr/searcher.rb, line 32 def parser @parser ||= parser_class.new @parser.reset @parser end
parser_class()
click to toggle source
# File lib/rgr/searcher.rb, line 28 def parser_class Parser::CurrentRuby end
search_node_rec(node, search, results = [])
click to toggle source
# File lib/rgr/searcher.rb, line 38 def search_node_rec(node, search, results = []) node.to_a.grep(Parser::AST::Node).each do |child| search_node_rec(child, search, results) end if match?(node, search) results << node.loc end results end
wildcard?(search_node)
click to toggle source
# File lib/rgr/searcher.rb, line 64 def wildcard?(search_node) search_node.type == :send && search_node.to_a == [nil, :_] end