Class: FastRuby::FastRubySexp::Edges

Inherits:
Object
  • Object
show all
Defined in:
lib/fastruby/sexp_extension_edges.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(frbsexp) ⇒ Edges

Returns a new instance of Edges.



24
25
26
# File 'lib/fastruby/sexp_extension_edges.rb', line 24

def initialize(frbsexp)
  @frbsexp = frbsexp
end

Class Method Details

.do_nothing_for(*nodename) ⇒ Object



199
200
201
202
203
204
# File 'lib/fastruby/sexp_extension_edges.rb', line 199

def self.do_nothing_for(*nodename)
  nodename.each do |nn|
    define_method("edges_#{nn}") do
    end
  end
end

Instance Method Details

#each(&blk) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fastruby/sexp_extension_edges.rb', line 28

def each(&blk)
  node_type = @frbsexp.node_type

  @frbsexp.each do|st|
    next unless FastRubySexp === st
    st.edges.each(&blk)
  end

  if respond_to? "edges_#{node_type}"
    send("edges_#{node_type}", &blk)
  end
end

#edges_block(&blk) ⇒ Object



172
173
174
175
176
177
# File 'lib/fastruby/sexp_extension_edges.rb', line 172

def edges_block(&blk)
  (2..@frbsexp.size-1).each do |i|
    blk.call(@frbsexp[i-1], @frbsexp[i].first_tree)
  end
  blk.call(@frbsexp.last, @frbsexp)
end

#edges_call(&blk) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/fastruby/sexp_extension_edges.rb', line 179

def edges_call(&blk)
  args_tree = @frbsexp[3]

  (2..args_tree.size-1).each do |i|
    blk.call(args_tree[i-1], args_tree[i])
  end

  recv_tree = @frbsexp[1]
  
  if recv_tree
    if args_tree.size > 1
      blk.call(recv_tree, args_tree[1])
    end
  end

  if args_tree.size > 1
    blk.call(args_tree.last, @frbsexp)
  end
end

#edges_ensure(&blk) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fastruby/sexp_extension_edges.rb', line 49

def edges_ensure(&blk)
  if @frbsexp.size > 2
    @frbsexp[1].walk_tree do |subtree|
      if subtree.node_type == :call or subtree.node_type == :iter
        blk.call(subtree,@frbsexp[2].first_tree)
      end
    end

    blk.call(@frbsexp[2],@frbsexp)
  else
    blk.call(@frbsexp[1],@frbsexp)
  end
end

#edges_if(&blk) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/fastruby/sexp_extension_edges.rb', line 138

def edges_if(&blk)
  if @frbsexp[2]
    blk.call(@frbsexp[1],@frbsexp[2].first_tree)
    blk.call(@frbsexp[2],@frbsexp)
  end

  if @frbsexp[3]
    blk.call(@frbsexp[3],@frbsexp)
    blk.call(@frbsexp[1],@frbsexp[3].first_tree)
  end

  unless @frbsexp[2] and @frbsexp[3]
    blk.call(@frbsexp[1],@frbsexp)
  end     
end

#edges_lasgn(&blk) ⇒ Object Also known as: edges_gasgn, edges_cdecl, edges_iasgn



41
42
43
# File 'lib/fastruby/sexp_extension_edges.rb', line 41

def edges_lasgn(&blk)
  blk.call(@frbsexp[2],@frbsexp)        
end

#edges_or(&blk) ⇒ Object Also known as: edges_and



129
130
131
132
133
134
135
# File 'lib/fastruby/sexp_extension_edges.rb', line 129

def edges_or(&blk)
  (1..@frbsexp.size-2).each do |i|
    blk.call(@frbsexp[i],@frbsexp[i+1].first_tree)
  end

  blk.call(@frbsexp[-1],@frbsexp)
end

#edges_rescue(&blk) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/fastruby/sexp_extension_edges.rb', line 63

def edges_rescue(&blk)
  return if @frbsexp[1].node_type == :resbody
  
  blk.call(@frbsexp[1],@frbsexp)

  exit_tree = nil
  resbody_array_tree = @frbsexp[2][1]

  if resbody_array_tree.size > 1
    exit_tree = resbody_array_tree[1].first_tree
    lasgn_tree = resbody_array_tree.find{|st| 
          if st.respond_to? :node_type
            st.node_type == :lasgn
          else
            false
          end
    }

    (1..resbody_array_tree.size-2).each do |i|
      blk.call(resbody_array_tree[i], resbody_array_tree[i+1].first_tree)

      if lasgn_tree
        blk.call(resbody_array_tree[i], lasgn_tree.first_tree)
      end 
    end

    if @frbsexp[2]
      if @frbsexp[2][2]
        blk.call(resbody_array_tree[-1],@frbsexp[2][2].first_tree)
      else            
        blk.call(resbody_array_tree[-1],@frbsexp)
      end
    else
      blk.call(resbody_array_tree[-1],@frbsexp)
    end
  else
    if @frbsexp[2]
    if @frbsexp[2][2]
      exit_tree = @frbsexp[2][2].first_tree
    end
    end
  end

  unless exit_tree
    exit_tree = @frbsexp
  end

  if @frbsexp[2]
    if @frbsexp[2][2]
      blk.call(@frbsexp[2][2],@frbsexp)
      blk.call(@frbsexp[1],exit_tree)
      retry_tree = @frbsexp[2][2].find_tree(:retry)
      if retry_tree
        blk.call(retry_tree,@frbsexp[1].first_tree)
      end
    end
  end

  @frbsexp[1].walk_tree do |subtree|
    if subtree.node_type == :call or subtree.node_type == :iter
      blk.call(subtree,exit_tree)
    end
  end    

end

#edges_scope(&blk) ⇒ Object



154
155
156
# File 'lib/fastruby/sexp_extension_edges.rb', line 154

def edges_scope(&blk)
  blk.call(@frbsexp[1], @frbsexp)
end

#edges_while(&blk) ⇒ Object Also known as: edges_until



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/fastruby/sexp_extension_edges.rb', line 158

def edges_while(&blk)
  if @frbsexp[2]
    blk.call(@frbsexp[1], @frbsexp[2].first_tree)
    blk.call(@frbsexp[2], @frbsexp[1].first_tree)
    @frbsexp[2].find_break do |subtree|
      blk.call(subtree, @frbsexp)
    end
  end

  blk.call(@frbsexp[1], @frbsexp)
end