mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 05:44:58 +00:00
AK: Add a GenericLexer and extend the JsonParser with it (#2696)
This commit is contained in:
parent
1222be7e3a
commit
7b356c33cb
5 changed files with 476 additions and 96 deletions
|
@ -31,44 +31,7 @@
|
|||
|
||||
namespace AK {
|
||||
|
||||
static inline bool is_whitespace(char ch)
|
||||
{
|
||||
return ch == ' ' || ch == '\n' || ch == '\t' || ch == '\v' || ch == '\r';
|
||||
}
|
||||
|
||||
char JsonParser::peek() const
|
||||
{
|
||||
if (m_index < m_input.length())
|
||||
return m_input[m_index];
|
||||
return '\0';
|
||||
}
|
||||
|
||||
char JsonParser::consume()
|
||||
{
|
||||
if (m_index < m_input.length())
|
||||
return m_input[m_index++];
|
||||
return '\0';
|
||||
}
|
||||
|
||||
template<typename C>
|
||||
void JsonParser::consume_while(C condition)
|
||||
{
|
||||
while (condition(peek()))
|
||||
consume();
|
||||
}
|
||||
|
||||
void JsonParser::consume_whitespace()
|
||||
{
|
||||
consume_while([](char ch) { return is_whitespace(ch); });
|
||||
}
|
||||
|
||||
bool JsonParser::consume_specific(char expected_ch)
|
||||
{
|
||||
char consumed_ch = consume();
|
||||
return consumed_ch == expected_ch;
|
||||
}
|
||||
|
||||
String JsonParser::consume_quoted_string()
|
||||
String JsonParser::consume_and_unescape_string()
|
||||
{
|
||||
if (!consume_specific('"'))
|
||||
return {};
|
||||
|
@ -86,11 +49,9 @@ String JsonParser::consume_quoted_string()
|
|||
++peek_index;
|
||||
}
|
||||
|
||||
if (peek_index != m_index) {
|
||||
while (peek_index != m_index) {
|
||||
final_sb.append(m_input.characters_without_null_termination()[m_index]);
|
||||
m_index++;
|
||||
}
|
||||
while (peek_index != m_index) {
|
||||
final_sb.append(m_input[m_index]);
|
||||
m_index++;
|
||||
}
|
||||
|
||||
if (m_index == m_input.length())
|
||||
|
@ -101,7 +62,7 @@ String JsonParser::consume_quoted_string()
|
|||
final_sb.append(consume());
|
||||
continue;
|
||||
}
|
||||
consume();
|
||||
ignore();
|
||||
char escaped_ch = consume();
|
||||
switch (escaped_ch) {
|
||||
case 'n':
|
||||
|
@ -120,18 +81,11 @@ String JsonParser::consume_quoted_string()
|
|||
final_sb.append('\f');
|
||||
break;
|
||||
case 'u': {
|
||||
StringBuilder sb;
|
||||
sb.append(consume());
|
||||
sb.append(consume());
|
||||
sb.append(consume());
|
||||
sb.append(consume());
|
||||
|
||||
auto code_point = AK::StringUtils::convert_to_uint_from_hex(sb.to_string());
|
||||
if (code_point.has_value()) {
|
||||
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 {
|
||||
else
|
||||
final_sb.append('?');
|
||||
}
|
||||
} break;
|
||||
default:
|
||||
final_sb.append(escaped_ch);
|
||||
|
@ -150,27 +104,27 @@ Optional<JsonValue> JsonParser::parse_object()
|
|||
if (!consume_specific('{'))
|
||||
return {};
|
||||
for (;;) {
|
||||
consume_whitespace();
|
||||
ignore_while(is_whitespace);
|
||||
if (peek() == '}')
|
||||
break;
|
||||
consume_whitespace();
|
||||
auto name = consume_quoted_string();
|
||||
ignore_while(is_whitespace);
|
||||
auto name = consume_and_unescape_string();
|
||||
if (name.is_null())
|
||||
return {};
|
||||
consume_whitespace();
|
||||
ignore_while(is_whitespace);
|
||||
if (!consume_specific(':'))
|
||||
return {};
|
||||
consume_whitespace();
|
||||
ignore_while(is_whitespace);
|
||||
auto value = parse_helper();
|
||||
if (!value.has_value())
|
||||
return {};
|
||||
object.set(name, move(value.value()));
|
||||
consume_whitespace();
|
||||
ignore_while(is_whitespace);
|
||||
if (peek() == '}')
|
||||
break;
|
||||
if (!consume_specific(','))
|
||||
return {};
|
||||
consume_whitespace();
|
||||
ignore_while(is_whitespace);
|
||||
if (peek() == '}')
|
||||
return {};
|
||||
}
|
||||
|
@ -185,23 +139,23 @@ Optional<JsonValue> JsonParser::parse_array()
|
|||
if (!consume_specific('['))
|
||||
return {};
|
||||
for (;;) {
|
||||
consume_whitespace();
|
||||
ignore_while(is_whitespace);
|
||||
if (peek() == ']')
|
||||
break;
|
||||
auto element = parse_helper();
|
||||
if (!element.has_value())
|
||||
return {};
|
||||
array.append(element.value());
|
||||
consume_whitespace();
|
||||
ignore_while(is_whitespace);
|
||||
if (peek() == ']')
|
||||
break;
|
||||
if (!consume_specific(','))
|
||||
return {};
|
||||
consume_whitespace();
|
||||
ignore_while(is_whitespace);
|
||||
if (peek() == ']')
|
||||
return {};
|
||||
}
|
||||
consume_whitespace();
|
||||
ignore_while(is_whitespace);
|
||||
if (!consume_specific(']'))
|
||||
return {};
|
||||
return array;
|
||||
|
@ -209,7 +163,7 @@ Optional<JsonValue> JsonParser::parse_array()
|
|||
|
||||
Optional<JsonValue> JsonParser::parse_string()
|
||||
{
|
||||
auto result = consume_quoted_string();
|
||||
auto result = consume_and_unescape_string();
|
||||
if (result.is_null())
|
||||
return {};
|
||||
return JsonValue(result);
|
||||
|
@ -283,39 +237,30 @@ Optional<JsonValue> JsonParser::parse_number()
|
|||
return value;
|
||||
}
|
||||
|
||||
bool JsonParser::consume_string(const char* str)
|
||||
{
|
||||
for (size_t i = 0, length = strlen(str); i < length; ++i) {
|
||||
if (!consume_specific(str[i]))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Optional<JsonValue> JsonParser::parse_true()
|
||||
{
|
||||
if (!consume_string("true"))
|
||||
if (!consume_specific("true"))
|
||||
return {};
|
||||
return JsonValue(true);
|
||||
}
|
||||
|
||||
Optional<JsonValue> JsonParser::parse_false()
|
||||
{
|
||||
if (!consume_string("false"))
|
||||
if (!consume_specific("false"))
|
||||
return {};
|
||||
return JsonValue(false);
|
||||
}
|
||||
|
||||
Optional<JsonValue> JsonParser::parse_null()
|
||||
{
|
||||
if (!consume_string("null"))
|
||||
if (!consume_specific("null"))
|
||||
return {};
|
||||
return JsonValue(JsonValue::Type::Null);
|
||||
}
|
||||
|
||||
Optional<JsonValue> JsonParser::parse_helper()
|
||||
{
|
||||
consume_whitespace();
|
||||
ignore_while(is_whitespace);
|
||||
auto type_hint = peek();
|
||||
switch (type_hint) {
|
||||
case '{':
|
||||
|
@ -347,12 +292,13 @@ Optional<JsonValue> JsonParser::parse_helper()
|
|||
return {};
|
||||
}
|
||||
|
||||
Optional<JsonValue> JsonParser::parse() {
|
||||
Optional<JsonValue> JsonParser::parse()
|
||||
{
|
||||
auto result = parse_helper();
|
||||
if (!result.has_value())
|
||||
return {};
|
||||
consume_whitespace();
|
||||
if (m_index != m_input.length())
|
||||
ignore_while(is_whitespace);
|
||||
if (!is_eof())
|
||||
return {};
|
||||
return result;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue