Class: Sexp
Overview
Sexp changes from ruby_parser and some changes for caching hash value and tracking ‘original’ line number of a Sexp.
Constant Summary collapse
- ASSIGNMENT_BOOL =
[:gasgn, :iasgn, :lasgn, :cvdecl, :cdecl, :or, :and]
Instance Attribute Summary collapse
-
#paren ⇒ Object
Returns the value of attribute paren.
Instance Method Summary collapse
-
#arglist ⇒ Object
Returns arglist for method call.
-
#arglist=(exp) ⇒ Object
Sets the arglist in a method call.
-
#args ⇒ Object
Returns arguments of a method call.
-
#block ⇒ Object
Returns block of a call with a block.
-
#block_args ⇒ Object
Returns parameters for a block.
-
#block_call ⇒ Object
Method call associated with a block:.
-
#body ⇒ Object
Returns body of a method definition, class, or module.
-
#body=(exp) ⇒ Object
Sets body.
- #class_name ⇒ Object (also: #module_name)
- #comments=(*args) ⇒ Object
- #compact ⇒ Object
-
#condition ⇒ Object
Returns condition of an if expression:.
-
#each_sexp ⇒ Object
Iterates over the Sexps in an Sexp, skipping values that are not an Sexp.
-
#else_clause ⇒ Object
Returns ‘else’ clause of an if expression:.
-
#expect(*types) ⇒ Object
Raise a WrongSexpError if the nodes type does not match one of the expected types.
- #file=(*args) ⇒ Object
- #find_and_replace_all(*args) ⇒ Object
- #find_node(*args) ⇒ Object
-
#first_arg ⇒ Object
Returns first argument of a method call.
-
#first_arg=(exp) ⇒ Object
Sets first argument of a method call.
- #hash ⇒ Object
-
#iasgn(delete = false) ⇒ Object
Don’t use this, please.
-
#lasgn(delete = false) ⇒ Object
Don’t use this, please.
-
#lhs ⇒ Object
Returns the left hand side of assignment or boolean:.
-
#lhs=(exp) ⇒ Object
Sets the left hand side of assignment or boolean.
- #line(num = nil) ⇒ Object
- #line=(*args) ⇒ Object
-
#method ⇒ Object
Returns method of a method call:.
- #method_missing(name, *args) ⇒ Object
-
#method_name ⇒ Object
Returns name of method being defined in a method definition.
- #node_type=(type) ⇒ Object
- #old_comments_set ⇒ Object
- #old_compact ⇒ Object
- #old_fara ⇒ Object
- #old_file_set ⇒ Object
- #old_find_node ⇒ Object
- #old_line ⇒ Object
- #old_line_set ⇒ Object
- #original_line(line = nil) ⇒ Object
- #parent_name ⇒ Object
- #render_type ⇒ Object
-
#resbody(delete = false) ⇒ Object
Don’t use this, please.
-
#rhs ⇒ Object
Returns right side (value) of assignment or boolean:.
-
#rhs=(exp) ⇒ Object
Sets the right hand side of assignment or boolean.
- #second ⇒ Object
-
#second_arg ⇒ Object
Returns second argument of a method call.
-
#second_arg=(exp) ⇒ Object
Sets second argument of a method call.
-
#target ⇒ Object
Returns target of a method call:.
-
#target=(exp) ⇒ Object
Sets the target of a method call:.
-
#then_clause ⇒ Object
Returns ‘then’ clause of an if expression:.
- #to_sym ⇒ Object
- #value ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/ruby_parser/bm_sexp.rb', line 8 def method_missing name, *args #Brakeman does not use this functionality, #so overriding it to raise a NoMethodError. # #The original functionality calls find_node and optionally #deletes the node if found. raise NoMethodError.new("No method '#{name}' for Sexp", name, args) end |
Instance Attribute Details
#paren ⇒ Object
Returns the value of attribute paren.
5 6 7 |
# File 'lib/ruby_parser/bm_sexp.rb', line 5 def paren @paren end |
Instance Method Details
#arglist ⇒ Object
Returns arglist for method call. This differs from Sexp#args, as Sexp#args does not return a ‘real’ Sexp (it does not have a node type) but Sexp#arglist returns a s(:arglist, …)
s(:call, s(:call, nil, :x, s(:arglist)), :y, s(:arglist, s(:lit, 1), s(:lit, 2)))
^------------ arglist ------------^
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/ruby_parser/bm_sexp.rb', line 187 def arglist expect :call, :attrasgn, :super, :zsuper case self.node_type when :call, :attrasgn self[3] when :super, :zsuper if self[1] Sexp.new(:arglist).concat self[1..-1] else Sexp.new(:arglist) end end #For new ruby_parser #Sexp.new(:arglist, *self[3..-1]) end |
#arglist=(exp) ⇒ Object
Sets the arglist in a method call.
175 176 177 178 179 |
# File 'lib/ruby_parser/bm_sexp.rb', line 175 def arglist= exp expect :call, :attrasgn self[3] = exp #RP 3 TODO end |
#args ⇒ Object
Returns arguments of a method call. This will be an ‘untyped’ Sexp.
s(:call, s(:call, nil, :x, s(:arglist)), :y, s(:arglist, s(:lit, 1), s(:lit, 2)))
^--------args--------^
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/ruby_parser/bm_sexp.rb', line 209 def args expect :call, :attrasgn, :super, :zsuper #For new ruby_parser #if self[3] # self[3..-1] #else # [] #end case self.node_type when :call, :attrasgn #For old ruby_parser if self[3] self[3][1..-1] else [] end when :super, :zsuper if self[1] self[1..-1] else [] end end end |
#block ⇒ Object
Returns block of a call with a block. Could be a single expression or a block:
s(:iter,
s(:call, nil, :x, s(:arglist)),
s(:lasgn, :y),
s(:block, s(:lvar, :y), s(:call, nil, :z, s(:arglist))))
^-------------------- block --------------------------^
320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/ruby_parser/bm_sexp.rb', line 320 def block expect :iter, :call_with_block, :scope, :resbody case self.node_type when :iter, :call_with_block self[3] when :scope self[1] when :resbody #This is for Ruby2Ruby ONLY find_node :block end end |
#block_args ⇒ Object
Returns parameters for a block
s(:iter,
s(:call, nil, :x, s(:arglist)),
s(:lasgn, :y), <- block_args
s(:call, nil, :p, s(:arglist, s(:lvar, :y))))
340 341 342 343 |
# File 'lib/ruby_parser/bm_sexp.rb', line 340 def block_args expect :iter, :call_with_block self[2] end |
#block_call ⇒ Object
Method call associated with a block:
s(:iter,
s(:call, nil, :x, s(:arglist)), <- block_call
s(:lasgn, :y),
s(:block, s(:lvar, :y), s(:call, nil, :z, s(:arglist))))
307 308 309 310 |
# File 'lib/ruby_parser/bm_sexp.rb', line 307 def block_call expect :iter, :call_with_block self[1] end |
#body ⇒ Object
Returns body of a method definition, class, or module.
402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/ruby_parser/bm_sexp.rb', line 402 def body expect :defn, :defs, :methdef, :selfdef, :class, :module case self.node_type when :defn, :methdef, :class self[3] when :defs, :selfdef self[4] when :module self[2] end end |
#body=(exp) ⇒ Object
Sets body
388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/ruby_parser/bm_sexp.rb', line 388 def body= exp expect :defn, :defs, :methdef, :selfdef, :class, :module case self.node_type when :defn, :methdef, :class self[3] = exp when :defs, :selfdef self[4] = exp when :module self[2] = exp end end |
#class_name ⇒ Object Also known as: module_name
420 421 422 423 |
# File 'lib/ruby_parser/bm_sexp.rb', line 420 def class_name expect :class, :module self[1] end |
#comments=(*args) ⇒ Object
123 124 125 126 |
# File 'lib/ruby_parser/bm_sexp.rb', line 123 def comments= *args @my_hash_value = nil old_comments_set(*args) end |
#compact ⇒ Object
103 104 105 106 |
# File 'lib/ruby_parser/bm_sexp.rb', line 103 def compact @my_hash_value = nil old_compact end |
#condition ⇒ Object
Returns condition of an if expression:
s(:if,
s(:lvar, :condition), <-- condition
s(:lvar, :then_val),
s(:lvar, :else_val)))
273 274 275 276 |
# File 'lib/ruby_parser/bm_sexp.rb', line 273 def condition expect :if self[1] end |
#each_sexp ⇒ Object
Iterates over the Sexps in an Sexp, skipping values that are not an Sexp.
130 131 132 133 134 |
# File 'lib/ruby_parser/bm_sexp.rb', line 130 def each_sexp self.each do |e| yield e if Sexp === e end end |
#else_clause ⇒ Object
Returns ‘else’ clause of an if expression:
s(:if,
s(:lvar, :condition),
s(:lvar, :then_val),
s(:lvar, :else_val)))
^---else caluse---^
296 297 298 299 |
# File 'lib/ruby_parser/bm_sexp.rb', line 296 def else_clause expect :if self[3] end |
#expect(*types) ⇒ Object
Raise a WrongSexpError if the nodes type does not match one of the expected types.
138 139 140 141 142 |
# File 'lib/ruby_parser/bm_sexp.rb', line 138 def expect *types unless types.include? self.node_type raise WrongSexpError, "Expected #{types.join ' or '} but given #{self.inspect}", caller[1..-1] end end |
#file=(*args) ⇒ Object
98 99 100 101 |
# File 'lib/ruby_parser/bm_sexp.rb', line 98 def file= *args @my_hash_value = nil old_file_set(*args) end |
#find_and_replace_all(*args) ⇒ Object
108 109 110 111 |
# File 'lib/ruby_parser/bm_sexp.rb', line 108 def find_and_replace_all *args @my_hash_value = nil old_fara(*args) end |
#find_node(*args) ⇒ Object
113 114 115 116 |
# File 'lib/ruby_parser/bm_sexp.rb', line 113 def find_node *args @my_hash_value = nil old_find_node(*args) end |
#first_arg ⇒ Object
Returns first argument of a method call.
236 237 238 239 240 241 |
# File 'lib/ruby_parser/bm_sexp.rb', line 236 def first_arg expect :call, :attrasgn if self[3] self[3][1] end end |
#first_arg=(exp) ⇒ Object
Sets first argument of a method call.
244 245 246 247 248 249 |
# File 'lib/ruby_parser/bm_sexp.rb', line 244 def first_arg= exp expect :call, :attrasgn if self[3] self[3][1] = exp end end |
#hash ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/ruby_parser/bm_sexp.rb', line 80 def hash #There still seems to be some instances in which the hash of the #Sexp changes, but I have not found what method call is doing it. #Of course, Sexp is subclasses from Array, so who knows what might #be going on. @my_hash_value ||= super end |
#iasgn(delete = false) ⇒ Object
Don’t use this, please. :nodoc:
54 55 56 |
# File 'lib/ruby_parser/bm_sexp.rb', line 54 def iasgn delete = false find_node :iasgn, delete end |
#lasgn(delete = false) ⇒ Object
Don’t use this, please. :nodoc:
48 49 50 |
# File 'lib/ruby_parser/bm_sexp.rb', line 48 def lasgn delete = false find_node :lasgn, delete end |
#lhs ⇒ Object
Returns the left hand side of assignment or boolean:
s(:lasgn, :x, s(:lit, 1))
^--lhs
349 350 351 352 |
# File 'lib/ruby_parser/bm_sexp.rb', line 349 def lhs expect *ASSIGNMENT_BOOL self[1] end |
#lhs=(exp) ⇒ Object
Sets the left hand side of assignment or boolean.
355 356 357 358 |
# File 'lib/ruby_parser/bm_sexp.rb', line 355 def lhs= exp expect *ASSIGNMENT_BOOL self[1] = exp end |
#line(num = nil) ⇒ Object
88 89 90 91 |
# File 'lib/ruby_parser/bm_sexp.rb', line 88 def line num = nil @my_hash_value = nil if num old_line(num) end |
#line=(*args) ⇒ Object
93 94 95 96 |
# File 'lib/ruby_parser/bm_sexp.rb', line 93 def line= *args @my_hash_value = nil old_line_set(*args) end |
#method ⇒ Object
Returns method of a method call:
s(:call, s(:call, nil, :x, s(:arglist)), :y, s(:arglist, s(:lit, 1)))
^- method
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/ruby_parser/bm_sexp.rb', line 163 def method expect :call, :attrasgn, :super, :zsuper case self.node_type when :call, :attrasgn self[2] when :super, :zsuper :super end end |
#method_name ⇒ Object
Returns name of method being defined in a method definition.
376 377 378 379 380 381 382 383 384 385 |
# File 'lib/ruby_parser/bm_sexp.rb', line 376 def method_name expect :defn, :defs, :methdef, :selfdef case self.node_type when :defn, :methdef self[1] when :defs, :selfdef self[2] end end |
#node_type=(type) ⇒ Object
34 35 36 |
# File 'lib/ruby_parser/bm_sexp.rb', line 34 def node_type= type self[0] = type end |
#old_comments_set ⇒ Object
65 |
# File 'lib/ruby_parser/bm_sexp.rb', line 65 alias :old_comments_set :comments= |
#old_compact ⇒ Object
66 |
# File 'lib/ruby_parser/bm_sexp.rb', line 66 alias :old_compact :compact |
#old_fara ⇒ Object
67 |
# File 'lib/ruby_parser/bm_sexp.rb', line 67 alias :old_fara :find_and_replace_all |
#old_file_set ⇒ Object
64 |
# File 'lib/ruby_parser/bm_sexp.rb', line 64 alias :old_file_set :file= |
#old_find_node ⇒ Object
68 |
# File 'lib/ruby_parser/bm_sexp.rb', line 68 alias :old_find_node :find_node |
#old_line ⇒ Object
62 |
# File 'lib/ruby_parser/bm_sexp.rb', line 62 alias :old_line :line |
#old_line_set ⇒ Object
63 |
# File 'lib/ruby_parser/bm_sexp.rb', line 63 alias :old_line_set :line= |
#original_line(line = nil) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/ruby_parser/bm_sexp.rb', line 70 def original_line line = nil if line @my_hash_value = nil @original_line = line self else @original_line ||= nil end end |
#parent_name ⇒ Object
427 428 429 430 |
# File 'lib/ruby_parser/bm_sexp.rb', line 427 def parent_name expect :class self[2] end |
#render_type ⇒ Object
415 416 417 418 |
# File 'lib/ruby_parser/bm_sexp.rb', line 415 def render_type expect :render self[1] end |
#resbody(delete = false) ⇒ Object
Don’t use this, please. :nodoc:
40 41 42 43 44 |
# File 'lib/ruby_parser/bm_sexp.rb', line 40 def resbody delete = false #RubyParser and Ruby2Ruby rely on method_missing for this, but since we #don't want to use method_missing, here's a real method. find_node :resbody, delete end |
#rhs ⇒ Object
Returns right side (value) of assignment or boolean:
s(:lasgn, :x, s(:lit, 1))
^--rhs---^
364 365 366 367 |
# File 'lib/ruby_parser/bm_sexp.rb', line 364 def rhs expect *ASSIGNMENT_BOOL self[2] end |
#rhs=(exp) ⇒ Object
Sets the right hand side of assignment or boolean.
370 371 372 373 |
# File 'lib/ruby_parser/bm_sexp.rb', line 370 def rhs= exp expect *ASSIGNMENT_BOOL self[2] = exp end |
#second ⇒ Object
26 27 28 |
# File 'lib/ruby_parser/bm_sexp.rb', line 26 def second self[1] end |
#second_arg ⇒ Object
Returns second argument of a method call.
252 253 254 255 256 257 |
# File 'lib/ruby_parser/bm_sexp.rb', line 252 def second_arg expect :call, :attrasgn if self[3] self[3][2] end end |
#second_arg=(exp) ⇒ Object
Sets second argument of a method call.
260 261 262 263 264 265 |
# File 'lib/ruby_parser/bm_sexp.rb', line 260 def second_arg= exp expect :call, :attrasgn if self[3] self[3][2] = exp end end |
#target ⇒ Object
Returns target of a method call:
s(:call, s(:call, nil, :x, s(:arglist)), :y, s(:arglist, s(:lit, 1)))
^-----------target-----------^
148 149 150 151 |
# File 'lib/ruby_parser/bm_sexp.rb', line 148 def target expect :call, :attrasgn self[1] end |
#target=(exp) ⇒ Object
Sets the target of a method call:
154 155 156 157 |
# File 'lib/ruby_parser/bm_sexp.rb', line 154 def target= exp expect :call, :attrasgn self[1] = exp end |
#then_clause ⇒ Object
Returns ‘then’ clause of an if expression:
s(:if,
s(:lvar, :condition),
s(:lvar, :then_val), <-- then clause
s(:lvar, :else_val)))
284 285 286 287 |
# File 'lib/ruby_parser/bm_sexp.rb', line 284 def then_clause expect :if self[2] end |
#to_sym ⇒ Object
30 31 32 |
# File 'lib/ruby_parser/bm_sexp.rb', line 30 def to_sym self.value.to_sym end |
#value ⇒ Object
21 22 23 24 |
# File 'lib/ruby_parser/bm_sexp.rb', line 21 def value raise WrongSexpError, "Sexp#value called on multi-item Sexp", caller[1..-1] if size > 2 last end |