-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileLexer.cpp
More file actions
644 lines (584 loc) · 12.6 KB
/
FileLexer.cpp
File metadata and controls
644 lines (584 loc) · 12.6 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
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
#include "FileLexer.h"
#include <cstring>
#include <iostream>
using namespace std;
Lexer::Lexer( LexerReader *reader ) : mReader( reader ),
mLastSym( -1 ), mCurrentPos( 0 )
{
lineno = 1;
charPos = 0;
}
const std::string &Lexer::getSymbolText()
{
return mMatchString;
}
bool Lexer::isAlpha( char c )
{
if (( c >= 'a' && c <= 'z' ) ||
( c >= 'A' && c <= 'Z' ) ||
( c == '_' ) )
{
return true;
}
return false;
}
bool Lexer::isAlphaNum( char c )
{
if (( c >= 'a' && c <= 'z' ) ||
( c >= 'A' && c <= 'Z' ) ||
( c >= '0' && c <= '9' ) ||
( c == '_' ) )
{
return true;
}
return false;
}
bool Lexer::match( const char *match_str )
{
int len = strlen( match_str );
for ( int i = 0; i < len; i++ )
{
if ( (*mReader)[ i ] != match_str[ i ] )
return false;
}
mMatchString = match_str;
mReader->popChar( len );
return true;
}
bool Lexer::matchKeyword( const char *match_str )
{
int len = strlen( match_str );
int i = 0;
for ( ; i < len; i++ )
{
if ( (*mReader)[ i ] != match_str[ i ] )
return false;
}
char endChar = (*mReader)[ i ];
if ( isAlphaNum( endChar ) )
return false;
mMatchString = match_str;
mReader->popChar( len );
return true;
}
void Lexer::readSymbol()
{
mMatchString.append( 1, mReader->popChar() );
while ( !mReader->isEOF() )
{
if ( isAlphaNum( mReader->peekChar() ) )
{
mMatchString.append( 1, mReader->popChar() );
}
else
return;
}
}
void Lexer::readStringConst()
{
// pop the " char
mReader->popChar();
while ( !mReader->isEOF() )
{
if ( mReader->peekChar() == '\n' )
printf( "Multi-line string constant on line %d\n", lineno );
if ( mReader->peekChar() == '\\' )
{
switch ( (*mReader)[ 1 ] )
{
case 'a':
mMatchString.append( 1, '\a' );
mReader->popChar( 2 );
break;
case 'b':
mMatchString.append( 1, '\b' );
mReader->popChar( 2 );
break;
case 't':
mMatchString.append( 1, '\t' );
mReader->popChar( 2 );
break;
case 'n':
mMatchString.append( 1, '\n' );
mReader->popChar( 2 );
break;
case 'v':
mMatchString.append( 1, '\v' );
mReader->popChar( 2 );
break;
case 'f':
mMatchString.append( 1, '\f' );
mReader->popChar( 2 );
break;
case 'r':
mMatchString.append( 1, '\r' );
mReader->popChar( 2 );
break;
case '\\':
mMatchString.append( 1, '\\' );
mReader->popChar( 2 );
break;
case '\"':
mMatchString.append( 1, '\"' );
mReader->popChar( 2 );
break;
case '\'':
mMatchString.append( 1, '\'' );
mReader->popChar( 2 );
break;
case '0':
mMatchString.append( 1, '\0' );
mReader->popChar( 2 );
break;
}
}
else if ( mReader->peekChar() != '\"' )
{
mMatchString.append( 1, mReader->popChar() );
}
else
{
mReader->popChar();
return;
}
}
}
void Lexer::readCharacterConst()
{
// pop the ' char
mReader->popChar();
while ( !mReader->isEOF() )
{
if ( mReader->peekChar() == '\n' )
printf( "Multi-line char constant on line %d\n", lineno );
// could improve the proper grammmer for the constant value.
if ( mReader->peekChar() != '\'' )
{
mMatchString.append( 1, mReader->popChar() );
}
else
{
mReader->popChar();
return;
}
}
}
bool Lexer::readConst()
{
bool hex = false;
bool oct = false;
bool valid_char = true;
bool isFloat = false;
if ( mReader->peekChar() == '0' )
{
if ( (*mReader)[ 1 ] == 'x' or (*mReader)[ 1 ] == 'X' )
{
hex = true;
mReader->popChar( 2 );
mMatchString = "0x";
}
else
{
oct = true;
mReader->popChar( 1 );
mMatchString = "0";
}
}
while ( !mReader->isEOF() and valid_char )
{
char c = mReader->peekChar();
if ( hex and ((c >= '0' and c <= '9') or (c >= 'a' and c <= 'f') or (c >= 'A' and c <= 'F')) )
mMatchString.append( 1, mReader->popChar() );
else if ( c == '.' and !hex and !isFloat )
{
// Check that the next char after '.' is a digit (not '..' range)
char nextC = (*mReader)[ 1 ];
if ( nextC >= '0' and nextC <= '9' )
{
isFloat = true;
oct = false; // 0.xxx is float, not octal
mMatchString.append( 1, mReader->popChar() );
}
else
valid_char = false;
}
else if ( oct and (c >= '0' and c <= '7') )
mMatchString.append( 1, mReader->popChar() );
else if ( c >= '0' and c <= '9' )
mMatchString.append( 1, mReader->popChar() );
else
valid_char = false;
}
return isFloat;
}
void Lexer::handleComment( bool singleLine )
{
while ( !mReader->isEOF() )
{
//std::cout << "single " << singleLine << ", char " << mReader->peekChar() << std::endl;
if ( !singleLine and match( "*/" ) )
{
mMatchString.clear();
return;
}
else if ( mReader->peekChar() == '\n' )
{
lineno += 1;
mReader->popChar();
if ( singleLine )
{
mMatchString.clear();
return;
}
}
else
mReader->popChar();
}
}
int Lexer::peekSymbol()
{
if ( mLastSym == -1 )
mLastSym = getSymbolInternal();
return mLastSym;
}
int Lexer::getSymbol()
{
int sym = -1;
if ( mLastSym != -1 )
{
sym = mLastSym;
mLastSym = -1;
}
else
sym = getSymbolInternal();
mCurrentPos += 1;
return sym;
}
int Lexer::getCurrentPos()
{
return mCurrentPos;
}
void Lexer::setCurrentPos( int pos )
{
if ( pos >= 0 or pos < mSymbolList.size() )
mCurrentPos = pos;
else
mCurrentPos = mSymbolList.size() - 1;
mLastSym = -1;
}
int Lexer::getSymbolInternal()
{
int sym = -1;
if ( mCurrentPos == mSymbolList.size() )
{
sym = getSymbolFromFile();
mSymbolList.push_back( SymbolInfo( sym, mMatchString ) );
}
else
{
sym = mSymbolList[ mCurrentPos ].symbol;
mMatchString = mSymbolList[ mCurrentPos ].symbolText;
}
if ( sym <= Lexer::QUESTION_MARK || (sym >= 256 && sym < Lexer::NUM_SYMBOLS) )
std::cout << "Symbol " << sym << " (" << mMatchString << ")" << std::endl;
else
std::cout << "Symbol " << (char)sym << std::endl;
return sym;
}
int Lexer::getSymbolFromFile()
{
mMatchString.clear();
mLastSym = -1;
while ( !mReader->isEOF() )
{
//std::cout << "Lexer Symbol " << (int)mReader->peekChar() << std::endl;
// check for keywords
switch ( mReader->peekChar() )
{
case 'a':
if ( matchKeyword( "assert" ) )
return KEYWORD_ASSERT;
else if ( matchKeyword( "async" ) )
return KEYWORD_ASYNC;
else if ( matchKeyword( "await" ) )
return KEYWORD_AWAIT;
break;
case 'b':
if ( matchKeyword( "bool" ) )
return BOOL;
else if ( matchKeyword( "break" ) )
return KEYWORD_BREAK;
break;
case 'c':
if ( matchKeyword( "continue" ) )
return KEYWORD_CONTINUE;
else if ( matchKeyword( "cstring" ) )
return KEYWORD_CSTRING;
else if ( matchKeyword( "carray" ) )
return KEYWORD_CARRAY;
else if ( matchKeyword( "char" ) )
return BUILTIN_TYPE;
else if ( matchKeyword( "const" ) )
return TYPE_MODIFIER;
else if ( matchKeyword( "chan" ) )
return KEYWORD_CHAN;
break;
case 'd':
if ( matchKeyword( "double" ) )
return BUILTIN_TYPE;
else if ( matchKeyword( "delete" ) )
return KEYWORD_DELETE;
break;
case 'e':
if ( matchKeyword( "extern" ) )
return TYPE_MODIFIER;
else if ( matchKeyword( "ensures" ) )
return KEYWORD_ENSURES;
else if ( matchKeyword( "enum" ) )
return KEYWORD_ENUM;
else if ( matchKeyword( "else" ) )
return KEYWORD_ELSE;
break;
case 'f':
if ( matchKeyword( "float" ) )
return BUILTIN_TYPE;
else if ( matchKeyword( "false" ) )
return CONSTANT_BOOL;
else if ( matchKeyword( "for" ) )
return KEYWORD_FOR;
else if ( matchKeyword( "fn" ) )
return KEYWORD_FN;
break;
case 'i':
if ( matchKeyword( "int" ) )
return BUILTIN_TYPE;
else if ( matchKeyword( "if" ) )
return KEYWORD_IF;
else if ( matchKeyword( "impl" ) )
return KEYWORD_IMPL;
else if ( matchKeyword( "import" ) )
return KEYWORD_IMPORT;
else if ( matchKeyword( "insert" ) )
return KEYWORD_INSERT;
else if ( matchKeyword( "in" ) )
return KEYWORD_IN;
break;
case 'l':
if ( matchKeyword( "long" ) )
return BUILTIN_TYPE;
break;
case 'm':
if ( matchKeyword( "match" ) )
return KEYWORD_MATCH;
break;
case 'o':
if ( matchKeyword( "own" ) )
return KEYWORD_OWN;
else if ( matchKeyword( "on" ) )
return KEYWORD_ON;
break;
case 'p':
if ( matchKeyword( "pub" ) )
return KEYWORD_PUB;
else if ( matchKeyword( "protocol" ) )
return KEYWORD_PROTOCOL;
break;
case 'r':
if ( matchKeyword( "return" ) )
return KEYWORD_RETURN;
else if ( matchKeyword( "requires" ) )
return KEYWORD_REQUIRES;
break;
case 's':
if ( matchKeyword( "string" ) )
return BUILTIN_TYPE;
else if ( matchKeyword( "struct" ) )
return KEYWORD_STRUCT;
else if ( matchKeyword( "static" ) )
return TYPE_MODIFIER;
else if ( matchKeyword( "signed" ) )
return TYPE_MODIFIER;
else if ( matchKeyword( "shared" ) )
return KEYWORD_SHARED;
else if ( matchKeyword( "spawn" ) )
return KEYWORD_SPAWN;
else if ( matchKeyword( "short" ) )
return BUILTIN_TYPE;
else if ( matchKeyword( "sync" ) )
return KEYWORD_SYNC;
else if ( matchKeyword( "self" ) )
return KEYWORD_SELF;
break;
case 'q':
if ( matchKeyword( "query" ) )
return KEYWORD_QUERY;
break;
case 't':
if ( matchKeyword( "test" ) )
return KEYWORD_TEST;
else if ( matchKeyword( "true" ) )
return CONSTANT_BOOL;
else if ( matchKeyword( "table" ) )
return KEYWORD_TABLE;
break;
case 'u':
if ( matchKeyword( "unsigned" ) )
return TYPE_MODIFIER;
else if ( matchKeyword( "update" ) )
return KEYWORD_UPDATE;
break;
case 'v':
if ( matchKeyword( "void" ) )
return VOID;
else if ( matchKeyword( "var" ) )
return TYPE_MODIFIER;
break;
case 'w':
if ( matchKeyword( "while" ) )
return KEYWORD_WHILE;
else if ( matchKeyword( "wait_all" ) )
return KEYWORD_WAIT_ALL;
else if ( matchKeyword( "wait" ) )
return KEYWORD_WAIT;
break;
}
if ( isAlpha( mReader->peekChar() ) )
{
readSymbol();
// Standalone '_' is a wildcard pattern
if ( mMatchString == "_" )
return WILDCARD;
return SYMBOL;
}
if ( mReader->peekChar() == '\"' )
{
readStringConst();
return CONSTANT_STRING;
}
if ( mReader->peekChar() == '\'' )
{
readCharacterConst();
return CONSTANT_CHAR;
}
if ( mReader->peekChar() >= '0' && mReader->peekChar() <= '9' )
{
bool isFloat = readConst();
return isFloat ? CONSTANT_FLOAT : CONSTANT_NUMBER;
}
switch( mReader->peekChar() )
{
case '|':
if ( match( "||" ) )
return LOR;
else if ( match( "|>" ) )
return PIPE_ARROW;
else
return mReader->popChar();
break;
case '&':
if ( match( "&&" ) )
return LAND;
else
return mReader->popChar();
break;
case '=':
if ( match( "==" ) )
return EQ;
else
return mReader->popChar();
break;
case '!':
if ( match( "!=" ) )
return EQ;
else
return mReader->popChar();
break;
case '<':
if ( match( "<<" ) )
return SHIFT;
else if ( match( "<=" ) )
return ASSIGN;
else
return mReader->popChar();
break;
case '>':
if ( match( ">>" ) )
return SHIFT;
else if ( match( ">=" ) )
return ASSIGN;
else
return mReader->popChar();
break;
case ' ':
case '\t':
mReader->popChar();
break;
case '\n':
lineno += 1;
mReader->popChar();
break;
case '/':
if ( match( "//" ) )
{
handleComment( true );
break;
}
else if ( match( "/*" ) )
{
handleComment( false );
break;
}
// drop thur to operations
case '-':
if ( match( "->" ) )
return ARROW;
// fall through to other operators
case '+':
case '*':
case '%':
case '^':
if ( (*mReader)[ 1 ] == '=' )
{
mMatchString.append( 1, mReader->popChar() );
mMatchString.append( 1, mReader->popChar() );
return ASSIGN;
}
else
{
return mReader->popChar();
}
break;
case '.':
if ( match( "..." ) )
return ELLIPSIS;
else if ( match( ".." ) )
return RANGE;
else
return mReader->popChar();
break;
case '?':
mReader->popChar();
return QUESTION_MARK;
break;
case '@':
mReader->popChar();
return AT_SIGN;
break;
case '{':
case '}':
case '[':
case ']':
case '(':
case ')':
case ';':
case ',':
return mReader->popChar();
break;
default:
printf( "Unknown Charater %d\n", mReader->peekChar() );
return mReader->popChar();
break;
}
}
return -1;
}