-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpythonlexer.cpp
More file actions
651 lines (631 loc) · 23.5 KB
/
Copy pathpythonlexer.cpp
File metadata and controls
651 lines (631 loc) · 23.5 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
644
645
646
647
648
649
650
651
/* KDevelop QMake Support
*
* Copyright 2007 Andreas Pakulat <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "pythonlexer.h"
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QRegExp>
#include <QtCore/QDebug>
#include "pythonparser.h"
#include <kdev-pg-location-table.h>
#include <kdev-pg-token-stream.h>
#include "kwcheck.h"
#include "numbercheck.h"
namespace PythonParser
{
/*
* @TODO: COMMENT all the stuff, really needed
*/
Lexer::Lexer( Parser* _parser, const QString& content ):
m_content( content ), m_parser( _parser ),
m_curpos( 0 ), m_contentSize( m_content.size() ),
m_tokenBegin( 0 ), m_tokenEnd( 0 ), m_openParenNum( 0 )
{
pushState( ErrorState );
pushState( DefaultState );
pushState( IndentState );
pushIndentation( 0 );
}
int Lexer::state() const
{
return m_state.top();
}
void Lexer::pushState( int state )
{
m_state.push( state );
}
void Lexer::popState()
{
m_state.pop();
}
int Lexer::indentation() const
{
return m_indentation.top();
}
void Lexer::pushIndentation( int indentation )
{
m_indentation.push( indentation );
}
void Lexer::popIndentation()
{
m_indentation.pop();
}
int Lexer::nextTokenKind()
{
kDebug() << "nextTokenKind called";
int token = Parser::Token_INVALID;
if ( m_curpos >= m_contentSize )
{
if( indentation() > 0 )
{
popIndentation();
m_tokenEnd = m_curpos;
return Parser::Token_DEDENT;
}
m_tokenBegin = -1;
m_tokenEnd = -1;
return 0;
}
QChar* it = m_content.data();
it += m_curpos;
if( state() == IndentState )
{
// Check wether we need to indent or dedent, this is quite complicated.
// Especially because it contains 3 exit points and one point that resets
// the current position and it and then lets the rest of the code run
if( !it->isSpace() && it->unicode() != '#' && indentation() > 0 )
{
// No whitespace at the start of the line, so we need to create
// as many DEDENT tokens as we have indenations in the stack
token = Parser::Token_DEDENT;
m_tokenBegin = m_curpos;
m_tokenEnd = m_curpos;
popIndentation();
return token;
}else if( it->isSpace() )
{
// We've got indentation, lets see how much
m_tokenBegin = m_curpos;
int spacecount = 0;
while( it->isSpace() && it->unicode() != '\n' && m_curpos < m_contentSize )
{
if( it->unicode() == '\t' )
{
spacecount += 8-( spacecount % 8 );
} else if( it->unicode() != '\f' && it->unicode() != '\n' )
{
spacecount++;
}else if( it->unicode() == '#' )
{
// find the next newline position and return a linebreak token for it
do
{
it++;
m_curpos++;
}while( it->unicode() != '\n' && m_curpos < m_contentSize );
m_tokenBegin = m_curpos;
m_tokenEnd = m_curpos;
createNewline( m_curpos );
m_curpos++;
return Parser::Token_LINEBREAK;
}
it++;
m_curpos++;
}
if( it->unicode() == '#' )
{
// Ooops, whitespace and then a #, this is a plain comment line, only create
// a newline token for this
do
{
it++;
m_curpos++;
}while( it->unicode() != '\n' && m_curpos < m_contentSize );
m_tokenBegin = m_curpos;
m_tokenEnd = m_curpos;
createNewline( m_curpos );
m_curpos++;
return Parser::Token_LINEBREAK;
}else if( it->unicode() == '\n' )
{
m_tokenBegin = m_curpos;
m_tokenEnd = m_curpos;
createNewline( m_curpos );
m_curpos++;
return Parser::Token_LINEBREAK;
}
it--;
m_tokenEnd = m_curpos-1;
if( spacecount > indentation() )
{
// We have more indentation, so we need to create an INDENT token
pushIndentation( spacecount );
popState();
token = Parser::Token_INDENT;
return token;
}else if( spacecount == indentation() )
{
// Don't do anything, same indentation level so we ignore the whitespace and move on with lexing the forthcoming text
popState();
m_curpos = m_tokenBegin;
it = m_content.data() + m_curpos;
}else
{
// We've got a dedentation, so create a DEDENT token
// If the next indentation level is still larger than what we've
// counted we'll do another try on the next call, else we'll go into
// usual parsing next time we enter this function
popIndentation();
token = Parser::Token_DEDENT;
if( spacecount < indentation() )
{
m_curpos = m_tokenBegin;
}else
{
popState();
}
return token;
}
}else
{
// Either we have a comment, which will be ignored in the next part
// or we have a non-whitespace character and no more indentation
// level to create a DEDENT token for. Thus get out of IndentState
// and do normal parsing
popState();
}
}
switch ( state() )
{
case DefaultState:
it = ignoreWhitespaceAndComments( it );
if( it->unicode() == '\\' && (it+1)->unicode() == '\n' )
{
createNewline(m_curpos+1);
m_curpos += 2;
it += 2;
}
m_tokenBegin = m_curpos;
if( isStringStart( it ) )
{
if( it->toLower().unicode() == 'u' )
{
it++;
m_curpos++;
}
if( it->toLower().unicode() == 'r' )
{
it++;
m_curpos++;
}
QChar* quotestart = it++;
m_curpos++;
if( it->unicode() == quotestart->unicode() && (it+1)->unicode() == quotestart->unicode() )
{
it += 2;
m_curpos += 2;
// read everything until we find 3 consecutive chars that are
// equal to quotestart
while( m_curpos < m_contentSize - 3 && !( it->unicode() == quotestart->unicode()
&& (it+1)->unicode() == quotestart->unicode()
&& (it+2)->unicode() == quotestart->unicode()) )
{
if( it->unicode() == '\\' )
{
it += 2;
m_curpos += 2;
}else
{
it++;
m_curpos++;
}
}
// We checked these 2 characters and they're either the last 2 chars
// or they're the closing '' or "" and thus we always consume them too
it += 2;
m_curpos += 2;
token = Parser::Token_STRINGLITERAL;
}else
{
// single quote so read until the end of line or closing quote
// if eol is found return invalid token
while( it->unicode() != quotestart->unicode() && it->unicode() != '\n' && m_curpos < m_contentSize )
{
if( it->unicode() == '\\' )
{
it += 2;
m_curpos += 2;
}else
{
it++;
m_curpos++;
}
}
if( it->unicode() == '\n' )
{
// We've read to the newline, now return to the last string character
m_curpos--;
}
token = Parser::Token_STRINGLITERAL;
}
//Go to the next character, it still points to the end of the literal
it++;
if( it->isSpace() )
{
// read more space until we find a non-space character
// if the non-space char is a quote again set the current position
// to the char before this non-space character, so next time
// we lex the next string. This allows for string concatenation
QChar* space = it;
int count = 0;
while( space->isSpace() && (m_curpos+count) < m_contentSize )
{
space++;
count++;
}
// Now check wether this is a string begin, if so we immediately
// return the token and set its endPos, we also set the parsing
// position to the next quote and ignore the newline that might exist
// between the strings
if( space->unicode() == '\'' || space->unicode() == '"' )
{
m_tokenEnd = m_curpos;
m_curpos += count;
return token;
}
}
}else if( it->isLetter() || it->unicode() == '_' )
{
QChar* start = it;
do{
it++;
m_curpos++;
}while( m_curpos < m_contentSize
&& ( it->isLetterOrNumber() || it->unicode() == '_' ) );
//Adjust current position to the last character that belongs to the identifier/keyword
m_curpos--;
token = checkForKeyword( start, m_curpos-m_tokenBegin+1 );
}else if( it->isNumber() || ( it->unicode() == '.' && (it+1)->isNumber() ) )
{
// all numeric literals start with a number or a . followed by a number
do{
it++;
m_curpos++;
}while( m_curpos < m_contentSize
&& ( it->isNumber() || it->unicode() == 'e'
|| it->unicode() == 'E' || it->unicode() == 'j'
|| it->unicode() == 'J' || it->unicode() == '.'
|| it->unicode() == 'A' || it->unicode() == 'B'
|| it->unicode() == 'C' || it->unicode() == 'D'
|| it->unicode() == 'F' || it->unicode() == 'a'
|| it->unicode() == 'b' || it->unicode() == 'c'
|| it->unicode() == 'd' || it->unicode() == 'f'
|| it->unicode() == 'l' || it->unicode() == 'L'
|| it->unicode() == 'X' || it->unicode() == 'x' ) );
//Adjust position to last character that belongs to the number
m_curpos--;
token = getTokenForNumberString( m_content.mid( m_tokenBegin, m_curpos-m_tokenBegin+1 ) );
}else
{
QChar* ch2 = m_curpos < m_contentSize ? it + 1 : 0;
QChar* ch3 = m_curpos < m_contentSize ? it + 2 : 0;
kDebug() << "it: " << it->toAscii();
switch ( it->unicode() )
{
case '\n':
pushState( IndentState );
createNewline( m_curpos );
token = Parser::Token_LINEBREAK;
break;
case '(':
m_openParenNum++;
token = Parser::Token_LPAREN;
break;
case ')':
m_openParenNum--;
token = Parser::Token_RPAREN;
break;
case '{':
m_openParenNum++;
token = Parser::Token_LBRACE;
break;
case '}':
m_openParenNum--;
token = Parser::Token_RBRACE;
break;
case '[':
m_openParenNum++;
token = Parser::Token_LBRACKET;
break;
case ']':
m_openParenNum--;
token = Parser::Token_RBRACKET;
break;
case ',':
token = Parser::Token_COMMA;
break;
case ';':
token = Parser::Token_SEMICOLON;
break;
case ':':
token = Parser::Token_COLON;
break;
case '.':
if( ch2 && ch3 && ch2->unicode() == '.' && ch3->unicode() == '.' )
{
m_curpos += 2;
token = Parser::Token_ELLIPSIS;
}else
{
token = Parser::Token_DOT;
}
break;
case '`':
token = Parser::Token_BACKTICK;
break;
case '@':
token = Parser::Token_AT;
break;
case '*':
if( ch2 && ch3 && ch2->unicode() == '*' && ch3->unicode() == '=')
{
m_curpos += 2;
token = Parser::Token_DOUBLESTAREQ;
}else if( ch2 && ch2->unicode() == '*' )
{
m_curpos++;
token = Parser::Token_DOUBLESTAR;
}else if( ch2 && ch2->unicode() == '=')
{
m_curpos++;
token = Parser::Token_STAREQ;
}else
{
token = Parser::Token_STAR;
}
break;
case '=':
if( ch2 && ch2->unicode() == '=' )
{
m_curpos++;
token = Parser::Token_ISEQUAL;
}else
{
token = Parser::Token_EQUAL;
}
break;
case '+':
if( ch2 && ch2->unicode() == '=' )
{
m_curpos++;
token = Parser::Token_PLUSEQ;
}else
{
token = Parser::Token_PLUS;
}
break;
case '-':
if( ch2 && ch2->unicode() == '=' )
{
m_curpos++;
token = Parser::Token_MINUSEQ;
}else
{
token = Parser::Token_MINUS;
}
break;
case '~':
if( ch2 && ch2->unicode() == '=' )
{
m_curpos++;
token = Parser::Token_TILDEEQ;
}else
{
token = Parser::Token_TILDE;
}
break;
case '/':
if( ch2 && ch2->unicode() == '=' )
{
m_curpos++;
token = Parser::Token_SLASHEQ;
}else if( ch2 && ch3 && ch2->unicode() == '/' && ch3->unicode() == '=' )
{
m_curpos += 2;
token = Parser::Token_DOUBLESLASHEQ;
}else if( ch2 && ch2->unicode() == '/' )
{
m_curpos++;
token = Parser::Token_DOUBLESLASH;
}else
{
token = Parser::Token_SLASH;
}
break;
case '%':
if( ch2 && ch2->unicode() == '=' )
{
m_curpos++;
token = Parser::Token_MODULOEQ;
}else
{
token = Parser::Token_MODULO;
}
break;
case '&':
if( ch2 && ch2->unicode() == '=' )
{
m_curpos++;
token = Parser::Token_ANDEQ;
}else
{
token = Parser::Token_BITAND;
}
break;
case '<':
if( ch2 && ch2->unicode() == '=' )
{
m_curpos++;
token = Parser::Token_LESSEQ;
}else if( ch2 && ch3 && ch2->unicode() == '<' && ch3->unicode() == '=' )
{
m_curpos += 2;
token = Parser::Token_LSHIFTEQ;
}else if( ch2 && ch2->unicode() == '<' )
{
m_curpos++;
token = Parser::Token_LSHIFT;
}else if( ch2 && ch2->unicode() == '>' )
{
m_curpos++;
token = Parser::Token_UNEQUAL;
}else
{
token = Parser::Token_LESS;
}
break;
case '>':
if( ch2 && ch2->unicode() == '=' )
{
m_curpos++;
token = Parser::Token_GREATEREQ;
}else if( ch2 && ch3 && ch2->unicode() == '>' && ch3->unicode() == '=' )
{
m_curpos += 2;
token = Parser::Token_RSHIFTEQ;
}else if( ch2 && ch2->unicode() == '>' )
{
m_curpos++;
token = Parser::Token_RSHIFT;
}else
{
token = Parser::Token_GREATER;
}
break;
case '|':
if( ch2 && ch2->unicode() == '=' )
{
m_curpos++;
token = Parser::Token_OREQ;
}else
{
token = Parser::Token_BITOR;
};
break;
case '^':
token = Parser::Token_BITXOR;
break;
case '!':
if( ch2 && ch2->unicode() == '=' )
{
m_curpos++;
token = Parser::Token_UNEQUAL;
}
break;
default:
break;
}
}
m_tokenEnd = m_curpos;
break;
default:
token = Parser::Token_INVALID;
break;
}
if ( m_curpos >= m_contentSize )
{
if( indentation() > 0 )
{
popIndentation();
m_tokenEnd = m_curpos;
return Parser::Token_DEDENT;
}
m_tokenBegin = -1;
m_tokenEnd = -1;
return 0;
}
m_tokenEnd = m_curpos;
m_curpos++;
return token;
}
qint64 Lexer::tokenBegin() const
{
return m_tokenBegin;
}
qint64 Lexer::tokenEnd() const
{
return m_tokenEnd;
}
QChar* Lexer::ignoreWhitespaceAndComments( QChar* it )
{
// Ignore whitespace and comments, but preserve the newline if we're not inside a parenthesis
bool isComment = false;
while ( m_curpos < m_contentSize
&& ( it->isSpace() || isComment || it->unicode() == '#' ||
( it->unicode() == '\\' && m_openParenNum && (it+1)->unicode() == '\n' )
)
&& ( it->unicode() != '\n' || m_openParenNum > 0 ) )
{
if( it->unicode() == '#' )
{
isComment = true;
}else if( it->unicode() == '\n' && m_openParenNum > 0 )
{
isComment = false;
createNewline( m_curpos );
}
++it;
++m_curpos;
}
return it;
}
void Lexer::createNewline( int curpos )
{
if( m_parser )
m_parser->tokenStream->locationTable()->newline( curpos );
}
bool Lexer::isStringStart( QChar* it )
{
if( it->unicode() == '\'' || it->unicode() == '"' )
{
return true;
}
if( it->toLower().unicode() == 'u' && m_curpos < m_contentSize - 1 )
{
if( (it+1)->unicode() == '\'' || (it+1)->unicode() == '"' )
{
return true;
}else if( (it+1)->toLower().unicode() == 'r' && m_curpos < m_contentSize - 2 )
{
if( (it+2)->unicode() == '\'' || (it+2)->unicode() == '"' )
{
return true;
}
}
}
if( it->toLower().unicode() == 'r' && m_curpos < m_contentSize - 1 )
{
if( (it+1)->unicode() == '\'' || (it+1)->unicode() == '"' )
{
return true;
}
}
return false;
}
}