Class: Parser::AST::Node
- Inherits:
-
Object
- Object
- Parser::AST::Node
- Defined in:
- lib/synvert/core/node_ext.rb
Overview
Parser::AST::Node monkey patch.
Instance Method Summary collapse
-
#arguments ⇒ Array<Parser::AST::Node>
Get arguments node of :send, :block or :defined? node.
-
#body ⇒ Array<Parser::AST::Node>
Get body node of :begin or :block node.
-
#caller ⇒ Parser::AST::Node
Get caller node of :block node.
-
#condition ⇒ Parser::AST::Node
Get condition node of :if node.
- #debug_info ⇒ Object
-
#has_key?(key) ⇒ Boolean
Test if hash node contains specified key.
-
#hash_value(key) ⇒ Parser::AST::Node
Get hash value node according to specified key.
-
#indent ⇒ Integer
Get the indent of current node.
-
#key ⇒ Parser::AST::Node
Get key node of hash :pair node.
-
#keys ⇒ Array<Parser::AST::Node>
Get keys node of :hash node.
-
#left_value ⇒ Parser::AST::Node
Return the left value.
-
#line ⇒ Integer
Get the line of current node.
-
#match?(rules) ⇒ Boolean
Match current node with rules.
-
#message ⇒ Parser::AST::Node
Get message node of :super or :send node.
-
#name ⇒ Parser::AST::Node
Get name node of :class, :module, :const, :mlhs, :def and :defs node.
-
#parent_class ⇒ Parser::AST::Node
Get parent_class node of :class node.
-
#parent_const ⇒ Parser::AST::Node
Get parent constant node of :const node.
-
#receiver ⇒ Parser::AST::Node
Get receiver node of :send node.
-
#recursive_children {|child| ... } ⇒ Object
Recursively iterate all child nodes of current node.
-
#rewritten_source(code) ⇒ String
Get rewritten source code.
-
#right_value ⇒ Array<Parser::AST::Node>
Return the right value.
- #to_s ⇒ Object
-
#to_source ⇒ String
Get the source code of current node.
-
#to_value ⇒ Object
Return the exact value.
-
#value ⇒ Parser::AST::Node
Get value node of hash :pair node.
-
#values ⇒ Array<Parser::AST::Node>
Get values node of :hash node.
Instance Method Details
#arguments ⇒ Array<Parser::AST::Node>
Get arguments node of :send, :block or :defined? node.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/synvert/core/node_ext.rb', line 99 def arguments case self.type when :def, :block ArgumentsNode.new self.children[1] when :defs ArgumentsNode.new self.children[2] when :send self.children[2..-1] when :defined? self.children else raise Synvert::Core::MethodNotSupported.new "arguments is not handled for #{self.debug_info}" end end |
#body ⇒ Array<Parser::AST::Node>
Get body node of :begin or :block node.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/synvert/core/node_ext.rb', line 130 def body case self.type when :begin self.children when :def, :block return [] if self.children[2].nil? :begin == self.children[2].type ? self.children[2].body : self.children[2..-1] when :defs return [] if self.children[3].nil? :begin == self.children[3].type ? self.children[3].body : self.children[3..-1] else raise Synvert::Core::MethodNotSupported.new "body is not handled for #{self.debug_info}" end end |
#caller ⇒ Parser::AST::Node
Get caller node of :block node.
118 119 120 121 122 123 124 |
# File 'lib/synvert/core/node_ext.rb', line 118 def caller if :block == self.type self.children[0] else raise Synvert::Core::MethodNotSupported.new "caller is not handled for #{self.debug_info}" end end |
#condition ⇒ Parser::AST::Node
Get condition node of :if node.
149 150 151 152 153 154 155 |
# File 'lib/synvert/core/node_ext.rb', line 149 def condition if :if == self.type self.children[0] else raise Synvert::Core::MethodNotSupported.new "condition is not handled for #{self.debug_info}" end end |
#debug_info ⇒ Object
285 286 287 288 289 290 291 292 |
# File 'lib/synvert/core/node_ext.rb', line 285 def debug_info "\n" + [ "file: #{self.loc.expression.source_buffer.name}", "line: #{self.loc.expression.line}", "source: #{self.to_source}", "node: #{self.inspect}" ].join("\n") end |
#has_key?(key) ⇒ Boolean
Test if hash node contains specified key.
186 187 188 189 190 191 192 |
# File 'lib/synvert/core/node_ext.rb', line 186 def has_key?(key) if :hash == self.type self.children.any? { |pair_node| pair_node.key.to_value == key } else raise Synvert::Core::MethodNotSupported.new "has_key? is not handled for #{self.debug_info}" end end |
#hash_value(key) ⇒ Parser::AST::Node
Get hash value node according to specified key.
199 200 201 202 203 204 205 206 |
# File 'lib/synvert/core/node_ext.rb', line 199 def hash_value(key) if :hash == self.type value_node = self.children.find { |pair_node| pair_node.key.to_value == key } value_node ? value_node.value : nil else raise Synvert::Core::MethodNotSupported.new "has_key? is not handled for #{self.debug_info}" end end |
#indent ⇒ Integer
Get the indent of current node.
306 307 308 |
# File 'lib/synvert/core/node_ext.rb', line 306 def indent self.loc.expression.column end |
#key ⇒ Parser::AST::Node
Get key node of hash :pair node.
212 213 214 215 216 217 218 |
# File 'lib/synvert/core/node_ext.rb', line 212 def key if :pair == self.type self.children.first else raise Synvert::Core::MethodNotSupported.new "key is not handled for #{self.debug_info}" end end |
#keys ⇒ Array<Parser::AST::Node>
Get keys node of :hash node.
161 162 163 164 165 166 167 |
# File 'lib/synvert/core/node_ext.rb', line 161 def keys if :hash == self.type self.children.map { |child| child.children[0] } else raise Synvert::Core::MethodNotSupported.new "keys is not handled for #{self.debug_info}" end end |
#left_value ⇒ Parser::AST::Node
Return the left value.
236 237 238 239 240 241 242 |
# File 'lib/synvert/core/node_ext.rb', line 236 def left_value if [:masgn, :lvasgn, :ivasgn].include? self.type self.children[0] else raise Synvert::Core::MethodNotSupported.new "left_value is not handled for #{self.debug_info}" end end |
#line ⇒ Integer
Get the line of current node.
313 314 315 |
# File 'lib/synvert/core/node_ext.rb', line 313 def line self.loc.expression.line end |
#match?(rules) ⇒ Boolean
Match current node with rules.
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/synvert/core/node_ext.rb', line 334 def match?(rules) flat_hash(rules).keys.all? do |multi_keys| if multi_keys.last == :any actual_values = actual_value(self, multi_keys[0...-1]) expected = expected_value(rules, multi_keys) actual_values.any? { |actual| match_value?(actual, expected) } elsif multi_keys.last == :not actual = actual_value(self, multi_keys[0...-1]) expected = expected_value(rules, multi_keys) !match_value?(actual, expected) else actual = actual_value(self, multi_keys) expected = expected_value(rules, multi_keys) match_value?(actual, expected) end end end |
#message ⇒ Parser::AST::Node
Get message node of :super or :send node.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/synvert/core/node_ext.rb', line 84 def case self.type when :super, :zsuper :super when :send self.children[1] else raise Synvert::Core::MethodNotSupported.new "message is not handled for #{self.debug_info}" end end |
#name ⇒ Parser::AST::Node
Get name node of :class, :module, :const, :mlhs, :def and :defs node.
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/synvert/core/node_ext.rb', line 31 def name case self.type when :class, :module, :def, :arg, :blockarg, :restarg self.children[0] when :defs, :const self.children[1] when :mlhs self else raise Synvert::Core::MethodNotSupported.new "name is not handled for #{self.debug_info}" end end |
#parent_class ⇒ Parser::AST::Node
Get parent_class node of :class node.
48 49 50 51 52 53 54 |
# File 'lib/synvert/core/node_ext.rb', line 48 def parent_class if :class == self.type self.children[1] else raise Synvert::Core::MethodNotSupported.new "parent_class is not handled for #{self.debug_info}" end end |
#parent_const ⇒ Parser::AST::Node
Get parent constant node of :const node.
60 61 62 63 64 65 66 |
# File 'lib/synvert/core/node_ext.rb', line 60 def parent_const if :const == self.type self.children[0] else raise Synvert::Core::MethodNotSupported.new "parent_const is not handled for #{self.debug_info}" end end |
#receiver ⇒ Parser::AST::Node
Get receiver node of :send node.
72 73 74 75 76 77 78 |
# File 'lib/synvert/core/node_ext.rb', line 72 def receiver if :send == self.type self.children[0] else raise Synvert::Core::MethodNotSupported.new "receiver is not handled for #{self.debug_info}" end end |
#recursive_children {|child| ... } ⇒ Object
Recursively iterate all child nodes of current node.
321 322 323 324 325 326 327 328 |
# File 'lib/synvert/core/node_ext.rb', line 321 def recursive_children self.children.each do |child| if Parser::AST::Node === child yield child child.recursive_children { |c| yield c } end end end |
#rewritten_source(code) ⇒ String
Get rewritten source code.
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/synvert/core/node_ext.rb', line 359 def rewritten_source(code) code.gsub(/{{(.*?)}}/m) do old_code = $1 if self.respond_to? old_code.split(/\.|\[/).first evaluated = self.instance_eval old_code case evaluated when Parser::AST::Node evaluated.loc.expression.source when Array, ArgumentsNode if evaluated.size > 0 file_source = evaluated.first.loc.expression.source_buffer.source source = file_source[evaluated.first.loc.expression.begin_pos...evaluated.last.loc.expression.end_pos] lines = source.split "\n" lines_count = lines.length if lines_count > 1 && lines_count == evaluated.size new_code = [] lines.each_with_index { |line, index| new_code << (index == 0 ? line : line[evaluated.first.indent-2..-1]) } new_code.join("\n") else source end end when String, Symbol evaluated when NilClass 'nil' else raise Synvert::Core::MethodNotSupported.new "rewritten_source is not handled for #{evaluated.inspect}" end else "{{#{old_code}}}" end end end |
#right_value ⇒ Array<Parser::AST::Node>
Return the right value.
248 249 250 251 252 253 254 |
# File 'lib/synvert/core/node_ext.rb', line 248 def right_value if [:masgn, :lvasgn, :ivasgn].include? self.type self.children[1] else raise Synvert::Core::MethodNotSupported.new "right_value is not handled for #{self.debug_info}" end end |
#to_s ⇒ Object
279 280 281 282 283 |
# File 'lib/synvert/core/node_ext.rb', line 279 def to_s if :mlhs == self.type "(#{self.children.map(&:name).join(', ')})" end end |
#to_source ⇒ String
Get the source code of current node.
297 298 299 300 301 |
# File 'lib/synvert/core/node_ext.rb', line 297 def to_source if self.loc.expression self.loc.expression.source end end |
#to_value ⇒ Object
Return the exact value.
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/synvert/core/node_ext.rb', line 260 def to_value case self.type when :int, :str, :sym self.children.last when :true true when :false false when :array self.children.map(&:to_value) when :irange (self.children.first.to_value..self.children.last.to_value) when :begin self.children.first.to_value else raise Synvert::Core::MethodNotSupported.new "to_value is not handled for #{self.debug_info}" end end |
#value ⇒ Parser::AST::Node
Get value node of hash :pair node.
224 225 226 227 228 229 230 |
# File 'lib/synvert/core/node_ext.rb', line 224 def value if :pair == self.type self.children.last else raise Synvert::Core::MethodNotSupported.new "value is not handled for #{self.debug_info}" end end |
#values ⇒ Array<Parser::AST::Node>
Get values node of :hash node.
173 174 175 176 177 178 179 |
# File 'lib/synvert/core/node_ext.rb', line 173 def values if :hash == self.type self.children.map { |child| child.children[1] } else raise Synvert::Core::MethodNotSupported.new "keys is not handled for #{self.debug_info}" end end |