Lexer.cpp 22.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 34 35 36 37 38
/*!
    \class Lexer
    \brief The Lexer generates tokens from an UTF-8 encoded source text.

    \sa Token
*/

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
/*!
    \fn static void Lexer::yyinp_utf8(const char *&currentSourceChar, unsigned char &yychar, unsigned &utf16charCounter)

    Process a single unicode code point in an UTF-8 encoded source.

    \a currentSourceChar points to the UTF-8 encoded source.
    \a yychar must be the byte pointed to by \a currentSourceChar.

    Points \a currentSourceChar to the byte of the next code point
    and modifies \a yychar to the value pointed by the updated
    \a currentSourceChar. \a utf16charCounter will be incremented by
    the number of UTF-16 code units that were needed for that code
    point.
*/

con's avatar
con committed
54 55
Lexer::Lexer(TranslationUnit *unit)
    : _translationUnit(unit),
hjk's avatar
hjk committed
56
      _control(unit->control()),
57
      _state(0),
con's avatar
con committed
58 59 60
      _flags(0),
      _currentLine(1)
{
61
    f._scanKeywords = true;
con's avatar
con committed
62 63 64 65 66 67
    setSource(_translationUnit->firstSourceChar(),
              _translationUnit->lastSourceChar());
}

Lexer::Lexer(const char *firstChar, const char *lastChar)
    : _translationUnit(0),
hjk's avatar
hjk committed
68
      _control(0),
69
      _state(0),
con's avatar
con committed
70 71 72
      _flags(0),
      _currentLine(1)
{
73
    f._scanKeywords = true;
con's avatar
con committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87
    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;
88
    _currentCharUtf16 = -1;
con's avatar
con committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    _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
108
{ return f._scanCommentTokens; }
con's avatar
con committed
109 110

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

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

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

void Lexer::setScanAngleStringLiteralTokens(bool onoff)
120
{ f._scanAngleStringLiteralTokens = onoff; }
con's avatar
con committed
121 122 123 124 125 126

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

    if (_translationUnit)
127
        _translationUnit->pushLineOffset(_currentCharUtf16);
con's avatar
con committed
128 129 130 131 132 133
}

void Lexer::scan(Token *tok)
{
    tok->reset();
    scan_helper(tok);
Nikolai Kosjar's avatar
Nikolai Kosjar committed
134
    tok->f.bytes = _currentChar - _tokenStart;
135
    tok->f.utf16chars = _currentCharUtf16 - _tokenStartUtf16;
con's avatar
con committed
136 137 138 139
}

void Lexer::scan_helper(Token *tok)
{
Erik Verbruggen's avatar
Erik Verbruggen committed
140
  again:
con's avatar
con committed
141
    while (_yychar && std::isspace(_yychar)) {
Erik Verbruggen's avatar
Erik Verbruggen committed
142
        if (_yychar == '\n') {
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
            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
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
    _tokenStart = _currentChar;
Nikolai Kosjar's avatar
Nikolai Kosjar committed
168
    tok->byteOffset = _currentChar - _firstChar;
169

170 171 172
    _tokenStartUtf16 = _currentCharUtf16;
    tok->utf16charOffset = _currentCharUtf16;

173 174 175
    if (_yychar) {
        s._newlineExpected = false;
    } else if (s._tokenKind) {
176 177 178 179
        tok->f.kind = T_EOF_SYMBOL;
        return;
    }

180
    switch (s._tokenKind) {
181 182
    case T_EOF_SYMBOL:
        break;
183 184
    case T_COMMENT:
    case T_DOXY_COMMENT: {
185
        const int originalKind = s._tokenKind;
186

con's avatar
con committed
187 188 189 190 191 192 193
        while (_yychar) {
            if (_yychar != '*')
                yyinp();
            else {
                yyinp();
                if (_yychar == '/') {
                    yyinp();
194
                    _state = 0;
con's avatar
con committed
195 196 197 198 199
                    break;
                }
            }
        }

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

203
        tok->f.kind = originalKind;
con's avatar
con committed
204 205
        return; // done
    }
206
    case T_CPP_COMMENT:
207 208
    case T_CPP_DOXY_COMMENT: {
        const Kind originalKind = (Kind)s._tokenKind;
209
        tok->f.joined = true;
210 211
        if (f._scanCommentTokens)
            tok->f.kind = originalKind;
212
        _state = 0;
213
        scanCppComment(originalKind);
214
        return;
215
    }
216
    default: // Strings
217 218 219
        tok->f.joined = true;
        tok->f.kind = s._tokenKind;
        _state = 0;
220 221
        scanUntilQuote(tok, '"');
        return;
222
    }
con's avatar
con committed
223 224

    if (! _yychar) {
225
        tok->f.kind = T_EOF_SYMBOL;
con's avatar
con committed
226 227 228
        return;
    }

229
    unsigned char ch = _yychar;
con's avatar
con committed
230 231 232 233
    yyinp();

    switch (ch) {
    case '\\':
234
        s._newlineExpected = true;
Erik Verbruggen's avatar
Erik Verbruggen committed
235
        goto again;
con's avatar
con committed
236

237 238 239
    case '"':
        scanStringLiteral(tok);
        break;
con's avatar
con committed
240

241 242 243
    case '\'':
        scanCharLiteral(tok);
        break;
con's avatar
con committed
244 245

    case '{':
246
        tok->f.kind = T_LBRACE;
con's avatar
con committed
247 248 249
        break;

    case '}':
250
        tok->f.kind = T_RBRACE;
con's avatar
con committed
251 252 253
        break;

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

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

    case '#':
        if (_yychar == '#') {
263
            tok->f.kind = T_POUND_POUND;
con's avatar
con committed
264 265
            yyinp();
        } else {
266
            tok->f.kind = T_POUND;
con's avatar
con committed
267 268 269 270
        }
        break;

    case '(':
271
        tok->f.kind = T_LPAREN;
con's avatar
con committed
272 273 274
        break;

    case ')':
275
        tok->f.kind = T_RPAREN;
con's avatar
con committed
276 277 278
        break;

    case ';':
279
        tok->f.kind = T_SEMICOLON;
con's avatar
con committed
280 281 282 283 284
        break;

    case ':':
        if (_yychar == ':') {
            yyinp();
285
            tok->f.kind = T_COLON_COLON;
286 287 288
        } else if (_yychar == '>') {
            yyinp();
            tok->f.kind = T_RBRACKET;
con's avatar
con committed
289
        } else {
290
            tok->f.kind = T_COLON;
con's avatar
con committed
291 292 293 294 295 296
        }
        break;

    case '.':
        if (_yychar == '*') {
            yyinp();
297
            tok->f.kind = T_DOT_STAR;
con's avatar
con committed
298 299
        } else if (_yychar == '.') {
            yyinp();
300
            // ### CPP_CHECK(_yychar);
con's avatar
con committed
301 302
            if (_yychar == '.') {
                yyinp();
303
                tok->f.kind = T_DOT_DOT_DOT;
con's avatar
con committed
304
            } else {
305
                tok->f.kind = T_ERROR;
con's avatar
con committed
306 307 308 309 310 311 312 313
            }
        } else if (std::isdigit(_yychar)) {
            const char *yytext = _currentChar - 2;
            do {
                if (_yychar == 'e' || _yychar == 'E') {
                    yyinp();
                    if (_yychar == '-' || _yychar == '+') {
                        yyinp();
314
                        // ### CPP_CHECK(std::isdigit(_yychar));
con's avatar
con committed
315 316 317 318 319 320 321 322
                    }
                } else if (std::isalnum(_yychar) || _yychar == '.') {
                    yyinp();
                } else {
                    break;
                }
            } while (_yychar);
            int yylen = _currentChar - yytext;
323
            tok->f.kind = T_NUMERIC_LITERAL;
con's avatar
con committed
324
            if (control())
325
                tok->number = control()->numericLiteral(yytext, yylen);
con's avatar
con committed
326
        } else {
327
            tok->f.kind = T_DOT;
con's avatar
con committed
328 329 330 331
        }
        break;

    case '?':
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
        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
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
            yyinp();

397
            Kind commentType = T_CPP_COMMENT;
398 399 400

            if (_yychar == '/' || _yychar == '!') {
                yyinp();
401
                commentType = T_CPP_DOXY_COMMENT;
402 403
            }

404
            scanCppComment(commentType);
405

406
            if (! f._scanCommentTokens)
Erik Verbruggen's avatar
Erik Verbruggen committed
407
                goto again;
408

409
            tok->f.kind = commentType;
410

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

414
            Kind commentKind = T_COMMENT;
415 416

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

419 420
                yyinp();

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

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

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

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

Erik Verbruggen's avatar
Erik Verbruggen committed
441
        done:
con's avatar
con committed
442 443 444
            if (_yychar)
                yyinp();
            else
445
                s._tokenKind = commentKind;
con's avatar
con committed
446

447
            if (! f._scanCommentTokens)
Erik Verbruggen's avatar
Erik Verbruggen committed
448
                goto again;
449

450
            tok->f.kind = commentKind;
451

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

    case '%':
        if (_yychar == '=') {
            yyinp();
463
            tok->f.kind = T_PERCENT_EQUAL;
464 465 466 467 468 469
        } else if (_yychar == '>') {
            yyinp();
            tok->f.kind = T_RBRACE;
        } else if (_yychar == ':') {
            yyinp();
            tok->f.kind = T_POUND;
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
            const char *yytext = _currentChar;
            while (_yychar && _yychar != '>')
                yyinp();
            int yylen = _currentChar - yytext;
541
            // ### CPP_CHECK(_yychar == '>');
con's avatar
con committed
542 543 544
            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;
557 558 559 560 561 562
        } else if (_yychar == ':') {
            yyinp();
            tok->f.kind = T_LBRACKET;
        } else if (_yychar == '%') {
            yyinp();
            tok->f.kind = T_LBRACE;
con's avatar
con committed
563
        } else {
564
            tok->f.kind = T_LESS;
con's avatar
con committed
565 566 567 568 569 570 571 572
        }
        break;

    case '>':
        if (_yychar == '>') {
            yyinp();
            if (_yychar == '=') {
                yyinp();
573
                tok->f.kind = T_GREATER_GREATER_EQUAL;
con's avatar
con committed
574
            } else
575 576
                tok->f.kind = T_LESS_LESS;
            tok->f.kind = T_GREATER_GREATER;
con's avatar
con committed
577 578
        } else if (_yychar == '=') {
            yyinp();
579
            tok->f.kind = T_GREATER_EQUAL;
con's avatar
con committed
580
        } else {
581
            tok->f.kind = T_GREATER;
con's avatar
con committed
582 583 584 585
        }
        break;

    case ',':
586
        tok->f.kind = T_COMMA;
con's avatar
con committed
587 588
        break;

589
    default: {
590
        if (_languageFeatures.objCEnabled) {
591 592 593 594 595 596 597 598 599 600 601 602 603 604
            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();
605 606 607 608
                scanStringLiteral(tok, '"');
                break;
            }
        }
609

610
        if (ch == 'L' || ch == 'u' || ch == 'U' || ch == 'R') {
611 612 613
            // Either a literal or still an identifier.
            if (_yychar == '"') {
                yyinp();
614 615 616 617
                if (ch == 'R')
                    scanRawStringLiteral(tok);
                else
                    scanStringLiteral(tok, ch);
618 619 620
            } else if (_yychar == '\'') {
                yyinp();
                scanCharLiteral(tok, ch);
621 622 623 624 625 626 627 628
            } else if (ch != 'R' && _yychar == 'R') {
                yyinp();
                if (_yychar == '"') {
                    yyinp();
                    scanRawStringLiteral(tok, ch);
                } else {
                    scanIdentifier(tok, 1);
                }
629
            } else if (ch == 'u' && _yychar == '8') {
630 631
                yyinp();
                if (_yychar == '"') {
632 633
                    yyinp();
                    scanStringLiteral(tok, '8');
634
                } else if (_yychar == '\'') {
635 636
                    yyinp();
                    scanCharLiteral(tok, '8');
637 638 639 640 641 642 643 644
                } else if (_yychar == 'R') {
                    yyinp();
                    if (_yychar == '"') {
                        yyinp();
                        scanRawStringLiteral(tok, '8');
                    } else {
                        scanIdentifier(tok, 2);
                    }
645
                } else {
646
                    scanIdentifier(tok, 1);
647
                }
648 649
            } else {
                scanIdentifier(tok);
650
            }
651 652
        } else if (std::isalpha(ch) || ch == '_' || ch == '$' || isByteOfMultiByteCodePoint(ch)) {
            scanIdentifier(tok, _currentChar - _tokenStart - 1);
653 654 655 656
        } else if (std::isdigit(ch)) {
            scanNumericLiteral(tok);
        } else {
            tok->f.kind = T_ERROR;
657
        }
658 659
        break;
    } // default
660

661 662
    } // switch
}
con's avatar
con committed
663

664 665 666 667 668 669 670 671 672 673 674 675 676 677
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;
678 679

    scanUntilQuote(tok, '"');
680
}
con's avatar
con committed
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 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
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;
}

