mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 09:04:59 +00:00
JsonParser: Bring parser more to spec
This commit is contained in:
parent
31a2f10927
commit
ce314c54bd
2 changed files with 109 additions and 43 deletions
|
@ -4,13 +4,18 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/CharacterTypes.h>
|
||||
#include <AK/JsonArray.h>
|
||||
#include <AK/JsonObject.h>
|
||||
#include <AK/JsonParser.h>
|
||||
#include <ctype.h>
|
||||
|
||||
namespace AK {
|
||||
|
||||
constexpr bool is_space(int ch)
|
||||
{
|
||||
return ch == '\t' || ch == '\n' || ch == '\r' || ch == ' ';
|
||||
}
|
||||
|
||||
String JsonParser::consume_and_unescape_string()
|
||||
{
|
||||
if (!consume_specific('"'))
|
||||
|
@ -26,6 +31,8 @@ String JsonParser::consume_and_unescape_string()
|
|||
ch = m_input[peek_index];
|
||||
if (ch == '"' || ch == '\\')
|
||||
break;
|
||||
if (is_ascii_c0_control(ch))
|
||||
return {};
|
||||
++peek_index;
|
||||
}
|
||||
|
||||
|
@ -43,34 +50,69 @@ String JsonParser::consume_and_unescape_string()
|
|||
continue;
|
||||
}
|
||||
ignore();
|
||||
char escaped_ch = consume();
|
||||
switch (escaped_ch) {
|
||||
case 'n':
|
||||
final_sb.append('\n');
|
||||
break;
|
||||
case 'r':
|
||||
final_sb.append('\r');
|
||||
break;
|
||||
case 't':
|
||||
final_sb.append('\t');
|
||||
break;
|
||||
case 'b':
|
||||
final_sb.append('\b');
|
||||
break;
|
||||
case 'f':
|
||||
final_sb.append('\f');
|
||||
break;
|
||||
case 'u': {
|
||||
auto code_point = AK::StringUtils::convert_to_uint_from_hex(consume(4));
|
||||
if (code_point.has_value())
|
||||
final_sb.append_code_point(code_point.value());
|
||||
else
|
||||
final_sb.append('?');
|
||||
} break;
|
||||
default:
|
||||
final_sb.append(escaped_ch);
|
||||
break;
|
||||
if (next_is('"')) {
|
||||
ignore();
|
||||
final_sb.append('"');
|
||||
continue;
|
||||
}
|
||||
|
||||
if (next_is('\\')) {
|
||||
ignore();
|
||||
final_sb.append('\\');
|
||||
continue;
|
||||
}
|
||||
|
||||
if (next_is('/')) {
|
||||
ignore();
|
||||
final_sb.append('/');
|
||||
continue;
|
||||
}
|
||||
|
||||
if (next_is('n')) {
|
||||
ignore();
|
||||
final_sb.append('\n');
|
||||
continue;
|
||||
}
|
||||
|
||||
if (next_is('r')) {
|
||||
ignore();
|
||||
final_sb.append('\r');
|
||||
continue;
|
||||
}
|
||||
|
||||
if (next_is('t')) {
|
||||
ignore();
|
||||
final_sb.append('\t');
|
||||
continue;
|
||||
}
|
||||
|
||||
if (next_is('b')) {
|
||||
ignore();
|
||||
final_sb.append('\b');
|
||||
continue;
|
||||
}
|
||||
|
||||
if (next_is('f')) {
|
||||
ignore();
|
||||
final_sb.append('\f');
|
||||
continue;
|
||||
}
|
||||
|
||||
if (next_is('u')) {
|
||||
ignore();
|
||||
if (tell_remaining() < 4)
|
||||
return {};
|
||||
|
||||
auto code_point = AK::StringUtils::convert_to_uint_from_hex(consume(4));
|
||||
if (code_point.has_value()) {
|
||||
final_sb.append_code_point(code_point.value());
|
||||
continue;
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
if (!consume_specific('"'))
|
||||
return {};
|
||||
|
@ -84,27 +126,27 @@ Optional<JsonValue> JsonParser::parse_object()
|
|||
if (!consume_specific('{'))
|
||||
return {};
|
||||
for (;;) {
|
||||
ignore_while(isspace);
|
||||
ignore_while(is_space);
|
||||
if (peek() == '}')
|
||||
break;
|
||||
ignore_while(isspace);
|
||||
ignore_while(is_space);
|
||||
auto name = consume_and_unescape_string();
|
||||
if (name.is_null())
|
||||
return {};
|
||||
ignore_while(isspace);
|
||||
ignore_while(is_space);
|
||||
if (!consume_specific(':'))
|
||||
return {};
|
||||
ignore_while(isspace);
|
||||
ignore_while(is_space);
|
||||
auto value = parse_helper();
|
||||
if (!value.has_value())
|
||||
return {};
|
||||
object.set(name, value.release_value());
|
||||
ignore_while(isspace);
|
||||
ignore_while(is_space);
|
||||
if (peek() == '}')
|
||||
break;
|
||||
if (!consume_specific(','))
|
||||
return {};
|
||||
ignore_while(isspace);
|
||||
ignore_while(is_space);
|
||||
if (peek() == '}')
|
||||
return {};
|
||||
}
|
||||
|
@ -119,23 +161,23 @@ Optional<JsonValue> JsonParser::parse_array()
|
|||
if (!consume_specific('['))
|
||||
return {};
|
||||
for (;;) {
|
||||
ignore_while(isspace);
|
||||
ignore_while(is_space);
|
||||
if (peek() == ']')
|
||||
break;
|
||||
auto element = parse_helper();
|
||||
if (!element.has_value())
|
||||
return {};
|
||||
array.append(element.release_value());
|
||||
ignore_while(isspace);
|
||||
ignore_while(is_space);
|
||||
if (peek() == ']')
|
||||
break;
|
||||
if (!consume_specific(','))
|
||||
return {};
|
||||
ignore_while(isspace);
|
||||
ignore_while(is_space);
|
||||
if (peek() == ']')
|
||||
return {};
|
||||
}
|
||||
ignore_while(isspace);
|
||||
ignore_while(is_space);
|
||||
if (!consume_specific(']'))
|
||||
return {};
|
||||
return JsonValue { move(array) };
|
||||
|
@ -159,15 +201,32 @@ Optional<JsonValue> JsonParser::parse_number()
|
|||
for (;;) {
|
||||
char ch = peek();
|
||||
if (ch == '.') {
|
||||
if (is_double)
|
||||
return {};
|
||||
|
||||
is_double = true;
|
||||
++m_index;
|
||||
continue;
|
||||
}
|
||||
if (ch == '-' || (ch >= '0' && ch <= '9')) {
|
||||
if (is_double)
|
||||
if (is_double) {
|
||||
if (ch == '-')
|
||||
return {};
|
||||
|
||||
fraction_buffer.append(ch);
|
||||
else
|
||||
} else {
|
||||
if (number_buffer.size() > 0) {
|
||||
if (number_buffer.at(0) == '0')
|
||||
return {};
|
||||
}
|
||||
|
||||
if (number_buffer.size() > 1) {
|
||||
if (number_buffer.at(0) == '-' && number_buffer.at(1) == '0')
|
||||
return {};
|
||||
}
|
||||
|
||||
number_buffer.append(ch);
|
||||
}
|
||||
++m_index;
|
||||
continue;
|
||||
}
|
||||
|
@ -247,7 +306,7 @@ Optional<JsonValue> JsonParser::parse_null()
|
|||
|
||||
Optional<JsonValue> JsonParser::parse_helper()
|
||||
{
|
||||
ignore_while(isspace);
|
||||
ignore_while(is_space);
|
||||
auto type_hint = peek();
|
||||
switch (type_hint) {
|
||||
case '{':
|
||||
|
@ -284,7 +343,7 @@ Optional<JsonValue> JsonParser::parse()
|
|||
auto result = parse_helper();
|
||||
if (!result.has_value())
|
||||
return {};
|
||||
ignore_while(isspace);
|
||||
ignore_while(is_space);
|
||||
if (!is_eof())
|
||||
return {};
|
||||
return result;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue