Class: CodeNode::SexpWalker

Inherits:
Object
  • Object
show all
Defined in:
lib/code_node/sexp_walker.rb

Overview

Walks a Sexp representing a ruby file looking for classes and modules.

Instance Method Summary collapse

Constructor Details

#initialize(graph, sexp, opt = {}) ⇒ SexpWalker

Initialize a walker with a graph and sexp

All files in a code base should be walked once in :find_nodes mode, and then walked again in :find_relations mode.

Parameters:

  • graph (IR::Graph)

    a graph to which nodes and relations will be added

  • sexp (Sexp)

    the root sexp of a ruby file

  • opt (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opt):

  • :mode (Symbol) — default: :find_nodes

    one of :find_nodes or :find_relations



14
15
16
17
18
# File 'lib/code_node/sexp_walker.rb', line 14

def initialize(graph, sexp, opt={})
  @graph = graph
  @root = sexp
  @mode = opt[:mode] || :find_nodes
end

Instance Method Details

#walk(s = nil) ⇒ nil

Walk the tree rooted at the given sexp

Parameters:

  • s (Sexp) (defaults to: nil)

    if nil will be the root sexp

Returns:

  • (nil)


23
24
25
26
27
28
29
30
31
32
# File 'lib/code_node/sexp_walker.rb', line 23

def walk(s = nil)
  s ||= @root
  if [:module, :class].member?(s[0])
    add_node s
  elsif find_relations? && s[0] == :call && s.length >= 4 && [:extend, :include].member?(s[2]) && !@graph.scope.empty?
    add_relation s
  else
    walk_siblings s.slice(1..-1)
  end
end