743 744 745 746 747 748 749 750 751 752
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;
753 754

    scanUntilQuote(tok, '\'');
755
}
con's avatar
con committed
756

757 758
void Lexer::scanUntilQuote(Token *tok, unsigned char quote)
{
759
    CPP_CHECK(quote == '"' || quote == '\'');
con's avatar
con committed
760

761
    const char *yytext = _currentChar;
762 763 764
    while (_yychar
           && _yychar != quote
           && _yychar != '\n') {
765 766 767
        if (_yychar == '\\')
            scanBackslash((Kind)tok->f.kind);
        else
768 769 770
            yyinp();
    }
    int yylen = _currentChar - yytext;
con's avatar
con committed
771

772 773
    if (_yychar == quote)
        yyinp();
774

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

779 780 781 782 783 784 785 786
void Lexer::scanNumericLiteral(Token *tok)
{
    const char *yytext = _currentChar - 1;
    while (_yychar) {
        if (_yychar == 'e' || _yychar == 'E') {
            yyinp();
            if (_yychar == '-' || _yychar == '+') {
                yyinp();
787
                // ### CPP_CHECK(std::isdigit(_yychar));
con's avatar
con committed
788
            }
789 790
        } else if (std::isalnum(_yychar) || _yychar == '.') {
            yyinp();
con's avatar
con committed
791 792 793
        } else {
            break;
        }
794 795
    }
    int yylen = _currentChar - yytext;
con's avatar
con committed
796

797 798 799 800
    tok->f.kind = T_NUMERIC_LITERAL;

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

803
void Lexer::scanIdentifier(Token *tok, unsigned extraProcessedChars)
804
{
805
    const char *yytext = _currentChar - 1 - extraProcessedChars;
806 807
    while (std::isalnum(_yychar) || _yychar == '_' || _yychar == '$'
            || isByteOfMultiByteCodePoint(_yychar)) {
808
        yyinp();
809
    }
810 811
    int yylen = _currentChar - yytext;
    if (f._scanKeywords)
812
        tok->f.kind = classify(yytext, yylen, _languageFeatures);
813 814 815 816 817
    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
818

819 820 821 822
        if (control())
            tok->identifier = control()->identifier(yytext, yylen);
    }
}
823 824 825 826 827 828 829 830 831 832 833

void Lexer::scanBackslash(Kind type)
{
    yyinp(); // skip '\\'
    if (_yychar && !std::isspace(_yychar)) {
        yyinp();
        return;
    }
    while (_yychar != '\n' && std::isspace(_yychar))
        yyinp();
    if (!_yychar) {
834 835
        s._tokenKind = type;
        s._newlineExpected = true;
836 837 838 839 840 841 842
        return;
    }
    if (_yychar == '\n') {
        yyinp();
        while (_yychar != '\n' && std::isspace(_yychar))
            yyinp();
        if (!_yychar)
843
            s._tokenKind = type;
844 845 846 847 848 849 850 851 852 853 854 855
    }
}

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