class RbGCCXML::NodeCache

Manager of the tree of Nodes we build from GCC-XML.

This is a static class that keeps around references to the entire generated Node tree as well as other structures designed for quick and easy searching of Nodes

Attributes

index_list[R]

Hash of id => node for all nodes

types_list[R]

Hash of Type => [node list] for easy searching of all nodes of a given type

Public Class Methods

<<(node) click to toggle source

Add a new node to the index list

# File lib/rbgccxml/node_cache.rb, line 19
def <<(node)
  @index_list ||= {}
  @types_list ||= {}

  @index_list[node.id] = node

  class_name = node.class.name.split("::")[-1]
  @types_list[class_name] ||= []
  @types_list[class_name] << node
end
all(type) click to toggle source

Get the list of all nodes of a given type

# File lib/rbgccxml/node_cache.rb, line 48
def all(type)
  @types_list[type] || []
end
clear() click to toggle source
# File lib/rbgccxml/node_cache.rb, line 30
def clear
  @index_list = {}
  @types_list = {}
end
find(id) click to toggle source

Given an id, find the node

# File lib/rbgccxml/node_cache.rb, line 43
def find(id)
  @index_list[id]
end
find_by_ids(ids) click to toggle source

Given an array of ids return an array of nodes that match

# File lib/rbgccxml/node_cache.rb, line 53
def find_by_ids(ids)
  QueryResult.new(ids.map {|id| @index_list[id] })
end
find_children_of_type(node, type, matcher = nil) click to toggle source

Look through the DOM under node for type nodes. type must be the string name of an existing Node subclass.

Returns a QueryResult with the findings.

# File lib/rbgccxml/node_cache.rb, line 61
def find_children_of_type(node, type, matcher = nil)
  results = QueryResult.new(self.select_nodes_of_type(node.children, type))
  results = results.find(:name => matcher) if matcher
  results
end
process_tree() click to toggle source

Once all parsing is done and all nodes are in memory we need to actually build up the parent / child relationships of the nodes. We can’t do this during parsing because there is no guarentee the nodes will come in the right order

# File lib/rbgccxml/node_cache.rb, line 71
def process_tree
  @index_list.each do |id, node|

    # If this node has a context, link to that context as a parent
    # and then put ourselves as that parent's child
    if node["context"]
      node.parent = @index_list[node["context"]]
      node.parent.children << node
    end

  end
end
root() click to toggle source

Get the root node of the parse

# File lib/rbgccxml/node_cache.rb, line 36
def root
  # This is simply a way to hide GCC_XML specifics,
  # the :: Namespace node is always id _1
  @index_list["_1"]
end

Protected Class Methods

select_nodes_of_type(nodes, type) click to toggle source
# File lib/rbgccxml/node_cache.rb, line 86
def select_nodes_of_type(nodes, type)
  nodes.select {|node| node.class.to_s == "RbGCCXML::#{type}" }
end