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