Class: Sexp

Inherits:
Object show all
Defined in:
lib/adsl/extract/sexp_utils.rb

Instance Method Summary collapse

Instance Method Details

#block_replace(*search_types, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/adsl/extract/sexp_utils.rb', line 4

def block_replace(*search_types, &block)
  options = search_types.last.is_a?(Hash) ? search_types.pop : {}
  
  propagate_to_children = true
  if options.include?(:unless_in)
    options[:unless_in] = Array.wrap options[:unless_in]
    propagate_to_children = false if options[:unless_in].include?(self.sexp_type)
  end

  new = self.map do |element|
    if propagate_to_children && element.is_a?(Sexp)
      result = element.block_replace *search_types, options, &block
      result = [result] unless result.class == Array
      result
    else
      [element]
    end
  end
  result = Sexp.from_array new.flatten(1)
  result = block[result] if search_types.include? result.sexp_type
  return result
end

#find_shallowest(search_type) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/adsl/extract/sexp_utils.rb', line 27

def find_shallowest(search_type)
  return [self] if sexp_type == search_type
  self.inject([]) do |collection, subsexp|
    collection << subsexp.find_shallowest(search_type) if subsexp.is_a?(Sexp)
    collection
  end.flatten(1)
end

#may_return_or_raise?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
# File 'lib/adsl/extract/sexp_utils.rb', line 35

def may_return_or_raise?
  return true if sexp_type == :return
  return true if sexp_type == :call && self[2] == :raise
  sexp_body.each do |subsexp|
    return true if subsexp.is_a?(Sexp) && subsexp.may_return_or_raise?
  end
  false
end