Lexer.cpp 20.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(T_EOF_SYMBOL),
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(T_EOF_SYMBOL),
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

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);
126
    tok->f.length = _currentChar - _tokenStart;
con's avatar
con committed
127 128 129 130 131 132
}

void Lexer::scan_helper(Token *tok)
{
  _Lagain:
    while (_yychar && std::isspace(_yychar)) {
Erik Verbruggen's avatar
Erik Verbruggen committed
133 134
        if (_yychar == '\n') {
            tok->f.joined = false;
135
            tok->f.newline = true;
Erik Verbruggen's avatar
Erik Verbruggen committed
136
        } else {
137
            tok->f.whitespace = true;
Erik Verbruggen's avatar
Erik Verbruggen committed
138
        }
con's avatar
con committed
139 140 141 142 143 144
        yyinp();
    }

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

145 146 147
    _tokenStart = _currentChar;
    tok->offset = _currentChar - _firstChar;

148 149 150
    switch (_state) {
    case T_COMMENT:
    case T_DOXY_COMMENT: {
151 152
        const int originalState = _state;

con's avatar
con committed
153
        if (! _yychar) {
154
            tok->f.kind = T_EOF_SYMBOL;
con's avatar
con committed
155 156 157 158 159 160 161 162 163 164
            return;
        }

        while (_yychar) {
            if (_yychar != '*')
                yyinp();
            else {
                yyinp();
                if (_yychar == '/') {
                    yyinp();
165
                    _state = T_EOF_SYMBOL;
con's avatar
con committed
166 167 168 169 170
                    break;
                }
            }
        }

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

174
        tok->f.kind = originalState;
con's avatar
con committed
175 176
        return; // done
    }
177
    }
con's avatar
con committed
178 179

    if (! _yychar) {
180
        tok->f.kind = T_EOF_SYMBOL;
con's avatar
con committed
181 182 183
        return;
    }

184
    unsigned char ch = _yychar;
con's avatar
con committed
185 186 187 188 189 190
    yyinp();

    switch (ch) {
    case '\\':
        while (_yychar != '\n' && std::isspace(_yychar))
            yyinp();
191
        // ### CPP_CHECK(! _yychar || _yychar == '\n');
con's avatar
con committed
192
        if (_yychar == '\n') {
193 194
            tok->f.joined = true;
            tok->f.newline = false;
con's avatar
con committed
195 196 197 198
            yyinp();
        }
        goto _Lagain;

199 200 201
    case '"':
        scanStringLiteral(tok);
        break;
con's avatar
con committed
202

203 204 205
    case '\'':
        scanCharLiteral(tok);
        break;
con's avatar
con committed
206 207

    case '{':
208
        tok->f.kind = T_LBRACE;
con's avatar
con committed
209 210 211
        break;

    case '}':
212
        tok->f.kind = T_RBRACE;
con's avatar
con committed
213 214 215
        break;

    case '[':
216
        tok->f.kind = T_LBRACKET;
con's avatar
con committed
217 218 219
        break;

    case ']':
220
        tok->f.kind = T_RBRACKET;
con's avatar
con committed
221 222 223 224
        break;

    case '#':
        if (_yychar == '#') {
225
            tok->f.kind = T_POUND_POUND;
con's avatar
con committed
226 227
            yyinp();
        } else {
228
            tok->f.kind = T_POUND;
con's avatar
con committed
229 230 231 232
        }
        break;

    case '(':
233
        tok->f.kind = T_LPAREN;
con's avatar
con committed
234 235 236
        break;

    case ')':
237
        tok->f.kind = T_RPAREN;
con's avatar
con committed
238 239 240
        break;

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

    case ':':
        if (_yychar == ':') {
            yyinp();
247
            tok->f.kind = T_COLON_COLON;
248 249 250
        } else if (_yychar == '>') {
            yyinp();
            tok->f.kind = T_RBRACKET;
con's avatar
con committed
251
        } else {
252
            tok->f.kind = T_COLON;
con's avatar
con committed
253 254 255 256 257 258
        }
        break;

    case '.':
        if (_yychar == '*') {
            yyinp();
259
            tok->f.kind = T_DOT_STAR;
con's avatar
con committed
260 261
        } else if (_yychar == '.') {
            yyinp();
262
            // ### CPP_CHECK(_yychar);
con's avatar
con committed
263 264
            if (_yychar == '.') {
                yyinp();
265
                tok->f.kind = T_DOT_DOT_DOT;
con's avatar
con committed
266
            } else {
267
                tok->f.kind = T_ERROR;
con's avatar
con committed
268 269 270 271 272 273 274 275
            }
        } else if (std::isdigit(_yychar)) {
            const char *yytext = _currentChar - 2;
            do {
                if (_yychar == 'e' || _yychar == 'E') {
                    yyinp();
                    if (_yychar == '-' || _yychar == '+') {
                        yyinp();
276
                        // ### CPP_CHECK(std::isdigit(_yychar));
con's avatar
con committed
277 278 279 280 281 282 283 284
                    }
                } else if (std::isalnum(_yychar) || _yychar == '.') {
                    yyinp();
                } else {
                    break;
                }
            } while (_yychar);
            int yylen = _currentChar - yytext;
285
            tok->f.kind = T_NUMERIC_LITERAL;
con's avatar
con committed
286
            if (control())
287
                tok->number = control()->numericLiteral(yytext, yylen);
con's avatar
con committed
288
        } else {
289
            tok->f.kind = T_DOT;
con's avatar
con committed
290 291 292 293
        }
        break;

    case '?':
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
        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
312 313 314 315 316
        break;

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

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

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

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

            bool doxy = false;

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

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

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

372
            tok->f.kind = doxy ? T_CPP_DOXY_COMMENT : T_CPP_COMMENT;
373

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

377
            Kind commentKind = T_COMMENT;
378 379

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

382 383
                yyinp();

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

387 388 389
                if (_yychar == '<')
                    yyinp();

390
                if (! _yychar || std::isspace(_yychar))
391
                    commentKind = T_DOXY_COMMENT;
392
            }
393

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

Roberto Raggi's avatar
Roberto Raggi committed
404
        _Ldone:
con's avatar
con committed
405 406 407
            if (_yychar)
                yyinp();
            else
408
                _state = commentKind;
con's avatar
con committed
409

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

413
            tok->f.kind = commentKind;
414

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

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

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

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

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

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

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

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

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

    case '>':
        if (_yychar == '>') {
            yyinp();
            if (_yychar == '=') {
                yyinp();
536
                tok->f.kind = T_GREATER_GREATER_EQUAL;
con's avatar
con committed
537
            } else
538 539
                tok->f.kind = T_LESS_LESS;
            tok->f.kind = T_GREATER_GREATER;
con's avatar
con committed
540 541
        } else if (_yychar == '=') {
            yyinp();
542
            tok->f.kind = T_GREATER_EQUAL;
con's avatar
con committed
543
        } else {
544
            tok->f.kind = T_GREATER;
con's avatar
con committed
545 546 547 548
        }
        break;

    case ',':
549
        tok->f.kind = T_COMMA;
con's avatar
con committed
550 551
        break;

552
    default: {
553
        if (_languageFeatures.objCEnabled) {
554 555 556 557 558 559 560 561 562 563 564 565 566 567
            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();
568 569 570 571
                scanStringLiteral(tok, '"');
                break;
            }
        }
572

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

624 625
    } // switch
}
con's avatar
con committed
626

