Mercurial > projects > dil
annotate trunk/src/dil/Settings.d @ 505:3bb94ba21490
Refactored a great amount of code.
Changed many declaration types from Token* to Identifier*.
Fix in parseStructInitializer(): append null to idents in else body.
Fixed class Parameter and parseParameterList().
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Wed, 12 Dec 2007 02:25:42 +0100 |
parents | 325714d8aa6c |
children | 6160ab7b1816 |
rev | line source |
---|---|
309 | 1 /++ |
2 Author: Aziz Köksal | |
3 License: GPL3 | |
4 +/ | |
326 | 5 module dil.Settings; |
327 | 6 import dil.Messages; |
7 import dil.Parser, dil.SyntaxTree, dil.Declarations, dil.Expressions; | |
358
0faf57d99c1c
- Replaced calls to std.file.read() with dil.loadFile().
aziz
parents:
349
diff
changeset
|
8 import dil.File; |
428
3751db263679
Looking for config.d and language file in executables folder now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
399
diff
changeset
|
9 import tango.io.FilePath; |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
368
diff
changeset
|
10 import common; |
309 | 11 |
12 struct GlobalSettings | |
13 { | |
14 static: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
368
diff
changeset
|
15 string language; /// Language of loaded messages catalogue. |
309 | 16 string[] messages; /// Table of localized compiler messages. |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
17 string[] importPaths; /// Array of import paths to look for modules. |
445
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
18 void load() |
309 | 19 { |
445
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
20 scope execPath = new FilePath(GetExecutableFilePath()); |
428
3751db263679
Looking for config.d and language file in executables folder now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
399
diff
changeset
|
21 auto fileName = execPath.file("config.d").toUtf8(); |
358
0faf57d99c1c
- Replaced calls to std.file.read() with dil.loadFile().
aziz
parents:
349
diff
changeset
|
22 auto sourceText = loadFile(fileName); |
309 | 23 auto parser = new Parser(sourceText, fileName); |
368 | 24 auto root = parser.start(); |
309 | 25 |
26 if (parser.errors.length || parser.lx.errors.length) | |
27 { | |
28 throw new Exception("There are errors in " ~ fileName ~ "."); | |
29 } | |
30 | |
31 foreach (decl; root.children) | |
32 { | |
33 auto v = Cast!(VariableDeclaration)(decl); | |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
34 if (v is null) |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
35 continue; |
505
3bb94ba21490
Refactored a great amount of code.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
464
diff
changeset
|
36 auto vname = v.idents[0].str; |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
37 if (vname == "langfile") |
309 | 38 { |
39 auto e = v.values[0]; | |
40 if (!e) | |
349 | 41 throw new Exception("langfile variable has no value set."); |
399
ff1d11c27061
Renamed some Expression classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
42 auto val = Cast!(StringExpression)(e); |
309 | 43 if (val) |
349 | 44 // Set fileName to d-file with messages table. |
45 fileName = val.getString(); | |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
46 } |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
47 else if (vname == "import_paths") |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
48 { |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
49 auto e = v.values[0]; |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
50 if (e is null) |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
51 throw new Exception("import_paths variable has no variable set."); |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
52 if (auto array = Cast!(ArrayInitializer)(e)) |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
53 { |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
54 foreach (value; array.values) |
399
ff1d11c27061
Renamed some Expression classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
55 if (auto str = Cast!(StringExpression)(value)) |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
56 GlobalSettings.importPaths ~= str.getString(); |
309 | 57 } |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
58 else |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
59 throw new Exception("import_paths variable is set to "~e.classinfo.name~" instead of an ArrayInitializer."); |
309 | 60 } |
61 } | |
62 | |
63 // Load messages | |
428
3751db263679
Looking for config.d and language file in executables folder now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
399
diff
changeset
|
64 sourceText = loadFile(execPath.file(fileName).toUtf8()); |
349 | 65 parser = new Parser(sourceText, fileName); |
368 | 66 root = parser.start(); |
349 | 67 |
68 if (parser.errors.length || parser.lx.errors.length) | |
309 | 69 { |
349 | 70 throw new Exception("There are errors in "~fileName~"."); |
71 } | |
309 | 72 |
349 | 73 char[][] messages; |
74 foreach (decl; root.children) | |
75 { | |
76 auto v = Cast!(VariableDeclaration)(decl); | |
77 if (v is null) | |
78 continue; | |
505
3bb94ba21490
Refactored a great amount of code.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
464
diff
changeset
|
79 if (v.idents[0].str == "messages") |
309 | 80 { |
349 | 81 auto e = v.values[0]; |
82 if (!e) | |
83 throw new Exception("messages variable in "~fileName~" has no value set."); | |
84 if (auto array = Cast!(ArrayInitializer)(e)) | |
309 | 85 { |
349 | 86 foreach (value; array.values) |
309 | 87 { |
399
ff1d11c27061
Renamed some Expression classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
88 if (auto str = Cast!(StringExpression)(value)) |
349 | 89 messages ~= str.getString(); |
309 | 90 } |
91 } | |
349 | 92 else |
93 throw new Exception("messages variable is set to "~e.classinfo.name~" instead of an ArrayInitializer."); | |
309 | 94 } |
505
3bb94ba21490
Refactored a great amount of code.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
464
diff
changeset
|
95 else if(v.idents[0].str == "lang_code") |
349 | 96 { |
97 auto e = v.values[0]; | |
98 if (!e) | |
99 throw new Exception("lang_code variable in "~fileName~" has no value set."); | |
399
ff1d11c27061
Renamed some Expression classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
100 if (auto str = Cast!(StringExpression)(e)) |
349 | 101 GlobalSettings.language = str.getString(); |
102 } | |
309 | 103 } |
349 | 104 if (messages.length != MID.max+1) |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
368
diff
changeset
|
105 throw new Exception(Format("messages table in {0} must exactly have {1} entries, but {2} were found.", fileName, MID.max+1, messages.length)); |
349 | 106 GlobalSettings.messages = messages; |
394
6440da4adb07
Fixed forward references of enum MID complaints by compiler.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
107 dil.Messages.SetMessages(messages); |
309 | 108 } |
109 } | |
445
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
110 |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
111 version(Windows) |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
112 { |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
113 private extern(Windows) uint GetModuleFileNameA(void*, char*, uint); |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
114 /++ |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
115 Get the fully qualified path to this executable. |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
116 +/ |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
117 char[] GetExecutableFilePath() |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
118 { |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
119 alias GetModuleFileNameA GetModuleFileName; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
120 char[] buffer = new char[256]; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
121 uint count; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
122 |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
123 while (1) |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
124 { |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
125 if (buffer is null) |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
126 return null; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
127 |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
128 count = GetModuleFileName(null, buffer.ptr, buffer.length); |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
129 if (count == 0) |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
130 return null; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
131 if (buffer.length != count && buffer[count] == 0) |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
132 break; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
133 // Increase size of buffer |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
134 buffer.length = buffer.length * 2; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
135 } |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
136 assert(buffer[count] == 0); |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
137 // Reduce buffer to the actual length of the string (excluding '\0'.) |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
138 if (count < buffer.length) |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
139 buffer.length = count; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
140 return buffer; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
141 } |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
142 } |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
143 else version(linux) |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
144 { |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
145 private extern(C) size_t readlink(char* path, char* buf, size_t bufsize); |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
146 /++ |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
147 Get the fully qualified path to this executable. |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
148 +/ |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
149 char[] GetExecutableFilePath() |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
150 { |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
151 char[] buffer = new char[256]; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
152 size_t count; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
153 |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
154 while (1) |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
155 { |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
156 // This won't work on very old Linux systems. |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
157 count = readlink("/proc/self/exe".ptr, buffer.ptr, buffer.length); |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
158 if (count == -1) |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
159 return null; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
160 if (count < buffer.length) |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
161 break; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
162 buffer.length = buffer.length * 2; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
163 } |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
164 buffer.length = count; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
165 return buffer; |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
166 } |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
167 } |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
168 else |
294353fe2514
Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
428
diff
changeset
|
169 static assert(0, "GetExecutableFilePath() is not implemented on your platform."); |