1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-16 16:24:59 +00:00

CppLexer: Support L, u, u8, U prefixes on string and char literals

This commit is contained in:
Nico Weber 2020-07-26 20:16:55 -04:00 committed by Andreas Kling
parent 34dc163441
commit 4d783338c1

View file

@ -308,6 +308,22 @@ Vector<CppToken> CppLexer::lex()
}
};
auto match_string_prefix = [&](char quote) -> size_t {
if (peek() == quote)
return 1;
if (peek() == 'L' && peek(1) == quote)
return 2;
if (peek() == 'u') {
if (peek(1) == quote)
return 2;
if (peek(1) == '8' && peek(2) == quote)
return 3;
}
if (peek() == 'U' && peek(1) == quote)
return 2;
return 0;
};
while (m_index < m_input.length()) {
auto ch = peek();
if (isspace(ch)) {
@ -597,13 +613,13 @@ Vector<CppToken> CppLexer::lex()
emit_token_equals(CppToken::Type::Slash, CppToken::Type::SlashEquals);
continue;
}
if (ch == '"') {
if (size_t prefix = match_string_prefix('"'); prefix > 0) {
begin_token();
consume();
for (size_t i = 0; i < prefix; ++i)
consume();
while (peek()) {
if (peek() == '\\') {
size_t escape = match_escape_sequence();
if (escape > 0) {
if (size_t escape = match_escape_sequence(); escape > 0) {
commit_token(CppToken::Type::DoubleQuotedString);
begin_token();
for (size_t i = 0; i < escape; ++i)
@ -620,13 +636,13 @@ Vector<CppToken> CppLexer::lex()
commit_token(CppToken::Type::DoubleQuotedString);
continue;
}
if (ch == '\'') {
if (size_t prefix = match_string_prefix('\''); prefix > 0) {
begin_token();
consume();
for (size_t i = 0; i < prefix; ++i)
consume();
while (peek()) {
if (peek() == '\\') {
size_t escape = match_escape_sequence();
if (escape > 0) {
if (size_t escape = match_escape_sequence(); escape > 0) {
commit_token(CppToken::Type::SingleQuotedString);
begin_token();
for (size_t i = 0; i < escape; ++i)