Lexer.cpp 17.5 KB
Newer Older
con's avatar
con committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// 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"
24
#include "Literals.h"
con's avatar
con committed
25 26 27
#include <cctype>
#include <cassert>

Roberto Raggi's avatar
Roberto Raggi committed
28
using namespace CPlusPlus;
con's avatar
con committed
29 30 31

Lexer::Lexer(TranslationUnit *unit)
    : _translationUnit(unit),
32
      _state(State_Default),
con's avatar
con committed
33 34 35
      _flags(0),
      _currentLine(1)
{
36
    f._scanKeywords = true;
con's avatar
con committed
37 38 39 40 41 42
    setSource(_translationUnit->firstSourceChar(),
              _translationUnit->lastSourceChar());
}

Lexer::Lexer(const char *firstChar, const char *lastChar)
    : _translationUnit(0),
43
      _state(State_Default),
con's avatar
con committed
44 45 46
      _flags(0),
      _currentLine(1)
{
47
    f._scanKeywords = true;
con's avatar
con committed
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
    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
89
{ return f._qtMocRunEnabled; }
con's avatar
con committed
90 91

void Lexer::setQtMocRunEnabled(bool onoff)
92
{ f._qtMocRunEnabled = onoff; }
con's avatar
con committed
93

94 95 96 97 98 99
bool Lexer::cxx0xEnabled() const
{ return f._cxx0xEnabled; }

void Lexer::setCxxOxEnabled(bool onoff)
{ f._cxx0xEnabled = onoff; }

Roberto Raggi's avatar
Roberto Raggi committed
100
bool Lexer::objCEnabled() const
101
{ return f._objCEnabled; }
Roberto Raggi's avatar
Roberto Raggi committed
102

Roberto Raggi's avatar
Roberto Raggi committed
103
void Lexer::setObjCEnabled(bool onoff)
104
{ f._objCEnabled = onoff; }
Roberto Raggi's avatar
Roberto Raggi committed
105

con's avatar
con committed
106
bool Lexer::isIncremental() const
107
{ return f._isIncremental; }
con's avatar
con committed
108 109

void Lexer::setIncremental(bool isIncremental)
110
{ f._isIncremental = isIncremental; }
con's avatar
con committed
111 112

bool Lexer::scanCommentTokens() const
113
{ return f._scanCommentTokens; }
con's avatar
con committed
114 115

void Lexer::setScanCommentTokens(bool onoff)
116
{ f._scanCommentTokens = onoff; }
con's avatar
con committed
117 118

bool Lexer::scanKeywords() const
119
{ return f._scanKeywords; }
con's avatar
con committed
120 121

void Lexer::setScanKeywords(bool onoff)
122
{ f._scanKeywords = onoff; }
con's avatar
con committed
123 124

void Lexer::setScanAngleStringLiteralTokens(bool onoff)
125
{ f._scanAngleStringLiteralTokens = onoff; }
con's avatar
con committed
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

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);
154
    tok->f.length = _currentChar - _tokenStart;
con's avatar
con committed
155 156 157 158 159 160
}

