1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 05:37:35 +00:00

LibJS: Lex private identifiers, identifiers prefixed with a '#'

This commit is contained in:
davidot 2021-10-08 00:39:17 +02:00 committed by Linus Groh
parent 6b2accce31
commit eeb42c21d1
2 changed files with 24 additions and 0 deletions

View file

@ -600,6 +600,29 @@ Token Lexer::next()
else else
token_type = TokenType::TemplateLiteralString; token_type = TokenType::TemplateLiteralString;
} }
} else if (m_current_char == '#') {
// Note: This has some duplicated code with the identifier lexing below
consume();
auto code_point = is_identifier_start(identifier_length);
if (code_point.has_value()) {
StringBuilder builder;
builder.append_code_point('#');
do {
builder.append_code_point(*code_point);
for (size_t i = 0; i < identifier_length; ++i)
consume();
code_point = is_identifier_middle(identifier_length);
} while (code_point.has_value());
identifier = builder.string_view();
token_type = TokenType::PrivateIdentifier;
m_parsed_identifiers->identifiers.set(*identifier);
} else {
token_type = TokenType::Invalid;
token_message = "Start of private name '#' but not followed by valid identifier";
}
} else if (auto code_point = is_identifier_start(identifier_length); code_point.has_value()) { } else if (auto code_point = is_identifier_start(identifier_length); code_point.has_value()) {
bool has_escaped_character = false; bool has_escaped_character = false;
// identifier or keyword // identifier or keyword

View file

@ -117,6 +117,7 @@ constexpr const u32 ZERO_WIDTH_JOINER { 0x200D };
__ENUMERATE_JS_TOKEN(PlusEquals, Operator) \ __ENUMERATE_JS_TOKEN(PlusEquals, Operator) \
__ENUMERATE_JS_TOKEN(PlusPlus, Operator) \ __ENUMERATE_JS_TOKEN(PlusPlus, Operator) \
__ENUMERATE_JS_TOKEN(Private, Keyword) \ __ENUMERATE_JS_TOKEN(Private, Keyword) \
__ENUMERATE_JS_TOKEN(PrivateIdentifier, Identifier) \
__ENUMERATE_JS_TOKEN(Protected, Keyword) \ __ENUMERATE_JS_TOKEN(Protected, Keyword) \
__ENUMERATE_JS_TOKEN(Public, Keyword) \ __ENUMERATE_JS_TOKEN(Public, Keyword) \
__ENUMERATE_JS_TOKEN(QuestionMark, Operator) \ __ENUMERATE_JS_TOKEN(QuestionMark, Operator) \