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