Mercurial > projects > dil
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 |
rev | line source |
---|---|
364 | 1 /++ |
2 Author: Aziz Köksal | |
3 License: GPL3 | |
4 +/ | |
5 module dil.Module; | |
6 import dil.SyntaxTree; | |
7 import dil.Declarations; | |
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 | 10 import dil.Lexer; |
364 | 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 | 18 |
19 class Module | |
20 { | |
368 | 21 bool isLightweight; /// If true an ImportParser is used instead of a full Parser. |
370 | 22 string filePath; /// Path to the source file. |
23 string moduleFQN; /// Fully qualified name of the module. | |
364 | 24 string packageName; |
25 string moduleName; | |
26 Declarations root; /// The root of the AST. | |
27 ImportDeclaration[] imports; | |
28 ModuleDeclaration moduleDecl; | |
365 | 29 private Parser parser; |
364 | 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 | 34 { |
370 | 35 this.filePath = filePath; |
368 | 36 this.isLightweight = isLightweight; |
364 | 37 } |
38 | |
39 void parse() | |
40 { | |
370 | 41 auto sourceText = loadFile(filePath); |
368 | 42 if (this.isLightweight) |
370 | 43 this.parser = new ImportParser(sourceText, filePath); |
368 | 44 else |
370 | 45 this.parser = new Parser(sourceText, filePath); |
364 | 46 |
368 | 47 this.root = parser.start(); |
364 | 48 |
49 if (root.children.length) | |
50 { | |
370 | 51 // moduleDecl will be null if first node can't be cast to ModuleDeclaration. |
364 | 52 this.moduleDecl = Cast!(ModuleDeclaration)(root.children[0]); |
365 | 53 if (moduleDecl) |
54 { | |
370 | 55 this.setFQN(moduleDecl.getFQN()); |
365 | 56 } |
57 else | |
58 { | |
370 | 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 | 61 if (Lexer.isNonReservedIdentifier(str)) |
370 | 62 { |
63 this.moduleFQN = moduleName = str; | |
64 } | |
65 // else | |
66 // TODO: error: file name has invalid identifier characters. | |
365 | 67 } |
364 | 68 |
69 this.imports = parser.imports; | |
70 } | |
71 } | |
366 | 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 | 79 string[] getImports() |
80 { | |
81 string[] result; | |
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 | 84 return result; |
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 | 89 return moduleFQN; |
90 } | |
91 | |
92 void setFQN(string moduleFQN) | |
93 { | |
94 uint i = moduleFQN.length; | |
95 if (i != 0) // Don't decrement if string has zero length. | |
96 i--; | |
97 // Find last dot. | |
98 for (; i != 0 && moduleFQN[i] != '.'; i--) | |
99 {} | |
100 this.moduleFQN = moduleFQN; | |
101 this.packageName = moduleFQN[0..i]; | |
102 this.moduleName = moduleFQN[(i == 0 ? 0 : i+1) .. $]; | |
103 } | |
104 | |
105 string getFQNPath() | |
106 { | |
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 | 109 else |
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 | 112 } |