mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 22:07:34 +00:00
LibGUI: Syntax highlight string escape sequences
This commit is contained in:
parent
bc10e0eeb2
commit
d58cf1a05d
3 changed files with 84 additions and 0 deletions
|
@ -243,6 +243,61 @@ Vector<CppToken> CppLexer::lex()
|
||||||
tokens.append(token);
|
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()) {
|
while (m_index < m_input.length()) {
|
||||||
auto ch = peek();
|
auto ch = peek();
|
||||||
if (isspace(ch)) {
|
if (isspace(ch)) {
|
||||||
|
@ -328,6 +383,19 @@ Vector<CppToken> CppLexer::lex()
|
||||||
begin_token();
|
begin_token();
|
||||||
consume();
|
consume();
|
||||||
while (peek()) {
|
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() == '"')
|
if (consume() == '"')
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -338,6 +406,19 @@ Vector<CppToken> CppLexer::lex()
|
||||||
begin_token();
|
begin_token();
|
||||||
consume();
|
consume();
|
||||||
while (peek()) {
|
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() == '\'')
|
if (consume() == '\'')
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,6 +46,7 @@ namespace GUI {
|
||||||
__TOKEN(Semicolon) \
|
__TOKEN(Semicolon) \
|
||||||
__TOKEN(DoubleQuotedString) \
|
__TOKEN(DoubleQuotedString) \
|
||||||
__TOKEN(SingleQuotedString) \
|
__TOKEN(SingleQuotedString) \
|
||||||
|
__TOKEN(EscapeSequence) \
|
||||||
__TOKEN(Comment) \
|
__TOKEN(Comment) \
|
||||||
__TOKEN(Number) \
|
__TOKEN(Number) \
|
||||||
__TOKEN(Keyword) \
|
__TOKEN(Keyword) \
|
||||||
|
|
|
@ -23,6 +23,8 @@ static TextStyle style_for_token_type(CppToken::Type type)
|
||||||
case CppToken::Type::SingleQuotedString:
|
case CppToken::Type::SingleQuotedString:
|
||||||
case CppToken::Type::Number:
|
case CppToken::Type::Number:
|
||||||
return { Color::from_rgb(0x800000) };
|
return { Color::from_rgb(0x800000) };
|
||||||
|
case CppToken::Type::EscapeSequence:
|
||||||
|
return { Color::from_rgb(0x800080), &Gfx::Font::default_bold_fixed_width_font() };
|
||||||
case CppToken::Type::PreprocessorStatement:
|
case CppToken::Type::PreprocessorStatement:
|
||||||
return { Color::from_rgb(0x008080) };
|
return { Color::from_rgb(0x008080) };
|
||||||
case CppToken::Type::Comment:
|
case CppToken::Type::Comment:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue