Lexer.cpp 18 KB
Newer Older
con's avatar
con committed
1
2
3
4
/***************************************************************************
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
7
8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
9
10
11
**
** Non-Open Source Usage
**
con's avatar
con committed
12
13
14
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
15
16
17
18
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
con's avatar
con committed
19
20
21
22
23
24
25
26
27
28
** Alternatively, this file may be used under the terms of the GNU General
** Public License 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.
**
** In addition, as a special exception, Nokia gives you certain additional
29
** rights. These rights are described in the Nokia Qt GPL Exception
30
** version 1.3, included in the file GPL_EXCEPTION.txt in this package.
31
32
**
***************************************************************************/
con's avatar
con committed
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
// Copyright (c) 2008 Roberto Raggi <roberto.raggi@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include "Lexer.h"
#include "Control.h"
#include "TranslationUnit.h"
#include <cctype>
#include <cassert>

CPLUSPLUS_BEGIN_NAMESPACE

Lexer::Lexer(TranslationUnit *unit)
    : _translationUnit(unit),
63
      _state(State_Default),
con's avatar
con committed
64
65
66
67
68
69
70
71
72
73
      _flags(0),
      _currentLine(1)
{
    _scanKeywords = true;
    setSource(_translationUnit->firstSourceChar(),
              _translationUnit->lastSourceChar());
}

Lexer::Lexer(const char *firstChar, const char *lastChar)
    : _translationUnit(0),
74
      _state(State_Default),
con's avatar
con committed
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
      _flags(0),
      _currentLine(1)
{
    _scanKeywords = true;
    setSource(firstChar, lastChar);
}

Lexer::~Lexer()
{ }

TranslationUnit *Lexer::translationUnit() const
{ return _translationUnit; }

Control *Lexer::control() const
{
    if (_translationUnit)
        return _translationUnit->control();

    return 0;
}

void Lexer::setSource(const char *firstChar, const char *lastChar)
{
    _firstChar = firstChar;
    _lastChar = lastChar;
    _currentChar = _firstChar - 1;
    _tokenStart = _currentChar;
    _yychar = '\n';
}

void Lexer::setStartWithNewline(bool enabled)
{
    if (enabled)
        _yychar = '\n';
    else
        _yychar = ' ';
}

int Lexer::state() const
{ return _state; }

void Lexer::setState(int state)
{ _state = state; }

bool Lexer::qtMocRunEnabled() const
{ return _qtMocRunEnabled; }

void Lexer::setQtMocRunEnabled(bool onoff)
{ _qtMocRunEnabled = onoff; }

Roberto Raggi's avatar
Roberto Raggi committed
125
126
bool Lexer::objCEnabled() const
{ return _objCEnabled; }
Roberto Raggi's avatar
Roberto Raggi committed
127

Roberto Raggi's avatar
Roberto Raggi committed
128
129
void Lexer::setObjCEnabled(bool onoff)
{ _objCEnabled = onoff; }
Roberto Raggi's avatar
Roberto Raggi committed
130

con's avatar
con committed
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
bool Lexer::isIncremental() const
{ return _isIncremental; }

void Lexer::setIncremental(bool isIncremental)
{ _isIncremental = isIncremental; }

bool Lexer::scanCommentTokens() const
{ return _scanCommentTokens; }

void Lexer::setScanCommentTokens(bool onoff)
{ _scanCommentTokens = onoff; }

bool Lexer::scanKeywords() const
{ return _scanKeywords; }

void Lexer::setScanKeywords(bool onoff)
{ _scanKeywords = onoff; }

void Lexer::setScanAngleStringLiteralTokens(bool onoff)
{ _scanAngleStringLiteralTokens = onoff; }

void Lexer::pushLineStartOffset()
{
    ++_currentLine;

    if (_translationUnit)
        _translationUnit->pushLineOffset(_currentChar - _firstChar);
}

unsigned Lexer::tokenOffset() const
{ return _tokenStart - _firstChar; }

unsigned Lexer::tokenLength() const
{ return _currentChar - _tokenStart; }

const char *Lexer::tokenBegin() const
{ return _tokenStart; }

const char *Lexer::tokenEnd() const
{ return _currentChar; }

unsigned Lexer::currentLine() const
{ return _currentLine; }

void Lexer::scan(Token *tok)
{
    tok->reset();
    scan_helper(tok);
    tok->length = _currentChar - _tokenStart;
}

