Mercurial > projects > dil
annotate trunk/src/cmd/ImportGraph.d @ 698:1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Sat, 26 Jan 2008 20:08:03 +0100 |
parents | 39fac5531b85 |
children | 4c0ea78a7f8b |
rev | line source |
---|---|
364 | 1 /++ |
2 Author: Aziz Köksal | |
3 License: GPL3 | |
4 +/ | |
5 module cmd.ImportGraph; | |
580
fa6d3c52757d
Moved SyntaxTree.d to new package 'ast'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
576
diff
changeset
|
6 |
fa6d3c52757d
Moved SyntaxTree.d to new package 'ast'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
576
diff
changeset
|
7 import dil.ast.Node; |
585
05c375fb2d5c
Moved dil.Declarations to dil.ast.Declarations.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
8 import dil.ast.Declarations; |
593
2848ce3becf5
Moved dil.Module to dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
585
diff
changeset
|
9 import dil.semantic.Module; |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
10 import dil.parser.ImportParser; |
364 | 11 import dil.File; |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
366
diff
changeset
|
12 import dil.Settings; |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
375
diff
changeset
|
13 import tango.text.Regex : RegExp = Regex; |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
375
diff
changeset
|
14 import tango.io.FilePath; |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
375
diff
changeset
|
15 import tango.io.FileConst; |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
375
diff
changeset
|
16 import tango.text.Util; |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
375
diff
changeset
|
17 import common; |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
375
diff
changeset
|
18 |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
375
diff
changeset
|
19 alias FileConst.PathSeparatorChar dirSep; |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
20 |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
21 enum IGraphOption |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
22 { |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
23 None, |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
24 IncludeUnlocatableModules = 1, |
427
e2bbc6406a14
Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
25 PrintDot = 1<<1, |
e2bbc6406a14
Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
26 HighlightCyclicEdges = 1<<2, |
e2bbc6406a14
Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
27 HighlightCyclicVertices = 1<<3, |
e2bbc6406a14
Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
28 GroupByPackageNames = 1<<4, |
e2bbc6406a14
Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
29 GroupByFullPackageName = 1<<5, |
e2bbc6406a14
Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
30 PrintPaths = 1<<6, |
e2bbc6406a14
Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
31 PrintList = 1<<7, |
e2bbc6406a14
Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
32 MarkCyclicModules = 1<<8, |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
33 } |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
366
diff
changeset
|
34 |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
35 string findModulePath(string moduleFQNPath, string[] importPaths) |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
366
diff
changeset
|
36 { |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
37 auto filePath = new FilePath(); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
38 foreach (importPath; importPaths) |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
366
diff
changeset
|
39 { |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
40 filePath.set(importPath); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
41 filePath.append(moduleFQNPath); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
42 foreach (moduleSuffix; [".d", ".di"/*interface file*/]) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
43 { |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
44 filePath.suffix(moduleSuffix); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
45 if (filePath.exists()) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
46 return filePath.toString(); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
47 } |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
366
diff
changeset
|
48 } |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
366
diff
changeset
|
49 return null; |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
366
diff
changeset
|
50 } |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
366
diff
changeset
|
51 |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
52 class Graph |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
53 { |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
54 Vertex[] vertices; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
55 Edge[] edges; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
56 |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
57 void addVertex(Vertex vertex) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
58 { |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
59 vertex.id = vertices.length; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
60 vertices ~= vertex; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
61 } |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
62 |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
63 void addEdge(Vertex from, Vertex to) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
64 { |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
65 edges ~= new Edge(from, to); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
66 from.outgoing ~= to; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
67 to.incoming ~= from; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
68 } |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
69 |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
70 /// Walks the graph and marks cyclic vertices and edges. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
71 void detectCycles() |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
72 { // Cycles could also be detected in the GraphBuilder, |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
73 // but having the code here makes things much clearer. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
74 void visit(Vertex vertex) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
75 { |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
76 if (vertex.status == Vertex.Status.Visiting) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
77 { |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
78 vertex.isCyclic = true; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
79 return; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
80 } |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
81 vertex.status = Vertex.Status.Visiting; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
82 foreach (outVertex; vertex.outgoing) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
83 visit(outVertex); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
84 vertex.status = Vertex.Status.Visited; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
85 } |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
86 // Start visiting vertices. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
87 visit(vertices[0]); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
88 |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
89 foreach (edge; edges) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
90 if (edge.from.isCyclic && edge.to.isCyclic) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
91 edge.isCyclic = true; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
92 } |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
93 } |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
94 |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
95 /// Represents a directed connection between two vertices. |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
96 class Edge |
370 | 97 { |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
98 Vertex from; /// Coming from vertex. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
99 Vertex to; /// Going to vertex. |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
100 bool isCyclic; |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
101 |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
102 this(Vertex from, Vertex to) |
370 | 103 { |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
104 this.from = from; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
105 this.to = to; |
370 | 106 } |
107 } | |
108 | |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
109 /// Represents a module in the graph. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
110 class Vertex |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
366
diff
changeset
|
111 { |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
112 Module modul; /// The module represented by this vertex. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
113 uint id; /// The nth vertex in the graph. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
114 Vertex[] incoming; /// Also called predecessors. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
115 Vertex[] outgoing; /// Also called successors. |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
116 bool isCyclic; /// Whether this vertex is in a cyclic relationship with other vertices. |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
117 |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
118 enum Status : ubyte |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
119 { None, Visiting, Visited } |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
120 Status status; /// Used by the cycle detection algorithm. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
121 } |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
122 |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
123 class GraphBuilder |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
124 { |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
125 Graph graph; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
126 IGraphOption options; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
127 string[] importPaths; /// Where to look for modules. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
128 Vertex[string] loadedModulesTable; /// Maps FQN paths to modules. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
129 bool delegate(string) filterPredicate; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
130 |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
131 this() |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
132 { |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
133 this.graph = new Graph; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
134 } |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
135 |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
136 /// Start building the graph and return that. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
137 Graph start(string fileName) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
138 { |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
139 loadModule(fileName); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
140 return graph; |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
141 } |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
142 |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
143 /++ |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
144 Loads all modules recursively and builds the graph at the same time. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
145 Params: |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
146 moduleFQNPath = e.g.: dil/ast/Node (module FQN = dil.ast.Node) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
147 +/ |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
148 Vertex loadModule(string moduleFQNPath) |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
149 { |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
150 // Filter out modules. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
151 if (filterPredicate(moduleFQNPath)) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
152 return null; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
153 |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
154 // Look up in table if the module is already loaded. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
155 auto pVertex = moduleFQNPath in loadedModulesTable; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
156 if (pVertex !is null) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
157 return *pVertex; // Return already loaded module. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
158 |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
159 // Locate the module in the file system. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
160 auto moduleFilePath = findModulePath(moduleFQNPath, importPaths); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
161 |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
162 Vertex vertex; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
163 |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
164 if (moduleFilePath is null) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
165 { // Module not found. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
166 if (options & IGraphOption.IncludeUnlocatableModules) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
167 { // Include module nevertheless. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
168 vertex = new Vertex; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
169 vertex.modul = new Module(""); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
170 vertex.modul.setFQN(replace(moduleFQNPath, dirSep, '.')); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
171 graph.addVertex(vertex); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
172 loadedModulesTable[moduleFQNPath] = vertex; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
173 } |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
174 } |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
175 else |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
176 { |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
177 auto modul = new Module(moduleFilePath); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
178 // Use lightweight ImportParser. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
179 modul.parser = new ImportParser(loadFile(moduleFilePath), moduleFilePath); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
180 modul.parse(); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
181 |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
182 vertex = new Vertex; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
183 vertex.modul = modul; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
184 |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
185 graph.addVertex(vertex); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
186 loadedModulesTable[modul.getFQNPath()] = vertex; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
187 // Load modules which this module depends on. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
188 foreach (moduleFQNPath_; modul.getImportPaths()) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
189 { |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
190 auto loaded = loadModule(moduleFQNPath_); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
191 if (loaded !is null) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
192 graph.addEdge(vertex, loaded); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
193 } |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
194 } |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
195 return vertex; |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
196 } |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
197 } |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
198 |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
199 void execute(string filePathString, string[] importPaths, string[] strRegexps, uint levels, IGraphOption options) |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
200 { |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
201 // Init regular expressions. |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
202 RegExp[] regexps; |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
203 foreach (strRegexp; strRegexps) |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
204 regexps ~= new RegExp(strRegexp); |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
205 |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
366
diff
changeset
|
206 // Add directory of file and global directories to import paths. |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
207 auto filePath = new FilePath(filePathString); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
208 auto fileDir = filePath.folder(); |
370 | 209 if (fileDir.length) |
210 importPaths ~= fileDir; | |
211 importPaths ~= GlobalSettings.importPaths; | |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
366
diff
changeset
|
212 |
364 | 213 |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
214 auto gbuilder = new GraphBuilder; |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
366
diff
changeset
|
215 |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
216 gbuilder.importPaths = importPaths; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
217 gbuilder.options = options; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
218 gbuilder.filterPredicate = (string moduleFQNPath) { |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
219 foreach (rx; regexps) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
220 // Replace slashes: dil/ast/Node -> dil.ast.Node |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
221 if (rx.test(replace(moduleFQNPath, dirSep, '.'))) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
222 return true; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
223 return false; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
224 }; |
364 | 225 |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
226 auto graph = gbuilder.start(filePath.name()); |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
227 |
427
e2bbc6406a14
Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
228 if (options & (IGraphOption.PrintList | IGraphOption.PrintPaths)) |
e2bbc6406a14
Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
229 { |
e2bbc6406a14
Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
230 if (options & IGraphOption.MarkCyclicModules) |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
231 graph.detectCycles(); |
427
e2bbc6406a14
Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
232 |
e2bbc6406a14
Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
233 if (options & IGraphOption.PrintPaths) |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
234 printModulePaths(graph.vertices, levels+1, ""); |
427
e2bbc6406a14
Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
235 else |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
236 printModuleList(graph.vertices, levels+1, ""); |
427
e2bbc6406a14
Added a new option '-m' to the igraph command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
237 } |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
238 else |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
239 printDotDocument(graph, options); |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
240 } |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
241 |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
242 void printModulePaths(Vertex[] vertices, uint level, char[] indent) |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
243 { |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
244 if (level == 0) |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
245 return; |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
246 foreach (vertex; vertices) |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
247 { |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
248 Stdout(indent)((vertex.isCyclic?"*":"")~vertex.modul.filePath).newline; |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
249 if (vertex.outgoing.length) |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
250 printModulePaths(vertex.outgoing, level-1, indent~" "); |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
251 } |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
252 } |
370 | 253 |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
254 void printModuleList(Vertex[] vertices, uint level, char[] indent) |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
255 { |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
256 if (level == 0) |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
257 return; |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
258 foreach (vertex; vertices) |
370 | 259 { |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
260 Stdout(indent)((vertex.isCyclic?"*":"")~vertex.modul.getFQN()).newline; |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
261 if (vertex.outgoing.length) |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
262 printModuleList(vertex.outgoing, level-1, indent~" "); |
370 | 263 } |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
264 } |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
265 |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
266 void printDotDocument(Graph graph, IGraphOption options) |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
267 { |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
268 Vertex[][string] verticesByPckgName; |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
269 if (options & IGraphOption.GroupByFullPackageName) |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
270 foreach (vertex; graph.vertices) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
271 verticesByPckgName[vertex.modul.packageName] ~= vertex; |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
272 |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
273 if (options & (IGraphOption.HighlightCyclicVertices | |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
274 IGraphOption.HighlightCyclicEdges)) |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
275 graph.detectCycles(); |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
276 |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
277 // Output header of the dot document. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
278 Stdout("Digraph ImportGraph\n{\n"); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
279 // Output nodes. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
280 // 'i' and vertex.id should be the same. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
281 foreach (i, vertex; graph.vertices) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
282 Stdout.formatln(` n{} [label="{}"{}];`, i, vertex.modul.getFQN(), (vertex.isCyclic ? ",style=filled,fillcolor=tomato" : "")); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
283 // Output edges. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
284 foreach (edge; graph.edges) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
285 Stdout.formatln(` n{} -> n{}{};`, edge.from.id, edge.to.id, (edge.isCyclic ? "[color=red]" : "")); |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
286 |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
287 if (options & IGraphOption.GroupByFullPackageName) |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
288 foreach (packageName, vertices; verticesByPckgName) |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
289 { // Output nodes in a cluster. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
290 Stdout.format(` subgraph "cluster_{}" {`\n` label="{}";color=blue;`"\n ", packageName, packageName); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
291 foreach (vertex; vertices) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
292 Stdout.format(`n{};`, vertex.id); |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
375
diff
changeset
|
293 Stdout("\n }\n"); |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
294 } |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
295 |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
375
diff
changeset
|
296 Stdout("}\n"); |
364 | 297 } |
370 | 298 |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
299 /+ |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
300 // This is the old algorithm that was used to detect cycles in a directed graph. |
431
7a6bfa569a52
Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
427
diff
changeset
|
301 void analyzeGraph(Vertex[] vertices_init, Edge[] edges) |
370 | 302 { |
431
7a6bfa569a52
Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
427
diff
changeset
|
303 void recursive(Vertex[] vertices) |
370 | 304 { |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
305 foreach (idx, vertex; vertices) |
370 | 306 { |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
307 uint outgoing, incoming; |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
308 foreach (j, edge; edges) |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
309 { |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
310 if (edge.from is vertex) |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
311 outgoing++; |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
312 if (edge.to is vertex) |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
313 incoming++; |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
314 } |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
315 |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
316 if (outgoing == 0) |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
317 { |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
318 if (incoming != 0) |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
319 { |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
320 // Vertex is a sink. |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
321 alias outgoing i; // Reuse |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
322 alias incoming j; // Reuse |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
323 // Remove edges. |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
324 for (i=j=0; i < edges.length; i++) |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
325 if (edges[i].to !is vertex) |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
326 edges[j++] = edges[i]; |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
327 edges.length = j; |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
328 vertices = vertices[0..idx] ~ vertices[idx+1..$]; |
431
7a6bfa569a52
Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
427
diff
changeset
|
329 recursive(vertices); |
7a6bfa569a52
Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
427
diff
changeset
|
330 return; |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
331 } |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
332 else |
431
7a6bfa569a52
Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
427
diff
changeset
|
333 { |
7a6bfa569a52
Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
427
diff
changeset
|
334 // Edges to this vertex were removed previously. |
7a6bfa569a52
Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
427
diff
changeset
|
335 // Only remove vertex now. |
7a6bfa569a52
Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
427
diff
changeset
|
336 vertices = vertices[0..idx] ~ vertices[idx+1..$]; |
7a6bfa569a52
Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
427
diff
changeset
|
337 recursive(vertices); |
7a6bfa569a52
Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
427
diff
changeset
|
338 return; |
7a6bfa569a52
Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
427
diff
changeset
|
339 } |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
340 } |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
341 else if (incoming == 0) |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
342 { |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
343 // Vertex is a source |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
344 alias outgoing i; // Reuse |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
345 alias incoming j; // Reuse |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
346 // Remove edges. |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
347 for (i=j=0; i < edges.length; i++) |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
348 if (edges[i].from !is vertex) |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
349 edges[j++] = edges[i]; |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
350 edges.length = j; |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
351 vertices = vertices[0..idx] ~ vertices[idx+1..$]; |
431
7a6bfa569a52
Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
427
diff
changeset
|
352 recursive(vertices); |
7a6bfa569a52
Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
427
diff
changeset
|
353 return; |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
354 } |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
355 // else |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
356 // { |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
357 // // source && sink |
431
7a6bfa569a52
Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
427
diff
changeset
|
358 // // continue loop. |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
359 // } |
370 | 360 } |
361 | |
375
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
362 // When reaching this point it means only cylic edges and vertices are left. |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
363 foreach (vertex; vertices) |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
364 vertex.isCyclic = true; |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
365 foreach (edge; edges) |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
366 if (edge) |
0bd21b746a04
- Added code to main() for recognizing options to the importgraph command.
aziz
parents:
370
diff
changeset
|
367 edge.isCyclic = true; |
370 | 368 } |
431
7a6bfa569a52
Fix: analyzeGraph() should work correctly now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
427
diff
changeset
|
369 recursive(vertices_init); |
370 | 370 } |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
371 +/ |