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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
#include "lexcer.h"
#include "lexconst.h"
#include <cctype>
#include <cstdlib>
// using LexCer::TokenClass;
// using LexCer::Token;
// using LexCer::keywords;
// using LexCer::preprocessor;
// using LexCer::tokenToClass;
// using LexCer::spanBegin;
// using LexCer::spanEnd;
//const char* Tokenizer::spanBegin = "<span class=\"";
//const char* Tokenizer::spanEnd = "</span>";
namespace LexCer {
Tokenizer::Tokenizer(FILE *in, FILE *out)
: in(in), out(out) {
token.type = TokenClass::Invalid;
typenames = {"void", "char", "short", "int", "long", "float", "double", "signed", "unsigned"};
}
void Tokenizer::makeToken(TokenClass type, unsigned line, std::string lexeme, double number) {
token.type = type;
token.line = line;
token.lexeme = lexeme;
token.number = number;
}
bool Tokenizer::readNextDirective(std::string *lexeme) {
char c;
while (isalpha((c = fgetc(in))))
lexeme->push_back(c);
// we may need that last bit
ungetc((unsigned) c, in);
auto directive = lexeme->substr(1, lexeme->size() - 1);
if (!preprocessor.count(directive))
return false;
if (directive.compare("define") == 0) {
//readDefine();
}
return true;
}
bool Tokenizer::readNextString(std::string *lexeme, char oldc) {
char c, prevc = oldc;
while (((c = fgetc(in)) != '"' || prevc == '\\') && c != '\n' && c != EOF) {
lexeme->push_back(c);
prevc = c;
}
if (c == '\n' || c == EOF) {
ungetc((unsigned) c, in);
return false;
}
lexeme->push_back(c);
return true;
}
bool Tokenizer::readNextIdentifier(std::string *lexeme) {
char c;
bool valid = true;
while (isalnum(c = fgetc(in)) || c == '_') {
lexeme->push_back(c);
}
ungetc((unsigned) c, in);
return valid;
}
bool Tokenizer::readNextNumber(char beg, double *number, std::string *numberRepr) {
bool valid = true;
num_t base = DEC;
char c;
if (beg == '0') {
c = fgetc(in);
numberRepr->push_back(c);
if (c == 'b')
base = BIN;
else if (c == 'x' || c == 'X')
base = HEX;
else if (isdigit(c))
base = OCT;
else
ungetc((unsigned) c, in);
}
while (isValidDigit(c = fgetc(in), base)) {
numberRepr->push_back(c);
}
if (c == '.') {
numberRepr->push_back(c);
while (isValidDigit(c = fgetc(in), base))
numberRepr->push_back(c);
}
if (tolower(c) == 'e') {
numberRepr->push_back(c);
if (base != DEC)
return false;
if (!isdigit(c = fgetc(in)) && c != '-' && c != '+') {
ungetc((unsigned) c, in);
return false;
}
numberRepr->push_back(c);
while (isValidDigit(c = fgetc(in), base))
numberRepr->push_back(c);
}
char lower = tolower(c);
if (lower == 'u') {
numberRepr->push_back(c);
lower = tolower(c = fgetc(in));
}
if (lower == 'f' || lower == 'l')
numberRepr->push_back(c);
else
ungetc((unsigned) c, in);
*number = this->atod(*numberRepr); // not working ...
return true;
}
bool Tokenizer::isValidDigit(char c, num_t numType) const {
bool valid;
switch (numType) {
case DEC:
return isdigit(c);
case BIN:
return (c == '0' || c == '1');
case OCT:
return (c >= '0' || c <= '7');
case HEX:
return (isdigit(c) || tolower(c) >= 'a' || tolower(c) <= 'f');
default:
return false;
}
}
bool Tokenizer::readLineComment(std::string *lexeme) {
char c;
while ((c = fgetc(in)) != '\n' && c != EOF)
lexeme->push_back(c);
ungetc((unsigned) c, in);
return true; // for consistency
}
bool Tokenizer::readMultilineComment(std::string *lexeme) {
char c, prevc = (char) -1;
while (((c = fgetc(in)) != '/' && prevc != '*') && c != EOF)
lexeme->push_back(c);
if (c == EOF) {
return false;
}
lexeme->push_back(c); // closing slash
return true;
}
bool Tokenizer::readInclude(std::string *lexeme) {
char c;
bool valid = true;
while ((c = fgetc(in)) != '>') {
lexeme->push_back(c);
if (!isalpha(c) && c != '_' && c != '.')
valid = false;
}
lexeme->push_back(c);
return valid;
}
bool Tokenizer::readUntilBlank(std::string *lexeme) {
char c;
while (!isblank(c = fgetc(in)) && c != EOF) ;
ungetc((unsigned) c, in);
return true;
}
bool Tokenizer::writeToOutput() {
if (token.type == TokenClass::Eof) {
printf("End of file!\n");
fputc('\n', out);
return false;
}
if (token.type == TokenClass::Skip) {
if (fputs(token.lexeme.c_str(), out) == EOF) {
printf("Error while writing to output");
return false;
}
return true;
}
printf("Token of type %s at line %d: ", tokenToClass.at(token.type).c_str(), token.line);
if (token.type == TokenClass::Number)
printf("%f\n", token.number);
else
printf("%s\n", token.lexeme.c_str());
auto lexeme = escapeHTML(token.lexeme);
std::string output;
output.append(spanBegin).append(tokenToClass.at(token.type)).append("\">").append(lexeme).append(spanEnd);
if (fputs(output.c_str(), out) == EOF) {
printf("Error while writing to output");
return false;
}
return true;
}
std::string Tokenizer::escapeHTML(const std::string &lexeme) {
std::string result;
for (auto c : lexeme) {
switch(c) {
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '&':
result.append("&");
break;
default:
result.push_back(c);
break;
}
}
return result;
}
TokenClass Tokenizer::nextToken(std::string *lexeme, double *number) {
if (token.type == TokenClass::Eof) {
return TokenClass::Invalid;
}
char c = fgetc(in);
if (c == EOF) {
return TokenClass::Eof;
}
lexeme->push_back(c);
// operator || symbol || string || preprocessor directive
// probably should check for errors like '5 * % 4;', but...
switch(c) {
case '\n':
++line;
case '\t':
case ' ':
case ';':
case ',':
case '{':
case '}':
return TokenClass::Skip;
case '(':
return TokenClass::LeftParen;
case ')':
return TokenClass::RightParen;
case '[':
case ']':
case '?':
case ':':
return TokenClass::Operator;
case '*':
c = fgetc(in);
if (c == '/') {
return TokenClass::Invalid;
}
ungetc((unsigned) c, in);
c = '*';
case '^':
case '%':
case '=':
case '!':
case '~':
c = fgetc(in);
if (c != '=') ungetc((unsigned) c, in);
else lexeme->push_back(c);
return TokenClass::Operator;
case '<':
case '>':
c = fgetc(in);
if (c == lexeme->at(lexeme->size() - 1)) {
lexeme->push_back(c);
c = fgetc(in);
if (c == '=')
lexeme->push_back(c);
else
ungetc((unsigned) c, in);
} else if (lexeme->at(lexeme->size() - 1) == '<' && isalpha(c)) {
lexeme->push_back(c);
if (!readInclude(lexeme))
return TokenClass::Invalid;
return TokenClass::String;
} else
ungetc((unsigned) c, in);
return TokenClass::Operator;
case '&':
case '|':
case '+':
c = fgetc(in);
if (c == lexeme->at(lexeme->size() - 1) || c == '=')
lexeme->push_back(c);
else
ungetc((unsigned) c, in);
return TokenClass::Operator;
case '-':
c = fgetc(in);
if (c == '-' || c == '=' || c == '>')
lexeme->push_back(c);
else
ungetc((unsigned) c, in);
return TokenClass::Operator;
case '#':
if (readNextDirective(lexeme))
return TokenClass::PreprocessorDirective;
return TokenClass::Invalid;
case '\'':
lexeme->push_back(c = fgetc(in));
if (c == '\\') {
lexeme->push_back(c = fgetc(in));
}
lexeme->push_back(c = fgetc(in)); // the closing mark
if (c != '\'')
return TokenClass::Invalid;
return TokenClass::String;
case '"':
if (readNextString(lexeme, c))
return TokenClass::String;
return TokenClass::Invalid;
case '/':
c = fgetc(in);
lexeme->push_back(c);
switch (c) {
case '=':
return TokenClass::Operator;
case '/':
if (!readLineComment(lexeme))
return TokenClass::Invalid;
return TokenClass::Comment;
case '*':
if (!readMultilineComment(lexeme))
return TokenClass::Invalid;
return TokenClass::Comment;
default:
if (!isblank(c) || !isdigit(c)) {
ungetc((unsigned) c, in);
lexeme->pop_back();
return TokenClass::Invalid;
}
return TokenClass::Operator;
}
case '\\':
c = fgetc(in);
ungetc((unsigned) c, in);
if (c != '\n')
return TokenClass::Invalid; // stray '\'
return TokenClass::Operator;
}
// identifier || keyword || typename
if (isalpha(c) || c == '_') {
if (readNextIdentifier(lexeme)) {
if (keywords.count(*lexeme) != 0)
return TokenClass::Keyword;
else if (typenames.count(*lexeme) != 0)
return TokenClass::Typename;
return TokenClass::Identifier;
} else {
return TokenClass::Invalid;
}
}
if (!isdigit(c)) {
return TokenClass::Invalid;
}
// number
if (readNextNumber(c, number, lexeme))
return TokenClass::Number;
return TokenClass::Invalid;
}
std::string getOutputFileName(const std::string &name) {
auto pos = name.find_last_of('.');
if (pos == std::string::npos) {
return name + ".html";
}
return name.substr(0, pos) + ".html";
}
void lexCit(FILE *in, FILE *out) {
Tokenizer hl {in, out};
std::string lexeme;
double number;
TokenClass type;
do {
type = hl.nextToken(&lexeme, &number);
hl.makeToken(type, hl.lineNumber(), lexeme, number);
if (!hl.writeToOutput())
break;
lexeme = "";
number = 0.0;
} while(hl.currentToken().type != TokenClass::Eof || hl.currentToken().type != TokenClass::Invalid);
}
} /* Namespace Lexcer */
|