void Lexer::scan_helper(Token *tok)
{
  _Lagain:
    while (_yychar && std::isspace(_yychar)) {
        if (_yychar == '\n')
            tok->newline = true;
        else
            tok->whitespace = true;
        yyinp();
    }

    if (! _translationUnit)
        tok->lineno = _currentLine;

    _tokenStart = _currentChar;
    tok->offset = _currentChar - _firstChar;

199
    if (_state == State_MultiLineComment || _state == State_MultiLineDoxyComment) {
200
201
        const int originalState = _state;

con's avatar
con committed
202
203
204
205
206
207
208
209
210
211
212
213
        if (! _yychar) {
            tok->kind = T_EOF_SYMBOL;
            return;
        }

        while (_yychar) {
            if (_yychar != '*')
                yyinp();
            else {
                yyinp();
                if (_yychar == '/') {
                    yyinp();
214
                    _state = State_Default;
con's avatar
con committed
215
216
217
218
219
220
221
222
                    break;
                }
            }
        }

        if (! _scanCommentTokens)
            goto _Lagain;

223
        else if (originalState == State_MultiLineComment)
224
225
226
            tok->kind = T_COMMENT;
        else
            tok->kind = T_DOXY_COMMENT;
con's avatar
con committed
227
228
229
230
231
232
233
234
        return; // done
    }

    if (! _yychar) {
        tok->kind = T_EOF_SYMBOL;
        return;
    }

235
    unsigned char ch = _yychar;
con's avatar
con committed
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
    yyinp();

    switch (ch) {
    case '\\':
        while (_yychar != '\n' && std::isspace(_yychar))
            yyinp();
        // ### assert(! _yychar || _yychar == '\n');
        if (_yychar == '\n') {
            tok->joined = true;
            tok->newline = false;
            yyinp();
        }
        goto _Lagain;

    case '"': case '\'': {
        const char quote = ch;

        tok->kind = quote == '"'
            ? T_STRING_LITERAL
            : T_CHAR_LITERAL;

        const char *yytext = _currentChar;

        while (_yychar && _yychar != quote) {
            if (_yychar != '\\')
                yyinp();
            else {
                yyinp(); // skip `\\'

                if (_yychar)
                    yyinp();
            }
        }
        // assert(_yychar == quote);

        int yylen = _currentChar - yytext;

        if (_yychar == quote)
            yyinp();

        if (control())
            tok->string = control()->findOrInsertStringLiteral(yytext, yylen);
    } break;

    case '{':
        tok->kind = T_LBRACE;
        break;

    case '}':
        tok->kind = T_RBRACE;
        break;

    case '[':
        tok->kind = T_LBRACKET;
        break;

    case ']':
        tok->kind = T_RBRACKET;
        break;

    case '#':
        if (_yychar == '#') {
            tok->kind = T_POUND_POUND;
            yyinp();
        } else {
            tok->kind = T_POUND;
        }
        break;

    case '(':
        tok->kind = T_LPAREN;
        break;

    case ')':
        tok->kind = T_RPAREN;
        break;

    case ';':
        tok->kind = T_SEMICOLON;
        break;

    case ':':
        if (_yychar == ':') {
            yyinp();
            tok->kind = T_COLON_COLON;
        } else {
            tok->kind = T_COLON;
        }
        break;

    case '.':
        if (_yychar == '*') {
            yyinp();
            tok->kind = T_DOT_STAR;
        } else if (_yychar == '.') {
            yyinp();
            // ### assert(_yychar);
            if (_yychar == '.') {
                yyinp();
                tok->kind = T_DOT_DOT_DOT;
            } else {
                tok->kind = T_ERROR;
            }
        } else if (std::isdigit(_yychar)) {
            const char *yytext = _currentChar - 2;
            do {
                if (_yychar == 'e' || _yychar == 'E') {
                    yyinp();
                    if (_yychar == '-' || _yychar == '+') {
                        yyinp();
                        // ### assert(std::isdigit(_yychar));
                    }
                } else if (std::isalnum(_yychar) || _yychar == '.') {
                    yyinp();
                } else {
                    break;
                }
            } while (_yychar);
            int yylen = _currentChar - yytext;
            tok->kind = T_INT_LITERAL;
            if (control())
                tok->number = control()->findOrInsertNumericLiteral(yytext, yylen);
        } else {
            tok->kind = T_DOT;
        }
        break;

    case '?':
        tok->kind = T_QUESTION;
        break;

    case '+':
        if (_yychar == '+') {
            yyinp();
            tok->kind = T_PLUS_PLUS;
        } else if (_yychar == '=') {
            yyinp();
            tok->kind = T_PLUS_EQUAL;
        } else {
            tok->kind = T_PLUS;
        }
        break;

    case '-':
        if (_yychar == '-') {
            yyinp();
            tok->kind = T_MINUS_MINUS;
        } else if (_yychar == '=') {
            yyinp();
            tok->kind = T_MINUS_EQUAL;
        } else if (_yychar == '>') {
            yyinp();
            if (_yychar == '*') {
                yyinp();
                tok->kind = T_ARROW_STAR;
            } else {
                tok->kind = T_ARROW;
            }
        } else {
            tok->kind = T_MINUS;
        }
        break;

    case '*':
        if (_yychar == '=') {
            yyinp();
            tok->kind = T_STAR_EQUAL;
        } else {
            tok->kind = T_STAR;
        }
        break;

    case '/':
        if (_yychar == '/') {
410
411
412
413
414
415
416
417
418
419
420
421
            yyinp();

            bool doxy = false;

            if (_yychar == '/' || _yychar == '!') {
                yyinp();

                if (_yychar != '\n' && std::isspace(_yychar))
                    doxy = true;
            }

            while (_yychar && _yychar != '\n')
con's avatar
con committed
422
                yyinp();
423

con's avatar
con committed
424
425
            if (! _scanCommentTokens)
                goto _Lagain;
426
427
428

            tok->kind = doxy ? T_DOXY_COMMENT : T_COMMENT;

con's avatar
con committed
429
430
        } else if (_yychar == '*') {
            yyinp();
431
432
433

            const bool doxy = _yychar == '*' || _yychar == '!';

con's avatar
con committed
434
435
436
437
438
439
440
441
442
443
444
445
446
            while (_yychar) {
                if (_yychar != '*') {
                    yyinp();
                } else {
                    yyinp();
                    if (_yychar == '/')
                        break;
                }
            }

            if (_yychar)
                yyinp();
            else
447
                _state = doxy ? State_MultiLineDoxyComment : State_MultiLineComment;
con's avatar
con committed
448
449
450

            if (! _scanCommentTokens)
                goto _Lagain;
451
452
453

            tok->kind = doxy ? T_DOXY_COMMENT : T_COMMENT;

con's avatar
con committed
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
        } else if (_yychar == '=') {
            yyinp();
            tok->kind = T_SLASH_EQUAL;
        } else {
            tok->kind = T_SLASH;
        }
        break;

    case '%':
        if (_yychar == '=') {
            yyinp();
            tok->kind = T_PERCENT_EQUAL;
        } else {
            tok->kind = T_PERCENT;
        }
        break;

    case '^':
        if (_yychar == '=') {
            yyinp();
            tok->kind = T_CARET_EQUAL;
        } else {
            tok->kind = T_CARET;
        }
        break;

    case '&':
        if (_yychar == '&') {
            yyinp();
            tok->kind = T_AMPER_AMPER;
        } else if (_yychar == '=') {
            yyinp();
            tok->kind = T_AMPER_EQUAL;
        } else {
            tok->kind = T_AMPER;
        }
        break;

    case '|':
        if (_yychar == '|') {
            yyinp();
            tok->kind = T_PIPE_PIPE;
        } else if (_yychar == '=') {
            yyinp();
            tok->kind = T_PIPE_EQUAL;
        } else {
            tok->kind = T_PIPE;
        }
        break;

    case '~':
        if (_yychar == '=') {
            yyinp();
            tok->kind = T_TILDE_EQUAL;
        } else {
            tok->kind = T_TILDE;
        }
        break;

    case '!':
        if (_yychar == '=') {
            yyinp();
            tok->kind = T_EXCLAIM_EQUAL;
        } else {
            tok->kind = T_EXCLAIM;
        }
        break;

    case '=':
        if (_yychar == '=') {
            yyinp();
            tok->kind = T_EQUAL_EQUAL;
        } else {
            tok->kind = T_EQUAL;
        }
        break;

    case '<':
        if (_scanAngleStringLiteralTokens) {
            const char *yytext = _currentChar;
            while (_yychar && _yychar != '>')
                yyinp();
            int yylen = _currentChar - yytext;
            // ### assert(_yychar == '>');
            if (_yychar == '>')
                yyinp();
            if (control())
                tok->string = control()->findOrInsertStringLiteral(yytext, yylen);
            tok->kind = T_ANGLE_STRING_LITERAL;
        } else if (_yychar == '<') {
            yyinp();
            if (_yychar == '=') {
                yyinp();
                tok->kind = T_LESS_LESS_EQUAL;
            } else
                tok->kind = T_LESS_LESS;
        } else if (_yychar == '=') {
            yyinp();
            tok->kind = T_LESS_EQUAL;
        } else {
            tok->kind = T_LESS;
        }
        break;

    case '>':
        if (_yychar == '>') {
            yyinp();
            if (_yychar == '=') {
                yyinp();
                tok->kind = T_GREATER_GREATER_EQUAL;
            } else
                tok->kind = T_LESS_LESS;
            tok->kind = T_GREATER_GREATER;
        } else if (_yychar == '=') {
            yyinp();
            tok->kind = T_GREATER_EQUAL;
        } else {
            tok->kind = T_GREATER;
        }
        break;

    case ',':
        tok->kind = T_COMMA;
        break;

    default: {
Roberto Raggi's avatar
Roberto Raggi committed
580
        if (_objCEnabled) {
Roberto Raggi's avatar
Roberto Raggi committed
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
            if (ch == '@' && _yychar >= 'a' && _yychar <= 'z') {
                const char *yytext = _currentChar;

                do {
                    yyinp();
                    if (! isalnum(_yychar))
                        break;
                } while (_yychar);

                const int yylen = _currentChar - yytext;
                tok->kind = classifyObjCAtKeyword(yytext, yylen);
                break;
            } else if (ch == '@' && _yychar == '"') {
                // objc @string literals
                ch = _yychar;
                yyinp();
                tok->kind = T_AT_STRING_LITERAL;

                const char *yytext = _currentChar;

                while (_yychar && _yychar != '"') {
                    if (_yychar != '\\')
                        yyinp();
                    else {
                        yyinp(); // skip `\\'

                        if (_yychar)
                            yyinp();
                    }
                }
                // assert(_yychar == '"');

                int yylen = _currentChar - yytext;

                if (_yychar == '"')
                    yyinp();

                if (control())
                    tok->string = control()->findOrInsertStringLiteral(yytext, yylen);

                break;
            }
        }

con's avatar
con committed
625
        if (ch == 'L' && (_yychar == '"' || _yychar == '\'')) {
Roberto Raggi's avatar
Roberto Raggi committed
626
            // wide char/string literals
con's avatar
con committed
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
            ch = _yychar;
            yyinp();

            const char quote = ch;

            tok->kind = quote == '"'
                ? T_WIDE_STRING_LITERAL
                : T_WIDE_CHAR_LITERAL;

            const char *yytext = _currentChar;

            while (_yychar && _yychar != quote) {
                if (_yychar != '\\')
                    yyinp();
                else {
                    yyinp(); // skip `\\'

                    if (_yychar)
                        yyinp();
                }
            }
            // assert(_yychar == quote);

            int yylen = _currentChar - yytext;

            if (_yychar == quote)
                yyinp();

            if (control())
                tok->string = control()->findOrInsertStringLiteral(yytext, yylen);
        } else if (std::isalpha(ch) || ch == '_') {
            const char *yytext = _currentChar - 1;
            while (std::isalnum(_yychar) || _yychar == '_')
                yyinp();
            int yylen = _currentChar - yytext;
            if (_scanKeywords)
                tok->kind = classify(yytext, yylen, _qtMocRunEnabled);
            else
                tok->kind = T_IDENTIFIER;
666
667
668
669
670
671
672

            if (tok->kind == T_IDENTIFIER) {
                tok->kind = classifyOperator(yytext, yylen);

                if (control())
                    tok->identifier = control()->findOrInsertIdentifier(yytext, yylen);
            }
con's avatar
con committed
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
            break;
        } else if (std::isdigit(ch)) {
            const char *yytext = _currentChar - 1;
            while (_yychar) {
                if (_yychar == 'e' || _yychar == 'E') {
                    yyinp();
                    if (_yychar == '-' || _yychar == '+') {
                        yyinp();
                        // ### assert(std::isdigit(_yychar));
                    }
                } else if (std::isalnum(_yychar) || _yychar == '.') {
                    yyinp();
                } else {
                    break;
                }
            }
            int yylen = _currentChar - yytext;
            tok->kind = T_INT_LITERAL;
            if (control())
                tok->number = control()->findOrInsertNumericLiteral(yytext, yylen);
            break;
        } else {
            tok->kind = T_ERROR;
            break;
        }
    } // default

    } // switch
}

CPLUSPLUS_END_NAMESPACE