diff --git a/Libraries/LibGUI/JSSyntaxHighlighter.cpp b/Libraries/LibGUI/JSSyntaxHighlighter.cpp index d3b74f22fb..3527f21866 100644 --- a/Libraries/LibGUI/JSSyntaxHighlighter.cpp +++ b/Libraries/LibGUI/JSSyntaxHighlighter.cpp @@ -42,6 +42,7 @@ static TextStyle style_for_token_type(Gfx::Palette palette, JS::TokenType type) case JS::TokenType::NumericLiteral: return { palette.syntax_number() }; case JS::TokenType::StringLiteral: + case JS::TokenType::TemplateLiteral: case JS::TokenType::RegexLiteral: case JS::TokenType::UnterminatedStringLiteral: return { palette.syntax_string() }; diff --git a/Libraries/LibJS/Lexer.cpp b/Libraries/LibJS/Lexer.cpp index f8bfce245a..79eadc4bbf 100644 --- a/Libraries/LibJS/Lexer.cpp +++ b/Libraries/LibJS/Lexer.cpp @@ -337,7 +337,7 @@ Token Lexer::next() } } token_type = TokenType::NumericLiteral; - } else if (m_current_char == '"' || m_current_char == '\'') { + } else if (m_current_char == '"' || m_current_char == '\'' || m_current_char == '`') { char stop_char = m_current_char; consume(); while (m_current_char != stop_char && m_current_char != '\n' && !is_eof()) { @@ -351,7 +351,10 @@ Token Lexer::next() token_type = TokenType::UnterminatedStringLiteral; } else { consume(); - token_type = TokenType::StringLiteral; + if (stop_char == '`') + token_type = TokenType::TemplateLiteral; + else + token_type = TokenType::StringLiteral; } } else if (m_current_char == EOF) { token_type = TokenType::Eof; diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp index 64fa1deab1..7855501f30 100644 --- a/Libraries/LibJS/Parser.cpp +++ b/Libraries/LibJS/Parser.cpp @@ -1037,6 +1037,7 @@ bool Parser::match_expression() const return type == TokenType::BoolLiteral || type == TokenType::NumericLiteral || type == TokenType::StringLiteral + || type == TokenType::TemplateLiteral || type == TokenType::NullLiteral || type == TokenType::Identifier || type == TokenType::New diff --git a/Libraries/LibJS/Token.cpp b/Libraries/LibJS/Token.cpp index fca1e4597d..3375146ca2 100644 --- a/Libraries/LibJS/Token.cpp +++ b/Libraries/LibJS/Token.cpp @@ -74,7 +74,7 @@ double Token::double_value() const String Token::string_value() const { - ASSERT(type() == TokenType::StringLiteral); + ASSERT(type() == TokenType::StringLiteral || type() == TokenType::TemplateLiteral); StringBuilder builder; for (size_t i = 1; i < m_value.length() - 1; ++i) { if (m_value[i] == '\\' && i + 1 < m_value.length() - 1) { @@ -107,6 +107,9 @@ String Token::string_value() const case '"': builder.append('"'); break; + case '`': + builder.append('`'); + break; case '\\': builder.append('\\'); break; diff --git a/Libraries/LibJS/Token.h b/Libraries/LibJS/Token.h index 596772bad5..b301332649 100644 --- a/Libraries/LibJS/Token.h +++ b/Libraries/LibJS/Token.h @@ -111,6 +111,7 @@ namespace JS { __ENUMERATE_JS_TOKEN(SlashEquals) \ __ENUMERATE_JS_TOKEN(StringLiteral) \ __ENUMERATE_JS_TOKEN(Switch) \ + __ENUMERATE_JS_TOKEN(TemplateLiteral) \ __ENUMERATE_JS_TOKEN(This) \ __ENUMERATE_JS_TOKEN(Throw) \ __ENUMERATE_JS_TOKEN(Tilde) \ diff --git a/Userland/js.cpp b/Userland/js.cpp index 0806089381..bc9cd5bd5e 100644 --- a/Userland/js.cpp +++ b/Userland/js.cpp @@ -453,6 +453,7 @@ int main(int argc, char** argv) stylize({ start, end }, { Line::Style::Foreground(Line::Style::Color::Magenta) }); break; case JS::TokenType::StringLiteral: + case JS::TokenType::TemplateLiteral: case JS::TokenType::RegexLiteral: case JS::TokenType::UnterminatedStringLiteral: stylize({ start, end }, { Line::Style::Foreground(Line::Style::Color::Green), Line::Style::Bold });