Lexer.cpp 19.8 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),
hjk's avatar
hjk committed
32
      _control(unit->control()),
33
      _state(State_Default),
con's avatar
con committed
34 35 36
      _flags(0),
      _currentLine(1)
{
37
    f._scanKeywords = true;
con's avatar
con committed
38 39 40 41 42 43
    setSource(_translationUnit->firstSourceChar(),
              _translationUnit->lastSourceChar());
}

Lexer::Lexer(const char *firstChar, const char *lastChar)
    : _translationUnit(0),
hjk's avatar
hjk committed
44
      _control(0),
45
      _state(State_Default),
con's avatar
con committed
46 47 48
      _flags(0),
      _currentLine(1)
{
49
    f._scanKeywords = true;
con's avatar
con committed
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
    setSource(firstChar, lastChar);
}

Lexer::~Lexer()
{ }

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

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
83
{ return f._qtMocRunEnabled; }
con's avatar
con committed
84 85

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

88 89 90 91 92 93
bool Lexer::cxx0xEnabled() const
{ return f._cxx0xEnabled; }

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

Roberto Raggi's avatar
Roberto Raggi committed
94
bool Lexer::objCEnabled() const
95
{ return f._objCEnabled; }
Roberto Raggi's avatar
Roberto Raggi committed
96

Roberto Raggi's avatar
Roberto Raggi committed
97
void Lexer::setObjCEnabled(bool onoff)
98
{ f._objCEnabled = onoff; }
Roberto Raggi's avatar
Roberto Raggi committed
99

con's avatar
con committed
100
bool Lexer::isIncremental() const
101
{ return f._isIncremental; }
con's avatar
con committed
102 103

void Lexer::setIncremental(bool isIncremental)
104
{ f._isIncremental = isIncremental; }
con's avatar
con committed
105 106

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

void Lexer::setScanCommentTokens(bool onoff)
110
{ f._scanCommentTokens = onoff; }
con's avatar
con committed
111 112

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

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

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

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);
148
    tok->f.length = _currentChar - _tokenStart;
con's avatar
con committed
149 150 151 152 153 154
}

void Lexer::scan_helper(Token *tok)
{
  _Lagain:
    while (_yychar && std::isspace(_yychar)) {
Erik Verbruggen's avatar
Erik Verbruggen committed
155 156
        if (_yychar == '\n') {
            tok->f.joined = false;
157
            tok->f.newline = true;
Erik Verbruggen's avatar
Erik Verbruggen committed
158
        } else {
159
            tok->f.whitespace = true;
Erik Verbruggen's avatar
Erik Verbruggen committed
160
        }
con's avatar
con committed
161 162 163 164 165 166
        yyinp();
    }

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

167 168 169
    _tokenStart = _currentChar;
    tok->offset = _currentChar - _firstChar;

170
    if (_state == State_MultiLineComment || _state == State_MultiLineDoxyComment) {
171 172
        const int originalState = _state;

con's avatar
con committed
173
        if (! _yychar) {
174
            tok->f.kind = T_EOF_SYMBOL;
con's avatar
con committed
175 176 177 178 179 180 181 182 183 184
            return;
        }

        while (_yychar) {
            if (_yychar != '*')
                yyinp();
            else {
                yyinp();
                if (_yychar == '/') {
                    yyinp();
185
                    _state = State_Default;
con's avatar
con committed
186 187 188 189 190
                    break;
                }
            }
        }

191
        if (! f._scanCommentTokens)
con's avatar
con committed
192 193
            goto _Lagain;

194
        else if (originalState == State_MultiLineComment)
195
            tok->f.kind = T_COMMENT;
196
        else
197
            tok->f.kind = T_DOXY_COMMENT;
con's avatar
con committed
198 199 200 201
        return; // done
    }

    if (! _yychar) {
202
        tok->f.kind = T_EOF_SYMBOL;
con's avatar
con committed
203 204 205
        return;
    }

206
    unsigned char ch = _yychar;
con's avatar
con committed
207 208 209 210 211 212 213 214
    yyinp();

    switch (ch) {
    case '\\':
        while (_yychar != '\n' && std::isspace(_yychar))
            yyinp();
        // ### assert(! _yychar || _yychar == '\n');
        if (_yychar == '\n') {
215 216
            tok->f.joined = true;
            tok->f.newline = false;
con's avatar
con committed
217 218 219 220
            yyinp();
        }
        goto _Lagain;

221 222 223
    case '"':
        scanStringLiteral(tok);
        break;
con's avatar
con committed
224

225 226 227
    case '\'':
        scanCharLiteral(tok);
        break;
con's avatar
con committed
228 229

    case '{':
230
        tok->f.kind = T_LBRACE;
con's avatar
con committed
231 232 233
        break;

    case '}':
234
        tok->f.kind = T_RBRACE;
con's avatar
con committed
235 236 237
        break;

    case '[':
238
        tok->f.kind = T_LBRACKET;
con's avatar
con committed
239 240 241
        break;

    case ']':
242
        tok->f.kind = T_RBRACKET;
con's avatar
con committed
243 244 245 246
        break;

    case '#':
        if (_yychar == '#') {
247
            tok->f.kind = T_POUND_POUND;
con's avatar
con committed
248 249
            yyinp();
        } else {
250
            tok->f.kind = T_POUND;
con's avatar
con committed
251 252 253 254
        }
        break;

    case '(':
255
        tok->f.kind = T_LPAREN;
con's avatar
con committed
256 257 258
        break;

    case ')':
259
        tok->f.kind = T_RPAREN;
con's avatar
con committed
260 261 262
        break;

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

    case ':':
        if (_yychar == ':') {
            yyinp();
269
            tok->f.kind = T_COLON_COLON;
con's avatar
con committed
270
        } else {
271
            tok->f.kind = T_COLON;
con's avatar
con committed
272 273 274 275 276 277
        }
        break;

    case '.':
        if (_yychar == '*') {
            yyinp();
278
            tok->f.kind = T_DOT_STAR;
con's avatar
con committed
279 280 281 282 283
        } else if (_yychar == '.') {
            yyinp();
            // ### assert(_yychar);
            if (_yychar == '.') {
                yyinp();
284
                tok->f.kind = T_DOT_DOT_DOT;
con's avatar
con committed
285
            } else {
286
                tok->f.kind = T_ERROR;
con's avatar
con committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
            }
        } 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;
304
            tok->f.kind = T_NUMERIC_LITERAL;
con's avatar
con committed
305
            if (control())
306
                tok->number = control()->numericLiteral(yytext, yylen);
con's avatar
con committed
307
        } else {
308
            tok->f.kind = T_DOT;
con's avatar
con committed
309 310 311 312
        }
        break;

    case '?':
313
        tok->f.kind = T_QUESTION;
con's avatar
con committed
314 315 316 317 318
        break;

    case '+':
        if (_yychar == '+') {
            yyinp();
319
            tok->f.kind = T_PLUS_PLUS;
con's avatar
con committed
320 321
        } else if (_yychar == '=') {
            yyinp();
322
            tok->f.kind = T_PLUS_EQUAL;
con's avatar
con committed
323
        } else {
324
            tok->f.kind = T_PLUS;
con's avatar
con committed
325 326 327 328 329 330
        }
        break;

    case '-':
        if (_yychar == '-') {
            yyinp();
331
            tok->f.kind = T_MINUS_MINUS;
con's avatar
con committed
332 333
        } else if (_yychar == '=') {
            yyinp();
334
            tok->f.kind = T_MINUS_EQUAL;
con's avatar
con committed
335 336 337 338
        } else if (_yychar == '>') {
            yyinp();
            if (_yychar == '*') {
                yyinp();
339
                tok->f.kind = T_ARROW_STAR;
con's avatar
con committed
340
            } else {
341
                tok->f.kind = T_ARROW;
con's avatar
con committed
342 343
            }
        } else {
344
            tok->f.kind = T_MINUS;
con's avatar
con committed
345 346 347 348 349 350
        }
        break;

    case '*':
        if (_yychar == '=') {
            yyinp();
351
            tok->f.kind = T_STAR_EQUAL;
con's avatar
con committed
352
        } else {
353
            tok->f.kind = T_STAR;
con's avatar
con committed
354 355 356 357 358
        }
        break;

    case '/':
        if (_yychar == '/') {
359 360 361 362 363 364
            yyinp();

            bool doxy = false;

            if (_yychar == '/' || _yychar == '!') {
                yyinp();
365
                doxy = true;
366 367 368
            }

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

371
            if (! f._scanCommentTokens)
con's avatar
con committed
372
                goto _Lagain;
373

374
            tok->f.kind = doxy ? T_CPP_DOXY_COMMENT : T_CPP_COMMENT;
375

con's avatar
con committed
376 377
        } else if (_yychar == '*') {
            yyinp();
378

379 380 381
            bool doxy = false;

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

384 385
                yyinp();

Roberto Raggi's avatar
Roberto Raggi committed
386 387 388
                if (ch == '*' && _yychar == '/')
                    goto _Ldone;

389 390 391
                if (_yychar == '<')
                    yyinp();

392 393 394
                if (! _yychar || std::isspace(_yychar))
                    doxy = true;
            }
395

con's avatar
con committed
396 397 398 399 400 401 402 403 404 405
            while (_yychar) {
                if (_yychar != '*') {
                    yyinp();
                } else {
                    yyinp();
                    if (_yychar == '/')
                        break;
                }
            }

Roberto Raggi's avatar
Roberto Raggi committed
406
        _Ldone:
con's avatar
con committed
407 408 409
            if (_yychar)
                yyinp();
            else
410
                _state = doxy ? State_MultiLineDoxyComment : State_MultiLineComment;
con's avatar
con committed
411

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

415
            tok->f.kind = doxy ? T_DOXY_COMMENT : T_COMMENT;
416

con's avatar
con committed
417 418
        } else if (_yychar == '=') {
            yyinp();
419
            tok->f.kind = T_SLASH_EQUAL;
con's avatar
con committed
420
        } else {
421
            tok->f.kind = T_SLASH;
con's avatar
con committed
422 423 424 425 426 427
        }
        break;

    case '%':
        if (_yychar == '=') {
            yyinp();
428
            tok->f.kind = T_PERCENT_EQUAL;
con's avatar
con committed
429
        } else {
430
            tok->f.kind = T_PERCENT;
con's avatar
con committed
431 432 433 434 435 436
        }
        break;

    case '^':
        if (_yychar == '=') {
            yyinp();
437
            tok->f.kind = T_CARET_EQUAL;
con's avatar
con committed
438
        } else {
439
            tok->f.kind = T_CARET;
con's avatar
con committed
440 441 442 443 444 445
        }
        break;

    case '&':
        if (_yychar == '&') {
            yyinp();
446
            tok->f.kind = T_AMPER_AMPER;
con's avatar
con committed
447 448
        } else if (_yychar == '=') {
            yyinp();
449
            tok->f.kind = T_AMPER_EQUAL;
con's avatar
con committed
450
        } else {
451
            tok->f.kind = T_AMPER;
con's avatar
con committed
452 453 454 455 456 457
        }
        break;

    case '|':
        if (_yychar == '|') {
            yyinp();
458
            tok->f.kind = T_PIPE_PIPE;
con's avatar
con committed
459 460
        } else if (_yychar == '=') {
            yyinp();
461
            tok->f.kind = T_PIPE_EQUAL;
con's avatar
con committed
462
        } else {
463
            tok->f.kind = T_PIPE;
con's avatar
con committed
464 465 466 467 468 469
        }
        break;

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

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

    case '=':
        if (_yychar == '=') {
            yyinp();
488
            tok->f.kind = T_EQUAL_EQUAL;
con's avatar
con committed
489
        } else {
490
            tok->f.kind = T_EQUAL;
con's avatar
con committed
491 492 493 494
        }
        break;

    case '<':
495
        if (f._scanAngleStringLiteralTokens) {
con's avatar
con committed
496 497 498 499 500 501 502 503
            const char *yytext = _currentChar;
            while (_yychar && _yychar != '>')
                yyinp();
            int yylen = _currentChar - yytext;
            // ### assert(_yychar == '>');
            if (_yychar == '>')
                yyinp();
            if (control())
504
                tok->string = control()->stringLiteral(yytext, yylen);
505
            tok->f.kind = T_ANGLE_STRING_LITERAL;
con's avatar
con committed
506 507 508 509
        } else if (_yychar == '<') {
            yyinp();
            if (_yychar == '=') {
                yyinp();
510
                tok->f.kind = T_LESS_LESS_EQUAL;
con's avatar
con committed
511
            } else
512
                tok->f.kind = T_LESS_LESS;
con's avatar
con committed
513 514
        } else if (_yychar == '=') {
            yyinp();
515
            tok->f.kind = T_LESS_EQUAL;
con's avatar
con committed
516
        } else {
517
            tok->f.kind = T_LESS;
con's avatar
con committed
518 519 520 521 522 523 524 525
        }
        break;

    case '>':
        if (_yychar == '>') {
            yyinp();
            if (_yychar == '=') {
                yyinp();
526
                tok->f.kind = T_GREATER_GREATER_EQUAL;
con's avatar
con committed
527
            } else
528 529
                tok->f.kind = T_LESS_LESS;
            tok->f.kind = T_GREATER_GREATER;
con's avatar
con committed
530 531
        } else if (_yychar == '=') {
            yyinp();
532
            tok->f.kind = T_GREATER_EQUAL;
con's avatar
con committed
533
        } else {
534
            tok->f.kind = T_GREATER;
con's avatar
con committed
535 536 537 538
        }
        break;

    case ',':
