rbgccxml 0.9.1 → 1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,152 +0,0 @@
1
- module RbGCCXML
2
-
3
- # A module of methods used to parse out the flat GCC-XML xml structure into
4
- # a proper heirarchy. These methods are used internally and not intended
5
- # for outside use.
6
- module XMLParsing
7
-
8
- # Save a reference to the root node of the parsed XML
9
- def self.doc_root=(root)
10
- @@doc_root = root
11
- self.clear_cache
12
- end
13
-
14
- # Clear the internal query cache
15
- def self.clear_cache
16
- @@find_query_cache = {}
17
- @@all_query_cache = {}
18
- end
19
-
20
- # Generic finding of nodes according to attributes.
21
- # Special options:
22
- #
23
- # <tt>:node_type</tt>:: Specify a certain node type to search by
24
- #
25
- # Any other options is directly mapped to attributes on the node. For example, to find
26
- # a Function node that have the name "functor":
27
- #
28
- # XMLParsing.find(:node_type => "Function", :name => "functor")
29
- #
30
- # Returns the first found node
31
- def self.find(options = {})
32
- return nil if options.empty?
33
-
34
- cache_key = options.to_s
35
- cached = @@find_query_cache[cache_key]
36
- return cached if cached
37
-
38
- type = options.delete(:node_type)
39
-
40
- attrs = options.map {|key, value| "[@#{key}='#{value}']"}.join
41
- xpath = "//#{type || '*'}#{attrs}"
42
-
43
- got = @@doc_root.search(xpath).first
44
-
45
- if got
46
- result = build_type(type || got.name, got)
47
- @@find_query_cache[cache_key] = result
48
- result
49
- else
50
- nil
51
- end
52
- end
53
-
54
- # Generic finding of nodes according to attributes.
55
- # Special options:
56
- #
57
- # <tt>:node_type</tt>:: Specify a certain node type to search by
58
- #
59
- # Any other options is directly mapped to attributes on the node. For example, to find all
60
- # Function nodes:
61
- #
62
- # XMLParsing.find_all(:node_type => "Function")
63
- #
64
- # Returns all matching nodes
65
- def self.find_all(options = {})
66
- results = QueryResult.new
67
- return results if options.empty?
68
- cache_key = options.to_s
69
-
70
- cached = @@all_query_cache[cache_key]
71
- return cached if cached
72
-
73
- type = options.delete(:node_type)
74
- attrs = options.map {|key, value| "[@#{key}='#{value}']"}.join
75
-
76
- xpath = "//#{type || "*"}#{attrs}"
77
-
78
- found = @@doc_root.search(xpath)
79
-
80
- if found
81
- found.each do |got|
82
- results << build_type(type || got.name, got)
83
- end
84
- @@all_query_cache[cache_key] = results
85
- end
86
- results
87
- end
88
-
89
- # Look through the DOM under +node+ for +node_type+ nodes.
90
- # +node_type+ must be the string name of an existing Node subclass.
91
- #
92
- # Returns a QueryResult with the findings.
93
- def self.find_nested_nodes_of_type(node, node_type)
94
- self.find_all(:node_type => node_type, :context => node["id"])
95
- end
96
-
97
- # Arguments are a special case in gccxml as they are actual children of
98
- # the functions / methods they are a part of.
99
- def self.find_arguments_for(node)
100
- get_children_nodes_of_type(node, "Argument")
101
- end
102
-
103
- # Classes / Structs can have superclasses. This method finds the
104
- # classes that are those superclasses according to the access type
105
- # passed in (nil means find all of them)
106
- def self.find_bases_for(node, access_type = nil)
107
- bases = get_children_nodes_of_type(node, "Base")
108
-
109
- if access_type
110
- bases = bases.select {|b| b["access"] == access_type.to_s }
111
- end
112
-
113
- bases.map {|b| b.cpp_type }
114
- end
115
-
116
- # Enumeration values are children of the Enumeration element
117
- def self.get_values_of(enum)
118
- get_children_nodes_of_type(enum.node, "EnumValue")
119
- end
120
-
121
- # Generic lookup method for nodes that are XML children of the passed in node.
122
- # See find_arguments_for and get_values_of for example usage
123
- def self.get_children_nodes_of_type(node, type)
124
- results = QueryResult.new
125
-
126
- node.children.each do |found|
127
- next unless found.element?
128
- results << build_type(type, found)
129
- end
130
-
131
- results.flatten
132
- end
133
-
134
- # Entrance into the type management. Given a GCC-XML node and an attribute
135
- # to reference, find the C++ type related. For example, finding the return
136
- # type of a function:
137
- #
138
- # +find_type_of(func_node, "returns")+ could return "std::string" node, "int" node, etc
139
- #
140
- def self.find_type_of(node, attr)
141
- self.find(:id => node[attr])
142
- end
143
-
144
- private
145
-
146
- # Builds up the related RbGCCXML node according to the GCC-XML node found
147
- # for a given query.
148
- def self.build_type(type_name, node)
149
- RbGCCXML.const_get(type_name).new(node)
150
- end
151
- end
152
- end