switched to GPLv3 ONLY, because i don't trust FSF anymore
[gaemu.git] / gaem / parser / gentokens.d
blob70b8260f06100e8ccd96cd178dd6bb2be9ac1207
1 /* GML parser
2 * coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
3 * Understanding is not required. Only obedience.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 module gaem.parser.gentokens is aliced;
19 string[] tokens = [
20 "false",
21 "true",
23 "all",
24 "noone",
25 "self",
26 "other",
27 "global",
29 "not",
30 "and",
31 "or",
32 "xor",
34 "break",
35 "continue",
37 "switch",
38 "case",
39 "default",
41 "div",
42 "mod",
44 "do",
45 "until",
46 "repeat",
47 "while",
49 "if",
50 "else",
52 "for",
54 "return",
55 "exit",
57 "var",
58 "globalvar",
60 "with",
62 "pi",
64 "function",
65 //"with_object",
69 struct SpTk {
70 string text;
71 string name;
74 SpTk[] sptk = [
75 SpTk("+", "Add"),
76 SpTk("-", "Sub"),
77 SpTk("*", "Mul"),
78 SpTk("/", "RDiv"),
79 SpTk("&", "BitAnd"),
80 SpTk("|", "BitOr"),
81 SpTk("^", "BitXor"),
82 SpTk("~", "BitNeg"),
83 SpTk("&&", "LogAnd"),
84 SpTk("||", "LogOr"),
85 SpTk("^^", "LogXor"),
86 SpTk("!", "LogNot"),
87 SpTk("<", "Less"),
88 SpTk(">", "Great"),
89 SpTk("<=", "LessEqu"),
90 SpTk(">=", "GreatEqu"),
91 SpTk("==", "Equ"),
92 SpTk("!=", "NotEqu"),
93 SpTk("=", "Ass"),
94 SpTk("+=", "AssAdd"),
95 SpTk("-=", "AssSub"),
96 SpTk("*=", "AssMul"),
97 SpTk("/=", "AssDiv"),
98 SpTk("&=", "AssBitAnd"),
99 SpTk("|=", "AssBitOr"),
100 SpTk("^=", "AssBitXor"),
101 SpTk("<<=", "AssLShift"),
102 SpTk(">>=", "AssRShift"),
103 SpTk(";", "Semi"),
104 SpTk(":", "Colon"),
105 SpTk(",", "Comma"),
106 SpTk(".", "Dot"),
107 SpTk("{", "LCurly"),
108 SpTk("}", "RCurly"),
109 SpTk("(", "LParen"),
110 SpTk(")", "RParen"),
111 SpTk("[", "LBracket"),
112 SpTk("]", "RBracket"),
113 SpTk("<<", "LShift"),
114 SpTk(">>", "RShift"),
115 SpTk("++", "PlusPlus"),
116 SpTk("--", "MinusMinus"),
120 void main () {
121 import std.algorithm;
122 import std.array;
123 import std.stdio;
124 tokens = tokens.sort!((a, b) => a < b).array;
127 bool[string] tk;
128 foreach (string s; tokens) {
129 if (s in tk) assert(0, "duplicate token: "~s);
130 tk[s] = true;
132 foreach (ref st; sptk) {
133 if (st.text in tk) assert(0, "duplicate token: "~st.text);
134 tk[st.text] = true;
138 bool[string] tnm;
140 auto fo = File("tokens.d", "w");
141 fo.writeln(`/* GML parser
142 * coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
143 * Understanding is not required. Only obedience.
145 * This program is free software: you can redistribute it and/or modify
146 * it under the terms of the GNU General Public License as published by
147 * the Free Software Foundation, version 3 of the License ONLY.
149 * This program is distributed in the hope that it will be useful,
150 * but WITHOUT ANY WARRANTY; without even the implied warranty of
151 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
152 * GNU General Public License for more details.
154 * You should have received a copy of the GNU General Public License
155 * along with this program. If not, see <http://www.gnu.org/licenses/>.
156 */`);
157 fo.writeln("module gaem.parser.tokens;");
159 // fo.write enum
160 fo.writeln();
161 fo.writeln();
162 fo.writeln("enum Keyword {");
163 fo.writeln(" NoKW,");
164 foreach (string n; tokens) {
165 import std.uni : toLower, toUpper;
166 string tename = n[0..1].toUpper~n[1..$].toLower;
167 if (tename in tnm) assert(0, "duplicate token name: "~tename);
168 tnm[tename] = true;
169 fo.writeln(" ", tename, ",");
171 foreach (ref ti; sptk) {
172 if (ti.name in tnm) assert(0, "duplicate token name: "~ti.name);
173 tnm[ti.name] = true;
174 fo.writeln(" ", ti.name, ",");
176 fo.writeln("}");
178 fo.writeln();
179 fo.writeln();
180 fo.writeln("__gshared immutable Keyword[string] keywords;");
181 fo.writeln("__gshared immutable string[int] keywordstx;");
183 fo.writeln();
184 fo.writeln();
185 fo.writeln("shared static this () {");
186 fo.writeln(" keywords = [");
187 foreach (string n; tokens) {
188 import std.uni : toLower, toUpper;
189 fo.writeln(" \"", n, "\": Keyword.", n[0..1].toUpper, n[1..$].toLower, ",");
191 foreach (ref ti; sptk) fo.writeln(" \"", ti.text, "\": Keyword.", ti.name, ",");
192 fo.writeln(" ];");
193 fo.writeln(" keywordstx = [");
194 foreach (string n; tokens) {
195 import std.uni : toLower, toUpper;
196 fo.writeln(" Keyword.", n[0..1].toUpper, n[1..$].toLower, ": \"", n, "\",");
198 foreach (ref ti; sptk) fo.writeln(" Keyword.", ti.name, ": \"", ti.text, "\",");
199 fo.writeln(" ];");
200 fo.writeln("}");
202 fo.writeln();
203 fo.writeln();
204 fo.writeln("static string keywordtext (uint id) {");
205 fo.writeln(" if (auto kw = id in keywordstx) return *kw;");
206 fo.writeln(" return \"<unknown>\";");
207 fo.writeln("}");