539
        tok->f.kind = T_COMMA;
con's avatar
con committed
540 541
        break;

542
    default: {
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
        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 == '"') {
                yyinp();
558 559 560 561
                scanStringLiteral(tok, '"');
                break;
            }
        }
562

563
        if (ch == 'L' || ch == 'u' || ch == 'U' || ch == 'R') {
564 565 566
            // Either a literal or still an identifier.
            if (_yychar == '"') {
                yyinp();
567 568 569 570
                if (ch == 'R')
                    scanRawStringLiteral(tok);
                else
                    scanStringLiteral(tok, ch);
571 572 573
            } else if (_yychar == '\'') {
                yyinp();
                scanCharLiteral(tok, ch);
574 575 576 577 578 579 580 581
            } else if (ch != 'R' && _yychar == 'R') {
                yyinp();
                if (_yychar == '"') {
                    yyinp();
                    scanRawStringLiteral(tok, ch);
                } else {
                    scanIdentifier(tok, 1);
                }
582
            } else if (ch == 'u' && _yychar == '8') {
583 584
                yyinp();
                if (_yychar == '"') {
585 586
                    yyinp();
                    scanStringLiteral(tok, '8');
587
                } else if (_yychar == '\'') {
588 589
                    yyinp();
                    scanCharLiteral(tok, '8');
590 591 592 593 594 595 596 597
                } else if (_yychar == 'R') {
                    yyinp();
                    if (_yychar == '"') {
                        yyinp();
                        scanRawStringLiteral(tok, '8');
                    } else {
                        scanIdentifier(tok, 2);
                    }
598
                } else {
599
                    scanIdentifier(tok, 1);
600
                }
601 602
            } else {
                scanIdentifier(tok);
603
            }
604 605 606 607 608 609
        } else if (std::isalpha(ch) || ch == '_' || ch == '$') {
            scanIdentifier(tok);
        } else if (std::isdigit(ch)) {
            scanNumericLiteral(tok);
        } else {
            tok->f.kind = T_ERROR;
610
        }
611 612
        break;
    } // default
613

614 615
    } // switch
}
con's avatar
con committed
616

617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
void Lexer::scanStringLiteral(Token *tok, unsigned char hint)
{
    scanUntilQuote(tok, '"');

    if (hint == 'L')
        tok->f.kind = T_WIDE_STRING_LITERAL;
    else if (hint == 'U')
        tok->f.kind = T_UTF32_STRING_LITERAL;
    else if (hint == 'u')
        tok->f.kind = T_UTF16_STRING_LITERAL;
    else if (hint == '8')
        tok->f.kind = T_UTF8_STRING_LITERAL;
    else if (hint == '@')
        tok->f.kind = T_AT_STRING_LITERAL;
    else
        tok->f.kind = T_STRING_LITERAL;
}
con's avatar
con committed
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 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
void Lexer::scanRawStringLiteral(Token *tok, unsigned char hint)
{
    const char *yytext = _currentChar;

    int delimLength = -1;
    const char *closingDelimCandidate = 0;
    while (_yychar) {
        if (_yychar == '(' && delimLength == -1) {
            delimLength = _currentChar - yytext;
            yyinp();
        } else if (_yychar == ')') {
            yyinp();
            if (delimLength == -1)
                break;
            closingDelimCandidate = _currentChar;
        } else {
            if (delimLength == -1) {
                if (_yychar == '\\' || std::isspace(_yychar))
                    break;
                yyinp();
            } else {
                if (!closingDelimCandidate) {
                    yyinp();
                } else {
                    if (_yychar == '"') {
                        if (delimLength == _currentChar - closingDelimCandidate) {
                            // Got a matching closing delimiter.
                            break;
                        }
                    }

                    // Make sure this continues to be a valid candidate.
                    if (_yychar != *(yytext + (_currentChar - closingDelimCandidate)))
                        closingDelimCandidate = 0;

                    yyinp();
                }
            }
        }
    }

    int yylen = _currentChar - yytext;

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

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

    if (hint == 'L')
        tok->f.kind = T_RAW_WIDE_STRING_LITERAL;
    else if (hint == 'U')
        tok->f.kind = T_RAW_UTF32_STRING_LITERAL;
    else if (hint == 'u')
        tok->f.kind = T_RAW_UTF16_STRING_LITERAL;
    else if (hint == '8')
        tok->f.kind = T_RAW_UTF8_STRING_LITERAL;
    else
        tok->f.kind = T_RAW_STRING_LITERAL;
}

