Mercurial > projects > dil
comparison trunk/src/cmd/ImportGraph.d @ 785:57ef69eced96
Added functions isCodeSection() and skipCodeSection().
Added a lot of documentation comments and revised some.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Sat, 23 Feb 2008 21:15:56 +0100 |
parents | edd217e14736 |
children |
comparison
equal
deleted
inserted
replaced
784:939097e0990f | 785:57ef69eced96 |
---|---|
18 import tango.io.FileConst; | 18 import tango.io.FileConst; |
19 import tango.text.Util; | 19 import tango.text.Util; |
20 | 20 |
21 alias FileConst.PathSeparatorChar dirSep; | 21 alias FileConst.PathSeparatorChar dirSep; |
22 | 22 |
23 /// Options for the importgraph command. | |
23 enum IGraphOption | 24 enum IGraphOption |
24 { | 25 { |
25 None, | 26 None, |
26 IncludeUnlocatableModules = 1, | 27 IncludeUnlocatableModules = 1, |
27 PrintDot = 1<<1, | 28 PrintDot = 1<<1, |
32 PrintPaths = 1<<6, | 33 PrintPaths = 1<<6, |
33 PrintList = 1<<7, | 34 PrintList = 1<<7, |
34 MarkCyclicModules = 1<<8, | 35 MarkCyclicModules = 1<<8, |
35 } | 36 } |
36 | 37 |
38 /// Represents a module dependency graph. | |
37 class Graph | 39 class Graph |
38 { | 40 { |
39 Vertex[] vertices; | 41 Vertex[] vertices; /// The vertices or modules. |
40 Edge[] edges; | 42 Edge[] edges; /// The edges or import statements. |
41 | 43 |
42 void addVertex(Vertex vertex) | 44 void addVertex(Vertex vertex) |
43 { | 45 { |
44 vertex.id = vertices.length; | 46 vertex.id = vertices.length; |
45 vertices ~= vertex; | 47 vertices ~= vertex; |
121 enum Status : ubyte | 123 enum Status : ubyte |
122 { None, Visiting, Visited } | 124 { None, Visiting, Visited } |
123 Status status; /// Used by the cycle detection algorithm. | 125 Status status; /// Used by the cycle detection algorithm. |
124 } | 126 } |
125 | 127 |
128 /// Searches for a module in the file system looking in importPaths. | |
129 /// Returns: the file path to the module, or null if it wasn't found. | |
126 string findModuleFilePath(string moduleFQNPath, string[] importPaths) | 130 string findModuleFilePath(string moduleFQNPath, string[] importPaths) |
127 { | 131 { |
128 auto filePath = new FilePath(); | 132 auto filePath = new FilePath(); |
129 foreach (importPath; importPaths) | 133 foreach (importPath; importPaths) |
130 { | 134 { |
138 } | 142 } |
139 } | 143 } |
140 return null; | 144 return null; |
141 } | 145 } |
142 | 146 |
147 /// Builds a module dependency graph. | |
143 class GraphBuilder | 148 class GraphBuilder |
144 { | 149 { |
145 Graph graph; | 150 Graph graph; |
146 IGraphOption options; | 151 IGraphOption options; |
147 string[] importPaths; /// Where to look for modules. | 152 string[] importPaths; /// Where to look for modules. |
152 { | 157 { |
153 this.graph = new Graph; | 158 this.graph = new Graph; |
154 } | 159 } |
155 | 160 |
156 /// Start building the graph and return that. | 161 /// Start building the graph and return that. |
162 /// Params: | |
163 /// fileName = the file name of the root module. | |
157 Graph start(string fileName) | 164 Graph start(string fileName) |
158 { | 165 { |
159 loadModule(fileName); | 166 loadModule(fileName); |
160 return graph; | 167 return graph; |
161 } | 168 } |
162 | 169 |
163 /++ | 170 /// Loads all modules recursively and builds the graph at the same time. |
164 Loads all modules recursively and builds the graph at the same time. | 171 /// Params: |
165 Params: | 172 /// moduleFQNPath = the path version of the module FQN.$(BR) |
166 moduleFQNPath = e.g.: dil/ast/Node (module FQN = dil.ast.Node) | 173 /// E.g.: FQN = dil.ast.Node -> FQNPath = dil/ast/Node |
167 +/ | |
168 Vertex loadModule(string moduleFQNPath) | 174 Vertex loadModule(string moduleFQNPath) |
169 { | 175 { |
170 // Look up in table if the module is already loaded. | 176 // Look up in table if the module is already loaded. |
171 auto pVertex = moduleFQNPath in loadedModulesTable; | 177 auto pVertex = moduleFQNPath in loadedModulesTable; |
172 if (pVertex !is null) | 178 if (pVertex !is null) |
226 } | 232 } |
227 return vertex; | 233 return vertex; |
228 } | 234 } |
229 } | 235 } |
230 | 236 |
237 /// Executes the importgraph command. | |
231 void execute(string filePathString, CompilationContext context, string[] strRegexps, | 238 void execute(string filePathString, CompilationContext context, string[] strRegexps, |
232 uint levels, string siStyle, string piStyle, IGraphOption options) | 239 uint levels, string siStyle, string piStyle, IGraphOption options) |
233 { | 240 { |
234 // Init regular expressions. | 241 // Init regular expressions. |
235 RegExp[] regexps; | 242 RegExp[] regexps; |
267 } | 274 } |
268 else | 275 else |
269 printDotDocument(graph, siStyle, piStyle, options); | 276 printDotDocument(graph, siStyle, piStyle, options); |
270 } | 277 } |
271 | 278 |
279 /// Prints the file paths to the modules. | |
272 void printModulePaths(Vertex[] vertices, uint level, char[] indent) | 280 void printModulePaths(Vertex[] vertices, uint level, char[] indent) |
273 { | 281 { |
274 if (level == 0) | 282 if (level == 0) |
275 return; | 283 return; |
276 foreach (vertex; vertices) | 284 foreach (vertex; vertices) |
279 if (vertex.outgoing.length) | 287 if (vertex.outgoing.length) |
280 printModulePaths(vertex.outgoing, level-1, indent~" "); | 288 printModulePaths(vertex.outgoing, level-1, indent~" "); |
281 } | 289 } |
282 } | 290 } |
283 | 291 |
292 /// Prints a list of module FQNs. | |
284 void printModuleList(Vertex[] vertices, uint level, char[] indent) | 293 void printModuleList(Vertex[] vertices, uint level, char[] indent) |
285 { | 294 { |
286 if (level == 0) | 295 if (level == 0) |
287 return; | 296 return; |
288 foreach (vertex; vertices) | 297 foreach (vertex; vertices) |
291 if (vertex.outgoing.length) | 300 if (vertex.outgoing.length) |
292 printModuleList(vertex.outgoing, level-1, indent~" "); | 301 printModuleList(vertex.outgoing, level-1, indent~" "); |
293 } | 302 } |
294 } | 303 } |
295 | 304 |
305 /// Prints the graph as a graphviz dot document. | |
296 void printDotDocument(Graph graph, string siStyle, string piStyle, | 306 void printDotDocument(Graph graph, string siStyle, string piStyle, |
297 IGraphOption options) | 307 IGraphOption options) |
298 { | 308 { |
299 Vertex[][string] verticesByPckgName; | 309 Vertex[][string] verticesByPckgName; |
300 if (options & IGraphOption.GroupByFullPackageName) | 310 if (options & IGraphOption.GroupByFullPackageName) |