Mercurial > projects > dil
annotate trunk/src/dil/semantic/Module.d @ 698:1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Sat, 26 Jan 2008 20:08:03 +0100 |
parents | 1ae72234db26 |
children | 8f8c9ab3f3ba |
rev | line source |
---|---|
364 | 1 /++ |
2 Author: Aziz Köksal | |
3 License: GPL3 | |
4 +/ | |
593
2848ce3becf5
Moved dil.Module to dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
592
diff
changeset
|
5 module dil.semantic.Module; |
520
f203c5248d0b
Added 'compile' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
516
diff
changeset
|
6 |
580
fa6d3c52757d
Moved SyntaxTree.d to new package 'ast'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
579
diff
changeset
|
7 import dil.ast.Node; |
585
05c375fb2d5c
Moved dil.Declarations to dil.ast.Declarations.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
8 import dil.ast.Declarations; |
578
c769bc239006
Moved Parser.d to new package 'parser'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
576
diff
changeset
|
9 import dil.parser.Parser; |
576
0df647660e76
Moved Lexer.d to new package 'lexer'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
570
diff
changeset
|
10 import dil.lexer.Lexer; |
364 | 11 import dil.File; |
589
de365ddcfbd4
Moved dil.Symbol to dil.semantic.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
585
diff
changeset
|
12 import dil.semantic.Symbol; |
590
641041912670
Moved dil.Symbols to dil.semantic.Symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
589
diff
changeset
|
13 import dil.semantic.Symbols; |
520
f203c5248d0b
Added 'compile' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
516
diff
changeset
|
14 import dil.Information; |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
370
diff
changeset
|
15 import tango.io.FilePath; |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
370
diff
changeset
|
16 import tango.io.FileConst; |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
370
diff
changeset
|
17 import common; |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
370
diff
changeset
|
18 |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
370
diff
changeset
|
19 alias FileConst.PathSeparatorChar dirSep; |
364 | 20 |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
21 class Module : ScopeSymbol |
364 | 22 { |
370 | 23 string filePath; /// Path to the source file. |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
24 string moduleFQN; /// Fully qualified name of the module. E.g. dil.ast.Node |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
25 string packageName; /// E.g. dil.ast |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
26 string moduleName; /// E.g. Node |
520
f203c5248d0b
Added 'compile' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
516
diff
changeset
|
27 |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
28 CompoundDeclaration root; /// The root of the parse tree. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
29 ImportDeclaration[] imports; /// ImportDeclarations found in this file. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
30 ModuleDeclaration moduleDecl; /// The optional ModuleDeclaration in this file. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
31 Parser parser; /// The parser used to parse this file. |
364 | 32 |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
366
diff
changeset
|
33 Module[] modules; |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
366
diff
changeset
|
34 |
532
50e64bab9c7a
Renamed InformationManager to InfoManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
520
diff
changeset
|
35 InfoManager infoMan; |
520
f203c5248d0b
Added 'compile' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
516
diff
changeset
|
36 |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
37 this() |
364 | 38 { |
683
1ae72234db26
Implemented some methods in SemanticPass1.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
680
diff
changeset
|
39 super(SYM.Module, null, null); |
364 | 40 } |
41 | |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
42 this(string filePath, InfoManager infoMan = null) |
520
f203c5248d0b
Added 'compile' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
516
diff
changeset
|
43 { |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
44 this(); |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
45 this.filePath = filePath; |
520
f203c5248d0b
Added 'compile' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
516
diff
changeset
|
46 this.infoMan = infoMan; |
f203c5248d0b
Added 'compile' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
516
diff
changeset
|
47 } |
f203c5248d0b
Added 'compile' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
516
diff
changeset
|
48 |
364 | 49 void parse() |
50 { | |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
51 if (this.parser is null) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
52 this.parser = new Parser(loadFile(filePath), filePath, infoMan); |
364 | 53 |
368 | 54 this.root = parser.start(); |
364 | 55 |
56 if (root.children.length) | |
57 { | |
673
64fec49651cf
Renamed VariableDeclaration to VariablesDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
649
diff
changeset
|
58 // moduleDecl will be null if first node isn't a ModuleDeclaration. |
64fec49651cf
Renamed VariableDeclaration to VariablesDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
649
diff
changeset
|
59 this.moduleDecl = root.children[0].Is!(ModuleDeclaration); |
365 | 60 if (moduleDecl) |
61 { | |
370 | 62 this.setFQN(moduleDecl.getFQN()); |
365 | 63 } |
64 else | |
65 { | |
370 | 66 // 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
|
67 auto str = (new FilePath(filePath)).name(); |
510
dd3ce87b3569
Added module dil.Unicode.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
503
diff
changeset
|
68 if (!Lexer.isReservedIdentifier(str)) |
370 | 69 { |
70 this.moduleFQN = moduleName = str; | |
71 } | |
72 // else | |
73 // TODO: error: file name has invalid identifier characters. | |
365 | 74 } |
364 | 75 |
76 this.imports = parser.imports; | |
77 } | |
78 } | |
366 | 79 |
563
c838ed7f2ac9
Added 'override' to some methods.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
80 /// Returns true if there are errors in the source file. |
515
7cb97346bc6f
Using class Module in SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
510
diff
changeset
|
81 bool hasErrors() |
7cb97346bc6f
Using class Module in SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
510
diff
changeset
|
82 { |
619
933cd8d24467
Renamed Parser.lx to Parser.lexer.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
593
diff
changeset
|
83 return parser.errors.length || parser.lexer.errors.length; |
515
7cb97346bc6f
Using class Module in SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
510
diff
changeset
|
84 } |
7cb97346bc6f
Using class Module in SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
510
diff
changeset
|
85 |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
86 string[] getImportPaths() |
366 | 87 { |
88 string[] result; | |
89 foreach (import_; imports) | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
370
diff
changeset
|
90 result ~= import_.getModuleFQNs(dirSep); |
366 | 91 return result; |
92 } | |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
366
diff
changeset
|
93 |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
94 /// Returns the fully qualified name of this module. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
95 /// E.g.: dil.ast.Node |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
366
diff
changeset
|
96 string getFQN() |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
366
diff
changeset
|
97 { |
370 | 98 return moduleFQN; |
99 } | |
100 | |
101 void setFQN(string moduleFQN) | |
102 { | |
103 uint i = moduleFQN.length; | |
104 if (i != 0) // Don't decrement if string has zero length. | |
105 i--; | |
106 // Find last dot. | |
107 for (; i != 0 && moduleFQN[i] != '.'; i--) | |
108 {} | |
109 this.moduleFQN = moduleFQN; | |
110 this.packageName = moduleFQN[0..i]; | |
111 this.moduleName = moduleFQN[(i == 0 ? 0 : i+1) .. $]; | |
112 } | |
113 | |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
114 /// Returns e.g. the FQN with slashes instead of dots. |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
115 /// E.g.: dil/ast/Node |
370 | 116 string getFQNPath() |
117 { | |
698
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
118 string FQNPath = moduleFQN.dup; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
119 foreach (i, c; FQNPath) |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
120 if (c == '.') |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
121 FQNPath[i] = dirSep; |
1564e41f454e
Revised modules cmd.ImportGraph and dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
122 return FQNPath; |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
366
diff
changeset
|
123 } |
364 | 124 } |