696 697 698 699 700 701 702 703 704 705 706 707 708
void Lexer::scanCharLiteral(Token *tok, unsigned char hint)
{
    scanUntilQuote(tok, '\'');

    if (hint == 'L')
        tok->f.kind = T_WIDE_CHAR_LITERAL;
    else if (hint == 'U')
        tok->f.kind = T_UTF32_CHAR_LITERAL;
    else if (hint == 'u')
        tok->f.kind = T_UTF16_CHAR_LITERAL;
    else
        tok->f.kind = T_CHAR_LITERAL;
}
con's avatar
con committed
709

710 711 712
void Lexer::scanUntilQuote(Token *tok, unsigned char quote)
{
    assert(quote == '"' || quote == '\'');
con's avatar
con committed
713

714
    const char *yytext = _currentChar;
715 716 717
    while (_yychar
           && _yychar != quote
           && _yychar != '\n') {
718 719 720 721 722
        if (_yychar != '\\')
            yyinp();
        else {
            yyinp(); // skip `\\'
            if (_yychar)
con's avatar
con committed
723
                yyinp();
724 725 726
        }
    }
    int yylen = _currentChar - yytext;
con's avatar
con committed
727

728 729
    if (_yychar == quote)
        yyinp();
730

731 732 733
    if (control())
        tok->string = control()->stringLiteral(yytext, yylen);
}
734

735 736 737 738 739 740 741 742 743
void Lexer::scanNumericLiteral(Token *tok)
{
    const char *yytext = _currentChar - 1;
    while (_yychar) {
        if (_yychar == 'e' || _yychar == 'E') {
            yyinp();
            if (_yychar == '-' || _yychar == '+') {
                yyinp();
                // ### assert(std::isdigit(_yychar));
con's avatar
con committed
744
            }
745 746
        } else if (std::isalnum(_yychar) || _yychar == '.') {
            yyinp();
con's avatar
con committed
747 748 749
        } else {
            break;
        }
750 751
    }
    int yylen = _currentChar - yytext;
con's avatar
con committed
752

753 754 755 756
    tok->f.kind = T_NUMERIC_LITERAL;

    if (control())
        tok->number = control()->numericLiteral(yytext, yylen);
con's avatar
con committed
757 758
}

759
void Lexer::scanIdentifier(Token *tok, unsigned extraProcessedChars)
760
{
761
    const char *yytext = _currentChar - 1 - extraProcessedChars;
762 763 764 765 766 767 768 769 770 771
    while (std::isalnum(_yychar) || _yychar == '_' || _yychar == '$')
        yyinp();
    int yylen = _currentChar - yytext;
    if (f._scanKeywords)
        tok->f.kind = classify(yytext, yylen, f._qtMocRunEnabled, f._cxx0xEnabled);
    else
        tok->f.kind = T_IDENTIFIER;

    if (tok->f.kind == T_IDENTIFIER) {
        tok->f.kind = classifyOperator(yytext, yylen);
Roberto Raggi's avatar
Roberto Raggi committed
772

773 774 775 776
        if (control())
            tok->identifier = control()->identifier(yytext, yylen);
    }
}