summaryrefslogtreecommitdiff
path: root/Libraries/LibGUI/CppLexer.cpp
diff options
context:
space:
mode:
authorOriko <oriko1010@protonmail.com>2020-03-10 22:26:11 +0200
committerAndreas Kling <kling@serenityos.org>2020-03-11 10:16:55 +0100
commitd58cf1a05d4056af381edac71b168ba3b82ffa0e (patch)
tree7aea1800d856fc274c4cf980c3ccc62b9b1cd9a3 /Libraries/LibGUI/CppLexer.cpp
parentbc10e0eeb23758a11f29a8baec4ca788d493334c (diff)
downloadserenity-d58cf1a05d4056af381edac71b168ba3b82ffa0e.zip
LibGUI: Syntax highlight string escape sequences
Diffstat (limited to 'Libraries/LibGUI/CppLexer.cpp')
-rw-r--r--Libraries/LibGUI/CppLexer.cpp81
1 files changed, 81 insertions, 0 deletions
diff --git a/Libraries/LibGUI/CppLexer.cpp b/Libraries/LibGUI/CppLexer.cpp
index bbd76a45de..78a7558779 100644
--- a/Libraries/LibGUI/CppLexer.cpp
+++ b/Libraries/LibGUI/CppLexer.cpp
@@ -243,6 +243,61 @@ Vector<CppToken> CppLexer::lex()
tokens.append(token);
};
+ auto match_escape_sequence = [&]() -> size_t {
+ switch (peek(1)) {
+ case '\'':
+ case '"':
+ case '?':
+ case '\\':
+ case 'a':
+ case 'b':
+ case 'f':
+ case 'n':
+ case 'r':
+ case 't':
+ case 'v':
+ return 2;
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7': {
+ size_t octal_digits = 1;
+ for (size_t i = 0; i < 2; ++i) {
+ char next = peek(2 + i);
+ if (next < '0' || next > '7')
+ break;
+ ++octal_digits;
+ }
+ return 1 + octal_digits;
+ }
+ case 'x': {
+ size_t hex_digits = 0;
+ for (size_t i = 0; i < 2; ++i) {
+ if (!isxdigit(peek(2 + i)))
+ break;
+ ++hex_digits;
+ }
+ return 2 + hex_digits;
+ }
+ case 'u': {
+ bool is_unicode = true;
+ for (size_t i = 0; i < 4; ++i) {
+ if (!isxdigit(peek(2 + i))) {
+ is_unicode = false;
+ break;
+ }
+ }
+ return is_unicode ? 6 : 0;
+ }
+ default:
+ return 0;
+ }
+ };
+
while (m_index < m_input.length()) {
auto ch = peek();
if (isspace(ch)) {
@@ -328,6 +383,19 @@ Vector<CppToken> CppLexer::lex()
begin_token();
consume();
while (peek()) {
+ if (peek() == '\\') {
+ size_t escape = match_escape_sequence();
+ if (escape > 0) {
+ commit_token(CppToken::Type::DoubleQuotedString);
+ begin_token();
+ for (size_t i = 0; i < escape; ++i)
+ consume();
+ commit_token(CppToken::Type::EscapeSequence);
+ begin_token();
+ continue;
+ }
+ }
+
if (consume() == '"')
break;
}
@@ -338,6 +406,19 @@ Vector<CppToken> CppLexer::lex()
begin_token();
consume();
while (peek()) {
+ if (peek() == '\\') {
+ size_t escape = match_escape_sequence();
+ if (escape > 0) {
+ commit_token(CppToken::Type::SingleQuotedString);
+ begin_token();
+ for (size_t i = 0; i < escape; ++i)
+ consume();
+ commit_token(CppToken::Type::EscapeSequence);
+ begin_token();
+ continue;
+ }
+ }
+
if (consume() == '\'')
break;
}