Changeset 114845 in webkit for trunk/Source/JavaScriptCore/parser/Lexer.cpp
- Timestamp:
- Apr 21, 2012, 1:03:13 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/JavaScriptCore/parser/Lexer.cpp
r114844 r114845 387 387 return "Invalid character: '`'"; 388 388 default: 389 return String::format("Invalid character '\\u%04u'", m_current).impl();389 return String::format("Invalid character '\\u%04u'", static_cast<unsigned>(m_current)).impl(); 390 390 } 391 391 } … … 426 426 m_current = *m_code; 427 427 else 428 m_current = -1;428 m_current = 0; 429 429 ASSERT(currentOffset() == source.startOffset()); 430 430 } … … 440 440 ALWAYS_INLINE void Lexer<T>::shift() 441 441 { 442 // Faster than an if-else sequence 443 ASSERT(m_current != -1); 444 m_current = -1; 445 m_code++; 442 // At one point timing showed that setting m_current to 0 unconditionally was faster than an if-else sequence. 443 m_current = 0; 444 ++m_code; 446 445 if (LIKELY(m_code < m_codeEnd)) 447 446 m_current = *m_code; … … 449 448 450 449 template <typename T> 451 ALWAYS_INLINE int Lexer<T>::peek(int offset) 450 ALWAYS_INLINE bool Lexer<T>::atEnd() const 451 { 452 ASSERT(!m_current || m_code < m_codeEnd); 453 return UNLIKELY(UNLIKELY(!m_current) && m_code == m_codeEnd); 454 } 455 456 template <typename T> 457 ALWAYS_INLINE T Lexer<T>::peek(int offset) const 452 458 { 453 459 ASSERT(offset > 0 && offset < 5); 454 460 const T* code = m_code + offset; 455 return (code < m_codeEnd) ? *code : -1;461 return (code < m_codeEnd) ? *code : 0; 456 462 } 457 463 … … 459 465 int Lexer<T>::parseFourDigitUnicodeHex() 460 466 { 461 intchar1 = peek(1);462 intchar2 = peek(2);463 intchar3 = peek(3);467 T char1 = peek(1); 468 T char2 = peek(2); 469 T char3 = peek(3); 464 470 465 471 if (UNLIKELY(!isASCIIHexDigit(m_current) || !isASCIIHexDigit(char1) || !isASCIIHexDigit(char2) || !isASCIIHexDigit(char3))) … … 477 483 void Lexer<T>::shiftLineTerminator() 478 484 { 479 ASSERT(isLineTerminator( static_cast<T>(m_current)));480 481 intprev = m_current;485 ASSERT(isLineTerminator(m_current)); 486 487 T prev = m_current; 482 488 shift(); 483 489 … … 646 652 const LChar* identifierStart = currentCharacter(); 647 653 648 while ( m_current != -1 && isIdentPart(static_cast<LChar>(m_current)))654 while (isIdentPart(m_current)) 649 655 shift(); 650 656 … … 696 702 UChar orAllChars = 0; 697 703 698 while ( m_current != -1 && isIdentPart(static_cast<UChar>(m_current))) {704 while (isIdentPart(m_current)) { 699 705 orAllChars |= m_current; 700 706 shift(); … … 748 754 749 755 while (true) { 750 if (LIKELY( m_current != -1 && isIdentPart(static_cast<T>(m_current)))) {756 if (LIKELY(isIdentPart(m_current))) { 751 757 shift(); 752 758 continue; … … 808 814 } 809 815 816 static ALWAYS_INLINE bool characterRequiresParseStringSlowCase(LChar character) 817 { 818 return character < 0xE; 819 } 820 821 static ALWAYS_INLINE bool characterRequiresParseStringSlowCase(UChar character) 822 { 823 return character < 0xE || character > 0xFF; 824 } 825 810 826 template <typename T> 811 827 template <bool shouldBuildStrings> ALWAYS_INLINE bool Lexer<T>::parseString(JSTokenData* tokenData, bool strictMode) … … 813 829 int startingOffset = currentOffset(); 814 830 int startingLineNumber = lineNumber(); 815 intstringQuoteCharacter = m_current;831 T stringQuoteCharacter = m_current; 816 832 shift(); 817 833 … … 819 835 820 836 while (m_current != stringQuoteCharacter) { 821 if (UNLIKELY( (m_current == '\\'))) {837 if (UNLIKELY(m_current == '\\')) { 822 838 if (stringStart != currentCharacter() && shouldBuildStrings) 823 839 append8(stringStart, currentCharacter() - stringStart); … … 839 855 return false; 840 856 } 841 intprev = m_current;857 T prev = m_current; 842 858 shift(); 843 859 if (shouldBuildStrings) … … 854 870 } 855 871 856 if (UNLIKELY( ((m_current > 0xff) || (m_current < 0xe)))) {872 if (UNLIKELY(characterRequiresParseStringSlowCase(m_current))) { 857 873 setOffset(startingOffset); 858 874 setLineNumber(startingLineNumber); … … 878 894 template <bool shouldBuildStrings> bool Lexer<T>::parseStringSlowCase(JSTokenData* tokenData, bool strictMode) 879 895 { 880 intstringQuoteCharacter = m_current;896 T stringQuoteCharacter = m_current; 881 897 shift(); 882 898 … … 896 912 record16(escape); 897 913 shift(); 898 } else if (UNLIKELY(isLineTerminator( static_cast<T>(m_current))))914 } else if (UNLIKELY(isLineTerminator(m_current))) 899 915 shiftLineTerminator(); 900 916 else if (m_current == 'x') { … … 904 920 return false; 905 921 } 906 intprev = m_current;922 T prev = m_current; 907 923 shift(); 908 924 if (shouldBuildStrings) … … 934 950 } else if (!strictMode && isASCIIOctalDigit(m_current)) { 935 951 // Octal character sequences 936 intcharacter1 = m_current;952 T character1 = m_current; 937 953 shift(); 938 954 if (isASCIIOctalDigit(m_current)) { 939 955 // Two octal characters 940 intcharacter2 = m_current;956 T character2 = m_current; 941 957 shift(); 942 958 if (character1 >= '0' && character1 <= '3' && isASCIIOctalDigit(m_current)) { … … 952 968 record16(character1 - '0'); 953 969 } 954 } else if ( m_current != -1) {970 } else if (!atEnd()) { 955 971 if (shouldBuildStrings) 956 972 record16(m_current); … … 965 981 } 966 982 // Fast check for characters that require special handling. 967 // Catches -1, \n, \r, 0x2028, and 0x2029 as efficiently983 // Catches 0, \n, \r, 0x2028, and 0x2029 as efficiently 968 984 // as possible, and lets through all common ASCII characters. 969 985 if (UNLIKELY(((static_cast<unsigned>(m_current) - 0xE) & 0x2000))) { 970 986 // New-line or end of input is not allowed 971 if ( UNLIKELY(m_current == -1) || UNLIKELY(isLineTerminator(static_cast<T>(m_current)))) {987 if (atEnd() || isLineTerminator(m_current)) { 972 988 m_lexErrorMessage = "Unexpected EOF"; 973 989 return false; … … 1146 1162 } 1147 1163 1148 if ( UNLIKELY(m_current == -1))1164 if (atEnd()) 1149 1165 return false; 1150 1166 1151 if (isLineTerminator( static_cast<T>(m_current))) {1167 if (isLineTerminator(m_current)) { 1152 1168 shiftLineTerminator(); 1153 1169 m_terminator = true; … … 1178 1194 1179 1195 start: 1180 while (m_current != -1 && isWhiteSpace(static_cast<T>(m_current))) 1181 shift(); 1182 1183 int startOffset = currentOffset(); 1184 1185 if (UNLIKELY(m_current == -1)) 1196 while (isWhiteSpace(m_current)) 1197 shift(); 1198 1199 if (atEnd()) 1186 1200 return EOFTOK; 1201 1202 tokenInfo->startOffset = currentOffset(); 1187 1203 1188 1204 CharacterType type; 1189 if (LIKELY(isLatin1( static_cast<T>(m_current))))1205 if (LIKELY(isLatin1(m_current))) 1190 1206 type = static_cast<CharacterType>(typesOfLatin1Characters[m_current]); 1191 1207 else if (isNonLatin1IdentStart(m_current)) 1192 1208 type = CharacterIdentifierStart; 1193 else if (isLineTerminator( static_cast<T>(m_current)))1209 else if (isLineTerminator(m_current)) 1194 1210 type = CharacterLineTerminator; 1195 1211 else … … 1476 1492 1477 1493 // No identifiers allowed directly after numeric literal, e.g. "3in" is bad. 1478 if (UNLIKELY( m_current != -1 && isIdentStart(static_cast<T>(m_current)))) {1494 if (UNLIKELY(isIdentStart(m_current))) { 1479 1495 m_lexErrorMessage = "At least one digit must occur after a decimal point"; 1480 1496 goto returnError; … … 1494 1510 break; 1495 1511 case CharacterIdentifierStart: 1496 ASSERT(isIdentStart( static_cast<T>(m_current)));1512 ASSERT(isIdentStart(m_current)); 1497 1513 // Fall through into CharacterBackSlash. 1498 1514 case CharacterBackSlash: … … 1503 1519 break; 1504 1520 case CharacterLineTerminator: 1505 ASSERT(isLineTerminator( static_cast<T>(m_current)));1521 ASSERT(isLineTerminator(m_current)); 1506 1522 shiftLineTerminator(); 1507 1523 m_atLineStart = true; … … 1521 1537 1522 1538 inSingleLineComment: 1523 while (!isLineTerminator( static_cast<T>(m_current))) {1524 if ( UNLIKELY(m_current == -1))1539 while (!isLineTerminator(m_current)) { 1540 if (atEnd()) 1525 1541 return EOFTOK; 1526 1542 shift(); … … 1537 1553 returnToken: 1538 1554 tokenInfo->line = m_lineNumber; 1539 tokenInfo->startOffset = startOffset;1540 1555 tokenInfo->endOffset = currentOffset(); 1541 1556 m_lastToken = token; … … 1545 1560 m_error = true; 1546 1561 tokenInfo->line = m_lineNumber; 1547 tokenInfo->startOffset = startOffset;1548 1562 tokenInfo->endOffset = currentOffset(); 1549 1563 return ERRORTOK; … … 1566 1580 1567 1581 while (true) { 1568 int current = m_current; 1569 1570 if (isLineTerminator(static_cast<T>(current)) || current == -1) { 1582 if (isLineTerminator(m_current) || atEnd()) { 1571 1583 m_buffer16.resize(0); 1572 1584 return false; 1573 1585 } 1574 1586 1575 shift(); 1576 1577 if (current == '/' && !lastWasEscape && !inBrackets) 1578 break; 1579 1580 record16(current); 1587 T prev = m_current; 1588 1589 shift(); 1590 1591 if (prev == '/' && !lastWasEscape && !inBrackets) 1592 break; 1593 1594 record16(prev); 1581 1595 1582 1596 if (lastWasEscape) { … … 1585 1599 } 1586 1600 1587 switch ( current) {1601 switch (prev) { 1588 1602 case '[': 1589 1603 inBrackets = true; … … 1601 1615 m_buffer16.resize(0); 1602 1616 1603 while ( m_current != -1 && isIdentPart(static_cast<T>(m_current))) {1617 while (isIdentPart(m_current)) { 1604 1618 record16(m_current); 1605 1619 shift(); … … 1619 1633 1620 1634 while (true) { 1621 int current = m_current; 1622 1623 if (isLineTerminator(static_cast<T>(current)) || current == -1) 1635 if (isLineTerminator(m_current) || atEnd()) 1624 1636 return false; 1625 1637 1626 shift(); 1627 1628 if (current == '/' && !lastWasEscape && !inBrackets) 1638 T prev = m_current; 1639 1640 shift(); 1641 1642 if (prev == '/' && !lastWasEscape && !inBrackets) 1629 1643 break; 1630 1644 … … 1634 1648 } 1635 1649 1636 switch ( current) {1650 switch (prev) { 1637 1651 case '[': 1638 1652 inBrackets = true; … … 1647 1661 } 1648 1662 1649 while ( m_current != -1 && isIdentPart(static_cast<T>(m_current)))1663 while (isIdentPart(m_current)) 1650 1664 shift(); 1651 1665
Note:
See TracChangeset
for help on using the changeset viewer.