Mercurial > projects > dil
annotate trunk/src/dil/Settings.d @ 391:33b566df6af4
Migrated project to Tango.
Decremented the numbers of the format placeholders in the localized messages
by one.
Replaced all instances of writef/ln with Stdout.
Added module common.d with string aliases and a global Layout!(char) instance.
Replaced %s format specifiers with index placeholders in html/xml_tags.
Changed member Information.arguments to string message.
Copied std.metastring, std.uni and std.utf from Phobos.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Sat, 15 Sep 2007 17:12:26 +0200 |
parents | 2adf808343d6 |
children | 6440da4adb07 |
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; |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
368
diff
changeset
|
9 import std.metastrings : FormatT = Format, ToString; |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
368
diff
changeset
|
10 import common; |
309 | 11 |
331 | 12 template Pad(char[] str, uint amount) |
13 { | |
14 static if (str.length >= amount) | |
15 const char[] Pad = str; | |
16 else | |
17 const char[] Pad = "0" ~ Pad!(str, amount-1); | |
18 } | |
19 | |
20 template Pad(int num, uint amount) | |
21 { | |
22 const char[] Pad = Pad!(ToString!(num), amount); | |
23 } | |
24 | |
333 | 25 version(D2) |
26 { | |
27 const VERSION_MAJOR = 2; | |
28 const VERSION_MINOR = 0; | |
29 } | |
30 else | |
31 { | |
32 const VERSION_MAJOR = 1; | |
33 const VERSION_MINOR = 0; | |
34 } | |
35 | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
368
diff
changeset
|
36 const string VERSION = FormatT!("%s.%s", VERSION_MAJOR, Pad!(VERSION_MINOR, 3)); |
343
95f1b6e43214
- Removed TOK.Special and added an own entry for each special token.
aziz
parents:
339
diff
changeset
|
37 const VENDOR = "dil"; |
309 | 38 |
339
9954367bcd4b
- Removed usageGenerate from Settings.d. Added some comments.
aziz
parents:
333
diff
changeset
|
39 /// Used in main help message. |
309 | 40 const COMPILED_WITH = __VENDOR__; |
339
9954367bcd4b
- Removed usageGenerate from Settings.d. Added some comments.
aziz
parents:
333
diff
changeset
|
41 /// ditto |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
368
diff
changeset
|
42 const COMPILED_VERSION = FormatT!("%s.%s", __VERSION__/1000, Pad!(__VERSION__%1000, 3)); |
339
9954367bcd4b
- Removed usageGenerate from Settings.d. Added some comments.
aziz
parents:
333
diff
changeset
|
43 /// ditto |
309 | 44 const COMPILED_DATE = __TIMESTAMP__; |
45 | |
46 struct GlobalSettings | |
47 { | |
48 static: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
368
diff
changeset
|
49 string language; /// Language of loaded messages catalogue. |
309 | 50 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
|
51 string[] importPaths; /// Array of import paths to look for modules. |
309 | 52 void load() |
53 { | |
54 auto fileName = "config.d"[]; | |
358
0faf57d99c1c
- Replaced calls to std.file.read() with dil.loadFile().
aziz
parents:
349
diff
changeset
|
55 auto sourceText = loadFile(fileName); |
309 | 56 auto parser = new Parser(sourceText, fileName); |
368 | 57 auto root = parser.start(); |
309 | 58 |
59 if (parser.errors.length || parser.lx.errors.length) | |
60 { | |
61 throw new Exception("There are errors in " ~ fileName ~ "."); | |
62 } | |
63 | |
64 foreach (decl; root.children) | |
65 { | |
66 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
|
67 if (v is null) |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
68 continue; |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
69 auto vname = v.idents[0].srcText; |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
70 if (vname == "langfile") |
309 | 71 { |
72 auto e = v.values[0]; | |
73 if (!e) | |
349 | 74 throw new Exception("langfile variable has no value set."); |
309 | 75 auto val = Cast!(StringLiteralsExpression)(e); |
76 if (val) | |
349 | 77 // Set fileName to d-file with messages table. |
78 fileName = val.getString(); | |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
79 } |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
80 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
|
81 { |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
82 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
|
83 if (e is null) |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
84 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
|
85 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
|
86 { |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
87 foreach (value; array.values) |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
88 if (auto str = Cast!(StringLiteralsExpression)(value)) |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
89 GlobalSettings.importPaths ~= str.getString(); |
309 | 90 } |
367
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
91 else |
dda55fae37de
- ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents:
358
diff
changeset
|
92 throw new Exception("import_paths variable is set to "~e.classinfo.name~" instead of an ArrayInitializer."); |
309 | 93 } |
94 } | |
95 | |
96 // Load messages | |
358
0faf57d99c1c
- Replaced calls to std.file.read() with dil.loadFile().
aziz
parents:
349
diff
changeset
|
97 sourceText = loadFile(fileName); |
349 | 98 parser = new Parser(sourceText, fileName); |
368 | 99 root = parser.start(); |
349 | 100 |
101 if (parser.errors.length || parser.lx.errors.length) | |
309 | 102 { |
349 | 103 throw new Exception("There are errors in "~fileName~"."); |
104 } | |
309 | 105 |
349 | 106 char[][] messages; |
107 foreach (decl; root.children) | |
108 { | |
109 auto v = Cast!(VariableDeclaration)(decl); | |
110 if (v is null) | |
111 continue; | |
112 if (v.idents[0].srcText == "messages") | |
309 | 113 { |
349 | 114 auto e = v.values[0]; |
115 if (!e) | |
116 throw new Exception("messages variable in "~fileName~" has no value set."); | |
117 if (auto array = Cast!(ArrayInitializer)(e)) | |
309 | 118 { |
349 | 119 foreach (value; array.values) |
309 | 120 { |
349 | 121 if (auto str = Cast!(StringLiteralsExpression)(value)) |
122 messages ~= str.getString(); | |
309 | 123 } |
124 } | |
349 | 125 else |
126 throw new Exception("messages variable is set to "~e.classinfo.name~" instead of an ArrayInitializer."); | |
309 | 127 } |
349 | 128 else if(v.idents[0].srcText == "lang_code") |
129 { | |
130 auto e = v.values[0]; | |
131 if (!e) | |
132 throw new Exception("lang_code variable in "~fileName~" has no value set."); | |
133 if (auto str = Cast!(StringLiteralsExpression)(e)) | |
134 GlobalSettings.language = str.getString(); | |
135 } | |
309 | 136 } |
349 | 137 if (messages.length != MID.max+1) |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
368
diff
changeset
|
138 throw new Exception(Format("messages table in {0} must exactly have {1} entries, but {2} were found.", fileName, MID.max+1, messages.length)); |
349 | 139 GlobalSettings.messages = messages; |
309 | 140 } |
141 } |