Class: orgorg::jrubyparserorg::jrubyparser::ast::Node

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/jruby-parser/core_ext/node.rb

Instance Method Summary collapse

Instance Method Details

#[](value) ⇒ Object

Find nth child element of this node



9
10
11
# File 'lib/jruby-parser/core_ext/node.rb', line 9

def [](value)
  child_nodes[value]
end

#[]=(index, value) ⇒ Object

Replace the nth child element of this node with the specified value. if the value is an actual Ruby value it will attempt to call to_ast_node on it to do automatic coercion to an AST node. If the node does not contain positioning information then it will just use the old nodes information



19
20
21
22
23
24
25
# File 'lib/jruby-parser/core_ext/node.rb', line 19

def []=(index, value)
  value = value.to_ast_node if value.respond_to? :to_ast_node

  old_value = child_nodes[index]
  value.position = old_value.position unless value.position
  child_nodes[index] = value
end

#each {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



48
49
50
51
# File 'lib/jruby-parser/core_ext/node.rb', line 48

def each(&block)
  yield self
  child_nodes.each { |child| child.each(&block) }
end

#find_type(name, &block) ⇒ Object Also known as: find_node

Find first node by name (which is short_name of actual node)

parameters

  • name is the name of the class you want to find

examples

root.find(:fcall) # Find first child node of type fcall (depth-first)



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jruby-parser/core_ext/node.rb', line 35

def find_type(name, &block)
  name = name.to_s
  return self if name == short_name && (!block_given? || yield(self))

  child_nodes.each do |child|
    value = child.find_type(name, &block)

    return value if value
  end
  nil
end

#short_nameObject



63
64
65
# File 'lib/jruby-parser/core_ext/node.rb', line 63

def short_name
  java_class.name.gsub(/(^.*\.|Node$)/, '').downcase
end

#to_source(opts = {}) ⇒ Object

Convert this node back to human-readable source code.



56
57
58
59
60
61
# File 'lib/jruby-parser/core_ext/node.rb', line 56

def to_source(opts = {})
  filename = opts[:filename] ? opts[:filename] : '(string)'
  java.io.StringWriter.new.tap do |writer|
    accept org.jrubyparser.rewriter.ReWriteVisitor.new(writer, filename)
  end.to_s
end