Mercurial > projects > dil
annotate trunk/src/docgen/misc/parser.d @ 466:db7e27b5c180
Fixed parts of HTML output, some reorganizing.
author | Jari-Matti M?kel? <jmjm@iki.fi> |
---|---|
date | Wed, 31 Oct 2007 15:17:20 +0200 |
parents | b7503e02fbe7 |
children | 381044a3feac |
rev | line source |
---|---|
441 | 1 /** |
2 * Author: Aziz Köksal & Jari-Matti Mäkelä | |
3 * License: GPL3 | |
4 */ | |
5 module docgen.misc.parser; | |
6 | |
7 import dil.Parser; | |
8 import dil.Settings; | |
453
4e5b35df3060
Parsing bugfixes, cleaned up imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
451
diff
changeset
|
9 public import dil.Module; |
441 | 10 import tango.text.Regex : RegExp = Regex; |
11 import tango.io.FilePath; | |
12 import tango.text.Util; | |
13 debug import tango.io.Stdout; | |
14 | |
462
b7503e02fbe7
Docgen code for handling public imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
454
diff
changeset
|
15 alias void delegate (char[] fqn, char[] path, Module module_) modDg; |
b7503e02fbe7
Docgen code for handling public imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
454
diff
changeset
|
16 alias void delegate (Module imported, Module importer, bool isPublic) importDg; |
b7503e02fbe7
Docgen code for handling public imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
454
diff
changeset
|
17 |
441 | 18 class Parser { |
466
db7e27b5c180
Fixed parts of HTML output, some reorganizing.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
462
diff
changeset
|
19 private: |
db7e27b5c180
Fixed parts of HTML output, some reorganizing.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
462
diff
changeset
|
20 |
db7e27b5c180
Fixed parts of HTML output, some reorganizing.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
462
diff
changeset
|
21 static char[] findModulePath(char[] moduleFQN, char[][] importPaths) { |
453
4e5b35df3060
Parsing bugfixes, cleaned up imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
451
diff
changeset
|
22 char[] modulePath; |
441 | 23 |
24 foreach (path; importPaths) { | |
25 modulePath = path ~ (path[$-1] == dirSep ? "" : [dirSep]) ~ moduleFQN ~ ".d"; | |
26 | |
27 // TODO: also check for *.di? | |
28 | |
29 if ((new FilePath(modulePath)).exists()) { | |
30 debug Stdout(" * File for ")(moduleFQN)(" found: ")(modulePath).newline; | |
31 return modulePath; | |
32 } | |
33 } | |
34 | |
35 debug Stdout(" * ")(moduleFQN)(" does not exist in imports")().newline()(); | |
36 return null; | |
37 } | |
38 | |
466
db7e27b5c180
Fixed parts of HTML output, some reorganizing.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
462
diff
changeset
|
39 public: |
db7e27b5c180
Fixed parts of HTML output, some reorganizing.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
462
diff
changeset
|
40 |
441 | 41 /** |
42 * Imports the transitive closure of imports starting from "filePath", | |
43 * limited by recursionDepth. | |
44 * | |
45 * The search can be filtered by providing a list of regexps that match the | |
46 * FQNs of modules to be ignored. | |
47 * | |
48 * Params: | |
49 * filePath = Path of the file to parse | |
50 * importPaths = Directories to look for imports | |
51 * strRegexps = Filter regexps | |
52 * IncludeUnlocatableModules = Call the delegate also for unlocatable files | |
53 * recursionDepth = How many levels of imports to follow (-1 = no limit) | |
54 * mdg = Delegate that gets called for every module found | |
55 * idg = Delegate that gets called for every import found | |
56 * modules = List of parsed modules | |
57 */ | |
466
db7e27b5c180
Fixed parts of HTML output, some reorganizing.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
462
diff
changeset
|
58 static void loadModules(char[] filePath, char[][] importPaths, char[][] strRegexps, |
441 | 59 bool IncludeUnlocatableModules, int recursionDepth, |
462
b7503e02fbe7
Docgen code for handling public imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
454
diff
changeset
|
60 modDg mdg, importDg idg, out Module[] modules) { |
441 | 61 |
62 loadModules([filePath], importPaths, strRegexps, IncludeUnlocatableModules, | |
63 recursionDepth, mdg, idg, modules); | |
64 } | |
65 | |
66 /** | |
67 * Imports the transitive closure of imports starting from "filePath", | |
68 * limited by recursionDepth. | |
69 * | |
70 * The search can be filtered by providing a list of regexps that match the | |
71 * FQNs of modules to be ignored. | |
72 * | |
73 * Params: | |
74 * filePaths = Paths of the files to parse | |
75 * importPaths = Directories to look for imports | |
76 * strRegexps = Filter regexps | |
77 * IncludeUnlocatableModules = Call the delegate also for unlocatable files | |
78 * recursionDepth = How many levels of imports to follow (-1 = no limit) | |
79 * mdg = Delegate that gets called for every module found | |
80 * idg = Delegate that gets called for every import found | |
81 * modules = List of parsed modules | |
82 */ | |
466
db7e27b5c180
Fixed parts of HTML output, some reorganizing.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
462
diff
changeset
|
83 static void loadModules(char[][] filePaths, char[][] importPaths, char[][] strRegexps, |
441 | 84 bool IncludeUnlocatableModules, int recursionDepth, |
462
b7503e02fbe7
Docgen code for handling public imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
454
diff
changeset
|
85 modDg mdg, importDg idg, out Module[] modules) { |
441 | 86 // Initialize regular expressions. |
87 RegExp[] regexps; | |
88 foreach (strRegexp; strRegexps) | |
89 regexps ~= new RegExp(strRegexp); | |
90 | |
91 // Add directory of file and global directories to import paths. | |
92 foreach(filePath; filePaths) { | |
93 auto fileDir = (new FilePath(filePath)).folder(); | |
94 if (fileDir.length) | |
95 importPaths ~= fileDir; | |
96 } | |
97 | |
98 importPaths ~= GlobalSettings.importPaths; | |
99 | |
100 debug foreach(path; importPaths) { | |
101 Stdout("Import path: ")(path).newline; | |
102 } | |
103 | |
453
4e5b35df3060
Parsing bugfixes, cleaned up imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
451
diff
changeset
|
104 Module[char[]] loadedModules; |
441 | 105 |
453
4e5b35df3060
Parsing bugfixes, cleaned up imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
451
diff
changeset
|
106 Module loadModule(char[] moduleFQNPath, int depth) { |
441 | 107 if (depth == 0) return null; |
108 | |
109 debug Stdout("Loading ")(moduleFQNPath).newline; | |
110 | |
111 // Return already loaded module. | |
112 auto mod_ = moduleFQNPath in loadedModules; | |
113 if (mod_ !is null) { | |
114 debug Stdout(" Already loaded.")(moduleFQNPath).newline; | |
115 return *mod_; | |
116 } | |
117 | |
118 auto FQN = replace(moduleFQNPath.dup, dirSep, '.'); | |
119 | |
120 // Ignore module names matching regular expressions. | |
121 foreach (rx; regexps) | |
122 if (rx.test(FQN)) return null; | |
123 | |
124 auto modulePath = findModulePath(moduleFQNPath, importPaths); | |
125 //foreach(filePath; filePaths) | |
126 //if (moduleFQNPath == filePath) modulePath = filePath; | |
127 | |
128 debug Stdout(" FQN ")(FQN).newline; | |
129 debug Stdout(" Module path ")(modulePath).newline; | |
130 | |
131 Module mod = null; | |
132 | |
133 if (modulePath is null) { | |
134 if (IncludeUnlocatableModules) | |
135 mdg(FQN, moduleFQNPath, null); | |
136 } else { | |
137 mod = new Module(modulePath); | |
138 loadedModules[moduleFQNPath] = mod; | |
139 mod.parse(); | |
140 | |
141 mdg(FQN, moduleFQNPath, mod); | |
142 | |
462
b7503e02fbe7
Docgen code for handling public imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
454
diff
changeset
|
143 auto imports = mod.imports; |
441 | 144 |
451
3f44c38bf870
Page templates, more flexible writer interfaces, small fixes.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
441
diff
changeset
|
145 // TODO: add public/private attribute to the dg parameters |
462
b7503e02fbe7
Docgen code for handling public imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
454
diff
changeset
|
146 foreach (importList; imports) |
b7503e02fbe7
Docgen code for handling public imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
454
diff
changeset
|
147 foreach(moduleFQN_; importList.getModuleFQNs(dirSep)) { |
b7503e02fbe7
Docgen code for handling public imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
454
diff
changeset
|
148 auto loaded_mod = loadModule(moduleFQN_, depth == -1 ? depth : depth-1); |
441 | 149 |
462
b7503e02fbe7
Docgen code for handling public imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
454
diff
changeset
|
150 if (loaded_mod !is null) { |
b7503e02fbe7
Docgen code for handling public imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
454
diff
changeset
|
151 idg(loaded_mod, mod, importList.isPublic()); |
b7503e02fbe7
Docgen code for handling public imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
454
diff
changeset
|
152 } else if (IncludeUnlocatableModules) { |
b7503e02fbe7
Docgen code for handling public imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
454
diff
changeset
|
153 auto tmp = new Module(null, true); |
b7503e02fbe7
Docgen code for handling public imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
454
diff
changeset
|
154 tmp.moduleFQN = replace(moduleFQN_.dup, dirSep, '.'); |
b7503e02fbe7
Docgen code for handling public imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
454
diff
changeset
|
155 idg(tmp, mod, importList.isPublic()); |
b7503e02fbe7
Docgen code for handling public imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
454
diff
changeset
|
156 } |
453
4e5b35df3060
Parsing bugfixes, cleaned up imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
451
diff
changeset
|
157 } |
441 | 158 } |
159 | |
160 return mod; | |
161 } // loadModule | |
162 | |
163 foreach(filePath; filePaths) | |
164 loadModule(filePath, recursionDepth); | |
165 | |
166 // Finished loading modules. | |
167 | |
168 // Ordered list of loaded modules. | |
169 modules = loadedModules.values; | |
170 } | |
453
4e5b35df3060
Parsing bugfixes, cleaned up imports.
Jari-Matti M?kel? <jmjm@iki.fi>
parents:
451
diff
changeset
|
171 } |