Commit cd78a045 authored by Leszek Swirski's avatar Leszek Swirski Committed by Commit Bot

[parser] Tighten CanBeKeywordCharacter

Use the list of keywords to tighten the CannotBeKeyword scan flag to
also exclude lower case letters which are not present in any of the
keywords.

Change-Id: I6a00b5f5ee8f47088539806f15890a7489441fea
Reviewed-on: https://chromium-review.googlesource.com/c/1347475
Commit-Queue: Toon Verwaest <verwaest@chromium.org>
Reviewed-by: 's avatarToon Verwaest <verwaest@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57754}
parent b03ae187
......@@ -126,11 +126,30 @@ V8_INLINE Token::Value KeywordOrIdentifierToken(const uint8_t* input,
KEYWORDS(KEYWORD_GROUP_CASE, KEYWORD)
}
return Token::IDENTIFIER;
#undef KEYWORDS
#undef KEYWORD
#undef KEYWORD_GROUP_CASE
}
// Recursive constexpr template magic to check if a character is in a given
// string.
template <int N>
constexpr bool IsInString(const char (&s)[N], char c, size_t i = 0) {
return i >= N ? false : s[i] == c ? true : IsInString(s, c, i + 1);
}
inline constexpr bool CanBeKeywordCharacter(char c) {
return IsInString(
#define KEYWORD_GROUP_CASE(ch) // Nothing
#define KEYWORD(keyword, token) keyword
// Use C string literal concatenation ("a" "b" becomes "ab") to build one
// giant string containing all the keywords.
KEYWORDS(KEYWORD_GROUP_CASE, KEYWORD)
#undef KEYWORD
#undef KEYWORD_GROUP_CASE
,
c);
}
// Make sure tokens are stored as a single byte.
STATIC_ASSERT(sizeof(Token::Value) == 1);
......@@ -189,6 +208,8 @@ static const constexpr Token::Value one_char_tokens[128] = {
#undef CALL_GET_SCAN_FLAGS
};
#undef KEYWORDS
V8_INLINE Token::Value Scanner::ScanIdentifierOrKeyword() {
next().literal_chars.Start();
return ScanIdentifierOrKeywordInner();
......@@ -208,10 +229,8 @@ constexpr uint8_t GetScanFlags(char c) {
return
// Keywords are all lowercase and only contain letters.
// Note that non-identifier characters do not set this flag, so
// that it plays well with kTerminatesLiteral
// TODO(leszeks): We could probably get an even tighter measure
// here if not all letters are present in keywords.
(IsAsciiIdentifier(c) && !IsInRange(c, 'a', 'z')
// that it plays well with kTerminatesLiteral.
(IsAsciiIdentifier(c) && !CanBeKeywordCharacter(c)
? static_cast<uint8_t>(ScanFlags::kCannotBeKeyword)
: 0) |
(IsKeywordStart(c)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment