Lexer.cpp 17.7 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
}

void Lexer::scan_helper(Token *tok)
{
  _Lagain:
160 161 162
    _tokenStart = _currentChar;
    tok->offset = _currentChar - _firstChar;

con's avatar
con committed
163
    while (_yychar && std::isspace(_yychar)) {
Erik Verbruggen's avatar
Erik Verbruggen committed
164 165
        if (_yychar == '\n') {
            tok->f.joined = false;
166
            tok->f.newline = true;
167 168
            if (_state == State_MultiLineComment || _state == State_MultiLineDoxyComment)
                break;
Erik Verbruggen's avatar
Erik Verbruggen committed
169
        } else {
170
            tok->f.whitespace = true;
Erik Verbruggen's avatar
Erik Verbruggen committed
171
        }
con's avatar
con committed
172 173 174 175 176 177
        yyinp();
    }

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

178
    if (_state == State_MultiLineComment || _state == State_MultiLineDoxyComment) {
179 180
        const int originalState = _state;

181

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

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

200
        if (! f._scanCommentTokens)
con's avatar
con committed
201 202
            goto _Lagain;

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

210 211 212
    _tokenStart = _currentChar;
    tok->offset = _currentChar - _firstChar;

con's avatar
con committed
213
    if (! _yychar) {
214
        tok->f.kind = T_EOF_SYMBOL;
con's avatar
con committed
215 216 217
        return;
    }

218
    unsigned char ch = _yychar;
con's avatar
con committed
219 220 221 222 223 224 225 226
    yyinp();

    switch (ch) {
    case '\\':
        while (_yychar != '\n' && std::isspace(_yychar))
            yyinp();
        // ### assert(! _yychar || _yychar == '\n');
        if (_yychar == '\n') {
227 228
            tok->f.joined = true;
            tok->f.newline = false;
con's avatar
con committed
229 230 231 232 233 234 235
            yyinp();
        }
        goto _Lagain;

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

236
        tok->f.kind = quote == '"'
con's avatar
con committed
237 238 239 240 241 242
            ? T_STRING_LITERAL
            : T_CHAR_LITERAL;

        const char *yytext = _currentChar;

        while (_yychar && _yychar != quote) {
243 244 245
            if (_yychar == '\n')
                break;
            else if (_yychar != '\\')
con's avatar
con committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
                yyinp();
            else {
                yyinp(); // skip `\\'

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

        int yylen = _currentChar - yytext;

        if (_yychar == quote)
            yyinp();

        if (control())
262
            tok->string = control()->stringLiteral(yytext, yylen);
con's avatar
con committed
263 264 265
    } break;

    case '{':
266
        tok->f.kind = T_LBRACE;
con's avatar
con committed
267 268 269
        break;

    case '}':
270
        tok->f.kind = T_RBRACE;
con's avatar
con committed
271 272 273
        break;

    case '[':
274
        tok->f.kind = T_LBRACKET;
con's avatar
con committed
275 276 277
        break;

    case ']':
278
        tok->f.kind = T_RBRACKET;
con's avatar
con committed
279 280 281 282
        break;

    case '#':
        if (_yychar == '#') {
283
            tok->f.kind = T_POUND_POUND;
con's avatar
con committed
284 285
            yyinp();
        } else {
286
            tok->f.kind = T_POUND;
con's avatar
con committed
287 288 289 290
        }
        break;

    case '(':
291
        tok->f.kind = T_LPAREN;
con's avatar
con committed
292 293 294
        break;

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

    case ';':
299
        tok->f.kind = T_SEMICOLON;
con's avatar
con committed
300 301 302 303 304
        break;

    case ':':
        if (_yychar == ':') {
            yyinp();
305
            tok->f.kind = T_COLON_COLON;
con's avatar
con committed
306
        } else {
307
            tok->f.kind = T_COLON;
con's avatar
con committed
308 309 310 311 312 313
        }
        break;

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

    case '?':
349
        tok->f.kind = T_QUESTION;
con's avatar
con committed
350 351 352 353 354
        break;

    case '+':
        if (_yychar == '+') {
            yyinp();
355
            tok->f.kind = T_PLUS_PLUS;
con's avatar
con committed
356 357
        } else if (_yychar == '=') {
            yyinp();
358
            tok->f.kind = T_PLUS_EQUAL;
con's avatar
con committed
359
        } else {
360
            tok->f.kind = T_PLUS;
con's avatar
con committed
361 362 363 364 365 366
        }
        break;

    case '-':
        if (_yychar == '-') {
            yyinp();
367
            tok->f.kind = T_MINUS_MINUS;
con's avatar
con committed
368 369
        } else if (_yychar == '=') {
            yyinp();
370
            tok->f.kind = T_MINUS_EQUAL;
con's avatar
con committed
371 372 373 374
        } else if (_yychar == '>') {
            yyinp();
            if (_yychar == '*') {
                yyinp();
375
                tok->f.kind = T_ARROW_STAR;
con's avatar
con committed
376
            } else {
377
                tok->f.kind = T_ARROW;
con's avatar
con committed
378 379
            }
        } else {
380
            tok->f.kind = T_MINUS;
con's avatar
con committed
381 382 383 384 385 386
        }
        break;

    case '*':
        if (_yychar == '=') {
            yyinp();
387
            tok->f.kind = T_STAR_EQUAL;
con's avatar
con committed
388
        } else {
389
            tok->f.kind = T_STAR;
con's avatar
con committed
390 391 392 393 394
        }
        break;

    case '/':
        if (_yychar == '/') {
395 396 397 398 399 400 401
            yyinp();

            bool doxy = false;

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

402 403 404
                if (_yychar == '<')
                    yyinp();

405 406 407 408 409
                if (_yychar != '\n' && std::isspace(_yychar))
                    doxy = true;
            }

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

412
            if (! f._scanCommentTokens)
con's avatar
con committed
413
                goto _Lagain;
414

415
            tok->f.kind = doxy ? T_CPP_DOXY_COMMENT : T_CPP_COMMENT;
416

con's avatar
con committed
417 418
        } else if (_yychar == '*') {
            yyinp();
419

420 421 422
            bool doxy = false;

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

425 426
                yyinp();

Roberto Raggi's avatar
Roberto Raggi committed
427 428 429
                if (ch == '*' && _yychar == '/')
                    goto _Ldone;

430 431 432
                if (_yychar == '<')
                    yyinp();

433 434 435
                if (! _yychar || std::isspace(_yychar))
                    doxy = true;
            }
436

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

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

453
            if (! f._scanCommentTokens)
con's avatar
con committed
454
                goto _Lagain;
455

456
            tok->f.kind = doxy ? T_DOXY_COMMENT : T_COMMENT;
457

con's avatar
con committed
458 459
        } else if (_yychar == '=') {
            yyinp();
460
            tok->f.kind = T_SLASH_EQUAL;
con's avatar
con committed
461
        } else {
462
            tok->f.kind = T_SLASH;
con's avatar
con committed
463 464 465 466 467 468
        }
        break;

    case '%':
        if (_yychar == '=') {
            yyinp();
469
            tok->f.kind = T_PERCENT_EQUAL;
con's avatar
con committed
470
        } else {
471
            tok->f.kind = T_PERCENT;
con's avatar
con committed
472 473 474 475 476 477
        }
        break;

    case '^':
        if (_yychar == '=') {
            yyinp();
478
            tok->f.kind = T_CARET_EQUAL;
con's avatar
con committed
479
        } else {
480
            tok->f.kind = T_CARET;
con's avatar
con committed
481 482 483 484 485 486
        }
        break;

    case '&':
        if (_yychar == '&') {
            yyinp();
487
            tok->f.kind = T_AMPER_AMPER;
con's avatar
con committed
488 489
        } else if (_yychar == '=') {
            yyinp();
490
            tok->f.kind = T_AMPER_EQUAL;
con's avatar
con committed
491
        } else {
492
            tok->f.kind = T_AMPER;
con's avatar
con committed
493 494 495 496 497 498
        }
        break;

    case '|':
        if (_yychar == '|') {
            yyinp();
499
            tok->f.kind = T_PIPE_PIPE;
con's avatar
con committed
500 501
        } else if (_yychar == '=') {
            yyinp();
502
            tok->f.kind = T_PIPE_EQUAL;
con's avatar
con committed
503
        } else {
504
            tok->f.kind = T_PIPE;
con's avatar
con committed
505 506 507 508 509 510
        }
        break;

    case '~':
        if (_yychar == '=') {
            yyinp();
511
            tok->f.kind = T_TILDE_EQUAL;
con's avatar
con committed
512
        } else {
513
            tok->f.kind = T_TILDE;
con's avatar
con committed
514 515 516 517 518 519
        }
        break;

    case '!':
        if (_yychar == '=') {
            yyinp();
520
            tok->f.kind = T_EXCLAIM_EQUAL;
con's avatar
con committed
521
        } else {
522
            tok->f.kind = T_EXCLAIM;
con's avatar
con committed
523 524 525 526 527 528
        }
        break;

    case '=':
        if (_yychar == '=') {
            yyinp();
529
            tok->f.kind = T_EQUAL_EQUAL;
con's avatar
con committed
530
        } else {
531
            tok->f.kind = T_EQUAL;
con's avatar
con committed
532 533 534 535
        }
        break;

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

    case '>':
        if (_yychar == '>') {
            yyinp();
            if (_yychar == '=') {
                yyinp();
567
                tok->f.kind = T_GREATER_GREATER_EQUAL;
con's avatar
con committed
568
            } else
569 570
                tok->f.kind = T_LESS_LESS;
            tok->f.kind = T_GREATER_GREATER;
con's avatar
con committed
571 572
        } else if (_yychar == '=') {
            yyinp();
573
            tok->f.kind = T_GREATER_EQUAL;
con's avatar
con committed
574
        } else {
575
            tok->f.kind = T_GREATER;
con's avatar
con committed
576 577 578 579
        }
        break;

    case ',':
580
        tok->f.kind = T_COMMA;
con's avatar
con committed
581 582
        break;

583
    default: {
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
        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())
622
                    tok->string = control()->stringLiteral(yytext, yylen);
623 624 625 626 627

                break;
            }
        }

con's avatar
con committed
628
        if (ch == 'L' && (_yychar == '"' || _yychar == '\'')) {
Roberto Raggi's avatar
Roberto Raggi committed
629
            // wide char/string literals
con's avatar
con committed
630 631 632 633 634
            ch = _yychar;
            yyinp();

            const char quote = ch;

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

670 671
            if (tok->f.kind == T_IDENTIFIER) {
                tok->f.kind = classifyOperator(yytext, yylen);
672 673

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

    } // switch
}

Roberto Raggi's avatar
Roberto Raggi committed
706