309
|
1 /++
|
|
2 Author: Aziz Köksal
|
|
3 License: GPL3
|
|
4 +/
|
|
5 module Settings;
|
|
6 import Parser, SyntaxTree, Declarations, Expressions;
|
|
7 import std.metastrings;
|
|
8
|
|
9 version(D2)
|
|
10 {
|
|
11 const VERSION_MAJOR = 2;
|
|
12 const VERSION_MINOR = 0;
|
|
13 }
|
|
14 else
|
|
15 {
|
|
16 const VERSION_MAJOR = 1;
|
|
17 const VERSION_MINOR = 0;
|
|
18 }
|
|
19 const string VERSION = Format!("%s.%s", VERSION_MAJOR, VERSION_MINOR);
|
|
20
|
|
21 const COMPILED_WITH = __VENDOR__;
|
|
22 const COMPILED_VERSION = Format!("%s.%s", __VERSION__/1000, __VERSION__%1000);
|
|
23 const COMPILED_DATE = __TIMESTAMP__;
|
|
24
|
|
25 const usageHighlight = "highlight (hl) file.d";
|
|
26
|
|
27 struct GlobalSettings
|
|
28 {
|
|
29 static:
|
|
30 string language; /// Language of messages catalogue to load.
|
|
31 string[] messages; /// Table of localized compiler messages.
|
|
32 void load()
|
|
33 {
|
|
34 auto fileName = "config.d"[];
|
|
35 auto sourceText = cast(char[]) std.file.read(fileName);
|
|
36 auto parser = new Parser(sourceText, fileName);
|
|
37 parser.start();
|
|
38 auto root = parser.parseModule();
|
|
39
|
|
40 if (parser.errors.length || parser.lx.errors.length)
|
|
41 {
|
|
42 throw new Exception("There are errors in " ~ fileName ~ ".");
|
|
43 }
|
|
44
|
|
45 foreach (decl; root.children)
|
|
46 {
|
|
47 auto v = Cast!(VariableDeclaration)(decl);
|
|
48 if (v && v.idents[0].srcText == "language")
|
|
49 {
|
|
50 auto e = v.values[0];
|
|
51 if (!e)
|
|
52 throw new Exception("language variable has no value set.");
|
|
53 auto val = Cast!(StringLiteralsExpression)(e);
|
|
54 if (val)
|
|
55 {
|
|
56 GlobalSettings.language = val.getString();
|
|
57 break;
|
|
58 }
|
|
59 }
|
|
60 }
|
|
61
|
|
62 // Load messages
|
|
63 if (GlobalSettings.language.length)
|
|
64 {
|
|
65 fileName = "lang_" ~ GlobalSettings.language ~ ".d";
|
|
66 sourceText = cast(char[]) std.file.read(fileName);
|
|
67 parser = new Parser(sourceText, fileName);
|
|
68 parser.start();
|
|
69 root = parser.parseModule();
|
|
70
|
|
71 if (parser.errors.length || parser.lx.errors.length)
|
|
72 {
|
|
73 throw new Exception("There are errors in "~fileName~".");
|
|
74 }
|
|
75
|
|
76 char[][] messages;
|
|
77 foreach (decl; root.children)
|
|
78 {
|
|
79 auto v = Cast!(VariableDeclaration)(decl);
|
|
80 if (v && v.idents[0].srcText == "messages")
|
|
81 {
|
|
82 auto e = v.values[0];
|
|
83 if (!e)
|
|
84 throw new Exception("messages variable in "~fileName~" has no value set.");
|
|
85 if (auto array = Cast!(ArrayInitializer)(e))
|
|
86 {
|
|
87 foreach (value; array.values)
|
|
88 {
|
|
89 if (auto str = Cast!(StringLiteralsExpression)(value))
|
|
90 messages ~= str.getString();
|
|
91 }
|
|
92 }
|
|
93 else
|
|
94 throw new Exception("messages variable is set to "~e.classinfo.name~" instead of an ArrayInitializer.");
|
|
95 }
|
|
96 }
|
|
97 GlobalSettings.messages = messages;
|
|
98 }
|
|
99 }
|
|
100
|
|
101 }
|