comparison trunk/src/docgen/misc/parser.d @ 736:2eee29aaa357

Fixed couple of regressions. Autoincludes for convenience.
author Jari-Matti M?kel? <jmjm@iki.fi>
date Mon, 04 Feb 2008 21:55:44 +0200
parents 231c9a44ba8e
children
comparison
equal deleted inserted replaced
735:30e6f1b302a1 736:2eee29aaa357
9 import dil.File; 9 import dil.File;
10 import dil.Settings; 10 import dil.Settings;
11 public import dil.semantic.Module; 11 public import dil.semantic.Module;
12 import tango.text.Regex : RegExp = Regex; 12 import tango.text.Regex : RegExp = Regex;
13 import tango.io.FilePath; 13 import tango.io.FilePath;
14 import tango.io.FileSystem;
15 import tango.core.Array : remove;
14 import tango.text.Util; 16 import tango.text.Util;
17 import docgen.misc.meta;
15 debug import tango.io.Stdout; 18 debug import tango.io.Stdout;
16 19
17 alias void delegate (char[] fqn, char[] path, Module module_) modDg; 20 alias void delegate (char[] fqn, char[] path, Module module_) modDg;
18 alias void delegate (Module imported, Module importer, bool isPublic, bool isStatic) importDg; 21 alias void delegate (Module imported, Module importer, bool isPublic, bool isStatic) importDg;
19 22
20 class Parser { 23 class Parser {
21 private: 24 protected:
25
26 // ParserOptions m_options;
27
22 28
23 static char[] findModuleFilePath(char[] moduleFQNPath, char[][] importPaths) { 29 static char[] findModuleFilePath(char[] moduleFQNPath, char[][] importPaths) {
24 auto filePath = new FilePath(); 30 auto filePath = new FilePath();
25 foreach (importPath; importPaths) { 31 foreach (importPath; importPaths) {
26 filePath.set(importPath); 32 filePath.set(importPath);
27 filePath.append(moduleFQNPath); 33 filePath.append(moduleFQNPath);
28 34
29 foreach (moduleSuffix; [".d", ".di"/*interface file*/]) 35 foreach (moduleSuffix; [".d", ".di"/*interface file*/])
30 { 36 {
31 filePath.suffix(moduleSuffix); 37 filePath.suffix(moduleSuffix);
38 debug Stdout("Trying ")(filePath.toString()).newline;
32 if (filePath.exists()) 39 if (filePath.exists())
33 return filePath.toString(); 40 return filePath.toString();
34 } 41 }
35 } 42 }
36 43
89 // Initialize regular expressions. 96 // Initialize regular expressions.
90 RegExp[] regexps; 97 RegExp[] regexps;
91 foreach (strRegexp; strRegexps) 98 foreach (strRegexp; strRegexps)
92 regexps ~= new RegExp(strRegexp); 99 regexps ~= new RegExp(strRegexp);
93 100
101 importPaths ~= ".";
102
94 // Add directory of file and global directories to import paths. 103 // Add directory of file and global directories to import paths.
95 foreach(filePath; filePaths) { 104 foreach(filePath; filePaths) {
96 auto fileDir = (new FilePath(filePath)).folder(); 105 auto fileDir = (new FilePath(filePath)).folder();
97 if (fileDir.length) 106 if (fileDir.length)
98 importPaths ~= fileDir; 107 importPaths ~= fileDir;
99 } 108 }
100 109
101 importPaths ~= GlobalSettings.importPaths; 110 // importPaths ~= GlobalSettings.importPaths;
102 111
103 debug foreach(path; importPaths) { 112 debug foreach(path; importPaths) {
104 Stdout("Import path: ")(path).newline; 113 Stdout("Import path: ")(path).newline;
105 } 114 }
106 115
123 // Ignore module names matching regular expressions. 132 // Ignore module names matching regular expressions.
124 foreach (rx; regexps) 133 foreach (rx; regexps)
125 if (rx.test(FQN)) return null; 134 if (rx.test(FQN)) return null;
126 135
127 auto moduleFilePath = findModuleFilePath(moduleFQNPath, importPaths); 136 auto moduleFilePath = findModuleFilePath(moduleFQNPath, importPaths);
128 //foreach(filePath; filePaths)
129 //if (moduleFQNPath == filePath) modulePath = filePath;
130 137
131 debug Stdout(" FQN ")(FQN).newline; 138 debug Stdout(" FQN ")(FQN).newline;
132 debug Stdout(" Module path ")(moduleFilePath).newline; 139 debug Stdout(" Module path ")(moduleFilePath).newline;
133 140
134 Module mod = null; 141 Module mod = null;
138 mdg(FQN, moduleFQNPath, null); 145 mdg(FQN, moduleFQNPath, null);
139 } else { 146 } else {
140 mod = new Module(moduleFilePath); 147 mod = new Module(moduleFilePath);
141 148
142 // Use lightweight ImportParser. 149 // Use lightweight ImportParser.
143 mod.parser = new ImportParser(loadFile(moduleFilePath), moduleFilePath); 150 // mod.parser = new ImportParser(loadFile(moduleFilePath), moduleFilePath);
144 mod.parse(); 151 mod.parse();
152
153 debug Stdout(" Parsed FQN ")(mod.getFQN()).newline;
154
155 // autoinclude dirs (similar to Java)
156 // running docgen in foo/bar/abc/ also finds foo/xyz/zzz.d if module names are right
157 {
158 // foo.bar.mod -> [ "foo", "bar" ]
159 auto modPackage = split(mod.getFQN, ".")[0..$-1];
160 auto modDir = split(FileSystem.toAbsolute(new FilePath(moduleFilePath)).standard().folder(), "/");
161 auto modLocation = modDir[0..modDir.remove(".")];
162
163 bool matches = false;
164 int i;
165
166 for(i = 1; i <= min(modPackage.length, modLocation.length); i++) {
167 matches = true;
168 debug Stdout(" Decl: ")(modPackage[$-i]).newline;
169 debug Stdout(" Path: ")(modLocation[$-i]).newline;
170 if (modPackage[$-i] != modLocation[$-i]) {
171 matches = false;
172 break;
173 }
174 }
175 if (matches) {
176 auto loc = modLocation[0..$-i+1].join("/");
177 debug Stdout(" Autoadding import: ")(loc).newline;
178 importPaths ~= loc;
179 }
180 }
145 181
146 mdg(FQN, moduleFQNPath, mod); 182 mdg(FQN, moduleFQNPath, mod);
147 loadedModules[moduleFQNPath] = mod; 183 loadedModules[moduleFQNPath] = mod;
148 184
149 foreach (importDecl; mod.imports) 185 foreach (importDecl; mod.imports)
150 foreach(moduleFQN_; importDecl.getModuleFQNs(dirSep)) { 186 foreach(moduleFQN_; importDecl.getModuleFQNs(dirSep)) {
151 auto loaded_mod = loadModule(moduleFQN_, depth == -1 ? depth : depth-1); 187 auto loaded_mod = loadModule(moduleFQN_, depth == -1 ? depth : depth-1);
152 188
153 if (loaded_mod !is null) { 189 if (loaded_mod !is null) {
154 idg(loaded_mod, mod, importDecl.isPublic(), importDecl.isStatic()); 190 idg(loaded_mod, mod, importDecl.isPublic(), importDecl.isStatic());
155 } else if (IncludeUnlocatableModules) {/* FIXME 191 } else if (IncludeUnlocatableModules) {
156 auto tmp = new Module(null, true); 192 auto tmp = new Module(null);
157 tmp.moduleFQN = replace(moduleFQN_.dup, dirSep, '.'); 193 tmp.setFQN(replace(moduleFQN_.dup, dirSep, '.'));
158 idg(tmp, mod, importList.isPublic());*/ 194 idg(tmp, mod, importDecl.isPublic(), importDecl.isStatic());
159 } 195 }
160 } 196 }
161 } 197 }
162 198
163 return mod; 199 return mod;