annotate trunk/src/dil/Module.d @ 492:9c208925a3d4

Added module ImportParser and new stuff from DMD2.008. Moved ImportParser from module Parser to its own module. Added a few methods from class Parser and simplified them. Now protection attributes are taken into consideration as well. Added class TemplateThisParameter. Adapted TypeofType so that typeof(return) can be recognized.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Thu, 06 Dec 2007 22:19:55 +0100
parents 33b566df6af4
children fa63ef408790
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
1 /++
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
2 Author: Aziz Köksal
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
3 License: GPL3
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
4 +/
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
5 module dil.Module;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
6 import dil.SyntaxTree;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
7 import dil.Declarations;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
8 import dil.Parser;
492
9c208925a3d4 Added module ImportParser and new stuff from DMD2.008.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 391
diff changeset
9 import dil.ImportParser;
365
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
10 import dil.Lexer;
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
11 import dil.File;
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
12 import tango.io.FilePath;
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
13 import tango.io.FileConst;
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
14 import common;
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
15
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
16 alias FileConst.PathSeparatorChar dirSep;
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
17
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
18 class Module
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
19 {
368
2adf808343d6 - Renamed method start() to init() in Parser.
aziz
parents: 367
diff changeset
20 bool isLightweight; /// If true an ImportParser is used instead of a full Parser.
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
21 string filePath; /// Path to the source file.
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
22 string moduleFQN; /// Fully qualified name of the module.
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
23 string packageName;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
24 string moduleName;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
25 Declarations root; /// The root of the AST.
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
26 ImportDeclaration[] imports;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
27 ModuleDeclaration moduleDecl;
365
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
28 private Parser parser;
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
29
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
30 Module[] modules;
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
31
492
9c208925a3d4 Added module ImportParser and new stuff from DMD2.008.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 391
diff changeset
32 this(string filePath, bool isLightweight = false)
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
33 {
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
34 this.filePath = filePath;
368
2adf808343d6 - Renamed method start() to init() in Parser.
aziz
parents: 367
diff changeset
35 this.isLightweight = isLightweight;
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
36 }
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
37
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
38 void parse()
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
39 {
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
40 auto sourceText = loadFile(filePath);
368
2adf808343d6 - Renamed method start() to init() in Parser.
aziz
parents: 367
diff changeset
41 if (this.isLightweight)
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
42 this.parser = new ImportParser(sourceText, filePath);
368
2adf808343d6 - Renamed method start() to init() in Parser.
aziz
parents: 367
diff changeset
43 else
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
44 this.parser = new Parser(sourceText, filePath);
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
45
368
2adf808343d6 - Renamed method start() to init() in Parser.
aziz
parents: 367
diff changeset
46 this.root = parser.start();
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
47
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
48 if (root.children.length)
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
49 {
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
50 // moduleDecl will be null if first node can't be cast to ModuleDeclaration.
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
51 this.moduleDecl = Cast!(ModuleDeclaration)(root.children[0]);
365
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
52 if (moduleDecl)
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
53 {
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
54 this.setFQN(moduleDecl.getFQN());
365
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
55 }
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
56 else
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
57 {
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
58 // Take base name of file path as module name.
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
59 auto str = (new FilePath(filePath)).name();
365
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
60 if (Lexer.isNonReservedIdentifier(str))
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
61 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
62 this.moduleFQN = moduleName = str;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
63 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
64 // else
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
65 // TODO: error: file name has invalid identifier characters.
365
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
66 }
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
67
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
68 this.imports = parser.imports;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
69 }
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
70 }
366
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 365
diff changeset
71
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 365
diff changeset
72 string[] getImports()
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 365
diff changeset
73 {
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 365
diff changeset
74 string[] result;
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 365
diff changeset
75 foreach (import_; imports)
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
76 result ~= import_.getModuleFQNs(dirSep);
366
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 365
diff changeset
77 return result;
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 365
diff changeset
78 }
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
79
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
80 string getFQN()
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
81 {
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
82 return moduleFQN;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
83 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
84
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
85 void setFQN(string moduleFQN)
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
86 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
87 uint i = moduleFQN.length;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
88 if (i != 0) // Don't decrement if string has zero length.
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
89 i--;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
90 // Find last dot.
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
91 for (; i != 0 && moduleFQN[i] != '.'; i--)
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
92 {}
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
93 this.moduleFQN = moduleFQN;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
94 this.packageName = moduleFQN[0..i];
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
95 this.moduleName = moduleFQN[(i == 0 ? 0 : i+1) .. $];
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
96 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
97
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
98 string getFQNPath()
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
99 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
100 if (packageName.length)
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
101 return packageName ~ dirSep ~ moduleName;
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
102 else
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
103 return moduleName;
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
104 }
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
105 }