summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorStephan Unverwerth <s.unverwerth@gmx.de>2020-03-14 13:39:05 +0100
committerAndreas Kling <kling@serenityos.org>2020-03-14 16:00:28 +0100
commit33890212918ff547d80ea549275765bc1d1fbd0c (patch)
tree2ca9391ba29a1146f41323632a64c026bc7106d2 /Libraries
parent644b4f420133d3945eca02d2734b7819e02f3c47 (diff)
downloadserenity-33890212918ff547d80ea549275765bc1d1fbd0c.zip
LibJS: Unescape strings in Token::string_value()
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibJS/Token.cpp53
1 files changed, 49 insertions, 4 deletions
diff --git a/Libraries/LibJS/Token.cpp b/Libraries/LibJS/Token.cpp
index 894b53795e..da5f75c839 100644
--- a/Libraries/LibJS/Token.cpp
+++ b/Libraries/LibJS/Token.cpp
@@ -26,6 +26,7 @@
#include "Token.h"
#include <AK/Assertions.h>
+#include <AK/StringBuilder.h>
namespace JS {
@@ -209,6 +210,7 @@ const char* Token::name() const
double Token::double_value() const
{
+ ASSERT(type() == TokenType::NumericLiteral);
// FIXME: need to parse double instead of int
bool ok;
return m_value.to_int(ok);
@@ -216,15 +218,58 @@ double Token::double_value() const
String Token::string_value() const
{
- if (m_value.length() >= 2 && m_value[0] == '"' && m_value[m_value.length() - 1]) {
- return m_value.substring_view(1, m_value.length() - 2);
+ ASSERT(type() == TokenType::StringLiteral);
+ StringBuilder builder;
+ for (size_t i = 1; i < m_value.length() - 1; ++i) {
+ if (m_value[i] == '\\' && i + 1 < m_value.length() - 1) {
+ i++;
+ switch (m_value[i]) {
+ case 'b':
+ builder.append('\b');
+ break;
+ case 'f':
+ builder.append('\f');
+ break;
+ case 'n':
+ builder.append('\n');
+ break;
+ case 'r':
+ builder.append('\r');
+ break;
+ case 't':
+ builder.append('\t');
+ break;
+ case 'v':
+ builder.append('\v');
+ break;
+ case '0':
+ builder.append((char)0);
+ break;
+ case '\'':
+ builder.append('\'');
+ break;
+ case '"':
+ builder.append('"');
+ break;
+ case '\\':
+ builder.append('\\');
+ break;
+ default:
+ // FIXME: Also parse octal, hex and unicode sequences
+ // should anything else generate a syntax error?
+ builder.append(m_value[i]);
+ }
+
+ } else {
+ builder.append(m_value[i]);
+ }
}
- // FIXME: unescape the string and remove quotes
- return m_value;
+ return builder.to_string();
}
bool Token::bool_value() const
{
+ ASSERT(type() == TokenType::BoolLiteral);
return m_value == "true";
}