Lexer.cpp 21.1 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(0),
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(0),
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::scanCommentTokens() const
85
{ return f._scanCommentTokens; }
con's avatar
con committed
86 87

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

bool Lexer::scanKeywords() const
91
{ return f._scanKeywords; }
con's avatar
con committed
92 93

void Lexer::setScanKeywords(bool onoff)
94
{ f._scanKeywords = onoff; }
con's avatar
con committed
95 96

void Lexer::setScanAngleStringLiteralTokens(bool onoff)
97
{ f._scanAngleStringLiteralTokens = onoff; }
con's avatar
con committed
98 99 100 101 102 103 104 105 106 107 108 109 110

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

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

void Lexer::scan(Token *tok)
{
    tok->reset();
    scan_helper(tok);
Nikolai Kosjar's avatar
Nikolai Kosjar committed
111
    tok->f.bytes = _currentChar - _tokenStart;
con's avatar
con committed
112 113 114 115 116 117
}

void Lexer::scan_helper(Token *tok)
{
  _Lagain:
    while (_yychar && std::isspace(_yychar)) {
Erik Verbruggen's avatar
Erik Verbruggen committed
118
        if (_yychar == '\n') {
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
            tok->f.joined = s._newlineExpected;
            tok->f.newline = !s._newlineExpected;

            if (s._newlineExpected) {
                s._newlineExpected = false;
            } else {
                switch (s._tokenKind) {
                case T_EOF_SYMBOL:
                case T_COMMENT:
                case T_DOXY_COMMENT:
                    break; // multiline tokens, don't break on newline
                default: // Strings and C++ comments
                    _state = 0;
                }
            }
Erik Verbruggen's avatar
Erik Verbruggen committed
134
        } else {
135
            tok->f.whitespace = true;
Erik Verbruggen's avatar
Erik Verbruggen committed
136
        }
con's avatar
con committed
137 138 139 140 141 142
        yyinp();
    }

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

143
    _tokenStart = _currentChar;
Nikolai Kosjar's avatar
Nikolai Kosjar committed
144
    tok->byteOffset = _currentChar - _firstChar;
145

146 147 148
    if (_yychar) {
        s._newlineExpected = false;
    } else if (s._tokenKind) {
149 150 151 152
        tok->f.kind = T_EOF_SYMBOL;
        return;
    }

153
    switch (s._tokenKind) {
154 155
    case T_EOF_SYMBOL:
        break;
156 157
    case T_COMMENT:
    case T_DOXY_COMMENT: {
158
        const int originalKind = s._tokenKind;
159

con's avatar
con committed
160 161 162 163 164 165 166
        while (_yychar) {
            if (_yychar != '*')
                yyinp();
            else {
                yyinp();
                if (_yychar == '/') {
                    yyinp();
167
                    _state = 0;
con's avatar
con committed
168 169 170 171 172
                    break;
                }
            }
        }

173
        if (! f._scanCommentTokens)
con's avatar
con committed
174 175
            goto _Lagain;

176
        tok->f.kind = originalKind;
con's avatar
con committed
177 178
        return; // done
    }
179
    case T_CPP_COMMENT:
180 181
    case T_CPP_DOXY_COMMENT: {
        const Kind originalKind = (Kind)s._tokenKind;
182
        tok->f.joined = true;
183 184
        if (f._scanCommentTokens)
            tok->f.kind = originalKind;
185
        _state = 0;
186
        scanCppComment(originalKind);
187
        return;
188
    }
189
    default: // Strings
190 191 192
        tok->f.joined = true;
        tok->f.kind = s._tokenKind;
        _state = 0;
193 194
        scanUntilQuote(tok, '"');
        return;
195
    }
con's avatar
con committed
196 197

    if (! _yychar) {
198
        tok->f.kind = T_EOF_SYMBOL;
con's avatar
con committed
199 200 201
        return;
    }

202
    unsigned char ch = _yychar;
con's avatar
con committed
203 204 205 206
    yyinp();

    switch (ch) {
    case '\\':
207
        s._newlineExpected = true;
con's avatar
con committed
208 209
        goto _Lagain;

210 211 212
    case '"':
        scanStringLiteral(tok);
        break;
con's avatar
con committed
213

214 215 216
    case '\'':
        scanCharLiteral(tok);
        break;
con's avatar
con committed
217 218

    case '{':
219
        tok->f.kind = T_LBRACE;
con's avatar
con committed
220 221 222
        break;

    case '}':
223
        tok->f.kind = T_RBRACE;
con's avatar
con committed
224 225 226
        break;

    case '[':
227
        tok->f.kind = T_LBRACKET;
con's avatar
con committed
228 229 230
        break;

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

    case '#':
        if (_yychar == '#') {
236
            tok->f.kind = T_POUND_POUND;
con's avatar
con committed
237 238
            yyinp();
        } else {
239
            tok->f.kind = T_POUND;
con's avatar
con committed
240 241 242 243
        }
        break;

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

    case ')':
248
        tok->f.kind = T_RPAREN;
con's avatar
con committed
249 250 251
        break;

    case ';':
252
        tok->f.kind = T_SEMICOLON;
con's avatar
con committed
253 254 255 256 257
        break;

    case ':':
        if (_yychar == ':') {
            yyinp();
258
            tok->f.kind = T_COLON_COLON;
259 260 261
        } else if (_yychar == '>') {
            yyinp();
            tok->f.kind = T_RBRACKET;
con's avatar
con committed
262
        } else {
263
            tok->f.kind = T_COLON;
con's avatar
con committed
264 265 266 267 268 269
        }
        break;

    case '.':
        if (_yychar == '*') {
            yyinp();
270
            tok->f.kind = T_DOT_STAR;
con's avatar
con committed
271 272
        } else if (_yychar == '.') {
            yyinp();
273
            // ### CPP_CHECK(_yychar);
con's avatar
con committed
274 275
            if (_yychar == '.') {
                yyinp();
276
                tok->f.kind = T_DOT_DOT_DOT;
con's avatar
con committed
277
            } else {
278
                tok->f.kind = T_ERROR;
con's avatar
con committed
279 280 281 282 283 284 285 286
            }
        } else if (std::isdigit(_yychar)) {
            const char *yytext = _currentChar - 2;
            do {
                if (_yychar == 'e' || _yychar == 'E') {
                    yyinp();
                    if (_yychar == '-' || _yychar == '+') {
                        yyinp();
287
                        // ### CPP_CHECK(std::isdigit(_yychar));
con's avatar
con committed
288 289 290 291 292 293 294 295
                    }
                } else if (std::isalnum(_yychar) || _yychar == '.') {
                    yyinp();
                } else {
                    break;
                }
            } while (_yychar);
            int yylen = _currentChar - yytext;
296
            tok->f.kind = T_NUMERIC_LITERAL;
con's avatar
con committed
297
            if (control())
298
                tok->number = control()->numericLiteral(yytext, yylen);
con's avatar
con committed
299
        } else {
300
            tok->f.kind = T_DOT;
con's avatar
con committed
301 302 303 304
        }
        break;

    case '?':
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
        if (_yychar == '?') {
            yyinp();
            if (_yychar == '(') {
                yyinp();
                tok->f.kind = T_LBRACKET;
            } else if (_yychar == ')') {
                yyinp();
                tok->f.kind = T_RBRACKET;
            } else if (_yychar == '<') {
                yyinp();
                tok->f.kind = T_LBRACE;
            } else if (_yychar == '>') {
                yyinp();
                tok->f.kind = T_RBRACE;
            }
        } else {
            tok->f.kind = T_QUESTION;
        }
con's avatar
con committed
323 324 325 326 327
        break;

    case '+':
        if (_yychar == '+') {
            yyinp();
328
            tok->f.kind = T_PLUS_PLUS;
con's avatar
con committed
329 330
        } else if (_yychar == '=') {
            yyinp();
331
            tok->f.kind = T_PLUS_EQUAL;
con's avatar
con committed
332
        } else {
333
            tok->f.kind = T_PLUS;
con's avatar
con committed
334 335 336 337 338 339
        }
        break;

    case '-':
        if (_yychar == '-') {
            yyinp();
340
            tok->f.kind = T_MINUS_MINUS;
con's avatar
con committed
341 342
        } else if (_yychar == '=') {
            yyinp();
343
            tok->f.kind = T_MINUS_EQUAL;
con's avatar
con committed
344 345 346 347
        } else if (_yychar == '>') {
            yyinp();
            if (_yychar == '*') {
                yyinp();
348
                tok->f.kind = T_ARROW_STAR;
con's avatar
con committed
349
            } else {
350
                tok->f.kind = T_ARROW;
con's avatar
con committed
351 352
            }
        } else {
353
            tok->f.kind = T_MINUS;
con's avatar
con committed
354 355 356 357 358 359
        }
        break;

    case '*':
        if (_yychar == '=') {
            yyinp();
360
            tok->f.kind = T_STAR_EQUAL;
con's avatar
con committed
361
        } else {
362
            tok->f.kind = T_STAR;
con's avatar
con committed
363 364 365 366 367
        }
        break;

    case '/':
        if (_yychar == '/') {
368 369
            yyinp();

370
            Kind commentType = T_CPP_COMMENT;
371 372 373

            if (_yychar == '/' || _yychar == '!') {
                yyinp();
374
                commentType = T_CPP_DOXY_COMMENT;
375 376
            }

377
            scanCppComment(commentType);
378

379
            if (! f._scanCommentTokens)
con's avatar
con committed
380
                goto _Lagain;
381

382
            tok->f.kind = commentType;
383

con's avatar
con committed
384 385
        } else if (_yychar == '*') {
            yyinp();
386

387
            Kind commentKind = T_COMMENT;
388 389

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

392 393
                yyinp();

Roberto Raggi's avatar
Roberto Raggi committed
394 395 396
                if (ch == '*' && _yychar == '/')
                    goto _Ldone;

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

400
                if (! _yychar || std::isspace(_yychar))
401
                    commentKind = T_DOXY_COMMENT;
402
            }
403

con's avatar
con committed
404 405 406 407 408 409 410 411 412 413
            while (_yychar) {
                if (_yychar != '*') {
                    yyinp();
                } else {
                    yyinp();
                    if (_yychar == '/')
                        break;
                }
            }

Roberto Raggi's avatar
Roberto Raggi committed
414
        _Ldone:
con's avatar
con committed
415 416 417
            if (_yychar)
                yyinp();
            else
418
                s._tokenKind = commentKind;
con's avatar
con committed
419

420
            if (! f._scanCommentTokens)
con's avatar
con committed
421
                goto _Lagain;
422

423
            tok->f.kind = commentKind;
424

con's avatar
con committed
425 426
        } else if (_yychar == '=') {
            yyinp();
427
            tok->f.kind = T_SLASH_EQUAL;
con's avatar
con committed
428
        } else {
429
            tok->f.kind = T_SLASH;
con's avatar
con committed
430 431 432 433 434 435
        }
        break;

    case '%':
        if (_yychar == '=') {
            yyinp();
436
            tok->f.kind = T_PERCENT_EQUAL;
437 438 439 440 441 442
        } else if (_yychar == '>') {
            yyinp();
            tok->f.kind = T_RBRACE;
        } else if (_yychar == ':') {
            yyinp();
            tok->f.kind = T_POUND;
con's avatar
con committed
443
        } else {
444
            tok->f.kind = T_PERCENT;
con's avatar
con committed
445 446 447 448 449 450
        }
        break;

    case '^':
        if (_yychar == '=') {
            yyinp();
451
            tok->f.kind = T_CARET_EQUAL;
con's avatar
con committed
452
        } else {
453
            tok->f.kind = T_CARET;
con's avatar
con committed
454 455 456 457 458 459
        }
        break;

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

    case '|':
        if (_yychar == '|') {
            yyinp();
472
            tok->f.kind = T_PIPE_PIPE;
con's avatar
con committed
473 474
        } else if (_yychar == '=') {
            yyinp();
475
            tok->f.kind = T_PIPE_EQUAL;
con's avatar
con committed
476
        } else {
477
            tok->f.kind = T_PIPE;
con's avatar
con committed
478 479 480 481 482 483
        }
        break;

    case '~':
        if (_yychar == '=') {
            yyinp();
484
            tok->f.kind = T_TILDE_EQUAL;
con's avatar
con committed
485
        } else {
486
            tok->f.kind = T_TILDE;
con's avatar
con committed
487 488 489 490 491 492
        }
        break;

    case '!':
        if (_yychar == '=') {
            yyinp();
493
            tok->f.kind = T_EXCLAIM_EQUAL;
con's avatar
con committed
494
        } else {
495
            tok->f.kind = T_EXCLAIM;
con's avatar
con committed
496 497 498 499 500 501
        }
        break;

    case '=':
        if (_yychar == '=') {
            yyinp();
502
            tok->f.kind = T_EQUAL_EQUAL;
con's avatar
con committed
503
        } else {
504
            tok->f.kind = T_EQUAL;
con's avatar
con committed
505 506 507 508
        }
        break;

    case '<':
509
        if (f._scanAngleStringLiteralTokens) {
con's avatar
con committed
510 511 512 513
            const char *yytext = _currentChar;
            while (_yychar && _yychar != '>')
                yyinp();
            int yylen = _currentChar - yytext;
514
            // ### CPP_CHECK(_yychar == '>');
con's avatar
con committed
515 516 517
            if (_yychar == '>')
                yyinp();
            if (control())
518
                tok->string = control()->stringLiteral(yytext, yylen);
519
            tok->f.kind = T_ANGLE_STRING_LITERAL;
con's avatar
con committed
520 521 522 523
        } else if (_yychar == '<') {
            yyinp();
            if (_yychar == '=') {
                yyinp();
524
                tok->f.kind = T_LESS_LESS_EQUAL;
con's avatar
con committed
525
            } else
526
                tok->f.kind = T_LESS_LESS;
con's avatar
con committed
527 528
        } else if (_yychar == '=') {
            yyinp();
529
            tok->f.kind = T_LESS_EQUAL;
530 531 532 533 534 535
        } else if (_yychar == ':') {
            yyinp();
            tok->f.kind = T_LBRACKET;
        } else if (_yychar == '%') {
            yyinp();
            tok->f.kind = T_LBRACE;
con's avatar
con committed
536
        } else {
537
            tok->f.kind = T_LESS;
con's avatar
con committed
538 539 540 541 542 543 544 545
        }
        break;

    case '>':
        if (_yychar == '>') {
            yyinp();
            if (_yychar == '=') {
                yyinp();
546
                tok->f.kind = T_GREATER_GREATER_EQUAL;
con's avatar
con committed
547
            } else
548 549
                tok->f.kind = T_LESS_LESS;
            tok->f.kind = T_GREATER_GREATER;
con's avatar
con committed
550 551
        } else if (_yychar == '=') {
            yyinp();
552
            tok->f.kind = T_GREATER_EQUAL;
con's avatar
con committed
553
        } else {
554
            tok->f.kind = T_GREATER;
con's avatar
con committed
555 556 557 558
        }
        break;

    case ',':
559
        tok->f.kind = T_COMMA;
con's avatar
con committed
560 561
        break;

562
    default: {
563
        if (_languageFeatures.objCEnabled) {
564 565 566 567 568 569 570 571 572 573 574 575 576 577
            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();
578 579 580 581
                scanStringLiteral(tok, '"');
                break;
            }
        }
582

583
        if (ch == 'L' || ch == 'u' || ch == 'U' || ch == 'R') {
584 585 586
            // Either a literal or still an identifier.
            if (_yychar == '"') {
                yyinp();
587 588 589 590
                if (ch == 'R')
                    scanRawStringLiteral(tok);
                else
                    scanStringLiteral(tok, ch);
591 592 593
            } else if (_yychar == '\'') {
                yyinp();
                scanCharLiteral(tok, ch);
594 595 596 597 598 599 600 601
            } else if (ch != 'R' && _yychar == 'R') {
                yyinp();
                if (_yychar == '"') {
                    yyinp();
                    scanRawStringLiteral(tok, ch);
                } else {
                    scanIdentifier(tok, 1);
                }
602
            } else if (ch == 'u' && _yychar == '8') {
603 604
                yyinp();
                if (_yychar == '"') {
605 606
                    yyinp();
                    scanStringLiteral(tok, '8');
607
                } else if (_yychar == '\'') {
608 609
                    yyinp();
                    scanCharLiteral(tok, '8');
610 611 612 613 614 615 616 617
                } else if (_yychar == 'R') {
                    yyinp();
                    if (_yychar == '"') {
                        yyinp();
                        scanRawStringLiteral(tok, '8');
                    } else {
                        scanIdentifier(tok, 2);
                    }
618
                } else {
619
                    scanIdentifier(tok, 1);
620
                }
621 622
            } else {
                scanIdentifier(tok);
623
            }
624 625 626 627 628 629
        } else if (std::isalpha(ch) || ch == '_' || ch == '$') {
            scanIdentifier(tok);
        } else if (std::isdigit(ch)) {
            scanNumericLiteral(tok);
        } else {
            tok->f.kind = T_ERROR;
630
        }
631 632
        break;
    } // default
633

634 635
    } // switch
}
con's avatar
con committed
636

637 638 639 640 641 642 643 644 645 646 647 648 649 650
void Lexer::scanStringLiteral(Token *tok, unsigned char hint)
{
    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;
651 652

    scanUntilQuote(tok, '"');
653
}
con's avatar
con committed
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 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
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;
}

716 717 718 719 720 721 722 723 724 725
void Lexer::scanCharLiteral(Token *tok, unsigned char hint)
{
    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;
726 727

    scanUntilQuote(tok, '\'');
728
}
con's avatar
con committed
729

730 731
void Lexer::scanUntilQuote(Token *tok, unsigned char quote)
{
732
    CPP_CHECK(quote == '"' || quote == '\'');
con's avatar
con committed
733

734
    const char *yytext = _currentChar;
735 736 737
    while (_yychar
           && _yychar != quote
           && _yychar != '\n') {
738 739 740
        if (_yychar == '\\')
            scanBackslash((Kind)tok->f.kind);
        else
741 742 743
            yyinp();
    }
    int yylen = _currentChar - yytext;
con's avatar
con committed
744

745 746
    if (_yychar == quote)
        yyinp();
747

748 749 750
    if (control())
        tok->string = control()->stringLiteral(yytext, yylen);
}
751

752 753 754 755 756 757 758 759
void Lexer::scanNumericLiteral(Token *tok)
{
    const char *yytext = _currentChar - 1;
    while (_yychar) {
        if (_yychar == 'e' || _yychar == 'E') {
            yyinp();
            if (_yychar == '-' || _yychar == '+') {
                yyinp();
760
                // ### CPP_CHECK(std::isdigit(_yychar));
con's avatar
con committed
761
            }
762 763
        } else if (std::isalnum(_yychar) || _yychar == '.') {
            yyinp();
con's avatar
con committed
764 765 766
        } else {
            break;
        }
767 768
    }
    int yylen = _currentChar - yytext;
con's avatar
con committed
769

770 771 772 773
    tok->f.kind = T_NUMERIC_LITERAL;

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

776
void Lexer::scanIdentifier(Token *tok, unsigned extraProcessedChars)
777
{
778
    const char *yytext = _currentChar - 1 - extraProcessedChars;
779 780 781 782
    while (std::isalnum(_yychar) || _yychar == '_' || _yychar == '$')
        yyinp();
    int yylen = _currentChar - yytext;
    if (f._scanKeywords)
783
        tok->f.kind = classify(yytext, yylen, _languageFeatures);
784 785 786 787 788
    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
789

790 791 792 793
        if (control())
            tok->identifier = control()->identifier(yytext, yylen);
    }
}
794 795 796 797 798 799 800 801 802 803 804

void Lexer::scanBackslash(Kind type)
{
    yyinp(); // skip '\\'
    if (_yychar && !std::isspace(_yychar)) {
        yyinp();
        return;
    }
    while (_yychar != '\n' && std::isspace(_yychar))
        yyinp();
    if (!_yychar) {
805 806
        s._tokenKind = type;
        s._newlineExpected = true;
807 808 809 810 811 812 813
        return;
    }
    if (_yychar == '\n') {
        yyinp();
        while (_yychar != '\n' && std::isspace(_yychar))
            yyinp();
        if (!_yychar)
814
            s._tokenKind = type;
815 816 817 818 819 820 821 822 823 824 825 826
    }
}

void Lexer::scanCppComment(Kind type)
{
    while (_yychar && _yychar != '\n') {
        if (_yychar == '\\')
            scanBackslash(type);
        else if (_yychar)
            yyinp();
    }
}