Skip to content

Commit 4d78333

Browse files
nicoawesomekling
authored andcommitted
CppLexer: Support L, u, u8, U prefixes on string and char literals
1 parent 34dc163 commit 4d78333

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

Libraries/LibGUI/CppLexer.cpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,22 @@ Vector<CppToken> CppLexer::lex()
308308
}
309309
};
310310

311+
auto match_string_prefix = [&](char quote) -> size_t {
312+
if (peek() == quote)
313+
return 1;
314+
if (peek() == 'L' && peek(1) == quote)
315+
return 2;
316+
if (peek() == 'u') {
317+
if (peek(1) == quote)
318+
return 2;
319+
if (peek(1) == '8' && peek(2) == quote)
320+
return 3;
321+
}
322+
if (peek() == 'U' && peek(1) == quote)
323+
return 2;
324+
return 0;
325+
};
326+
311327
while (m_index < m_input.length()) {
312328
auto ch = peek();
313329
if (isspace(ch)) {
@@ -597,13 +613,13 @@ Vector<CppToken> CppLexer::lex()
597613
emit_token_equals(CppToken::Type::Slash, CppToken::Type::SlashEquals);
598614
continue;
599615
}
600-
if (ch == '"') {
616+
if (size_t prefix = match_string_prefix('"'); prefix > 0) {
601617
begin_token();
602-
consume();
618+
for (size_t i = 0; i < prefix; ++i)
619+
consume();
603620
while (peek()) {
604621
if (peek() == '\\') {
605-
size_t escape = match_escape_sequence();
606-
if (escape > 0) {
622+
if (size_t escape = match_escape_sequence(); escape > 0) {
607623
commit_token(CppToken::Type::DoubleQuotedString);
608624
begin_token();
609625
for (size_t i = 0; i < escape; ++i)
@@ -620,13 +636,13 @@ Vector<CppToken> CppLexer::lex()
620636
commit_token(CppToken::Type::DoubleQuotedString);
621637
continue;
622638
}
623-
if (ch == '\'') {
639+
if (size_t prefix = match_string_prefix('\''); prefix > 0) {
624640
begin_token();
625-
consume();
641+
for (size_t i = 0; i < prefix; ++i)
642+
consume();
626643
while (peek()) {
627644
if (peek() == '\\') {
628-
size_t escape = match_escape_sequence();
629-
if (escape > 0) {
645+
if (size_t escape = match_escape_sequence(); escape > 0) {
630646
commit_token(CppToken::Type::SingleQuotedString);
631647
begin_token();
632648
for (size_t i = 0; i < escape; ++i)

0 commit comments

Comments
 (0)