Menu

[30a0cb]: / codetml.d  Maximize  Restore  History

Download this file

385 lines (327 with data), 9.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
Monster - an advanced game scripting language
Copyright (C) 2007-2009 Nicolay Korslund
Email: <korslund@gmail.com>
WWW: http://monster.snaptoad.com/
This file (codetml.d) is part of the Monster script language
package.
Monster is distributed as free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License
version 3, as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
version 3 along with this program. If not, see
http://www.gnu.org/licenses/ .
*/
module codetml;
import monster.minibos.string;
import monster.minibos.stream;
import monster.minibos.stdio;
import monster.util.string : begins;
//import monster.minibos.stdio;
// Check if a character is alpha-numerical or an underscore
bool validIdentChar(char c)
{
if(validFirstIdentChar(c) || numericalChar(c))
return true;
return false;
}
// Same as above, except numbers are not allowed as the first
// character. Will extend to support UTF8 later.
bool validFirstIdentChar(char c)
{
if((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c == '_') ) return true;
return false;
}
bool numericalChar(char c)
{
return c >= '0' && c <= '9';
}
int keywordLookup[char[]];
int typeLookup[char[]];
static this()
{
foreach(int i, tok; keywordList)
keywordLookup[tok] = i;
foreach(int i, tok; typeList)
typeLookup[tok] = i;
}
// Index table of all basic types
const char[][] typeList =
[ "int", "float", "bool", "char", "double", "long", "uint", "ulong" ];
// Index table of all keywords
const char[][] keywordList =
[
"class", "return", "for", "this",
"new", "if", "else", "foreach",
"foreach_reverse", "do", "while",
"until", "continue", "break", "switch",
"select", "state", "typeof", "singleton",
"clone", "static", "const", "abstract",
"idle", "out", "ref", "public", "private",
"protected", "true", "false", "native",
"null", "goto", "halt", "auto", "var", "in", "import"
];
int lineNum;
void fail(char[] msg)
{
throw new Exception(format("%s: %s", lineNum, msg));
}
void main(char[][] args)
{
if(args.length != 2)
{
writefln("no input file");
return;
}
scope Stream inf = new BufferedFile(args[1]);
// Various parsing modes
enum
{
Normal, // Normal mode
Block, // Block comment
Nest // Nested block comment
}
int mode = Normal;
int nests = 0; // Nest level
writefln("<pre class=\"mcode\">");
foreach(ulong lnum, char[] line; inf)
{
lineNum = lnum;
void remWord(char[] str)
{
assert(line.begins(str));
line = line[str.length..$];
}
// Removes 'str' from the beginning of 'line', or from
// line[leadIn..$] if leadIn != 0.
void remWordWrite(char[] str, int leadIn)
{
assert(line.length >= leadIn+str.length);
writef("%s", line[0..leadIn+str.length]);
line = line[leadIn..$];
remWord(str);
}
void remRest()
{
writef("%s", line);
line = null;
}
line = stripr(line);
restart:
if(line.length == 0)
{
writefln();
continue;
}
if(mode == Block)
{
int index = line.find("*/");
// If we find a '*/', the comment is done
if(index != -1)
{
mode = Normal;
// Cut it the comment from the input
remWordWrite("*/", index);
writef("</span>");
}
else
{
// Comment not ended on this line, try the next
remRest();
}
// Start over
goto restart;
}
if(mode == Nest)
{
// Check for nested /+ and +/ in here, but go to restart if
// none is found (meaning the comment continues on the next
// line), or reset mode and go to restart if nest level ever
// gets to 0.
do
{
int incInd = -1;
int decInd = -1;
// Find the first matching '/+' or '+/
foreach(int i, char c; line[0..$-1])
{
if(c == '/' && line[i+1] == '+')
{
incInd = i;
break;
}
else if(c == '+' && line[i+1] == '/')
{
decInd = i;
break;
}
}
// Add a nest level when '/+' is found
if(incInd != -1)
{
remWordWrite("/+", incInd);
nests++;
continue; // Search more in this line
}
// Remove a nest level when '+/' is found
if(decInd != -1)
{
// Remove the +/ from input
remWordWrite("+/", decInd);
nests--; // Remove a level
assert(nests >= 0);
// Are we done? If so, return to normal mode.
if(nests == 0)
{
mode = Normal;
writef("</span>");
break;
}
continue;
}
// Nothing found on this line, try the next
remRest();
break;
}
while(line.length >= 2);
goto restart;
}
// Comment - start next line
if(line.begins("//"))
{
writef("<span class=\"m_comment\">%s</span>", line);
line = null;
goto restart;
}
// Block comment
if(line.begins("/*"))
{
mode = Block;
writef("<span class=\"m_comment\">/*");
line = line[2..$];
goto restart;
}
// Nested comment
if(line.begins("/+"))
{
mode = Nest;
writef("<span class=\"m_comment\">/+");
line = line[2..$];
nests++;
goto restart;
}
if(line.begins("*/")) fail("Unexpected end of block comment");
if(line.begins("+/")) fail("Unexpected end of nested comment");
// String literals (multi-line literals not implemented yet)
if(line.begins("\""))
{
int len = 1;
bool found = false;
foreach(char ch; line[1..$])
{
len++;
// No support for escape sequences as of now
if(ch == '"')
{
found = true;
break;
}
}
if(!found) fail("Unterminated string literal '" ~line~"'");
writef("<span class=\"m_string\">%s</span>", line[0..len]);
remWord(line[0..len]);
goto restart;
}
// Character literals (not parsed yet, so escape sequences like
// '\n', '\'', or unicode stuff won't work.)
if(line[0] == '\'')
{
if(line.length < 2 || line[2] != '\'')
fail("Malformed character literal " ~line);
writef("<span class=\"m_char\">%s</span>", line[0..3]);
remWord(line[0..3]);
goto restart;
}
// Numerical literals - if it starts with a number, we accept
// it, until it is interupted by an unacceptible character. We
// also accept numbers on the form .NUM. We do not try to parse
// the number here.
if(numericalChar(line[0]) ||
// Cover the .num case
( line.length >= 2 && line[0] == '.' &&
numericalChar(line[1]) ))
{
// Treat the rest as we would an identifier - the actual
// interpretation will be done later. We allow non-numerical
// tokens in the literal, such as 0x0a or 1_000_000. We must
// also explicitly allow '.' dots
int len = 1;
bool lastDot = false; // Was the last char a '.'?
foreach(char ch; line[1..$])
{
if(ch == '.')
{
// We accept "." but not "..", as this might be an
// operator.
if(lastDot)
{
len--; // Remove the last dot and exit.
break;
}
lastDot = true;
}
else
{
if(!validIdentChar(ch)) break;
lastDot = false;
}
len++;
}
writef("<span class=\"m_number\">%s</span>", line[0..len]);
remWord(line[0..len]);
goto restart;
}
// Check for identifiers
if(validFirstIdentChar(line[0]))
{
// It's an identifier or name, find the length
int len = 1;
foreach(char ch; line[1..$])
{
if(!validIdentChar(ch)) break;
len++;
}
char[] id = line[0..len];
// We only allow certain identifiers to begin with __, as
// these are reserved for internal use.
if(id.begins("__"))
if(id != "__STACK__")
fail("Identifier " ~ id ~ " is not allowed to begin with __");
// Check if this is a keyword
if(id in keywordLookup)
writef("<span class=\"m_keyword\">%s</span>", id);
// Type?
else if(id in typeLookup)
writef("<span class=\"m_type\">%s</span>", id);
else
// It's an identifier
writef("%s", id);
remWord(id);
goto restart;
}
// We don't know what the hell it is, so just print one char and
// hope it works out.
writef("%s", line[0]);
line = line[1..$];
goto restart;
}
if(mode == Block) fail("Unterminated block comment");
if(mode == Nest) fail("Unterminated nested comment");
writefln("</pre>");
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.