-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLispLexer.cpp
620 lines (578 loc) · 14.5 KB
/
LispLexer.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
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
/*
* Copyright 2024 Rochus Keller <mailto:[email protected]>
*
* This file is part of the Interlisp project.
*
* The following is the license that applies to this copy of the
* library. For a license to use the library under conditions
* other than those described here, please email to [email protected].
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
// Adopted from the Luon project
#include "LispLexer.h"
#include <QBuffer>
#include <QFile>
#include <QHash>
#include <QFileInfo>
#include <QtDebug>
using namespace Lisp;
static QHash<QByteArray,QByteArray> s_symbols;
bool Token::isValid() const
{
return type != Tok_Eof && type != Tok_Invalid;
}
bool Token::isEof() const
{
return type == Tok_Eof;
}
const char*Token::getName() const
{
switch(type)
{
case Tok_Invalid:
return "Tok_Invalid";
case Tok_Eof:
return "Tok_Eof";
case Tok_lpar:
return "Tok_lpar";
case Tok_rpar:
return "Tok_rpar";
case Tok_lbrack:
return "Tok_lbrack";
case Tok_rbrack:
return "Tok_rbrack";
case Tok_string:
return "Tok_string";
case Tok_atom:
return "Tok_atom";
case Tok_float:
return "Tok_float";
case Tok_integer:
return "Tok_integer";
}
return "???";
}
const char*Token::getString() const
{
return ""; // TODO tokenTypeString(d_type);
}
QByteArray Token::getSymbol(const QByteArray& str)
{
if( str.isEmpty() )
return str;
QByteArray& sym = s_symbols[str];
if( sym.isEmpty() )
sym = str;
return sym;
}
QByteArrayList Token::getAllSymbols()
{
QHash<QByteArray,QByteArray>::const_iterator i;
QByteArrayList res;
for( i = s_symbols.begin(); i != s_symbols.end(); ++i )
res.append( i.key() );
return res;
}
char Lexer::readc()
{
static char last = 0;
char res;
if( in && !in->atEnd() && in->getChar(&res) )
{
if( res == '\r' )
{
char c;
in->getChar(&c);
in->ungetChar(c);
if( c == '\n' )
res = ' ';
else
res = '\n'; // immediatedly convert to \n
}else if( res == 0 || (!isprint(res) && !isspace(res)) )
{
if( last == '%' )
return ' '; // TODO: we need another solution, maybe (CHARACTER res); TODO: sync with Navigator::decode
return readc(); // ignore all clutter
}
if( res == '\n' )
{
pos.row++;
pos.col = 1;
}else
pos.col++;
if( res == 0 )
res = -1;
Q_ASSERT(isspace(res) || isprint(res));
last = res;
return res;
}else
{
if( in && in->atEnd() && in->parent() == this )
{
in->deleteLater();
in = 0;
}
return 0;
}
}
void Lexer::ungetc(char c)
{
if( c == 0 )
return;
Q_ASSERT(isspace(c) || isprint(c));
if( in && c )
{
if( c == '\n' )
c = ' ';
//Q_ASSERT( pos.col != 0 );
if( pos.col != 0 )
pos.col--;
in->ungetChar(c);
}
}
QByteArray Lexer::peek(int count)
{
if( in )
return in->peek(count);
else
return QByteArray();
}
void Lexer::ungetstr(const QByteArray& str)
{
for( int i = str.size() - 1; i >= 0; i-- )
ungetc(str[i]);
}
Lexer::Lexer(QObject* parent):QObject(parent),in(0),emitComments(false),packed(true),inQuote(false)
{
}
void Lexer::setStream(QIODevice* in, const QString& sourcePath)
{
if( in == 0 )
setStream( sourcePath );
else
{
this->in = in;
pos.row = 1;
pos.col = 1;
this->sourcePath = sourcePath;
}
}
void Lexer::setStream(const QByteArray& code, const QString& sourcePath)
{
QBuffer* buf = new QBuffer(this);
buf->setData(code);
buf->open(QIODevice::ReadOnly);
in = buf;
setStream( in, sourcePath );
}
bool Lexer::setStream(const QString& sourcePath)
{
QIODevice* in = 0;
QFile* file = new QFile(sourcePath, this);
if( !file->open(QIODevice::ReadOnly) )
{
delete file;
return false;
}
in = file;
// else
setStream( in, sourcePath );
return true;
}
Token Lexer::nextToken()
{
if( !buffer.isEmpty() )
return buffer.takeLast();
else if( emitComments )
return nextTokenImp();
// else
Token res = nextTokenImp();
while( res.isValid() && res.type == Tok_comment )
res = nextTokenImp();
return res;
}
Token Lexer::readString()
{
QByteArray str;
int extra = 0; // count left and right quote
char c;
while( true )
{
c = readc();
if( c == '%' )
{
extra++;
c = readc(); // escape
}else if( c == '"' )
{
str += c;
break;
}else if( c == 0 )
break;
str += c;
}
return token(Tok_string, str.size() + extra, str);
}
void Lexer::unget(const Token& t)
{
buffer.push_front(t);
}
Token Lexer::nextTokenImp()
{
skipWhiteSpace();
start = pos;
const char c = readc();
if( c == 0 )
return token(Tok_Eof);
else if( isdigit(c) )
{
// this is a number
ungetc(c);
return number();
}else if( c == '+' || c == '-' || c == '.' )
{
const QByteArray la = peek(2);
ungetc(c);
if( !la.isEmpty() && isdigit(la[0]) )
return number();
else if( (c == '+' || c == '-') && la.size() == 2 && la[0] == '.' && isdigit(la[1]) )
return number(); // +/-.
else
return atom();
}else if( c == '"' )
{
// this is a string
if( !packed )
return token(Tok_DblQuote, 1);
ungetc(c);
return string();
}else if( c == '(' || c == '[' || c == ')' || c == ']' )
{
const QByteArray la = peek(1);
if( c == '(' && !la.isEmpty() && la[0] == '*' )
{
if( !packed )
{
readc();
return token(Tok_Lattr, 2);
}
ungetc(c);
return comment();
}
switch( c )
{
case '(':
return token(Tok_lpar,1);
case '[':
return token(Tok_lbrack,1);
case ')':
return token(Tok_rpar,1);
case ']':
return token(Tok_rbrack,1);
}
}else
{
// this is an atom
ungetc(c);
return atom();
}
return Token();
}
QList<Token> Lexer::tokens(const QString& code)
{
return tokens( code.toLatin1() );
}
QList<Token> Lexer::tokens(const QByteArray& code, const QString& path)
{
QBuffer in;
in.setData( code );
in.open(QIODevice::ReadOnly);
setStream( &in, path );
QList<Token> res;
Token t = nextToken();
while( t.isValid() )
{
res << t;
t = nextToken();
}
return res;
}
void Lexer::startQuote()
{
inQuote = true;
}
void Lexer::endQuote()
{
inQuote = false;
}
Token Lexer::token(TokenType tt, int len, const QByteArray& val)
{
Token t( tt, start, len, val );
t.sourcePath = sourcePath;
return t;
}
static inline int digit(char c)
{
if( c >= '0' && c <= '9' )
return c - '0';
else
return -1;
}
bool Lexer::atom_delimiter(char c)
{
return isspace(c) ||
c == '(' || c == ')' || c == '[' || c == ']' || c == '"';
}
Token Lexer::number()
{
enum Status { idle, dec_seq, dec_or_oct_seq, fraction, exponent, exponent2 } status = idle;
char c = readc();
QByteArray number;
number += c;
if( c == '+' || c == '-' )
{
status = dec_or_oct_seq;
c = readc();
number += c;
}else if( c == '.' )
{
status = fraction;
c = readc();
number += c;
}
while(true)
{
const int d = digit(c);
switch(status)
{
case idle:
if( d >= 0 && d <= 7 )
status = dec_or_oct_seq;
else if( d == 8 || d == 9 )
status = dec_seq;
else
Q_ASSERT(false);
break;
case dec_or_oct_seq:
case dec_seq:
if( c == 'Q' )
{
if( status == dec_seq )
return token(Tok_Invalid, number.size(), "invalid decimal number");
else
{
// octal number found
bool ok;
number.left(number.size()-1).toLongLong(&ok, 8);
if( !ok )
return token(Tok_Invalid, number.size(), "invalid octal number");
else
return token(Tok_integer, number.size(), number);
}
}else if( c == '.' )
status = fraction;
else if( c == 'E' )
status = exponent;
else if( atom_delimiter(c) )
{
// not a digit and not an atom, break here
number.chop(1);
ungetc(c);
return token(Tok_integer, number.size(), number);
}else if( !isdigit(c) )
{
ungetstr(number);
return atom();
}
// else: it's a digit
break;
case fraction:
if( c == 'E' )
status = exponent;
else if( atom_delimiter(c) )
{
// not a digit and not and atom, break here
number.chop(1);
ungetc(c);
bool ok;
number.toDouble(&ok);
if( !ok )
return token(Tok_Invalid, number.size(), "invalid float");
else
return token(Tok_float, number.size(), number);
}else if( !isdigit(c) )
{
ungetstr(number);
return atom();
}
// else: it's a digit
break;
case exponent:
if( c == '+' || c == '-' || d >= 0 )
status = exponent2;
else
return token(Tok_Invalid, number.size(), "invalid exponent");
break;
case exponent2:
if( atom_delimiter(c) )
{
// not a digit and not an atom, break here
number.chop(1);
ungetc(c);
bool ok;
number.toDouble(&ok);
if( !ok )
return token(Tok_Invalid, number.size(), "invalid float");
else
return token(Tok_float, number.size(), number);
}else if( !isdigit(c) )
{
ungetstr(number);
return atom();
}
// else: it's a digit
break;
default:
Q_ASSERT(false);
}
c = readc();
number += c;
}
Q_ASSERT(false);
return Token();
}
Token Lexer::atom()
{
QByteArray a;
int extra = 0;
while( true )
{
char c = readc();
if( !inQuote && c == '%' )
{
extra++;
c = readc(); // escape
}else if( c == 0 || atom_delimiter(c) || !isprint(c) )
{
// atom can be terminated by 0x06 0x01, thus isprint(c)
ungetc(c);
break;
}
a += c;
}
a = Token::getSymbol(a);
return token(Tok_atom, a.size() + extra, a);
}
Token Lexer::string()
{
char c = readc();
QByteArray str;
str += c;
int extra = 0; // count left and right quote
while( true )
{
c = readc();
if( c == '%' ) // QUOTE has no influence here, see MACHINEINDEPENDENT line 1327
{
extra++;
c = readc(); // escape
}else if( c == '"' )
{
str += c;
break;
}else if( c == 0 )
break;
str += c;
}
if( packed && c != '"' )
return token(Tok_Invalid, str.size() + extra, "unterminated string" );
return token(Tok_string, str.size() + extra, str);
}
Token Lexer::comment()
{
QByteArray str;
// first eat (*
char c = readc();
str += c;
c = readc();
str += c;
// now find end of comment considering included lists and strings
int level = 0;
bool inString = false;
QList<int> brackets; // bracket at level
int extra = 0;
while( true )
{
c = readc();
if( c == 0 )
break;
if( c == '%' )
{
extra++;
c = readc();
}else if( c == '"' )
{
inString = !inString;
}else if( !inString && c == '(' )
level++;
else if( !inString && c == '[' )
{
brackets << level;
level++;
}else if( !inString && c == ']' )
{
if( brackets.isEmpty() )
{
// the string is terminated by ], and we pass it back so that it
// can continue to terminate until [
ungetc(c);
c = ')';
str += c;
break;
}
level = brackets.takeLast();
}else if( !inString && c == ')' )
{
if( level == 0 )
{
if( !brackets.isEmpty() )
return token(Tok_Invalid, str.size() + extra, "unterminated bracket in comment");
str += c;
break;
}
level--;
}
str += c;
}
if( packed && c != ')')
return token(Tok_Invalid, str.size() + extra, "unterminated comment" );
return token(Tok_comment, str.size() + extra, str);
}
void Lexer::skipWhiteSpace()
{
char c = readc();
if( c == 0 )
return;
while( true )
{
while( isspace(c) || !isprint(c) )
// seen in Fugue-2: c == 0x06 || c == 0x1e || c == 12 || c == 1 || c == 27 || c == 127 || c == -32 )
{
if( c == 0x06 )
readc(); // skip next
c = readc();
if( c == 0 )
return;
}
break;
}
ungetc(c);
}