void Lexer::scan_helper(Token *tok)
{
  _Lagain:
    while (_yychar && std::isspace(_yychar)) {
Erik Verbruggen's avatar
Erik Verbruggen committed
161 162
        if (_yychar == '\n') {
            tok->f.joined = false;
163
            tok->f.newline = true;
Erik Verbruggen's avatar
Erik Verbruggen committed
164
        } else {
165
            tok->f.whitespace = true;
Erik Verbruggen's avatar
Erik Verbruggen committed
166
        }
con's avatar
con committed
167 168 169 170 171 172
        yyinp();
    }

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

173 174 175
    _tokenStart = _currentChar;
    tok->offset = _currentChar - _firstChar;

176
    if (_state == State_MultiLineComment || _state == State_MultiLineDoxyComment) {
177 178
        const int originalState = _state;

con's avatar
con committed
179
        if (! _yychar) {
180
            tok->f.kind = T_EOF_SYMBOL;
con's avatar
con committed
181 182 183 184 185 186 187 188 189 190
            return;
        }

        while (_yychar) {
            if (_yychar != '*')
                yyinp();
            else {
                yyinp();
                if (_yychar == '/') {
                    yyinp();
191
                    _state = State_Default;
con's avatar
con committed
192 193 194 195 196
                    break;
                }
            }
        }

197
        if (! f._scanCommentTokens)
con's avatar
con committed
198 199
            goto _Lagain;

200
        else if (originalState == State_MultiLineComment)
201
            tok->f.kind = T_COMMENT;
202
        else
203
            tok->f.kind = T_DOXY_COMMENT;
con's avatar
con committed
204 205 206 207
        return; // done
    }

    if (! _yychar) {
208
        tok->f.kind = T_EOF_SYMBOL;
con's avatar
con committed
209 210 211
        return;
    }

212
    unsigned char ch = _yychar;
con's avatar
con committed
213 214 215 216 217 218 219 220
    yyinp();

    switch (ch) {
    case '\\':
        while (_yychar != '\n' && std::isspace(_yychar))
            yyinp();
        // ### assert(! _yychar || _yychar == '\n');
        if (_yychar == '\n') {
221 222
            tok->f.joined = true;
            tok->f.newline = false;
con's avatar
con committed
223 224 225 226 227 228 229
            yyinp();
        }
        goto _Lagain;

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

230
        tok->f.kind = quote == '"'
con's avatar
con committed
231 232 233 234 235 236
            ? T_STRING_LITERAL
            : T_CHAR_LITERAL;

        const char *yytext = _currentChar;

        while (_yychar && _yychar != quote) {
237 238 239
            if (_yychar == '\n')
                break;
            else if (_yychar != '\\')
con's avatar
con committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
                yyinp();
            else {
                yyinp(); // skip `\\'

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

        int yylen = _currentChar - yytext;

        if (_yychar == quote)
            yyinp();

        if (control())
256
            tok->string = control()->stringLiteral(yytext, yylen);
con's avatar
con committed
257 258 259
    } break;

    case '{':
260
        tok->f.kind = T_LBRACE;
con's avatar
con committed
261 262 263
        break;

    case '}':
264
        tok->f.kind = T_RBRACE;
con's avatar
con committed
265 266 267
        break;

    case '[':
268
        tok->f.kind = T_LBRACKET;
con's avatar
con committed
269 270 271
        break;

    case ']':
272
        tok->f.kind = T_RBRACKET;
con's avatar
con committed
273 274 275 276
        break;

    case '#':
        if (_yychar == '#') {
277
            tok->f.kind = T_POUND_POUND;
con's avatar
con committed
278 279
            yyinp();
        } else {
280
            tok->f.kind = T_POUND;
con's avatar
con committed
281 282 283 284
        }
        break;

    case '(':
285
        tok->f.kind = T_LPAREN;
con's avatar
con committed
286 287 288
        break;

    case ')':
289
        tok->f.kind = T_RPAREN;
con's avatar
con committed
290 291 292
        break;

    case ';':
293
        tok->f.kind = T_SEMICOLON;
con's avatar
con committed
294 295 296 297 298
        break;

    case ':':
        if (_yychar == ':') {
            yyinp();
299
            tok->f.kind = T_COLON_COLON;
con's avatar
con committed
300
        } else {
301
            tok->f.kind = T_COLON;
con's avatar
con committed
302 303 304 305 306 307
        }
        break;

    case '.':
        if (_yychar == '*') {
            yyinp();
308
            tok->f.kind = T_DOT_STAR;
con's avatar
con committed
309 310 311 312 313
        } else if (_yychar == '.') {
            yyinp();
            // ### assert(_yychar);
            if (_yychar == '.') {
                yyinp();
314
                tok->f.kind = T_DOT_DOT_DOT;
con's avatar
con committed
315
            } else {
316
                tok->f.kind = T_ERROR;
con's avatar
con committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
            }
        } 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;
334
            tok->f.kind = T_NUMERIC_LITERAL;
con's avatar
con committed
335
            if (control())
336
                tok->number = control()->numericLiteral(yytext, yylen);
con's avatar
con committed
337
        } else {
338
            tok->f.kind = T_DOT;
con's avatar
con committed
339 340 341 342
        }
        break;

    case '?':
343
        tok->f.kind = T_QUESTION;
con's avatar
con committed
344 345 346 347 348
        break;

    case '+':
        if (_yychar == '+') {
            yyinp();
349
            tok->f.kind = T_PLUS_PLUS;
con's avatar
con committed
350 351
        } else if (_yychar == '=') {
            yyinp();
352
            tok->f.kind = T_PLUS_EQUAL;
con's avatar
con committed
353
        } else {
354
            tok->f.kind = T_PLUS;
con's avatar
con committed
355 356 357 358 359 360
        }
        break;

    case '-':
        if (_yychar == '-') {
            yyinp();
361
            tok->f.kind = T_MINUS_MINUS;
con's avatar
con committed
362 363
        } else if (_yychar == '=') {
            yyinp();
364
            tok->f.kind = T_MINUS_EQUAL;
con's avatar
con committed
365 366 367 368
        } else if (_yychar == '>') {
            yyinp();
            if (_yychar == '*') {
                yyinp();
369
                tok->f.kind = T_ARROW_STAR;
con's avatar
con committed
370
            } else {
371
                tok->f.kind = T_ARROW;
con's avatar
con committed
372 373
            }
        } else {
374
            tok->f.kind = T_MINUS;
con's avatar
con committed
375 376 377 378 379 380
        }
        break;

    case '*':
        if (_yychar == '=') {
            yyinp();
381
            tok->f.kind = T_STAR_EQUAL;
con's avatar
con committed
382
        } else {
383
            tok->f.kind = T_STAR;
con's avatar
con committed
384 385 386 387 388
        }
        break;

    case '/':
        if (_yychar == '/') {
389 390 391 392 393 394 395
            yyinp();

            bool doxy = false;

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

396 397 398
                if (_yychar == '<')
                    yyinp();

399 400 401 402 403
                if (_yychar != '\n' && std::isspace(_yychar))
                    doxy = true;
            }

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

406
            if (! f._scanCommentTokens)
con's avatar
con committed
407
                goto _Lagain;
408

409
            tok->f.kind = doxy ? T_CPP_DOXY_COMMENT : T_CPP_COMMENT;
410

con's avatar
con committed
411 412
        } else if (_yychar == '*') {
            yyinp();
413

414 415 416
            bool doxy = false;

            if (_yychar == '*' || _yychar == '!') {
Roberto Raggi's avatar
Roberto Raggi committed
417 418
                const char ch = _yychar;

419 420
                yyinp();

Roberto Raggi's avatar
Roberto Raggi committed
421 422 423
                if (ch == '*' && _yychar == '/')
                    goto _Ldone;

424 425 426
                if (_yychar == '<')
                    yyinp();

427 428 429
                if (! _yychar || std::isspace(_yychar))
                    doxy = true;
            }
430

con's avatar
con committed
431 432 433 434 435 436 437 438 439 440
            while (_yychar) {
                if (_yychar != '*') {
                    yyinp();
                } else {
                    yyinp();
                    if (_yychar == '/')
                        break;
                }
            }

Roberto Raggi's avatar
Roberto Raggi committed
441
        _Ldone:
con's avatar
con committed
442 443 444
            if (_yychar)
                yyinp();
            else
445
                _state = doxy ? State_MultiLineDoxyComment : State_MultiLineComment;
con's avatar
con committed
446

447
            if (! f._scanCommentTokens)
con's avatar
con committed
448
                goto _Lagain;
449

450
            tok->f.kind = doxy ? T_DOXY_COMMENT : T_COMMENT;
451

con's avatar
con committed
452 453
        } else if (_yychar == '=') {
            yyinp();
454
            tok->f.kind = T_SLASH_EQUAL;
con's avatar
con committed
455
        } else {
456
            tok->f.kind = T_SLASH;
con's avatar
con committed
457 458 459 460 461 462
        }
        break;

    case '%':
        if (_yychar == '=') {
            yyinp();
463
            tok->f.kind = T_PERCENT_EQUAL;
con's avatar
con committed
464
        } else {
465
            tok->f.kind = T_PERCENT;
con's avatar
con committed
466 467 468 469 470 471
        }
        break;

    case '^':
        if (_yychar == '=') {
            yyinp();
472
            tok->f.kind = T_CARET_EQUAL;
con's avatar
con committed
473
        } else {
474
            tok->f.kind = T_CARET;
con's avatar
con committed
475 476 477 478 479 480
        }
        break;

    case '&':
        if (_yychar == '&') {
            yyinp();
481
            tok->f.kind = T_AMPER_AMPER;
con's avatar
con committed
482 483
        } else if (_yychar == '=') {
            yyinp();
484
            tok->f.kind = T_AMPER_EQUAL;
con's avatar
con committed
485
        } else {
486
            tok->f.kind = T_AMPER;
con's avatar
con committed
487 488 489 490 491 492
        }
        break;

    case '|':
        if (_yychar == '|') {
            yyinp();
493
            tok->f.kind = T_PIPE_PIPE;
con's avatar
con committed
494 495
        } else if (_yychar == '=') {
            yyinp();
496
            tok->f.kind = T_PIPE_EQUAL;
con's avatar
con committed
497
        } else {
498
            tok->f.kind = T_PIPE;
con's avatar
con committed
499 500 501 502 503 504
        }
        break;

    case '~':
        if (_yychar == '=') {
            yyinp();
505
            tok->f.kind = T_TILDE_EQUAL;
con's avatar
con committed
506
        } else {
507
            tok->f.kind = T_TILDE;
con's avatar
con committed
508 509 510 511 512 513
        }
        break;

    case '!':
        if (_yychar == '=') {
            yyinp();
514
            tok->f.kind = T_EXCLAIM_EQUAL;
con's avatar
con committed
515
        } else {
516
            tok->f.kind = T_EXCLAIM;
con's avatar
con committed
517 518 519 520 521 522
        }
        break;

    case '=':
        if (_yychar == '=') {
            yyinp();
523
            tok->f.kind = T_EQUAL_EQUAL;
con's avatar
con committed
524
        } else {
525
            tok->f.kind = T_EQUAL;
con's avatar
con committed
526 527 528 529
        }
        break;

    case '<':
530
        if (f._scanAngleStringLiteralTokens) {
con's avatar
con committed
531 532 533 534 535 536 537 538
            const char *yytext = _currentChar;
            while (_yychar && _yychar != '>')
                yyinp();
            int yylen = _currentChar - yytext;
            // ### assert(_yychar == '>');
            if (_yychar == '>')
                yyinp();
            if (control())
539
                tok->string = control()->stringLiteral(yytext, yylen);
540
            tok->f.kind = T_ANGLE_STRING_LITERAL;
con's avatar
con committed
541 542 543 544
        } else if (_yychar == '<') {
            yyinp();
            if (_yychar == '=') {
                yyinp();
545
                tok->f.kind = T_LESS_LESS_EQUAL;
con's avatar
con committed
546
            } else
547
                tok->f.kind = T_LESS_LESS;
con's avatar
con committed
548 549
        } else if (_yychar == '=') {
            yyinp();
550
            tok->f.kind = T_LESS_EQUAL;
con's avatar
con committed
551
        } else {
552
            tok->f.kind = T_LESS;
con's avatar
con committed
553 554 555 556 557 558 559 560
        }
        break;

    case '>':
        if (_yychar == '>') {
            yyinp();
            if (_yychar == '=') {
                yyinp();
561
                tok->f.kind = T_GREATER_GREATER_EQUAL;
con's avatar
con committed
562
            } else
563 564
                tok->f.kind = T_LESS_LESS;
            tok->f.kind = T_GREATER_GREATER;
con's avatar
con committed
565 566
        } else if (_yychar == '=') {
            yyinp();
567
            tok->f.kind = T_GREATER_EQUAL;
con's avatar
con committed
568
        } else {
569
            tok->f.kind = T_GREATER;
con's avatar
con committed
570 571 572 573
        }
        break;

    case ',':
574
        tok->f.kind = T_COMMA;
con's avatar
con committed
575 576
        break;

577
    default: {
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
        if (f._objCEnabled) {
            if (ch == '@' && _yychar >= 'a' && _yychar <= 'z') {
                const char *yytext = _currentChar;

                do {
                    yyinp();
                    if (! (isalnum(_yychar) || _yychar == '_' || _yychar == '$'))
                        break;
                } while (_yychar);

                const int yylen = _currentChar - yytext;
                tok->f.kind = classifyObjCAtKeyword(yytext, yylen);
                break;
            } else if (ch == '@' && _yychar == '"') {
                // objc @string literals
                yyinp();
                tok->f.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())
616
                    tok->string = control()->stringLiteral(yytext, yylen);
617 618 619 620 621

                break;
            }
        }

con's avatar
con committed
622
        if (ch == 'L' && (_yychar == '"' || _yychar == '\'')) {
Roberto Raggi's avatar
Roberto Raggi committed
623
            // wide char/string literals
con's avatar
con committed
624 625 626 627 628
            ch = _yychar;
            yyinp();

            const char quote = ch;

629
            tok->f.kind = quote == '"'
con's avatar
con committed
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
                ? 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())
653
                tok->string = control()->stringLiteral(yytext, yylen);
654
        } else if (std::isalpha(ch) || ch == '_' || ch == '$') {
con's avatar
con committed
655
            const char *yytext = _currentChar - 1;
656
            while (std::isalnum(_yychar) || _yychar == '_' || _yychar == '$')
con's avatar
con committed
657 658
                yyinp();
            int yylen = _currentChar - yytext;
659
            if (f._scanKeywords)
660
                tok->f.kind = classify(yytext, yylen, f._qtMocRunEnabled, f._cxx0xEnabled);
661
            else
662
                tok->f.kind = T_IDENTIFIER;
663

664 665
            if (tok->f.kind == T_IDENTIFIER) {
                tok->f.kind = classifyOperator(yytext, yylen);
666 667

                if (control())
668
                    tok->identifier = control()->identifier(yytext, yylen);
669
            }
con's avatar
con committed
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
            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;
687
            tok->f.kind = T_NUMERIC_LITERAL;
con's avatar
con committed
688
            if (control())
689
                tok->number = control()->numericLiteral(yytext, yylen);
con's avatar
con committed
690 691
            break;
        } else {
692
            tok->f.kind = T_ERROR;
con's avatar
con committed
693 694 695 696 697 698 699
            break;
        }
    } // default

    } // switch
}

Roberto Raggi's avatar
Roberto Raggi committed
700