comparison 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
comparison
equal deleted inserted replaced
730:5cb236c6fe52 732:231c9a44ba8e
3 * License: GPL3 3 * License: GPL3
4 */ 4 */
5 module docgen.misc.parser; 5 module docgen.misc.parser;
6 6
7 import dil.parser.Parser; 7 import dil.parser.Parser;
8 import dil.parser.ImportParser;
9 import dil.File;
8 import dil.Settings; 10 import dil.Settings;
9 public import dil.semantic.Module; 11 public import dil.semantic.Module;
10 import tango.text.Regex : RegExp = Regex; 12 import tango.text.Regex : RegExp = Regex;
11 import tango.io.FilePath; 13 import tango.io.FilePath;
12 import tango.text.Util; 14 import tango.text.Util;
13 debug import tango.io.Stdout; 15 debug import tango.io.Stdout;
14 16
15 alias void delegate (char[] fqn, char[] path, Module module_) modDg; 17 alias void delegate (char[] fqn, char[] path, Module module_) modDg;
16 alias void delegate (Module imported, Module importer, bool isPublic) importDg; 18 alias void delegate (Module imported, Module importer, bool isPublic, bool isStatic) importDg;
17 19
18 class Parser { 20 class Parser {
19 private: 21 private:
20 22
21 static char[] findModulePath(char[] moduleFQN, char[][] importPaths) { 23 static char[] findModuleFilePath(char[] moduleFQNPath, char[][] importPaths) {
22 char[] modulePath; 24 auto filePath = new FilePath();
25 foreach (importPath; importPaths) {
26 filePath.set(importPath);
27 filePath.append(moduleFQNPath);
23 28
24 foreach (path; importPaths) { 29 foreach (moduleSuffix; [".d", ".di"/*interface file*/])
25 modulePath = path ~ (path[$-1] == dirSep ? "" : [dirSep]) ~ moduleFQN ~ ".d"; 30 {
26 31 filePath.suffix(moduleSuffix);
27 // TODO: also check for *.di? 32 if (filePath.exists())
28 33 return filePath.toString();
29 if ((new FilePath(modulePath)).exists()) {
30 debug Stdout(" * File for ")(moduleFQN)(" found: ")(modulePath).newline;
31 return modulePath;
32 } 34 }
33 } 35 }
34 36
35 debug Stdout(" * ")(moduleFQN)(" does not exist in imports")().newline()(); 37 debug Stdout(" * ")(moduleFQNPath)(" does not exist in imports\n")();
36 return null; 38 return null;
37 } 39 }
38 40
39 public: 41 public:
40 42
54 * mdg = Delegate that gets called for every module found 56 * mdg = Delegate that gets called for every module found
55 * idg = Delegate that gets called for every import found 57 * idg = Delegate that gets called for every import found
56 * modules = List of parsed modules 58 * modules = List of parsed modules
57 */ 59 */
58 static void loadModules(char[] filePath, char[][] importPaths, char[][] strRegexps, 60 static void loadModules(char[] filePath, char[][] importPaths, char[][] strRegexps,
59 bool IncludeUnlocatableModules, int recursionDepth, 61 bool IncludeUnlocatableModules, int recursionDepth,
60 modDg mdg, importDg idg, out Module[] modules) { 62 modDg mdg, importDg idg, out Module[] modules) {
61 63
62 loadModules([filePath], importPaths, strRegexps, IncludeUnlocatableModules, 64 loadModules([filePath], importPaths, strRegexps, IncludeUnlocatableModules,
63 recursionDepth, mdg, idg, modules); 65 recursionDepth, mdg, idg, modules);
64 } 66 }
65 67
79 * mdg = Delegate that gets called for every module found 81 * mdg = Delegate that gets called for every module found
80 * idg = Delegate that gets called for every import found 82 * idg = Delegate that gets called for every import found
81 * modules = List of parsed modules 83 * modules = List of parsed modules
82 */ 84 */
83 static void loadModules(char[][] filePaths, char[][] importPaths, char[][] strRegexps, 85 static void loadModules(char[][] filePaths, char[][] importPaths, char[][] strRegexps,
84 bool IncludeUnlocatableModules, int recursionDepth, 86 bool IncludeUnlocatableModules, int recursionDepth,
85 modDg mdg, importDg idg, out Module[] modules) { 87 modDg mdg, importDg idg, out Module[] modules) {
88
86 // Initialize regular expressions. 89 // Initialize regular expressions.
87 RegExp[] regexps; 90 RegExp[] regexps;
88 foreach (strRegexp; strRegexps) 91 foreach (strRegexp; strRegexps)
89 regexps ~= new RegExp(strRegexp); 92 regexps ~= new RegExp(strRegexp);
90 93
119 122
120 // Ignore module names matching regular expressions. 123 // Ignore module names matching regular expressions.
121 foreach (rx; regexps) 124 foreach (rx; regexps)
122 if (rx.test(FQN)) return null; 125 if (rx.test(FQN)) return null;
123 126
124 auto modulePath = findModulePath(moduleFQNPath, importPaths); 127 auto moduleFilePath = findModuleFilePath(moduleFQNPath, importPaths);
125 //foreach(filePath; filePaths) 128 //foreach(filePath; filePaths)
126 //if (moduleFQNPath == filePath) modulePath = filePath; 129 //if (moduleFQNPath == filePath) modulePath = filePath;
127 130
128 debug Stdout(" FQN ")(FQN).newline; 131 debug Stdout(" FQN ")(FQN).newline;
129 debug Stdout(" Module path ")(modulePath).newline; 132 debug Stdout(" Module path ")(moduleFilePath).newline;
130 133
131 Module mod = null; 134 Module mod = null;
132 135
133 if (modulePath is null) { 136 if (moduleFilePath is null) {
134 if (IncludeUnlocatableModules) 137 if (IncludeUnlocatableModules)
135 mdg(FQN, moduleFQNPath, null); 138 mdg(FQN, moduleFQNPath, null);
136 } else { 139 } else {
137 mod = new Module(modulePath); 140 mod = new Module(moduleFilePath);
138 loadedModules[moduleFQNPath] = mod; 141
142 // Use lightweight ImportParser.
143 mod.parser = new ImportParser(loadFile(moduleFilePath), moduleFilePath);
139 mod.parse(); 144 mod.parse();
140 145
141 mdg(FQN, moduleFQNPath, mod); 146 mdg(FQN, moduleFQNPath, mod);
147 loadedModules[moduleFQNPath] = mod;
142 148
143 auto imports = mod.imports; 149 foreach (importDecl; mod.imports)
144 150 foreach(moduleFQN_; importDecl.getModuleFQNs(dirSep)) {
145 foreach (importList; imports)
146 foreach(moduleFQN_; importList.getModuleFQNs(dirSep)) {
147 auto loaded_mod = loadModule(moduleFQN_, depth == -1 ? depth : depth-1); 151 auto loaded_mod = loadModule(moduleFQN_, depth == -1 ? depth : depth-1);
148 152
149 if (loaded_mod !is null) { 153 if (loaded_mod !is null) {
150 idg(loaded_mod, mod, importList.isPublic()); 154 idg(loaded_mod, mod, importDecl.isPublic(), importDecl.isStatic());
151 } else if (IncludeUnlocatableModules) {/* FIXME 155 } else if (IncludeUnlocatableModules) {/* FIXME
152 auto tmp = new Module(null, true); 156 auto tmp = new Module(null, true);
153 tmp.moduleFQN = replace(moduleFQN_.dup, dirSep, '.'); 157 tmp.moduleFQN = replace(moduleFQN_.dup, dirSep, '.');
154 idg(tmp, mod, importList.isPublic());*/ 158 idg(tmp, mod, importList.isPublic());*/
155 } 159 }