From b95c80e9e14de6d21c5d06dd7378e35413943bf8 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Fri, 19 May 2017 16:57:00 -0400 Subject: [PATCH 17/31] FIXME: handle some escaping in JSON --- gcc/json.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/gcc/json.c b/gcc/json.c index f773719..482fede 100644 --- a/gcc/json.c +++ b/gcc/json.c @@ -175,7 +175,26 @@ number::clone () const void string::print (pretty_printer *pp) const { - pp_printf (pp, "\"%s\"", m_utf8); // FIXME: escaping + pp_character (pp, '"'); + for (const char *ptr = m_utf8; *ptr; ptr++) + { + char ch = *ptr; + switch (ch) + { + case '\\': + pp_string (pp, "\\n"); + break; + case '"': + pp_string (pp, "\\\""); + break; + + // FIXME: handle other escapes + + default: + pp_character (pp, ch); + } + } + pp_character (pp, '"'); } value * @@ -526,16 +545,31 @@ lexer::lex_string (token *out) case '"': still_going = false; break; -#if 0 case '\\': { - unichar next = m_buffer[m_next_char_idx++]; - switch (next) + unichar next_char; + if (!get_char (next_char)) + { + out->id = TOK_ERROR; + out->length = 1; + return; + } + switch (next_char) { + case '\\': + case '"': + content.safe_push (next_char); + break; + + // FIXME: implement other chars + + default: + out->id = TOK_ERROR; + out->length = 1; + return; } } break; -#endif default: content.safe_push (uc); @@ -797,6 +831,14 @@ test_parse_string () ASSERT_STREQ ("foo", ((json::string *)jv)->get_string ()); assert_to_str_eq ("\"foo\"", jv); delete jv; + + const char *contains_quotes = "\"before \\\"quoted\\\" after\""; + jv = parse_utf8_string (contains_quotes, &err); + ASSERT_EQ (NULL, err); + ASSERT_EQ (JSON_STRING, jv->get_kind ()); + ASSERT_STREQ ("before \"quoted\" after", ((json::string *)jv)->get_string ()); + assert_to_str_eq (contains_quotes, jv); + delete jv; } static void -- 1.8.5.3