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"
25 26 27

#include "cppassert.h"

con's avatar
con committed
28 29
#include <cctype>

Roberto Raggi's avatar
Roberto Raggi committed
30
using namespace CPlusPlus;
con's avatar
con committed
31 32 33

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

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

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

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

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

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

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

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

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

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

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

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

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

void Lexer::setScanAngleStringLiteralTokens(bool onoff)
121
{ f._scanAngleStringLiteralTokens = onoff; }
con's avatar
con committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

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

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

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

169 170 171
    _tokenStart = _currentChar;
    tok->offset = _currentChar - _firstChar;

172
    if (_state == State_MultiLineComment || _state == State_MultiLineDoxyComment) {
173 174
        const int originalState = _state;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    case '.':
        if (_yychar == '*') {
            yyinp();
280
            tok->f.kind = T_DOT_STAR;
con's avatar
con committed
281 282
        } else if (_yychar == '.') {
            yyinp();
283
            // ### CPP_CHECK(_yychar);
con's avatar
con committed
284 285
            if (_yychar == '.') {
                yyinp();
286
                tok->f.kind = T_DOT_DOT_DOT;
con's avatar
con committed
287
            } else {
288
                tok->f.kind = T_ERROR;
con's avatar
con committed
289 290 291 292 293 294 295 296
            }
        } else if (std::isdigit(_yychar)) {
            const char *yytext = _currentChar - 2;
            do {
                if (_yychar == 'e' || _yychar == 'E') {
                    yyinp();
                    if (_yychar == '-' || _yychar == '+') {
                        yyinp();
297
                        // ### CPP_CHECK(std::isdigit(_yychar));
con's avatar
con committed
298 299 300 301 302 303 304 305
                    }
                } else if (std::isalnum(_yychar) || _yychar == '.') {
                    yyinp();
                } else {
                    break;
                }
            } while (_yychar);
            int yylen = _currentChar - yytext;
306
            tok->f.kind = T_NUMERIC_LITERAL;
con's avatar
con committed
307
            if (control())
308
                tok->number = control()->numericLiteral(yytext, yylen);
con's avatar
con committed
309
        } else {
310
            tok->f.kind = T_DOT;
con's avatar
con committed
311 312 313 314
        }
        break;

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

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

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

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

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

            bool doxy = false;

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

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

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

376
            tok->f.kind = doxy ? T_CPP_DOXY_COMMENT : T_CPP_COMMENT;
377

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

381 382 383
            bool doxy = false;

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

386 387
                yyinp();

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

391 392 393
                if (_yychar == '<')
                    yyinp();

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

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

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

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

417
            tok->f.kind = doxy ? T_DOXY_COMMENT : T_COMMENT;
418

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

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

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

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

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

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

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

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

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

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

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

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

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

616 617
    } // switch
}
con's avatar
con committed
618

619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
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
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 696 697
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;
}

698 699 700 701 702 703 704 705 706 707 708 709 710
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
711

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

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

730 731
    if (_yychar == quote)
        yyinp();
732

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

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

755 756 757 758
    tok->f.kind = T_NUMERIC_LITERAL;

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

761
void Lexer::scanIdentifier(Token *tok, unsigned extraProcessedChars)
762
{
763
    const char *yytext = _currentChar - 1 - extraProcessedChars;
764 765 766 767 768 769 770 771 772 773
    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
774

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