2020-12-20 13:47:44 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-12-20 13:47:44 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/GenericLexer.h>
|
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/JsonValue.h>
|
2020-12-21 14:32:27 +03:00
|
|
|
#include <AK/Queue.h>
|
|
|
|
#include <LibGUI/GMLLexer.h>
|
2020-12-20 13:47:44 +03:00
|
|
|
#include <LibGUI/GMLParser.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
namespace GUI {
|
|
|
|
|
2020-12-21 14:32:27 +03:00
|
|
|
static Optional<JsonValue> parse_core_object(Queue<GMLToken>& tokens)
|
2020-12-20 13:47:44 +03:00
|
|
|
{
|
|
|
|
JsonObject object;
|
|
|
|
JsonArray children;
|
|
|
|
|
2020-12-21 14:32:27 +03:00
|
|
|
auto peek = [&] {
|
|
|
|
if (tokens.is_empty())
|
|
|
|
return GMLToken::Type::Unknown;
|
|
|
|
return tokens.head().m_type;
|
|
|
|
};
|
|
|
|
|
2021-01-03 20:34:30 +03:00
|
|
|
while (peek() == GMLToken::Type::Comment)
|
|
|
|
tokens.dequeue();
|
|
|
|
|
2020-12-21 14:32:27 +03:00
|
|
|
if (peek() != GMLToken::Type::ClassMarker) {
|
|
|
|
dbgln("Expected class marker");
|
2020-12-20 16:28:41 +03:00
|
|
|
return {};
|
|
|
|
}
|
2020-12-20 13:47:44 +03:00
|
|
|
|
2020-12-21 14:32:27 +03:00
|
|
|
tokens.dequeue();
|
2020-12-20 13:47:44 +03:00
|
|
|
|
2020-12-21 14:32:27 +03:00
|
|
|
if (peek() != GMLToken::Type::ClassName) {
|
|
|
|
dbgln("Expected class name");
|
2020-12-20 16:28:41 +03:00
|
|
|
return {};
|
|
|
|
}
|
2020-12-20 13:47:44 +03:00
|
|
|
|
2020-12-21 14:32:27 +03:00
|
|
|
auto class_name = tokens.dequeue();
|
|
|
|
object.set("class", JsonValue(class_name.m_view));
|
2020-12-20 13:47:44 +03:00
|
|
|
|
2020-12-21 14:32:27 +03:00
|
|
|
if (peek() != GMLToken::Type::LeftCurly) {
|
2021-03-21 22:13:41 +03:00
|
|
|
// Empty object
|
|
|
|
return object;
|
2020-12-21 14:32:27 +03:00
|
|
|
}
|
|
|
|
tokens.dequeue();
|
2020-12-20 13:47:44 +03:00
|
|
|
|
2020-12-21 14:32:27 +03:00
|
|
|
for (;;) {
|
|
|
|
if (peek() == GMLToken::Type::RightCurly) {
|
2020-12-20 13:47:44 +03:00
|
|
|
// End of object
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-12-21 14:32:27 +03:00
|
|
|
if (peek() == GMLToken::Type::ClassMarker) {
|
2020-12-20 13:47:44 +03:00
|
|
|
// It's a child object.
|
2020-12-21 14:32:27 +03:00
|
|
|
auto value = parse_core_object(tokens);
|
|
|
|
if (!value.has_value()) {
|
|
|
|
dbgln("Parsing child object failed");
|
2020-12-20 16:28:41 +03:00
|
|
|
return {};
|
2020-12-21 14:32:27 +03:00
|
|
|
}
|
2020-12-20 16:28:41 +03:00
|
|
|
if (!value.value().is_object()) {
|
|
|
|
dbgln("Expected child to be Core::Object");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
children.append(value.release_value());
|
2020-12-21 14:32:27 +03:00
|
|
|
} else if (peek() == GMLToken::Type::Identifier) {
|
2020-12-20 13:47:44 +03:00
|
|
|
// It's a property.
|
2020-12-21 14:32:27 +03:00
|
|
|
auto property_name = tokens.dequeue();
|
2020-12-20 13:47:44 +03:00
|
|
|
|
2020-12-21 14:32:27 +03:00
|
|
|
if (property_name.m_view.is_empty()) {
|
2020-12-20 16:28:41 +03:00
|
|
|
dbgln("Expected non-empty property name");
|
|
|
|
return {};
|
|
|
|
}
|
2020-12-20 13:47:44 +03:00
|
|
|
|
2020-12-21 14:32:27 +03:00
|
|
|
if (peek() != GMLToken::Type::Colon) {
|
2020-12-20 16:28:41 +03:00
|
|
|
dbgln("Expected ':'");
|
|
|
|
return {};
|
|
|
|
}
|
2020-12-21 14:32:27 +03:00
|
|
|
tokens.dequeue();
|
2020-12-20 13:47:44 +03:00
|
|
|
|
|
|
|
JsonValue value;
|
2020-12-21 14:32:27 +03:00
|
|
|
if (peek() == GMLToken::Type::ClassMarker) {
|
|
|
|
auto parsed_value = parse_core_object(tokens);
|
2020-12-20 16:28:41 +03:00
|
|
|
if (!parsed_value.has_value())
|
|
|
|
return {};
|
|
|
|
if (!parsed_value.value().is_object()) {
|
|
|
|
dbgln("Expected property to be Core::Object");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
value = parsed_value.release_value();
|
2020-12-21 14:32:27 +03:00
|
|
|
} else if (peek() == GMLToken::Type::JsonValue) {
|
|
|
|
auto value_string = tokens.dequeue();
|
|
|
|
auto parsed_value = JsonValue::from_string(value_string.m_view);
|
2020-12-20 16:28:41 +03:00
|
|
|
if (!parsed_value.has_value()) {
|
|
|
|
dbgln("Expected property to be JSON value");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
value = parsed_value.release_value();
|
2020-12-20 13:47:44 +03:00
|
|
|
}
|
2020-12-21 14:32:27 +03:00
|
|
|
object.set(property_name.m_view, move(value));
|
2020-12-28 23:02:56 +03:00
|
|
|
} else if (peek() == GMLToken::Type::Comment) {
|
|
|
|
tokens.dequeue();
|
2020-12-21 14:32:27 +03:00
|
|
|
} else {
|
2020-12-28 23:02:56 +03:00
|
|
|
dbgln("Expected child, property, comment, or }}");
|
2020-12-21 14:32:27 +03:00
|
|
|
return {};
|
2020-12-20 13:47:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 14:32:27 +03:00
|
|
|
if (peek() != GMLToken::Type::RightCurly) {
|
|
|
|
dbgln("Expected }}");
|
2020-12-20 16:28:41 +03:00
|
|
|
return {};
|
|
|
|
}
|
2020-12-21 14:32:27 +03:00
|
|
|
tokens.dequeue();
|
2020-12-20 13:47:44 +03:00
|
|
|
|
|
|
|
if (!children.is_empty())
|
|
|
|
object.set("children", move(children));
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonValue parse_gml(const StringView& string)
|
|
|
|
{
|
2020-12-21 14:32:27 +03:00
|
|
|
auto lexer = GMLLexer(string);
|
|
|
|
|
|
|
|
Queue<GMLToken> tokens;
|
|
|
|
for (auto& token : lexer.lex())
|
|
|
|
tokens.enqueue(token);
|
|
|
|
|
|
|
|
auto root = parse_core_object(tokens);
|
2020-12-20 13:47:44 +03:00
|
|
|
|
2020-12-20 16:28:41 +03:00
|
|
|
if (!root.has_value())
|
2020-12-20 13:47:44 +03:00
|
|
|
return JsonValue();
|
|
|
|
|
2020-12-20 16:28:41 +03:00
|
|
|
return root.release_value();
|
2020-12-20 13:47:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|