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