627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
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
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 698 699 700 701 702 703 704 705
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;
}

706 707 708 709 710 711 712 713 714 715 716 717 718
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
719

720 721
void Lexer::scanUntilQuote(Token *tok, unsigned char quote)
{
722
    CPP_CHECK(quote == '"' || quote == '\'');
con's avatar
con committed
723

724
    const char *yytext = _currentChar;
725 726 727
    while (_yychar
           && _yychar != quote
           && _yychar != '\n') {
728 729 730 731 732
        if (_yychar != '\\')
            yyinp();
        else {
            yyinp(); // skip `\\'
            if (_yychar)
con's avatar
con committed
733
                yyinp();
734 735 736
        }
    }
    int yylen = _currentChar - yytext;
con's avatar
con committed
737

738 739
    if (_yychar == quote)
        yyinp();
740

741 742 743
    if (control())
        tok->string = control()->stringLiteral(yytext, yylen);
}
744

745 746 747 748 749 750 751 752
void Lexer::scanNumericLiteral(Token *tok)
{
    const char *yytext = _currentChar - 1;
    while (_yychar) {
        if (_yychar == 'e' || _yychar == 'E') {
            yyinp();
            if (_yychar == '-' || _yychar == '+') {
                yyinp();
753
                // ### CPP_CHECK(std::isdigit(_yychar));
con's avatar
con committed
754
            }
755 756
        } else if (std::isalnum(_yychar) || _yychar == '.') {
            yyinp();
con's avatar
con committed
757 758 759
        } else {
            break;
        }
760 761
    }
    int yylen = _currentChar - yytext;
con's avatar
con committed
762

763 764 765 766
    tok->f.kind = T_NUMERIC_LITERAL;

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

769
void Lexer::scanIdentifier(Token *tok, unsigned extraProcessedChars)
770
{
771
    const char *yytext = _currentChar - 1 - extraProcessedChars;
772 773 774 775
    while (std::isalnum(_yychar) || _yychar == '_' || _yychar == '$')
        yyinp();
    int yylen = _currentChar - yytext;
    if (f._scanKeywords)
776
        tok->f.kind = classify(yytext, yylen, _languageFeatures);
777 778 779 780 781
    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
782

783 784 785 786
        if (control())
            tok->identifier = control()->identifier(yytext, yylen);
    }
}