forked from google/jsonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.cpp
572 lines (490 loc) · 17.8 KB
/
lexer.cpp
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*
Copyright 2015 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <string>
#include <sstream>
#include "static_error.h"
#include "lexer.h"
static bool is_upper(char c)
{
return c >= 'A' && c <= 'Z';
}
static bool is_lower(char c)
{
return c >= 'a' && c <= 'z';
}
static bool is_number(char c)
{
return c >= '0' && c <= '9';
}
static bool is_identifier_first(char c)
{
return is_upper(c) || is_lower(c) || c == '_';
}
static bool is_identifier(char c)
{
return is_identifier_first(c) || is_number(c);
}
static bool is_symbol(char c)
{
switch (c) {
case '&': case '|': case '^':
case '=': case '<': case '>':
case '*': case '/': case '%':
case '#':
return true;
}
return false;
}
std::string lex_number(const char *&c, const std::string &filename, const Location &begin)
{
// This function should be understood with reference to the linked image:
// http://www.json.org/number.gif
// Note, we deviate from the json.org documentation as follows:
// There is no reason to lex negative numbers as atomic tokens, it is better to parse them
// as a unary operator combined with a numeric literal. This avoids x-1 being tokenized as
// <identifier> <number> instead of the intended <identifier> <binop> <number>.
enum State {
BEGIN,
AFTER_ZERO,
AFTER_ONE_TO_NINE,
AFTER_DOT,
AFTER_DIGIT,
AFTER_E,
AFTER_EXP_SIGN,
AFTER_EXP_DIGIT
} state;
std::string r;
state = BEGIN;
while (true) {
switch (state) {
case BEGIN:
switch (*c) {
case '0':
state = AFTER_ZERO;
break;
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
state = AFTER_ONE_TO_NINE;
break;
default:
throw StaticError(filename, begin, "Couldn't lex number");
}
break;
case AFTER_ZERO:
switch (*c) {
case '.':
state = AFTER_DOT;
break;
case 'e': case 'E':
state = AFTER_E;
break;
default:
goto end;
}
break;
case AFTER_ONE_TO_NINE:
switch (*c) {
case '.':
state = AFTER_DOT;
break;
case 'e': case 'E':
state = AFTER_E;
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
state = AFTER_ONE_TO_NINE;
break;
default:
goto end;
}
break;
case AFTER_DOT:
switch (*c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
state = AFTER_DIGIT;
break;
default: {
std::stringstream ss;
ss << "Couldn't lex number, junk after decimal point: " << *c;
throw StaticError(filename, begin, ss.str());
}
}
break;
case AFTER_DIGIT:
switch (*c) {
case 'e': case 'E':
state = AFTER_E;
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
state = AFTER_DIGIT;
break;
default:
goto end;
}
break;
case AFTER_E:
switch (*c) {
case '+': case '-':
state = AFTER_EXP_SIGN;
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
state = AFTER_EXP_DIGIT;
break;
default: {
std::stringstream ss;
ss << "Couldn't lex number, junk after 'E': " << *c;
throw StaticError(filename, begin, ss.str());
}
}
break;
case AFTER_EXP_SIGN:
switch (*c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
state = AFTER_EXP_DIGIT;
break;
default: {
std::stringstream ss;
ss << "Couldn't lex number, junk after exponent sign: " << *c;
throw StaticError(filename, begin, ss.str());
}
}
break;
case AFTER_EXP_DIGIT:
switch (*c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
state = AFTER_EXP_DIGIT;
break;
default:
goto end;
}
break;
}
r += *c;
c++;
}
end:
c--;
return r;
}
std::list<Token> jsonnet_lex(const std::string &filename, const char *input)
{
unsigned long line_number = 1;
const char *line_start = input;
std::list<Token> r;
const char *c = input;
for ( ; *c!='\0' ; ++c) {
Location begin(line_number, c - line_start + 1);
Token::Kind kind;
std::string data;
switch (*c) {
// Skip non-\n whitespace
case ' ': case '\t': case '\r':
continue;
// Skip \n and maintain line numbers
case '\n':
line_number++;
line_start = c+1;
continue;
case '{':
kind = Token::BRACE_L;
break;
case '}':
kind = Token::BRACE_R;
break;
case '[':
kind = Token::BRACKET_L;
break;
case ']':
kind = Token::BRACKET_R;
break;
case ':':
kind = Token::COLON;
break;
case ',':
kind = Token::COMMA;
break;
case '$':
kind = Token::DOLLAR;
break;
case '.':
kind = Token::DOT;
break;
case '(':
kind = Token::PAREN_L;
break;
case ')':
kind = Token::PAREN_R;
break;
case ';':
kind = Token::SEMICOLON;
break;
// Special cases for unary operators.
case '!':
kind = Token::OPERATOR;
if (*(c+1) == '=') {
c++;
data = "!=";
} else {
data = "!";
}
break;
case '~':
kind = Token::OPERATOR;
data = "~";
break;
case '+':
kind = Token::OPERATOR;
data = "+";
break;
case '-':
kind = Token::OPERATOR;
data = "-";
break;
// Numeric literals.
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
kind = Token::NUMBER;
data = lex_number(c, filename, begin);
break;
// String literals.
case '"': {
c++;
for (; ; ++c) {
if (*c == '\0') {
throw StaticError(filename, begin, "Unterminated string");
}
if (*c == '"') {
break;
}
switch (*c) {
case '\\':
switch (*(++c)) {
case '"':
data += *c;
break;
case '\\':
data += *c;
break;
case '/':
data += *c;
break;
case 'b':
data += '\b';
break;
case 'f':
data += '\f';
break;
case 'n':
data += '\n';
break;
case 'r':
data += '\r';
break;
case 't':
data += '\t';
break;
case 'u': {
++c; // Consume the 'u'.
unsigned long codepoint = 0;
// Expect 4 hex digits.
for (unsigned i=0 ; i<4 ; ++i) {
auto x = (unsigned char)(c[i]);
unsigned digit;
if (x == '\0') {
auto msg = "Unterminated string";
throw StaticError(filename, begin, msg);
} else if (x == '"') {
auto msg = "Truncated unicode escape sequence in "
"string literal.";
throw StaticError(filename, begin, msg);
} else if (x >= '0' && x <= '9') {
digit = x - '0';
} else if (x >= 'a' && x <= 'f') {
digit = x - 'a' + 10;
} else if (x >= 'A' && x <= 'F') {
digit = x - 'A' + 10;
} else {
std::stringstream ss;
ss << "Malformed unicode escape character, "
<< "should be hex: '" << x << "'";
throw StaticError(filename, begin, ss.str());
}
codepoint *= 16;
codepoint += digit;
}
// Encode in UTF-8.
if (codepoint < 0x0080) {
data += codepoint;
} else {
auto msg = "Codepoint out of ascii range.";
throw StaticError(filename, begin, msg);
}
/*
} else if (codepoint < 0x0800) {
data += 0xC0 | (codepoint >> 6);
data += 0x80 | (codepoint & 0x3F);
} else {
data += 0xE0 | (codepoint >> 12);
data += 0x80 | ((codepoint >> 6) & 0x3F);
data += 0x80 | (codepoint & 0x3F);
}
*/
// Leave us on the last char, ready for the ++c at
// the outer for loop.
c += 3;
}
break;
case '\0': {
auto msg = "Truncated escape sequence in string literal.";
throw StaticError(filename, begin, msg);
}
default: {
std::stringstream ss;
ss << "Unknown escape sequence in string literal: '" << *c << "'";
throw StaticError(filename, begin, ss.str());
}
}
break;
// Treat as a regular letter, but maintain line/column counters.
case '\n':
line_number++;
line_start = c+1;
data += *c;
break;
default:
// Just a regular letter.
data += *c;
}
}
kind = Token::STRING;
}
break;
// Keywords
default:
if (is_identifier_first(*c)) {
std::string id;
for (; *c != '\0' ; ++c) {
if (!is_identifier(*c)) {
break;
}
id += *c;
}
--c;
if (id == "else") {
kind = Token::ELSE;
} else if (id == "error") {
kind = Token::ERROR;
} else if (id == "false") {
kind = Token::FALSE;
} else if (id == "for") {
kind = Token::FOR;
} else if (id == "function") {
kind = Token::FUNCTION;
} else if (id == "if") {
kind = Token::IF;
} else if (id == "import") {
kind = Token::IMPORT;
} else if (id == "importstr") {
kind = Token::IMPORTSTR;
} else if (id == "in") {
kind = Token::IN;
} else if (id == "local") {
kind = Token::LOCAL;
} else if (id == "null") {
kind = Token::NULL_LIT;
} else if (id == "self") {
kind = Token::SELF;
} else if (id == "super") {
kind = Token::SUPER;
} else if (id == "tailcall") {
kind = Token::TAILCALL;
} else if (id == "then") {
kind = Token::THEN;
} else if (id == "true") {
kind = Token::TRUE;
} else {
// Not a keyword, must be an identifier.
kind = Token::IDENTIFIER;
data = id;
}
} else if (is_symbol(*c)) {
// Single line C++ style comment
if (*c == '/' && *(c+1) == '/') {
while (*c != '\0' && *c != '\n') {
++c;
}
// Leaving it on the \n allows processing of \n on next iteration,
// i.e. managing of the line & column counter.
c--;
continue;
}
// Single line # comment
if (*c == '#') {
while (*c != '\0' && *c != '\n') {
++c;
}
// Leaving it on the \n allows processing of \n on next iteration,
// i.e. managing of the line & column counter.
c--;
continue;
}
// Multi-line comment.
if (*c == '/' && *(c+1) == '*') {
c += 2; // Avoid matching /*/: skip the /* before starting the search for */.
while (*c != '\0' && !(*c == '*' && *(c+1) == '/')) {
if (*c == '\n') {
// Just keep track of the line / column counters.
line_number++;
line_start = c+1;
}
++c;
}
if (*c == '\0') {
auto msg = "Multi-line comment has no terminating */.";
throw StaticError(filename, begin, msg);
}
// Leave the counter on the closing /.
c++;
continue;
}
for (; *c != '\0' ; ++c) {
if (!is_symbol(*c)) {
break;
}
data += *c;
}
--c;
kind = Token::OPERATOR;
} else {
std::stringstream ss;
ss << "Could not lex the character ";
if (*c < 32)
ss << "code " << int(*c);
else
ss << "'" << *c << "'";
throw StaticError(filename, begin, ss.str());
}
break;
}
Location end(line_number, c - line_start + 1);
r.push_back(Token(kind, data, LocationRange(filename, begin, end)));
}
Location end(line_number, c - line_start + 1);
r.push_back(Token(Token::END_OF_FILE, "", LocationRange(filename, end, end)));
return r;
}