annotate trunk/src/dil/Module.d @ 503:fa63ef408790

Added module dil.Package and semantic() to class Module.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Tue, 11 Dec 2007 15:05:08 +0100
parents 9c208925a3d4
children dd3ce87b3569
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;
503
fa63ef408790 Added module dil.Package and semantic() to class Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 492
diff changeset
12 import dil.Scope;
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
13 import tango.io.FilePath;
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
14 import tango.io.FileConst;
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
15 import common;
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
16
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
17 alias FileConst.PathSeparatorChar dirSep;
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
18
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
19 class Module
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
20 {
368
2adf808343d6 - Renamed method start() to init() in Parser.
aziz
parents: 367
diff changeset
21 bool isLightweight; /// If true an ImportParser is used instead of a full Parser.
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
22 string filePath; /// Path to the source file.
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
23 string moduleFQN; /// Fully qualified name of the module.
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
24 string packageName;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
25 string moduleName;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
26 Declarations root; /// The root of the AST.
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
27 ImportDeclaration[] imports;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
28 ModuleDeclaration moduleDecl;
365
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
29 private Parser parser;
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
30
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
31 Module[] modules;
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
32
492
9c208925a3d4 Added module ImportParser and new stuff from DMD2.008.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 391
diff changeset
33 this(string filePath, bool isLightweight = false)
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
34 {
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
35 this.filePath = filePath;
368
2adf808343d6 - Renamed method start() to init() in Parser.
aziz
parents: 367
diff changeset
36 this.isLightweight = isLightweight;
364
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
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
39 void parse()
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
40 {
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
41 auto sourceText = loadFile(filePath);
368
2adf808343d6 - Renamed method start() to init() in Parser.
aziz
parents: 367
diff changeset
42 if (this.isLightweight)
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
43 this.parser = new ImportParser(sourceText, filePath);
368
2adf808343d6 - Renamed method start() to init() in Parser.
aziz
parents: 367
diff changeset
44 else
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
45 this.parser = new Parser(sourceText, filePath);
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
46
368
2adf808343d6 - Renamed method start() to init() in Parser.
aziz
parents: 367
diff changeset
47 this.root = parser.start();
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
48
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
49 if (root.children.length)
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
50 {
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
51 // 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
52 this.moduleDecl = Cast!(ModuleDeclaration)(root.children[0]);
365
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
53 if (moduleDecl)
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
54 {
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
55 this.setFQN(moduleDecl.getFQN());
365
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
56 }
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
57 else
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
58 {
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
59 // 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
60 auto str = (new FilePath(filePath)).name();
365
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
61 if (Lexer.isNonReservedIdentifier(str))
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
62 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
63 this.moduleFQN = moduleName = str;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
64 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
65 // else
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
66 // TODO: error: file name has invalid identifier characters.
365
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
67 }
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
68
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
69 this.imports = parser.imports;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
70 }
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
71 }
366
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 365
diff changeset
72
503
fa63ef408790 Added module dil.Package and semantic() to class Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 492
diff changeset
73 void semantic()
fa63ef408790 Added module dil.Package and semantic() to class Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 492
diff changeset
74 {
fa63ef408790 Added module dil.Package and semantic() to class Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 492
diff changeset
75 auto scop = new Scope();
fa63ef408790 Added module dil.Package and semantic() to class Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 492
diff changeset
76 this.root.semantic(scop);
fa63ef408790 Added module dil.Package and semantic() to class Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 492
diff changeset
77 }
fa63ef408790 Added module dil.Package and semantic() to class Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 492
diff changeset
78
366
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 365
diff changeset
79 string[] getImports()
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 365
diff changeset
80 {
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 365
diff changeset
81 string[] result;
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 365
diff changeset
82 foreach (import_; imports)
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
83 result ~= import_.getModuleFQNs(dirSep);
366
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 365
diff changeset
84 return result;
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 365
diff changeset
85 }
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
86
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
87 string getFQN()
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
88 {
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
89 return moduleFQN;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
90 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
91
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
92 void setFQN(string moduleFQN)
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
93 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
94 uint i = moduleFQN.length;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
95 if (i != 0) // Don't decrement if string has zero length.
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
96 i--;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
97 // Find last dot.
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
98 for (; i != 0 && moduleFQN[i] != '.'; i--)
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
99 {}
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
100 this.moduleFQN = moduleFQN;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
101 this.packageName = moduleFQN[0..i];
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
102 this.moduleName = moduleFQN[(i == 0 ? 0 : i+1) .. $];
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
103 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
104
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
105 string getFQNPath()
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
106 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
107 if (packageName.length)
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
108 return packageName ~ dirSep ~ moduleName;
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
109 else
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
110 return moduleName;
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
111 }
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
112 }