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