mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 22:17:44 +00:00
Everywhere: Move all host tools into the Lagom/Tools subdirectory
This allows us to remove all the add_subdirectory calls from the top level CMakeLists.txt that referred to targets linking LagomCore. Segregating the host tools and Serenity targets helps us get to a place where the main Serenity build can simply use a CMake toolchain file rather than swapping all the compiler/sysroot variables after building host libraries and tools.
This commit is contained in:
parent
fb15cdcc10
commit
63956b36d0
19 changed files with 18 additions and 22 deletions
|
@ -220,6 +220,12 @@ if (NOT APPLE)
|
|||
target_link_libraries(LagomCore crypt) # Core::Account uses crypt() but it's not in libcrypt on macOS
|
||||
endif()
|
||||
|
||||
# Code Generators and other host tools
|
||||
# We need to make sure not to build code generators for Fuzzer builds, as they already have their own main.cpp
|
||||
if (NOT ENABLE_OSS_FUZZ AND NOT ENABLE_FUZZER_SANITIZER)
|
||||
add_subdirectory(Tools)
|
||||
endif()
|
||||
|
||||
if (BUILD_LAGOM)
|
||||
# Lagom Libraries
|
||||
|
||||
|
@ -364,13 +370,8 @@ if (BUILD_LAGOM)
|
|||
)
|
||||
|
||||
# Unicode
|
||||
# We need to make sure not to build code generators for Fuzzer builds, as they already have their own main.cpp
|
||||
# Don't include UnicodeData for Fuzzer builds, we didn't build the CodeGenerators
|
||||
if (NOT ENABLE_OSS_FUZZ AND NOT ENABLE_FUZZER_SANITIZER)
|
||||
# FIXME: Make this logic smarter in 4594
|
||||
if (NOT CMAKE_SOURCE_DIR STREQUAL SERENITY_PROJECT_ROOT)
|
||||
set(write_if_different ${CMAKE_CURRENT_SOURCE_DIR}/../write-only-on-difference.sh)
|
||||
add_subdirectory(../../Userland/Libraries/LibUnicode/CodeGenerators ${CMAKE_CURRENT_BINARY_DIR}/LibUnicode/CodeGenerators)
|
||||
endif()
|
||||
include(${SERENITY_PROJECT_ROOT}/Meta/CMake/unicode_data.cmake)
|
||||
else()
|
||||
set(ENABLE_UNICODE_DATABASE_DOWNLOAD OFF)
|
||||
|
|
2
Meta/Lagom/Tools/CMakeLists.txt
Normal file
2
Meta/Lagom/Tools/CMakeLists.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
add_subdirectory(ConfigureComponents)
|
||||
add_subdirectory(CodeGenerators)
|
4
Meta/Lagom/Tools/CodeGenerators/CMakeLists.txt
Normal file
4
Meta/Lagom/Tools/CodeGenerators/CMakeLists.txt
Normal file
|
@ -0,0 +1,4 @@
|
|||
add_subdirectory(IPCCompiler)
|
||||
add_subdirectory(LibUnicode)
|
||||
add_subdirectory(LibWeb)
|
||||
add_subdirectory(StateMachineGenerator)
|
|
@ -0,0 +1,6 @@
|
|||
set(SOURCES
|
||||
main.cpp
|
||||
)
|
||||
|
||||
add_executable(IPCCompiler ${SOURCES})
|
||||
target_link_libraries(IPCCompiler LagomCore)
|
899
Meta/Lagom/Tools/CodeGenerators/IPCCompiler/main.cpp
Normal file
899
Meta/Lagom/Tools/CodeGenerators/IPCCompiler/main.cpp
Normal file
|
@ -0,0 +1,899 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Debug.h>
|
||||
#include <AK/Function.h>
|
||||
#include <AK/GenericLexer.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/SourceGenerator.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct Parameter {
|
||||
Vector<String> attributes;
|
||||
String type;
|
||||
String name;
|
||||
};
|
||||
|
||||
static String pascal_case(String const& identifier)
|
||||
{
|
||||
StringBuilder builder;
|
||||
bool was_new_word = true;
|
||||
for (auto ch : identifier) {
|
||||
if (ch == '_') {
|
||||
was_new_word = true;
|
||||
continue;
|
||||
}
|
||||
if (was_new_word) {
|
||||
builder.append(toupper(ch));
|
||||
was_new_word = false;
|
||||
} else
|
||||
builder.append(ch);
|
||||
}
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
struct Message {
|
||||
String name;
|
||||
bool is_synchronous { false };
|
||||
Vector<Parameter> inputs;
|
||||
Vector<Parameter> outputs;
|
||||
|
||||
String response_name() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
builder.append(pascal_case(name));
|
||||
builder.append("Response");
|
||||
return builder.to_string();
|
||||
}
|
||||
};
|
||||
|
||||
struct Endpoint {
|
||||
Vector<String> includes;
|
||||
String name;
|
||||
u32 magic;
|
||||
Vector<Message> messages;
|
||||
};
|
||||
|
||||
static bool is_primitive_type(String const& type)
|
||||
{
|
||||
return type.is_one_of("u8", "i8", "u16", "i16", "u32", "i32", "u64", "i64", "bool", "double", "float", "int", "unsigned", "unsigned int");
|
||||
}
|
||||
|
||||
static String message_name(String const& endpoint, String& message, bool is_response)
|
||||
{
|
||||
StringBuilder builder;
|
||||
builder.append("Messages::");
|
||||
builder.append(endpoint);
|
||||
builder.append("::");
|
||||
builder.append(pascal_case(message));
|
||||
if (is_response)
|
||||
builder.append("Response");
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 2) {
|
||||
outln("usage: {} <IPC endpoint definition file>", argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto file = Core::File::construct(argv[1]);
|
||||
if (!file->open(Core::OpenMode::ReadOnly)) {
|
||||
warnln("Error: Cannot open {}: {}", argv[1], file->error_string());
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto file_contents = file->read_all();
|
||||
GenericLexer lexer(file_contents);
|
||||
|
||||
Vector<Endpoint> endpoints;
|
||||
|
||||
auto assert_specific = [&](char ch) {
|
||||
if (lexer.peek() != ch)
|
||||
warnln("assert_specific: wanted '{}', but got '{}' at index {}", ch, lexer.peek(), lexer.tell());
|
||||
bool saw_expected = lexer.consume_specific(ch);
|
||||
VERIFY(saw_expected);
|
||||
};
|
||||
|
||||
auto consume_whitespace = [&] {
|
||||
lexer.ignore_while([](char ch) { return isspace(ch); });
|
||||
if (lexer.peek() == '/' && lexer.peek(1) == '/')
|
||||
lexer.ignore_until([](char ch) { return ch == '\n'; });
|
||||
};
|
||||
|
||||
auto parse_parameter = [&](Vector<Parameter>& storage) {
|
||||
for (;;) {
|
||||
Parameter parameter;
|
||||
consume_whitespace();
|
||||
if (lexer.peek() == ')')
|
||||
break;
|
||||
if (lexer.consume_specific('[')) {
|
||||
for (;;) {
|
||||
if (lexer.consume_specific(']')) {
|
||||
consume_whitespace();
|
||||
break;
|
||||
}
|
||||
if (lexer.consume_specific(',')) {
|
||||
consume_whitespace();
|
||||
}
|
||||
auto attribute = lexer.consume_until([](char ch) { return ch == ']' || ch == ','; });
|
||||
parameter.attributes.append(attribute);
|
||||
consume_whitespace();
|
||||
}
|
||||
}
|
||||
parameter.type = lexer.consume_until([](char ch) { return isspace(ch); });
|
||||
consume_whitespace();
|
||||
parameter.name = lexer.consume_until([](char ch) { return isspace(ch) || ch == ',' || ch == ')'; });
|
||||
consume_whitespace();
|
||||
storage.append(move(parameter));
|
||||
if (lexer.consume_specific(','))
|
||||
continue;
|
||||
if (lexer.peek() == ')')
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
auto parse_parameters = [&](Vector<Parameter>& storage) {
|
||||
for (;;) {
|
||||
consume_whitespace();
|
||||
parse_parameter(storage);
|
||||
consume_whitespace();
|
||||
if (lexer.consume_specific(','))
|
||||
continue;
|
||||
if (lexer.peek() == ')')
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
auto parse_message = [&] {
|
||||
Message message;
|
||||
consume_whitespace();
|
||||
message.name = lexer.consume_until([](char ch) { return isspace(ch) || ch == '('; });
|
||||
consume_whitespace();
|
||||
assert_specific('(');
|
||||
parse_parameters(message.inputs);
|
||||
assert_specific(')');
|
||||
consume_whitespace();
|
||||
assert_specific('=');
|
||||
|
||||
auto type = lexer.consume();
|
||||
if (type == '>')
|
||||
message.is_synchronous = true;
|
||||
else if (type == '|')
|
||||
message.is_synchronous = false;
|
||||
else
|
||||
VERIFY_NOT_REACHED();
|
||||
|
||||
consume_whitespace();
|
||||
|
||||
if (message.is_synchronous) {
|
||||
assert_specific('(');
|
||||
parse_parameters(message.outputs);
|
||||
assert_specific(')');
|
||||
}
|
||||
|
||||
consume_whitespace();
|
||||
|
||||
endpoints.last().messages.append(move(message));
|
||||
};
|
||||
|
||||
auto parse_messages = [&] {
|
||||
for (;;) {
|
||||
consume_whitespace();
|
||||
if (lexer.peek() == '}')
|
||||
break;
|
||||
parse_message();
|
||||
consume_whitespace();
|
||||
}
|
||||
};
|
||||
|
||||
auto parse_include = [&] {
|
||||
String include;
|
||||
consume_whitespace();
|
||||
include = lexer.consume_while([](char ch) { return ch != '\n'; });
|
||||
consume_whitespace();
|
||||
|
||||
endpoints.last().includes.append(move(include));
|
||||
};
|
||||
|
||||
auto parse_includes = [&] {
|
||||
for (;;) {
|
||||
consume_whitespace();
|
||||
if (lexer.peek() != '#')
|
||||
break;
|
||||
parse_include();
|
||||
consume_whitespace();
|
||||
}
|
||||
};
|
||||
|
||||
auto parse_endpoint = [&] {
|
||||
endpoints.empend();
|
||||
consume_whitespace();
|
||||
parse_includes();
|
||||
consume_whitespace();
|
||||
lexer.consume_specific("endpoint");
|
||||
consume_whitespace();
|
||||
endpoints.last().name = lexer.consume_while([](char ch) { return !isspace(ch); });
|
||||
endpoints.last().magic = Traits<String>::hash(endpoints.last().name);
|
||||
consume_whitespace();
|
||||
if (lexer.peek() == '[') {
|
||||
// This only supports a single parameter for now, and adding multiple
|
||||
// endpoint parameter support is left as an exercise for the reader. :^)
|
||||
|
||||
lexer.consume_specific('[');
|
||||
consume_whitespace();
|
||||
|
||||
auto parameter = lexer.consume_while([](char ch) { return !isspace(ch) && ch != '='; });
|
||||
consume_whitespace();
|
||||
assert_specific('=');
|
||||
consume_whitespace();
|
||||
|
||||
if (parameter == "magic") {
|
||||
// "magic" overwrites the default magic with a hardcoded one.
|
||||
auto magic_string = lexer.consume_while([](char ch) { return !isspace(ch) && ch != ']'; });
|
||||
endpoints.last().magic = magic_string.to_uint().value();
|
||||
} else {
|
||||
warnln("parse_endpoint: unknown parameter '{}' passed", parameter);
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
assert_specific(']');
|
||||
consume_whitespace();
|
||||
}
|
||||
assert_specific('{');
|
||||
parse_messages();
|
||||
assert_specific('}');
|
||||
consume_whitespace();
|
||||
};
|
||||
|
||||
while (lexer.tell() < file_contents.size())
|
||||
parse_endpoint();
|
||||
|
||||
StringBuilder builder;
|
||||
SourceGenerator generator { builder };
|
||||
|
||||
generator.append("#pragma once\n");
|
||||
|
||||
// This must occur before LibIPC/Decoder.h
|
||||
for (auto& endpoint : endpoints) {
|
||||
for (auto& include : endpoint.includes) {
|
||||
generator.append(include);
|
||||
generator.append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
generator.append(R"~~~(#include <AK/MemoryStream.h>
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <AK/Result.h>
|
||||
#include <AK/Utf8View.h>
|
||||
#include <LibIPC/Connection.h>
|
||||
#include <LibIPC/Decoder.h>
|
||||
#include <LibIPC/Dictionary.h>
|
||||
#include <LibIPC/Encoder.h>
|
||||
#include <LibIPC/File.h>
|
||||
#include <LibIPC/Message.h>
|
||||
#include <LibIPC/Stub.h>
|
||||
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wdefaulted-function-deleted"
|
||||
#endif
|
||||
)~~~");
|
||||
|
||||
for (auto& endpoint : endpoints) {
|
||||
auto endpoint_generator = generator.fork();
|
||||
|
||||
endpoint_generator.set("endpoint.name", endpoint.name);
|
||||
endpoint_generator.set("endpoint.magic", String::number(endpoint.magic));
|
||||
|
||||
endpoint_generator.append(R"~~~(
|
||||
namespace Messages::@endpoint.name@ {
|
||||
)~~~");
|
||||
|
||||
HashMap<String, int> message_ids;
|
||||
|
||||
endpoint_generator.append(R"~~~(
|
||||
enum class MessageID : i32 {
|
||||
)~~~");
|
||||
for (auto& message : endpoint.messages) {
|
||||
auto message_generator = endpoint_generator.fork();
|
||||
|
||||
message_ids.set(message.name, message_ids.size() + 1);
|
||||
message_generator.set("message.name", message.name);
|
||||
message_generator.set("message.pascal_name", pascal_case(message.name));
|
||||
message_generator.set("message.id", String::number(message_ids.size()));
|
||||
|
||||
message_generator.append(R"~~~(
|
||||
@message.pascal_name@ = @message.id@,
|
||||
)~~~");
|
||||
if (message.is_synchronous) {
|
||||
message_ids.set(message.response_name(), message_ids.size() + 1);
|
||||
message_generator.set("message.name", message.response_name());
|
||||
message_generator.set("message.pascal_name", pascal_case(message.response_name()));
|
||||
message_generator.set("message.id", String::number(message_ids.size()));
|
||||
|
||||
message_generator.append(R"~~~(
|
||||
@message.pascal_name@ = @message.id@,
|
||||
)~~~");
|
||||
}
|
||||
}
|
||||
endpoint_generator.append(R"~~~(
|
||||
};
|
||||
)~~~");
|
||||
|
||||
auto constructor_for_message = [&](const String& name, const Vector<Parameter>& parameters) {
|
||||
StringBuilder builder;
|
||||
builder.append(name);
|
||||
|
||||
if (parameters.is_empty()) {
|
||||
builder.append("() {}");
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
builder.append('(');
|
||||
for (size_t i = 0; i < parameters.size(); ++i) {
|
||||
auto& parameter = parameters[i];
|
||||
builder.append(parameter.type);
|
||||
builder.append(" ");
|
||||
builder.append(parameter.name);
|
||||
if (i != parameters.size() - 1)
|
||||
builder.append(", ");
|
||||
}
|
||||
builder.append(") : ");
|
||||
for (size_t i = 0; i < parameters.size(); ++i) {
|
||||
auto& parameter = parameters[i];
|
||||
builder.append("m_");
|
||||
builder.append(parameter.name);
|
||||
builder.append("(move(");
|
||||
builder.append(parameter.name);
|
||||
builder.append("))");
|
||||
if (i != parameters.size() - 1)
|
||||
builder.append(", ");
|
||||
}
|
||||
builder.append(" {}");
|
||||
return builder.to_string();
|
||||
};
|
||||
|
||||
auto do_message = [&](const String& name, const Vector<Parameter>& parameters, const String& response_type = {}) {
|
||||
auto message_generator = endpoint_generator.fork();
|
||||
auto pascal_name = pascal_case(name);
|
||||
message_generator.set("message.name", name);
|
||||
message_generator.set("message.pascal_name", pascal_name);
|
||||
message_generator.set("message.response_type", response_type);
|
||||
message_generator.set("message.constructor", constructor_for_message(pascal_name, parameters));
|
||||
|
||||
message_generator.append(R"~~~(
|
||||
class @message.pascal_name@ final : public IPC::Message {
|
||||
public:
|
||||
)~~~");
|
||||
|
||||
if (!response_type.is_null())
|
||||
message_generator.append(R"~~~(
|
||||
typedef class @message.response_type@ ResponseType;
|
||||
)~~~");
|
||||
|
||||
message_generator.append(R"~~~(
|
||||
@message.pascal_name@(decltype(nullptr)) : m_ipc_message_valid(false) { }
|
||||
@message.pascal_name@(@message.pascal_name@ const&) = default;
|
||||
@message.pascal_name@(@message.pascal_name@&&) = default;
|
||||
@message.pascal_name@& operator=(@message.pascal_name@ const&) = default;
|
||||
@message.constructor@
|
||||
virtual ~@message.pascal_name@() override {}
|
||||
|
||||
virtual u32 endpoint_magic() const override { return @endpoint.magic@; }
|
||||
virtual i32 message_id() const override { return (int)MessageID::@message.pascal_name@; }
|
||||
static i32 static_message_id() { return (int)MessageID::@message.pascal_name@; }
|
||||
virtual const char* message_name() const override { return "@endpoint.name@::@message.pascal_name@"; }
|
||||
|
||||
static OwnPtr<@message.pascal_name@> decode(InputMemoryStream& stream, [[maybe_unused]] int sockfd)
|
||||
{
|
||||
IPC::Decoder decoder { stream, sockfd };
|
||||
)~~~");
|
||||
|
||||
for (auto& parameter : parameters) {
|
||||
auto parameter_generator = message_generator.fork();
|
||||
|
||||
parameter_generator.set("parameter.type", parameter.type);
|
||||
parameter_generator.set("parameter.name", parameter.name);
|
||||
|
||||
if (parameter.type == "bool")
|
||||
parameter_generator.set("parameter.initial_value", "false");
|
||||
else
|
||||
parameter_generator.set("parameter.initial_value", "{}");
|
||||
|
||||
parameter_generator.append(R"~~~(
|
||||
@parameter.type@ @parameter.name@ = @parameter.initial_value@;
|
||||
if (!decoder.decode(@parameter.name@))
|
||||
return {};
|
||||
)~~~");
|
||||
|
||||
if (parameter.attributes.contains_slow("UTF8")) {
|
||||
parameter_generator.append(R"~~~(
|
||||
if (!Utf8View(@parameter.name@).validate())
|
||||
return {};
|
||||
)~~~");
|
||||
}
|
||||
}
|
||||
|
||||
StringBuilder builder;
|
||||
for (size_t i = 0; i < parameters.size(); ++i) {
|
||||
auto& parameter = parameters[i];
|
||||
builder.append("move(");
|
||||
builder.append(parameter.name);
|
||||
builder.append(")");
|
||||
if (i != parameters.size() - 1)
|
||||
builder.append(", ");
|
||||
}
|
||||
|
||||
message_generator.set("message.constructor_call_parameters", builder.build());
|
||||
|
||||
message_generator.append(R"~~~(
|
||||
return make<@message.pascal_name@>(@message.constructor_call_parameters@);
|
||||
}
|
||||
)~~~");
|
||||
|
||||
message_generator.append(R"~~~(
|
||||
virtual bool valid() const { return m_ipc_message_valid; }
|
||||
|
||||
virtual IPC::MessageBuffer encode() const override
|
||||
{
|
||||
VERIFY(valid());
|
||||
|
||||
IPC::MessageBuffer buffer;
|
||||
IPC::Encoder stream(buffer);
|
||||
stream << endpoint_magic();
|
||||
stream << (int)MessageID::@message.pascal_name@;
|
||||
)~~~");
|
||||
|
||||
for (auto& parameter : parameters) {
|
||||
auto parameter_generator = message_generator.fork();
|
||||
|
||||
parameter_generator.set("parameter.name", parameter.name);
|
||||
parameter_generator.append(R"~~~(
|
||||
stream << m_@parameter.name@;
|
||||
)~~~");
|
||||
}
|
||||
|
||||
message_generator.append(R"~~~(
|
||||
return buffer;
|
||||
}
|
||||
)~~~");
|
||||
|
||||
for (auto& parameter : parameters) {
|
||||
auto parameter_generator = message_generator.fork();
|
||||
parameter_generator.set("parameter.type", parameter.type);
|
||||
parameter_generator.set("parameter.name", parameter.name);
|
||||
parameter_generator.append(R"~~~(
|
||||
const @parameter.type@& @parameter.name@() const { return m_@parameter.name@; }
|
||||
@parameter.type@ take_@parameter.name@() { return move(m_@parameter.name@); }
|
||||
)~~~");
|
||||
}
|
||||
|
||||
message_generator.append(R"~~~(
|
||||
private:
|
||||
bool m_ipc_message_valid { true };
|
||||
)~~~");
|
||||
|
||||
for (auto& parameter : parameters) {
|
||||
auto parameter_generator = message_generator.fork();
|
||||
parameter_generator.set("parameter.type", parameter.type);
|
||||
parameter_generator.set("parameter.name", parameter.name);
|
||||
parameter_generator.append(R"~~~(
|
||||
@parameter.type@ m_@parameter.name@;
|
||||
)~~~");
|
||||
}
|
||||
|
||||
message_generator.append(R"~~~(
|
||||
};
|
||||
)~~~");
|
||||
};
|
||||
for (auto& message : endpoint.messages) {
|
||||
String response_name;
|
||||
if (message.is_synchronous) {
|
||||
response_name = message.response_name();
|
||||
do_message(response_name, message.outputs);
|
||||
}
|
||||
do_message(message.name, message.inputs, response_name);
|
||||
}
|
||||
|
||||
endpoint_generator.append(R"~~~(
|
||||
} // namespace Messages::@endpoint.name@
|
||||
)~~~");
|
||||
|
||||
endpoint_generator.append(R"~~~(
|
||||
template<typename LocalEndpoint, typename PeerEndpoint>
|
||||
class @endpoint.name@Proxy {
|
||||
public:
|
||||
// Used to disambiguate the constructor call.
|
||||
struct Tag { };
|
||||
|
||||
@endpoint.name@Proxy(IPC::Connection<LocalEndpoint, PeerEndpoint>& connection, Tag)
|
||||
: m_connection(connection)
|
||||
{ }
|
||||
)~~~");
|
||||
|
||||
for (auto& message : endpoint.messages) {
|
||||
auto message_generator = endpoint_generator.fork();
|
||||
|
||||
auto do_implement_proxy = [&](String const& name, Vector<Parameter> const& parameters, bool is_synchronous, bool is_try) {
|
||||
String return_type = "void";
|
||||
if (is_synchronous) {
|
||||
if (message.outputs.size() == 1)
|
||||
return_type = message.outputs[0].type;
|
||||
else if (!message.outputs.is_empty())
|
||||
return_type = message_name(endpoint.name, message.name, true);
|
||||
}
|
||||
String inner_return_type = return_type;
|
||||
if (is_try) {
|
||||
StringBuilder builder;
|
||||
builder.append("Result<");
|
||||
builder.append(return_type);
|
||||
builder.append(", IPC::ErrorCode>");
|
||||
return_type = builder.to_string();
|
||||
}
|
||||
message_generator.set("message.name", message.name);
|
||||
message_generator.set("message.pascal_name", pascal_case(message.name));
|
||||
message_generator.set("message.complex_return_type", return_type);
|
||||
message_generator.set("async_prefix_maybe", is_synchronous ? "" : "async_");
|
||||
message_generator.set("try_prefix_maybe", is_try ? "try_" : "");
|
||||
|
||||
message_generator.set("handler_name", name);
|
||||
message_generator.append(R"~~~(
|
||||
@message.complex_return_type@ @try_prefix_maybe@@async_prefix_maybe@@handler_name@()~~~");
|
||||
|
||||
for (size_t i = 0; i < parameters.size(); ++i) {
|
||||
auto& parameter = parameters[i];
|
||||
auto argument_generator = message_generator.fork();
|
||||
argument_generator.set("argument.type", parameter.type);
|
||||
argument_generator.set("argument.name", parameter.name);
|
||||
argument_generator.append("@argument.type@ @argument.name@");
|
||||
if (i != parameters.size() - 1)
|
||||
argument_generator.append(", ");
|
||||
}
|
||||
|
||||
message_generator.append(") {");
|
||||
|
||||
if (is_synchronous && !is_try) {
|
||||
if (return_type != "void") {
|
||||
message_generator.append(R"~~~(
|
||||
return )~~~");
|
||||
if (message.outputs.size() != 1)
|
||||
message_generator.append("move(*");
|
||||
} else {
|
||||
message_generator.append(R"~~~(
|
||||
)~~~");
|
||||
}
|
||||
|
||||
message_generator.append("m_connection.template send_sync<Messages::@endpoint.name@::@message.pascal_name@>(");
|
||||
} else if (is_try) {
|
||||
message_generator.append(R"~~~(
|
||||
auto result = m_connection.template send_sync_but_allow_failure<Messages::@endpoint.name@::@message.pascal_name@>()~~~");
|
||||
} else {
|
||||
message_generator.append(R"~~~(
|
||||
m_connection.post_message(Messages::@endpoint.name@::@message.pascal_name@ { )~~~");
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < parameters.size(); ++i) {
|
||||
auto& parameter = parameters[i];
|
||||
auto argument_generator = message_generator.fork();
|
||||
argument_generator.set("argument.name", parameter.name);
|
||||
if (is_primitive_type(parameters[i].type))
|
||||
argument_generator.append("@argument.name@");
|
||||
else
|
||||
argument_generator.append("move(@argument.name@)");
|
||||
if (i != parameters.size() - 1)
|
||||
argument_generator.append(", ");
|
||||
}
|
||||
|
||||
if (is_synchronous && !is_try) {
|
||||
if (return_type != "void") {
|
||||
message_generator.append(")");
|
||||
}
|
||||
|
||||
if (message.outputs.size() == 1) {
|
||||
message_generator.append("->take_");
|
||||
message_generator.append(message.outputs[0].name);
|
||||
message_generator.append("()");
|
||||
} else
|
||||
message_generator.append(")");
|
||||
|
||||
message_generator.append(";");
|
||||
} else if (is_try) {
|
||||
message_generator.append(R"~~~();
|
||||
if (!result)
|
||||
return IPC::ErrorCode::PeerDisconnected;
|
||||
)~~~");
|
||||
if (inner_return_type != "void") {
|
||||
message_generator.append(R"~~~(
|
||||
return move(*result);
|
||||
)~~~");
|
||||
} else {
|
||||
message_generator.append(R"~~~(
|
||||
return { };
|
||||
)~~~");
|
||||
}
|
||||
} else {
|
||||
message_generator.append(R"~~~( });
|
||||
)~~~");
|
||||
}
|
||||
|
||||
message_generator.append(R"~~~(
|
||||
}
|
||||
)~~~");
|
||||
};
|
||||
|
||||
do_implement_proxy(message.name, message.inputs, message.is_synchronous, false);
|
||||
if (message.is_synchronous) {
|
||||
do_implement_proxy(message.name, message.inputs, false, false);
|
||||
do_implement_proxy(message.name, message.inputs, true, true);
|
||||
}
|
||||
}
|
||||
|
||||
endpoint_generator.append(R"~~~(
|
||||
private:
|
||||
IPC::Connection<LocalEndpoint, PeerEndpoint>& m_connection;
|
||||
};
|
||||
)~~~");
|
||||
|
||||
endpoint_generator.append(R"~~~(
|
||||
template<typename LocalEndpoint, typename PeerEndpoint>
|
||||
class @endpoint.name@Proxy;
|
||||
class @endpoint.name@Stub;
|
||||
|
||||
class @endpoint.name@Endpoint {
|
||||
public:
|
||||
template<typename LocalEndpoint>
|
||||
using Proxy = @endpoint.name@Proxy<LocalEndpoint, @endpoint.name@Endpoint>;
|
||||
using Stub = @endpoint.name@Stub;
|
||||
|
||||
static u32 static_magic() { return @endpoint.magic@; }
|
||||
|
||||
static OwnPtr<IPC::Message> decode_message(ReadonlyBytes buffer, [[maybe_unused]] int sockfd)
|
||||
{
|
||||
InputMemoryStream stream { buffer };
|
||||
u32 message_endpoint_magic = 0;
|
||||
stream >> message_endpoint_magic;
|
||||
if (stream.handle_any_error()) {
|
||||
)~~~");
|
||||
if constexpr (GENERATE_DEBUG) {
|
||||
endpoint_generator.append(R"~~~(
|
||||
dbgln("Failed to read message endpoint magic");
|
||||
)~~~");
|
||||
}
|
||||
endpoint_generator.append(R"~~~(
|
||||
return {};
|
||||
}
|
||||
|
||||
if (message_endpoint_magic != @endpoint.magic@) {
|
||||
)~~~");
|
||||
if constexpr (GENERATE_DEBUG) {
|
||||
endpoint_generator.append(R"~~~(
|
||||
dbgln("@endpoint.name@: Endpoint magic number message_endpoint_magic != @endpoint.magic@, not my message! (the other endpoint may have handled it)");
|
||||
)~~~");
|
||||
}
|
||||
endpoint_generator.append(R"~~~(
|
||||
return {};
|
||||
}
|
||||
|
||||
i32 message_id = 0;
|
||||
stream >> message_id;
|
||||
if (stream.handle_any_error()) {
|
||||
)~~~");
|
||||
if constexpr (GENERATE_DEBUG) {
|
||||
endpoint_generator.append(R"~~~(
|
||||
dbgln("Failed to read message ID");
|
||||
)~~~");
|
||||
}
|
||||
endpoint_generator.append(R"~~~(
|
||||
return {};
|
||||
}
|
||||
|
||||
OwnPtr<IPC::Message> message;
|
||||
switch (message_id) {
|
||||
)~~~");
|
||||
|
||||
for (auto& message : endpoint.messages) {
|
||||
auto do_decode_message = [&](const String& name) {
|
||||
auto message_generator = endpoint_generator.fork();
|
||||
|
||||
message_generator.set("message.name", name);
|
||||
message_generator.set("message.pascal_name", pascal_case(name));
|
||||
|
||||
message_generator.append(R"~~~(
|
||||
case (int)Messages::@endpoint.name@::MessageID::@message.pascal_name@:
|
||||
message = Messages::@endpoint.name@::@message.pascal_name@::decode(stream, sockfd);
|
||||
break;
|
||||
)~~~");
|
||||
};
|
||||
|
||||
do_decode_message(message.name);
|
||||
if (message.is_synchronous)
|
||||
do_decode_message(message.response_name());
|
||||
}
|
||||
|
||||
endpoint_generator.append(R"~~~(
|
||||
default:
|
||||
)~~~");
|
||||
if constexpr (GENERATE_DEBUG) {
|
||||
endpoint_generator.append(R"~~~(
|
||||
dbgln("Failed to decode @endpoint.name@.({})", message_id);
|
||||
)~~~");
|
||||
}
|
||||
endpoint_generator.append(R"~~~(
|
||||
return {};
|
||||
}
|
||||
|
||||
if (stream.handle_any_error()) {
|
||||
)~~~");
|
||||
if constexpr (GENERATE_DEBUG) {
|
||||
endpoint_generator.append(R"~~~(
|
||||
dbgln("Failed to read the message");
|
||||
)~~~");
|
||||
}
|
||||
endpoint_generator.append(R"~~~(
|
||||
return {};
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class @endpoint.name@Stub : public IPC::Stub {
|
||||
public:
|
||||
@endpoint.name@Stub() { }
|
||||
virtual ~@endpoint.name@Stub() override { }
|
||||
|
||||
virtual u32 magic() const override { return @endpoint.magic@; }
|
||||
virtual String name() const override { return "@endpoint.name@"; }
|
||||
|
||||
virtual OwnPtr<IPC::MessageBuffer> handle(const IPC::Message& message) override
|
||||
{
|
||||
switch (message.message_id()) {
|
||||
)~~~");
|
||||
for (auto& message : endpoint.messages) {
|
||||
auto do_handle_message = [&](String const& name, Vector<Parameter> const& parameters, bool returns_something) {
|
||||
auto message_generator = endpoint_generator.fork();
|
||||
|
||||
StringBuilder argument_generator;
|
||||
for (size_t i = 0; i < parameters.size(); ++i) {
|
||||
auto& parameter = parameters[i];
|
||||
argument_generator.append("request.");
|
||||
argument_generator.append(parameter.name);
|
||||
argument_generator.append("()");
|
||||
if (i != parameters.size() - 1)
|
||||
argument_generator.append(", ");
|
||||
}
|
||||
|
||||
message_generator.set("message.pascal_name", pascal_case(name));
|
||||
message_generator.set("message.response_type", pascal_case(message.response_name()));
|
||||
message_generator.set("handler_name", name);
|
||||
message_generator.set("arguments", argument_generator.to_string());
|
||||
message_generator.append(R"~~~(
|
||||
case (int)Messages::@endpoint.name@::MessageID::@message.pascal_name@: {
|
||||
)~~~");
|
||||
if (returns_something) {
|
||||
if (message.outputs.is_empty()) {
|
||||
message_generator.append(R"~~~(
|
||||
[[maybe_unused]] auto& request = static_cast<const Messages::@endpoint.name@::@message.pascal_name@&>(message);
|
||||
@handler_name@(@arguments@);
|
||||
auto response = Messages::@endpoint.name@::@message.response_type@ { };
|
||||
return make<IPC::MessageBuffer>(response.encode());
|
||||
)~~~");
|
||||
} else {
|
||||
message_generator.append(R"~~~(
|
||||
[[maybe_unused]] auto& request = static_cast<const Messages::@endpoint.name@::@message.pascal_name@&>(message);
|
||||
auto response = @handler_name@(@arguments@);
|
||||
if (!response.valid())
|
||||
return {};
|
||||
return make<IPC::MessageBuffer>(response.encode());
|
||||
)~~~");
|
||||
}
|
||||
} else {
|
||||
message_generator.append(R"~~~(
|
||||
[[maybe_unused]] auto& request = static_cast<const Messages::@endpoint.name@::@message.pascal_name@&>(message);
|
||||
@handler_name@(@arguments@);
|
||||
return {};
|
||||
)~~~");
|
||||
}
|
||||
message_generator.append(R"~~~(
|
||||
}
|
||||
)~~~");
|
||||
};
|
||||
do_handle_message(message.name, message.inputs, message.is_synchronous);
|
||||
}
|
||||
endpoint_generator.append(R"~~~(
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
}
|
||||
)~~~");
|
||||
|
||||
for (auto& message : endpoint.messages) {
|
||||
auto message_generator = endpoint_generator.fork();
|
||||
|
||||
auto do_handle_message_decl = [&](String const& name, Vector<Parameter> const& parameters, bool is_response) {
|
||||
String return_type = "void";
|
||||
if (message.is_synchronous && !message.outputs.is_empty() && !is_response)
|
||||
return_type = message_name(endpoint.name, message.name, true);
|
||||
message_generator.set("message.complex_return_type", return_type);
|
||||
|
||||
message_generator.set("handler_name", name);
|
||||
message_generator.append(R"~~~(
|
||||
virtual @message.complex_return_type@ @handler_name@()~~~");
|
||||
|
||||
auto make_argument_type = [](String const& type) {
|
||||
StringBuilder builder;
|
||||
|
||||
bool const_ref = !is_primitive_type(type);
|
||||
|
||||
builder.append(type);
|
||||
if (const_ref)
|
||||
builder.append(" const&");
|
||||
|
||||
return builder.to_string();
|
||||
};
|
||||
|
||||
for (size_t i = 0; i < parameters.size(); ++i) {
|
||||
auto& parameter = parameters[i];
|
||||
auto argument_generator = message_generator.fork();
|
||||
argument_generator.set("argument.type", make_argument_type(parameter.type));
|
||||
argument_generator.set("argument.name", parameter.name);
|
||||
argument_generator.append("[[maybe_unused]] @argument.type@ @argument.name@");
|
||||
if (i != parameters.size() - 1)
|
||||
argument_generator.append(", ");
|
||||
}
|
||||
|
||||
if (is_response) {
|
||||
message_generator.append(R"~~~() { };
|
||||
)~~~");
|
||||
} else {
|
||||
message_generator.append(R"~~~() = 0;
|
||||
)~~~");
|
||||
}
|
||||
};
|
||||
|
||||
do_handle_message_decl(message.name, message.inputs, false);
|
||||
}
|
||||
|
||||
endpoint_generator.append(R"~~~(
|
||||
private:
|
||||
};
|
||||
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
)~~~");
|
||||
}
|
||||
|
||||
outln("{}", generator.as_string_view());
|
||||
|
||||
if constexpr (GENERATE_DEBUG) {
|
||||
for (auto& endpoint : endpoints) {
|
||||
warnln("Endpoint '{}' (magic: {})", endpoint.name, endpoint.magic);
|
||||
for (auto& message : endpoint.messages) {
|
||||
warnln(" Message: '{}'", message.name);
|
||||
warnln(" Sync: {}", message.is_synchronous);
|
||||
warnln(" Inputs:");
|
||||
for (auto& parameter : message.inputs)
|
||||
warnln(" Parameter: {} ({})", parameter.name, parameter.type);
|
||||
if (message.inputs.is_empty())
|
||||
warnln(" (none)");
|
||||
if (message.is_synchronous) {
|
||||
warnln(" Outputs:");
|
||||
for (auto& parameter : message.outputs)
|
||||
warnln(" Parameter: {} ({})", parameter.name, parameter.type);
|
||||
if (message.outputs.is_empty())
|
||||
warnln(" (none)");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
add_executable(GenerateUnicodeData GenerateUnicodeData.cpp)
|
||||
target_link_libraries(GenerateUnicodeData LagomCore)
|
||||
|
||||
add_executable(GenerateUnicodeLocale GenerateUnicodeLocale.cpp)
|
||||
target_link_libraries(GenerateUnicodeLocale LagomCore)
|
1066
Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeData.cpp
Normal file
1066
Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeData.cpp
Normal file
File diff suppressed because it is too large
Load diff
|
@ -0,0 +1,526 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/AllOf.h>
|
||||
#include <AK/CharacterTypes.h>
|
||||
#include <AK/Format.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/JsonObject.h>
|
||||
#include <AK/JsonParser.h>
|
||||
#include <AK/JsonValue.h>
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <AK/QuickSort.h>
|
||||
#include <AK/SourceGenerator.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibCore/DirIterator.h>
|
||||
#include <LibCore/File.h>
|
||||
|
||||
struct Locale {
|
||||
String language;
|
||||
Optional<String> territory;
|
||||
Optional<String> variant;
|
||||
HashMap<String, String> languages;
|
||||
HashMap<String, String> territories;
|
||||
HashMap<String, String> scripts;
|
||||
HashMap<String, String> currencies;
|
||||
};
|
||||
|
||||
struct UnicodeLocaleData {
|
||||
HashMap<String, Locale> locales;
|
||||
Vector<String> languages;
|
||||
Vector<String> territories;
|
||||
Vector<String> scripts;
|
||||
Vector<String> variants;
|
||||
Vector<String> currencies;
|
||||
};
|
||||
|
||||
static void write_to_file_if_different(Core::File& file, StringView contents)
|
||||
{
|
||||
auto const current_contents = file.read_all();
|
||||
|
||||
if (StringView { current_contents.bytes() } == contents)
|
||||
return;
|
||||
|
||||
VERIFY(file.seek(0));
|
||||
VERIFY(file.truncate(0));
|
||||
VERIFY(file.write(contents));
|
||||
}
|
||||
|
||||
static void parse_identity(String locale_path, UnicodeLocaleData& locale_data, Locale& locale)
|
||||
{
|
||||
LexicalPath languages_path(move(locale_path)); // Note: Every JSON file defines identity data, so we can use any of them.
|
||||
languages_path = languages_path.append("languages.json"sv);
|
||||
VERIFY(Core::File::exists(languages_path.string()));
|
||||
|
||||
auto languages_file_or_error = Core::File::open(languages_path.string(), Core::OpenMode::ReadOnly);
|
||||
VERIFY(!languages_file_or_error.is_error());
|
||||
|
||||
auto languages = JsonParser(languages_file_or_error.value()->read_all()).parse();
|
||||
VERIFY(languages.has_value());
|
||||
|
||||
auto const& main_object = languages->as_object().get("main"sv);
|
||||
auto const& locale_object = main_object.as_object().get(languages_path.parent().basename());
|
||||
auto const& identity_object = locale_object.as_object().get("identity"sv);
|
||||
auto const& language_string = identity_object.as_object().get("language"sv);
|
||||
auto const& territory_string = identity_object.as_object().get("territory"sv);
|
||||
auto const& variant_string = identity_object.as_object().get("variant"sv);
|
||||
|
||||
locale.language = language_string.as_string();
|
||||
if (!locale_data.languages.contains_slow(locale.language))
|
||||
locale_data.languages.append(locale.language);
|
||||
|
||||
if (territory_string.is_string()) {
|
||||
locale.territory = territory_string.as_string();
|
||||
if (!locale_data.territories.contains_slow(*locale.territory))
|
||||
locale_data.territories.append(*locale.territory);
|
||||
}
|
||||
|
||||
if (variant_string.is_string()) {
|
||||
locale.variant = variant_string.as_string();
|
||||
if (!locale_data.variants.contains_slow(*locale.variant))
|
||||
locale_data.variants.append(*locale.variant);
|
||||
}
|
||||
}
|
||||
|
||||
static void parse_locale_languages(String locale_path, Locale& locale)
|
||||
{
|
||||
LexicalPath languages_path(move(locale_path));
|
||||
languages_path = languages_path.append("languages.json"sv);
|
||||
VERIFY(Core::File::exists(languages_path.string()));
|
||||
|
||||
auto languages_file_or_error = Core::File::open(languages_path.string(), Core::OpenMode::ReadOnly);
|
||||
VERIFY(!languages_file_or_error.is_error());
|
||||
|
||||
auto languages = JsonParser(languages_file_or_error.value()->read_all()).parse();
|
||||
VERIFY(languages.has_value());
|
||||
|
||||
auto const& main_object = languages->as_object().get("main"sv);
|
||||
auto const& locale_object = main_object.as_object().get(languages_path.parent().basename());
|
||||
auto const& locale_display_names_object = locale_object.as_object().get("localeDisplayNames"sv);
|
||||
auto const& languages_object = locale_display_names_object.as_object().get("languages"sv);
|
||||
|
||||
languages_object.as_object().for_each_member([&](auto const& key, JsonValue const& value) {
|
||||
locale.languages.set(key, value.as_string());
|
||||
});
|
||||
}
|
||||
|
||||
static void parse_locale_territories(String locale_path, Locale& locale)
|
||||
{
|
||||
LexicalPath territories_path(move(locale_path));
|
||||
territories_path = territories_path.append("territories.json"sv);
|
||||
VERIFY(Core::File::exists(territories_path.string()));
|
||||
|
||||
auto territories_file_or_error = Core::File::open(territories_path.string(), Core::OpenMode::ReadOnly);
|
||||
VERIFY(!territories_file_or_error.is_error());
|
||||
|
||||
auto territories = JsonParser(territories_file_or_error.value()->read_all()).parse();
|
||||
VERIFY(territories.has_value());
|
||||
|
||||
auto const& main_object = territories->as_object().get("main"sv);
|
||||
auto const& locale_object = main_object.as_object().get(territories_path.parent().basename());
|
||||
auto const& locale_display_names_object = locale_object.as_object().get("localeDisplayNames"sv);
|
||||
auto const& territories_object = locale_display_names_object.as_object().get("territories"sv);
|
||||
|
||||
territories_object.as_object().for_each_member([&](auto const& key, JsonValue const& value) {
|
||||
locale.territories.set(key, value.as_string());
|
||||
});
|
||||
}
|
||||
|
||||
static void parse_locale_scripts(String locale_path, UnicodeLocaleData& locale_data, Locale& locale)
|
||||
{
|
||||
LexicalPath scripts_path(move(locale_path));
|
||||
scripts_path = scripts_path.append("scripts.json"sv);
|
||||
VERIFY(Core::File::exists(scripts_path.string()));
|
||||
|
||||
auto scripts_file_or_error = Core::File::open(scripts_path.string(), Core::OpenMode::ReadOnly);
|
||||
VERIFY(!scripts_file_or_error.is_error());
|
||||
|
||||
auto scripts = JsonParser(scripts_file_or_error.value()->read_all()).parse();
|
||||
VERIFY(scripts.has_value());
|
||||
|
||||
auto const& main_object = scripts->as_object().get("main"sv);
|
||||
auto const& locale_object = main_object.as_object().get(scripts_path.parent().basename());
|
||||
auto const& locale_display_names_object = locale_object.as_object().get("localeDisplayNames"sv);
|
||||
auto const& scripts_object = locale_display_names_object.as_object().get("scripts"sv);
|
||||
|
||||
scripts_object.as_object().for_each_member([&](auto const& key, JsonValue const& value) {
|
||||
locale.scripts.set(key, value.as_string());
|
||||
if (!locale_data.scripts.contains_slow(key))
|
||||
locale_data.scripts.append(key);
|
||||
});
|
||||
}
|
||||
|
||||
static void parse_locale_currencies(String numbers_path, UnicodeLocaleData& locale_data, Locale& locale)
|
||||
{
|
||||
LexicalPath currencies_path(move(numbers_path));
|
||||
currencies_path = currencies_path.append("currencies.json"sv);
|
||||
VERIFY(Core::File::exists(currencies_path.string()));
|
||||
|
||||
auto currencies_file_or_error = Core::File::open(currencies_path.string(), Core::OpenMode::ReadOnly);
|
||||
VERIFY(!currencies_file_or_error.is_error());
|
||||
|
||||
auto currencies = JsonParser(currencies_file_or_error.value()->read_all()).parse();
|
||||
VERIFY(currencies.has_value());
|
||||
|
||||
auto const& main_object = currencies->as_object().get("main"sv);
|
||||
auto const& locale_object = main_object.as_object().get(currencies_path.parent().basename());
|
||||
auto const& locale_numbers_object = locale_object.as_object().get("numbers"sv);
|
||||
auto const& currencies_object = locale_numbers_object.as_object().get("currencies"sv);
|
||||
|
||||
currencies_object.as_object().for_each_member([&](auto const& key, JsonValue const& value) {
|
||||
auto const& display_name = value.as_object().get("displayName"sv);
|
||||
locale.currencies.set(key, display_name.as_string());
|
||||
if (!locale_data.currencies.contains_slow(key))
|
||||
locale_data.currencies.append(key);
|
||||
});
|
||||
}
|
||||
|
||||
static Core::DirIterator path_to_dir_iterator(String path)
|
||||
{
|
||||
LexicalPath lexical_path(move(path));
|
||||
lexical_path = lexical_path.append("main"sv);
|
||||
VERIFY(Core::File::is_directory(lexical_path.string()));
|
||||
|
||||
Core::DirIterator iterator(lexical_path.string(), Core::DirIterator::SkipParentAndBaseDir);
|
||||
if (iterator.has_error()) {
|
||||
warnln("{}: {}", lexical_path.string(), iterator.error_string());
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
return iterator;
|
||||
}
|
||||
|
||||
static void parse_all_locales(String locale_names_path, String numbers_path, UnicodeLocaleData& locale_data)
|
||||
{
|
||||
auto locale_names_iterator = path_to_dir_iterator(move(locale_names_path));
|
||||
auto numbers_iterator = path_to_dir_iterator(move(numbers_path));
|
||||
|
||||
while (locale_names_iterator.has_next()) {
|
||||
auto locale_path = locale_names_iterator.next_full_path();
|
||||
VERIFY(Core::File::is_directory(locale_path));
|
||||
|
||||
auto& locale = locale_data.locales.ensure(LexicalPath::basename(locale_path));
|
||||
parse_identity(locale_path, locale_data, locale);
|
||||
parse_locale_languages(locale_path, locale);
|
||||
parse_locale_territories(locale_path, locale);
|
||||
parse_locale_scripts(locale_path, locale_data, locale);
|
||||
}
|
||||
|
||||
while (numbers_iterator.has_next()) {
|
||||
auto numbers_path = numbers_iterator.next_full_path();
|
||||
VERIFY(Core::File::is_directory(numbers_path));
|
||||
|
||||
auto& locale = locale_data.locales.ensure(LexicalPath::basename(numbers_path));
|
||||
parse_locale_currencies(numbers_path, locale_data, locale);
|
||||
}
|
||||
}
|
||||
|
||||
static String format_identifier(StringView owner, String identifier)
|
||||
{
|
||||
identifier.replace("-"sv, "_"sv, true);
|
||||
|
||||
if (all_of(identifier, is_ascii_digit))
|
||||
return String::formatted("{}_{}", owner[0], identifier);
|
||||
return identifier.to_titlecase();
|
||||
}
|
||||
|
||||
static void generate_unicode_locale_header(Core::File& file, UnicodeLocaleData& locale_data)
|
||||
{
|
||||
StringBuilder builder;
|
||||
SourceGenerator generator { builder };
|
||||
|
||||
auto generate_enum = [&](StringView name, StringView default_, Vector<String>& values) {
|
||||
quick_sort(values);
|
||||
|
||||
generator.set("name", name);
|
||||
generator.set("underlying", ((values.size() + !default_.is_empty()) < 256) ? "u8"sv : "u16"sv);
|
||||
|
||||
generator.append(R"~~~(
|
||||
enum class @name@ : @underlying@ {)~~~");
|
||||
|
||||
if (!default_.is_empty()) {
|
||||
generator.set("default", default_);
|
||||
generator.append(R"~~~(
|
||||
@default@,)~~~");
|
||||
}
|
||||
|
||||
for (auto const& value : values) {
|
||||
generator.set("value", format_identifier(name, value));
|
||||
generator.append(R"~~~(
|
||||
@value@,)~~~");
|
||||
}
|
||||
|
||||
generator.append(R"~~~(
|
||||
};
|
||||
)~~~");
|
||||
};
|
||||
|
||||
generator.append(R"~~~(
|
||||
#pragma once
|
||||
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/Types.h>
|
||||
#include <LibUnicode/Forward.h>
|
||||
|
||||
namespace Unicode {
|
||||
)~~~");
|
||||
|
||||
auto locales = locale_data.locales.keys();
|
||||
generate_enum("Locale"sv, "None"sv, locales);
|
||||
generate_enum("Language"sv, {}, locale_data.languages);
|
||||
generate_enum("Territory"sv, {}, locale_data.territories);
|
||||
generate_enum("ScriptTag"sv, {}, locale_data.scripts);
|
||||
generate_enum("Currency"sv, {}, locale_data.currencies);
|
||||
generate_enum("Variant"sv, {}, locale_data.variants);
|
||||
|
||||
generator.append(R"~~~(
|
||||
namespace Detail {
|
||||
|
||||
Optional<Locale> locale_from_string(StringView const& locale);
|
||||
|
||||
Optional<StringView> get_locale_language_mapping(StringView locale, StringView language);
|
||||
Optional<Language> language_from_string(StringView const& language);
|
||||
|
||||
Optional<StringView> get_locale_territory_mapping(StringView locale, StringView territory);
|
||||
Optional<Territory> territory_from_string(StringView const& territory);
|
||||
|
||||
Optional<StringView> get_locale_script_tag_mapping(StringView locale, StringView script_tag);
|
||||
Optional<ScriptTag> script_tag_from_string(StringView const& script_tag);
|
||||
|
||||
Optional<StringView> get_locale_currency_mapping(StringView locale, StringView currency);
|
||||
Optional<Currency> currency_from_string(StringView const& currency);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
)~~~");
|
||||
|
||||
write_to_file_if_different(file, generator.as_string_view());
|
||||
}
|
||||
|
||||
static void generate_unicode_locale_implementation(Core::File& file, UnicodeLocaleData& locale_data)
|
||||
{
|
||||
StringBuilder builder;
|
||||
SourceGenerator generator { builder };
|
||||
generator.set("locales_size"sv, String::number(locale_data.locales.size()));
|
||||
generator.set("territories_size", String::number(locale_data.territories.size()));
|
||||
|
||||
generator.append(R"~~~(
|
||||
#include <AK/Array.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/Span.h>
|
||||
#include <LibUnicode/UnicodeLocale.h>
|
||||
|
||||
namespace Unicode {
|
||||
)~~~");
|
||||
|
||||
auto format_mapping_name = [](StringView format, StringView name) {
|
||||
auto mapping_name = name.to_lowercase_string();
|
||||
mapping_name.replace("-"sv, "_"sv, true);
|
||||
return String::formatted(format, mapping_name);
|
||||
};
|
||||
|
||||
auto append_mapping_list = [&](String name, auto const& keys, auto const& mappings) {
|
||||
generator.set("name", name);
|
||||
generator.set("size", String::number(keys.size()));
|
||||
|
||||
generator.append(R"~~~(
|
||||
static constexpr Array<StringView, @size@> @name@ { {
|
||||
)~~~");
|
||||
|
||||
constexpr size_t max_values_per_row = 10;
|
||||
size_t values_in_current_row = 0;
|
||||
|
||||
for (auto const& key : keys) {
|
||||
if (values_in_current_row++ > 0)
|
||||
generator.append(" ");
|
||||
|
||||
if (auto it = mappings.find(key); it != mappings.end())
|
||||
generator.set("mapping"sv, String::formatted("\"{}\"sv", it->value));
|
||||
else
|
||||
generator.set("mapping"sv, "{}"sv);
|
||||
generator.append("@mapping@,");
|
||||
|
||||
if (values_in_current_row == max_values_per_row) {
|
||||
values_in_current_row = 0;
|
||||
generator.append("\n ");
|
||||
}
|
||||
}
|
||||
|
||||
generator.append(R"~~~(
|
||||
} };
|
||||
)~~~");
|
||||
};
|
||||
|
||||
auto append_mapping = [&](StringView name, StringView format, auto const& keys, auto get_mapping_callback) {
|
||||
Vector<String> mapping_names;
|
||||
|
||||
for (auto const& locale : locale_data.locales) {
|
||||
auto mapping_name = format_mapping_name(format, locale.key);
|
||||
append_mapping_list(mapping_name, keys, get_mapping_callback(locale.value));
|
||||
mapping_names.append(move(mapping_name));
|
||||
}
|
||||
|
||||
quick_sort(mapping_names);
|
||||
|
||||
generator.set("name", name);
|
||||
generator.set("size", String::number(locale_data.locales.size()));
|
||||
generator.append(R"~~~(
|
||||
static constexpr Array<Span<StringView const>, @size@> @name@ { {
|
||||
)~~~");
|
||||
|
||||
constexpr size_t max_values_per_row = 10;
|
||||
size_t values_in_current_row = 0;
|
||||
|
||||
for (auto& mapping_name : mapping_names) {
|
||||
if (values_in_current_row++ > 0)
|
||||
generator.append(" ");
|
||||
|
||||
generator.set("name", move(mapping_name));
|
||||
generator.append("@name@.span(),");
|
||||
|
||||
if (values_in_current_row == max_values_per_row) {
|
||||
values_in_current_row = 0;
|
||||
generator.append("\n ");
|
||||
}
|
||||
}
|
||||
|
||||
generator.append(R"~~~(
|
||||
} };
|
||||
)~~~");
|
||||
};
|
||||
|
||||
append_mapping("s_languages"sv, "s_languages_{}", locale_data.languages, [](auto const& value) { return value.languages; });
|
||||
append_mapping("s_territories"sv, "s_territories_{}", locale_data.territories, [](auto const& value) { return value.territories; });
|
||||
append_mapping("s_scripts"sv, "s_scripts_{}", locale_data.scripts, [](auto const& value) { return value.scripts; });
|
||||
append_mapping("s_currencies"sv, "s_currencies_{}", locale_data.currencies, [](auto const& value) { return value.currencies; });
|
||||
|
||||
generator.append(R"~~~(
|
||||
namespace Detail {
|
||||
)~~~");
|
||||
|
||||
auto append_mapping_search = [&](StringView enum_title, StringView enum_snake, StringView collection_name) {
|
||||
generator.set("enum_title", enum_title);
|
||||
generator.set("enum_snake", enum_snake);
|
||||
generator.set("collection_name", collection_name);
|
||||
generator.append(R"~~~(
|
||||
Optional<StringView> get_locale_@enum_snake@_mapping(StringView locale, StringView @enum_snake@)
|
||||
{
|
||||
auto locale_value = locale_from_string(locale);
|
||||
if (!locale_value.has_value())
|
||||
return {};
|
||||
|
||||
auto @enum_snake@_value = @enum_snake@_from_string(@enum_snake@);
|
||||
if (!@enum_snake@_value.has_value())
|
||||
return {};
|
||||
|
||||
auto locale_index = to_underlying(*locale_value) - 1; // Subtract 1 because 0 == Locale::None.
|
||||
auto @enum_snake@_index = to_underlying(*@enum_snake@_value);
|
||||
|
||||
auto const& mappings = @collection_name@.at(locale_index);
|
||||
auto @enum_snake@_mapping = mappings.at(@enum_snake@_index);
|
||||
|
||||
if (@enum_snake@_mapping.is_empty())
|
||||
return {};
|
||||
return @enum_snake@_mapping;
|
||||
}
|
||||
)~~~");
|
||||
};
|
||||
|
||||
auto append_from_string = [&](StringView enum_title, StringView enum_snake, Vector<String> const& values) {
|
||||
generator.set("enum_title", enum_title);
|
||||
generator.set("enum_snake", enum_snake);
|
||||
|
||||
generator.append(R"~~~(
|
||||
Optional<@enum_title@> @enum_snake@_from_string(StringView const& @enum_snake@)
|
||||
{
|
||||
static HashMap<StringView, @enum_title@> @enum_snake@_values { {)~~~");
|
||||
|
||||
for (auto const& value : values) {
|
||||
generator.set("key"sv, value);
|
||||
generator.set("value"sv, format_identifier(enum_title, value));
|
||||
|
||||
generator.append(R"~~~(
|
||||
{ "@key@"sv, @enum_title@::@value@ },)~~~");
|
||||
}
|
||||
|
||||
generator.append(R"~~~(
|
||||
} };
|
||||
|
||||
if (auto value = @enum_snake@_values.get(@enum_snake@); value.has_value())
|
||||
return value.value();
|
||||
return {};
|
||||
}
|
||||
)~~~");
|
||||
};
|
||||
|
||||
append_from_string("Locale"sv, "locale"sv, locale_data.locales.keys());
|
||||
|
||||
append_mapping_search("Language"sv, "language"sv, "s_languages"sv);
|
||||
append_from_string("Language"sv, "language"sv, locale_data.languages);
|
||||
|
||||
append_mapping_search("Territory"sv, "territory"sv, "s_territories"sv);
|
||||
append_from_string("Territory"sv, "territory"sv, locale_data.territories);
|
||||
|
||||
append_mapping_search("ScriptTag"sv, "script_tag"sv, "s_scripts"sv);
|
||||
append_from_string("ScriptTag"sv, "script_tag"sv, locale_data.scripts);
|
||||
|
||||
append_mapping_search("Currency"sv, "currency"sv, "s_currencies"sv);
|
||||
append_from_string("Currency"sv, "currency"sv, locale_data.currencies);
|
||||
|
||||
generator.append(R"~~~(
|
||||
}
|
||||
|
||||
}
|
||||
)~~~");
|
||||
|
||||
write_to_file_if_different(file, generator.as_string_view());
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
char const* generated_header_path = nullptr;
|
||||
char const* generated_implementation_path = nullptr;
|
||||
char const* locale_names_path = nullptr;
|
||||
char const* numbers_path = nullptr;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_option(generated_header_path, "Path to the Unicode locale header file to generate", "generated-header-path", 'h', "generated-header-path");
|
||||
args_parser.add_option(generated_implementation_path, "Path to the Unicode locale implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path");
|
||||
args_parser.add_option(locale_names_path, "Path to cldr-localenames directory", "locale-names-path", 'l', "locale-names-path");
|
||||
args_parser.add_option(numbers_path, "Path to cldr-numbers directory", "numbers-path", 'n', "numbers-path");
|
||||
args_parser.parse(argc, argv);
|
||||
|
||||
auto open_file = [&](StringView path, StringView flags, Core::OpenMode mode = Core::OpenMode::ReadOnly) {
|
||||
if (path.is_empty()) {
|
||||
warnln("{} is required", flags);
|
||||
args_parser.print_usage(stderr, argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
auto file_or_error = Core::File::open(path, mode);
|
||||
if (file_or_error.is_error()) {
|
||||
warnln("Failed to open {}: {}", path, file_or_error.release_error());
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return file_or_error.release_value();
|
||||
};
|
||||
|
||||
auto generated_header_file = open_file(generated_header_path, "-h/--generated-header-path", Core::OpenMode::ReadWrite);
|
||||
auto generated_implementation_file = open_file(generated_implementation_path, "-c/--generated-implementation-path", Core::OpenMode::ReadWrite);
|
||||
|
||||
UnicodeLocaleData locale_data;
|
||||
parse_all_locales(locale_names_path, numbers_path, locale_data);
|
||||
|
||||
generate_unicode_locale_header(generated_header_file, locale_data);
|
||||
generate_unicode_locale_implementation(generated_implementation_file, locale_data);
|
||||
|
||||
return 0;
|
||||
}
|
11
Meta/Lagom/Tools/CodeGenerators/LibWeb/CMakeLists.txt
Normal file
11
Meta/Lagom/Tools/CodeGenerators/LibWeb/CMakeLists.txt
Normal file
|
@ -0,0 +1,11 @@
|
|||
add_executable(Generate_CSS_PropertyID_h Generate_CSS_PropertyID_h.cpp)
|
||||
add_executable(Generate_CSS_PropertyID_cpp Generate_CSS_PropertyID_cpp.cpp)
|
||||
add_executable(Generate_CSS_ValueID_h Generate_CSS_ValueID_h.cpp)
|
||||
add_executable(Generate_CSS_ValueID_cpp Generate_CSS_ValueID_cpp.cpp)
|
||||
add_executable(WrapperGenerator WrapperGenerator.cpp)
|
||||
target_compile_options(WrapperGenerator PUBLIC -g)
|
||||
target_link_libraries(Generate_CSS_PropertyID_h LagomCore)
|
||||
target_link_libraries(Generate_CSS_PropertyID_cpp LagomCore)
|
||||
target_link_libraries(Generate_CSS_ValueID_h LagomCore)
|
||||
target_link_libraries(Generate_CSS_ValueID_cpp LagomCore)
|
||||
target_link_libraries(WrapperGenerator LagomCore)
|
|
@ -0,0 +1,193 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/JsonObject.h>
|
||||
#include <AK/SourceGenerator.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <ctype.h>
|
||||
|
||||
static String title_casify(const String& dashy_name)
|
||||
{
|
||||
auto parts = dashy_name.split('-');
|
||||
StringBuilder builder;
|
||||
for (auto& part : parts) {
|
||||
if (part.is_empty())
|
||||
continue;
|
||||
builder.append(toupper(part[0]));
|
||||
if (part.length() == 1)
|
||||
continue;
|
||||
builder.append(part.substring_view(1, part.length() - 1));
|
||||
}
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 2) {
|
||||
warnln("usage: {} <path/to/CSS/Properties.json>", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
auto file = Core::File::construct(argv[1]);
|
||||
if (!file->open(Core::OpenMode::ReadOnly))
|
||||
return 1;
|
||||
|
||||
auto json = JsonValue::from_string(file->read_all());
|
||||
VERIFY(json.has_value());
|
||||
VERIFY(json.value().is_object());
|
||||
|
||||
StringBuilder builder;
|
||||
SourceGenerator generator { builder };
|
||||
|
||||
generator.append(R"~~~(
|
||||
#include <AK/Assertions.h>
|
||||
#include <LibWeb/CSS/Parser/Parser.h>
|
||||
#include <LibWeb/CSS/PropertyID.h>
|
||||
#include <LibWeb/CSS/StyleValue.h>
|
||||
|
||||
namespace Web::CSS {
|
||||
|
||||
PropertyID property_id_from_string(const StringView& string)
|
||||
{
|
||||
)~~~");
|
||||
|
||||
json.value().as_object().for_each_member([&](auto& name, auto& value) {
|
||||
VERIFY(value.is_object());
|
||||
|
||||
auto member_generator = generator.fork();
|
||||
member_generator.set("name", name);
|
||||
member_generator.set("name:titlecase", title_casify(name));
|
||||
member_generator.append(R"~~~(
|
||||
if (string.equals_ignoring_case("@name@"))
|
||||
return PropertyID::@name:titlecase@;
|
||||
)~~~");
|
||||
});
|
||||
|
||||
generator.append(R"~~~(
|
||||
return PropertyID::Invalid;
|
||||
}
|
||||
|
||||
const char* string_from_property_id(PropertyID property_id) {
|
||||
switch (property_id) {
|
||||
)~~~");
|
||||
|
||||
json.value().as_object().for_each_member([&](auto& name, auto& value) {
|
||||
VERIFY(value.is_object());
|
||||
|
||||
auto member_generator = generator.fork();
|
||||
member_generator.set("name", name);
|
||||
member_generator.set("name:titlecase", title_casify(name));
|
||||
member_generator.append(R"~~~(
|
||||
case PropertyID::@name:titlecase@:
|
||||
return "@name@";
|
||||
)~~~");
|
||||
});
|
||||
|
||||
generator.append(R"~~~(
|
||||
default:
|
||||
return "(invalid CSS::PropertyID)";
|
||||
}
|
||||
}
|
||||
|
||||
bool is_inherited_property(PropertyID property_id)
|
||||
{
|
||||
switch (property_id) {
|
||||
)~~~");
|
||||
|
||||
json.value().as_object().for_each_member([&](auto& name, auto& value) {
|
||||
VERIFY(value.is_object());
|
||||
|
||||
bool inherited = false;
|
||||
if (value.as_object().has("inherited")) {
|
||||
auto& inherited_value = value.as_object().get("inherited");
|
||||
VERIFY(inherited_value.is_bool());
|
||||
inherited = inherited_value.as_bool();
|
||||
}
|
||||
|
||||
if (inherited) {
|
||||
auto member_generator = generator.fork();
|
||||
member_generator.set("name:titlecase", title_casify(name));
|
||||
member_generator.append(R"~~~(
|
||||
case PropertyID::@name:titlecase@:
|
||||
return true;
|
||||
)~~~");
|
||||
}
|
||||
});
|
||||
|
||||
generator.append(R"~~~(
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool is_pseudo_property(PropertyID property_id)
|
||||
{
|
||||
switch (property_id) {
|
||||
)~~~");
|
||||
|
||||
json.value().as_object().for_each_member([&](auto& name, auto& value) {
|
||||
VERIFY(value.is_object());
|
||||
|
||||
bool pseudo = false;
|
||||
if (value.as_object().has("pseudo")) {
|
||||
auto& pseudo_value = value.as_object().get("pseudo");
|
||||
VERIFY(pseudo_value.is_bool());
|
||||
pseudo = pseudo_value.as_bool();
|
||||
}
|
||||
|
||||
if (pseudo) {
|
||||
auto member_generator = generator.fork();
|
||||
member_generator.set("name:titlecase", title_casify(name));
|
||||
member_generator.append(R"~~~(
|
||||
case PropertyID::@name:titlecase@:
|
||||
return true;
|
||||
)~~~");
|
||||
}
|
||||
});
|
||||
|
||||
generator.append(R"~~~(
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
RefPtr<StyleValue> property_initial_value(PropertyID property_id)
|
||||
{
|
||||
static HashMap<PropertyID, NonnullRefPtr<StyleValue>> initial_values;
|
||||
if (initial_values.is_empty()) {
|
||||
ParsingContext parsing_context;
|
||||
)~~~");
|
||||
|
||||
json.value().as_object().for_each_member([&](auto& name, auto& value) {
|
||||
VERIFY(value.is_object());
|
||||
|
||||
if (value.as_object().has("initial")) {
|
||||
auto& initial_value = value.as_object().get("initial");
|
||||
VERIFY(initial_value.is_string());
|
||||
auto initial_value_string = initial_value.as_string();
|
||||
|
||||
auto member_generator = generator.fork();
|
||||
member_generator.set("name:titlecase", title_casify(name));
|
||||
member_generator.set("initial_value_string", initial_value_string);
|
||||
member_generator.append(R"~~~(
|
||||
if (auto parsed_value = Parser(parsing_context, "@initial_value_string@").parse_as_css_value(PropertyID::@name:titlecase@))
|
||||
initial_values.set(PropertyID::@name:titlecase@, parsed_value.release_nonnull());
|
||||
)~~~");
|
||||
}
|
||||
});
|
||||
|
||||
generator.append(R"~~~(
|
||||
}
|
||||
return initial_values.get(property_id).value_or(nullptr);
|
||||
}
|
||||
|
||||
} // namespace Web::CSS
|
||||
|
||||
)~~~");
|
||||
|
||||
outln("{}", generator.as_string_view());
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/JsonObject.h>
|
||||
#include <AK/SourceGenerator.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <ctype.h>
|
||||
|
||||
static String title_casify(const String& dashy_name)
|
||||
{
|
||||
auto parts = dashy_name.split('-');
|
||||
StringBuilder builder;
|
||||
for (auto& part : parts) {
|
||||
if (part.is_empty())
|
||||
continue;
|
||||
builder.append(toupper(part[0]));
|
||||
if (part.length() == 1)
|
||||
continue;
|
||||
builder.append(part.substring_view(1, part.length() - 1));
|
||||
}
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 2) {
|
||||
warnln("usage: {} <path/to/CSS/Properties.json>", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
auto file = Core::File::construct(argv[1]);
|
||||
if (!file->open(Core::OpenMode::ReadOnly))
|
||||
return 1;
|
||||
|
||||
auto json = JsonValue::from_string(file->read_all());
|
||||
VERIFY(json.has_value());
|
||||
VERIFY(json.value().is_object());
|
||||
|
||||
StringBuilder builder;
|
||||
SourceGenerator generator { builder };
|
||||
generator.append(R"~~~(
|
||||
#pragma once
|
||||
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/Traits.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
|
||||
namespace Web::CSS {
|
||||
|
||||
enum class PropertyID {
|
||||
Invalid,
|
||||
Custom,
|
||||
)~~~");
|
||||
|
||||
json.value().as_object().for_each_member([&](auto& name, auto& value) {
|
||||
VERIFY(value.is_object());
|
||||
|
||||
auto member_generator = generator.fork();
|
||||
member_generator.set("name:titlecase", title_casify(name));
|
||||
|
||||
member_generator.append(R"~~~(
|
||||
@name:titlecase@,
|
||||
)~~~");
|
||||
});
|
||||
|
||||
generator.append(R"~~~(
|
||||
};
|
||||
|
||||
PropertyID property_id_from_string(const StringView&);
|
||||
const char* string_from_property_id(PropertyID);
|
||||
bool is_inherited_property(PropertyID);
|
||||
bool is_pseudo_property(PropertyID);
|
||||
RefPtr<StyleValue> property_initial_value(PropertyID);
|
||||
|
||||
} // namespace Web::CSS
|
||||
|
||||
namespace AK {
|
||||
template<>
|
||||
struct Traits<Web::CSS::PropertyID> : public GenericTraits<Web::CSS::PropertyID> {
|
||||
static unsigned hash(Web::CSS::PropertyID property_id) { return int_hash((unsigned)property_id); }
|
||||
};
|
||||
} // namespace AK
|
||||
)~~~");
|
||||
|
||||
outln("{}", generator.as_string_view());
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/JsonObject.h>
|
||||
#include <AK/SourceGenerator.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <ctype.h>
|
||||
|
||||
static String title_casify(const String& dashy_name)
|
||||
{
|
||||
auto parts = dashy_name.split('-');
|
||||
StringBuilder builder;
|
||||
for (auto& part : parts) {
|
||||
if (part.is_empty())
|
||||
continue;
|
||||
builder.append(toupper(part[0]));
|
||||
if (part.length() == 1)
|
||||
continue;
|
||||
builder.append(part.substring_view(1, part.length() - 1));
|
||||
}
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 2) {
|
||||
warnln("usage: {} <path/to/CSS/Identifiers.json>", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
auto file = Core::File::construct(argv[1]);
|
||||
if (!file->open(Core::OpenMode::ReadOnly))
|
||||
return 1;
|
||||
|
||||
auto json = JsonValue::from_string(file->read_all());
|
||||
VERIFY(json.has_value());
|
||||
VERIFY(json.value().is_array());
|
||||
|
||||
StringBuilder builder;
|
||||
SourceGenerator generator { builder };
|
||||
|
||||
generator.append(R"~~~(
|
||||
#include <AK/Assertions.h>
|
||||
#include <LibWeb/CSS/ValueID.h>
|
||||
|
||||
namespace Web::CSS {
|
||||
|
||||
ValueID value_id_from_string(const StringView& string)
|
||||
{
|
||||
)~~~");
|
||||
|
||||
json.value().as_array().for_each([&](auto& name) {
|
||||
auto member_generator = generator.fork();
|
||||
member_generator.set("name", name.to_string());
|
||||
member_generator.set("name:titlecase", title_casify(name.to_string()));
|
||||
member_generator.append(R"~~~(
|
||||
if (string.equals_ignoring_case("@name@"))
|
||||
return ValueID::@name:titlecase@;
|
||||
)~~~");
|
||||
});
|
||||
|
||||
generator.append(R"~~~(
|
||||
return ValueID::Invalid;
|
||||
}
|
||||
|
||||
const char* string_from_value_id(ValueID value_id) {
|
||||
switch (value_id) {
|
||||
)~~~");
|
||||
|
||||
json.value().as_array().for_each([&](auto& name) {
|
||||
auto member_generator = generator.fork();
|
||||
member_generator.set("name", name.to_string());
|
||||
member_generator.set("name:titlecase", title_casify(name.to_string()));
|
||||
member_generator.append(R"~~~(
|
||||
case ValueID::@name:titlecase@:
|
||||
return "@name@";
|
||||
)~~~");
|
||||
});
|
||||
|
||||
generator.append(R"~~~(
|
||||
default:
|
||||
return "(invalid CSS::ValueID)";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Web::CSS
|
||||
)~~~");
|
||||
|
||||
outln("{}", generator.as_string_view());
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/JsonObject.h>
|
||||
#include <AK/SourceGenerator.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <ctype.h>
|
||||
|
||||
static String title_casify(const String& dashy_name)
|
||||
{
|
||||
auto parts = dashy_name.split('-');
|
||||
StringBuilder builder;
|
||||
for (auto& part : parts) {
|
||||
if (part.is_empty())
|
||||
continue;
|
||||
builder.append(toupper(part[0]));
|
||||
if (part.length() == 1)
|
||||
continue;
|
||||
builder.append(part.substring_view(1, part.length() - 1));
|
||||
}
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 2) {
|
||||
warnln("usage: {} <path/to/CSS/Identifiers.json>", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
auto file = Core::File::construct(argv[1]);
|
||||
if (!file->open(Core::OpenMode::ReadOnly))
|
||||
return 1;
|
||||
|
||||
auto json = JsonValue::from_string(file->read_all());
|
||||
VERIFY(json.has_value());
|
||||
VERIFY(json.value().is_array());
|
||||
|
||||
StringBuilder builder;
|
||||
SourceGenerator generator { builder };
|
||||
generator.append(R"~~~(
|
||||
#pragma once
|
||||
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/Traits.h>
|
||||
|
||||
namespace Web::CSS {
|
||||
|
||||
enum class ValueID {
|
||||
Invalid,
|
||||
)~~~");
|
||||
|
||||
json.value().as_array().for_each([&](auto& name) {
|
||||
auto member_generator = generator.fork();
|
||||
member_generator.set("name:titlecase", title_casify(name.to_string()));
|
||||
|
||||
member_generator.append(R"~~~(
|
||||
@name:titlecase@,
|
||||
)~~~");
|
||||
});
|
||||
|
||||
generator.append(R"~~~(
|
||||
};
|
||||
|
||||
ValueID value_id_from_string(const StringView&);
|
||||
const char* string_from_value_id(ValueID);
|
||||
|
||||
}
|
||||
|
||||
)~~~");
|
||||
|
||||
outln("{}", generator.as_string_view());
|
||||
}
|
1610
Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator.cpp
Normal file
1610
Meta/Lagom/Tools/CodeGenerators/LibWeb/WrapperGenerator.cpp
Normal file
File diff suppressed because it is too large
Load diff
|
@ -0,0 +1,6 @@
|
|||
set(SOURCES
|
||||
main.cpp
|
||||
)
|
||||
|
||||
add_executable(StateMachineGenerator ${SOURCES})
|
||||
target_link_libraries(StateMachineGenerator LagomCore)
|
451
Meta/Lagom/Tools/CodeGenerators/StateMachineGenerator/main.cpp
Normal file
451
Meta/Lagom/Tools/CodeGenerators/StateMachineGenerator/main.cpp
Normal file
|
@ -0,0 +1,451 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Daniel Bertalan <dani@danielbertalan.dev>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/GenericLexer.h>
|
||||
#include <AK/HashTable.h>
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <AK/SourceGenerator.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <AK/Types.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <ctype.h>
|
||||
|
||||
struct Range {
|
||||
int begin;
|
||||
int end;
|
||||
};
|
||||
|
||||
struct StateTransition {
|
||||
Optional<String> new_state;
|
||||
Optional<String> action;
|
||||
};
|
||||
|
||||
struct MatchedAction {
|
||||
Range range;
|
||||
StateTransition action;
|
||||
};
|
||||
|
||||
struct State {
|
||||
String name;
|
||||
Vector<MatchedAction> actions;
|
||||
Optional<String> entry_action;
|
||||
Optional<String> exit_action;
|
||||
};
|
||||
|
||||
struct StateMachine {
|
||||
String name;
|
||||
String initial_state;
|
||||
Vector<State> states;
|
||||
Optional<State> anywhere;
|
||||
Optional<String> namespaces;
|
||||
};
|
||||
|
||||
static OwnPtr<StateMachine>
|
||||
parse_state_machine(StringView input)
|
||||
{
|
||||
auto state_machine = make<StateMachine>();
|
||||
GenericLexer lexer(input);
|
||||
|
||||
auto consume_whitespace = [&] {
|
||||
bool consumed = true;
|
||||
while (consumed) {
|
||||
consumed = lexer.consume_while(isspace).length() > 0;
|
||||
if (lexer.consume_specific("//")) {
|
||||
lexer.consume_line();
|
||||
consumed = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
auto consume_identifier = [&] {
|
||||
consume_whitespace();
|
||||
return lexer.consume_while([](char c) { return isalnum(c) || c == '_'; });
|
||||
};
|
||||
|
||||
auto get_hex_value = [&](char c) {
|
||||
if (isdigit(c))
|
||||
return c - '0';
|
||||
else
|
||||
return c - 'a' + 10;
|
||||
};
|
||||
|
||||
auto consume_number = [&] {
|
||||
int num = 0;
|
||||
consume_whitespace();
|
||||
if (lexer.consume_specific("0x")) {
|
||||
auto hex_digits = lexer.consume_while([](char c) {
|
||||
if (isdigit(c)) return true;
|
||||
else {
|
||||
c = tolower(c);
|
||||
return (c >= 'a' && c <= 'f');
|
||||
} });
|
||||
for (auto c : hex_digits)
|
||||
num = 16 * num + get_hex_value(c);
|
||||
} else {
|
||||
lexer.consume_specific('\'');
|
||||
if (lexer.next_is('\\'))
|
||||
num = (int)lexer.consume_escaped_character('\\');
|
||||
else
|
||||
num = lexer.consume_until('\'').to_int().value();
|
||||
lexer.consume_specific('\'');
|
||||
}
|
||||
return num;
|
||||
};
|
||||
|
||||
auto consume_condition = [&] {
|
||||
Range condition;
|
||||
consume_whitespace();
|
||||
if (lexer.consume_specific('[')) {
|
||||
consume_whitespace();
|
||||
condition.begin = consume_number();
|
||||
consume_whitespace();
|
||||
lexer.consume_specific("..");
|
||||
consume_whitespace();
|
||||
condition.end = consume_number();
|
||||
consume_whitespace();
|
||||
lexer.consume_specific(']');
|
||||
} else {
|
||||
auto num = consume_number();
|
||||
condition.begin = num;
|
||||
condition.end = num;
|
||||
}
|
||||
return condition;
|
||||
};
|
||||
|
||||
auto consume_action = [&]() {
|
||||
StateTransition action;
|
||||
consume_whitespace();
|
||||
lexer.consume_specific("=>");
|
||||
consume_whitespace();
|
||||
lexer.consume_specific('(');
|
||||
consume_whitespace();
|
||||
if (!lexer.consume_specific("_"))
|
||||
action.new_state = consume_identifier();
|
||||
consume_whitespace();
|
||||
lexer.consume_specific(',');
|
||||
consume_whitespace();
|
||||
if (!lexer.consume_specific("_"))
|
||||
action.action = consume_identifier();
|
||||
consume_whitespace();
|
||||
lexer.consume_specific(')');
|
||||
return action;
|
||||
};
|
||||
|
||||
auto consume_state_description
|
||||
= [&] {
|
||||
State state;
|
||||
consume_whitespace();
|
||||
state.name = consume_identifier();
|
||||
consume_whitespace();
|
||||
consume_whitespace();
|
||||
lexer.consume_specific('{');
|
||||
for (;;) {
|
||||
consume_whitespace();
|
||||
if (lexer.consume_specific('}')) {
|
||||
break;
|
||||
}
|
||||
if (lexer.consume_specific("@entry")) {
|
||||
consume_whitespace();
|
||||
state.entry_action = consume_identifier();
|
||||
} else if (lexer.consume_specific("@exit")) {
|
||||
consume_whitespace();
|
||||
state.exit_action = consume_identifier();
|
||||
} else if (lexer.next_is('@')) {
|
||||
auto directive = consume_identifier().to_string();
|
||||
fprintf(stderr, "Unimplemented @ directive %s\n", directive.characters());
|
||||
exit(1);
|
||||
} else {
|
||||
MatchedAction matched_action;
|
||||
matched_action.range = consume_condition();
|
||||
matched_action.action = consume_action();
|
||||
state.actions.append(matched_action);
|
||||
}
|
||||
}
|
||||
return state;
|
||||
};
|
||||
|
||||
while (!lexer.is_eof()) {
|
||||
consume_whitespace();
|
||||
if (lexer.is_eof())
|
||||
break;
|
||||
if (lexer.consume_specific("@namespace")) {
|
||||
consume_whitespace();
|
||||
state_machine->namespaces = lexer.consume_while([](char c) { return isalpha(c) || c == ':'; });
|
||||
} else if (lexer.consume_specific("@begin")) {
|
||||
consume_whitespace();
|
||||
state_machine->initial_state = consume_identifier();
|
||||
} else if (lexer.consume_specific("@name")) {
|
||||
consume_whitespace();
|
||||
state_machine->name = consume_identifier();
|
||||
} else if (lexer.next_is("@anywhere")) {
|
||||
lexer.consume_specific('@');
|
||||
state_machine->anywhere = consume_state_description();
|
||||
} else if (lexer.consume_specific('@')) {
|
||||
auto directive = consume_identifier().to_string();
|
||||
fprintf(stderr, "Unimplemented @ directive %s\n", directive.characters());
|
||||
exit(1);
|
||||
} else {
|
||||
auto description = consume_state_description();
|
||||
state_machine->states.append(description);
|
||||
}
|
||||
}
|
||||
|
||||
if (state_machine->initial_state.is_empty()) {
|
||||
fprintf(stderr, "Missing @begin directive\n");
|
||||
exit(1);
|
||||
} else if (state_machine->name.is_empty()) {
|
||||
fprintf(stderr, "Missing @name directive\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (state_machine->anywhere.has_value()) {
|
||||
state_machine->anywhere.value().name = "_Anywhere";
|
||||
}
|
||||
return state_machine;
|
||||
}
|
||||
|
||||
void output_header(const StateMachine&, SourceGenerator&);
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
Core::ArgsParser args_parser;
|
||||
const char* path = nullptr;
|
||||
args_parser.add_positional_argument(path, "Path to parser description", "input", Core::ArgsParser::Required::Yes);
|
||||
args_parser.parse(argc, argv);
|
||||
|
||||
auto file_or_error = Core::File::open(path, Core::OpenMode::ReadOnly);
|
||||
if (file_or_error.is_error()) {
|
||||
fprintf(stderr, "Cannot open %s\n", path);
|
||||
}
|
||||
|
||||
auto content = file_or_error.value()->read_all();
|
||||
auto state_machine = parse_state_machine(content);
|
||||
|
||||
StringBuilder builder;
|
||||
SourceGenerator generator { builder };
|
||||
output_header(*state_machine, generator);
|
||||
outln("{}", generator.as_string_view());
|
||||
return 0;
|
||||
}
|
||||
|
||||
HashTable<String> actions(const StateMachine& machine)
|
||||
{
|
||||
HashTable<String> table;
|
||||
|
||||
auto do_state = [&](const State& state) {
|
||||
if (state.entry_action.has_value())
|
||||
table.set(state.entry_action.value());
|
||||
if (state.exit_action.has_value())
|
||||
table.set(state.exit_action.value());
|
||||
for (auto action : state.actions) {
|
||||
if (action.action.action.has_value())
|
||||
table.set(action.action.action.value());
|
||||
}
|
||||
};
|
||||
for (auto state : machine.states) {
|
||||
do_state(state);
|
||||
}
|
||||
if (machine.anywhere.has_value())
|
||||
do_state(machine.anywhere.value());
|
||||
return table;
|
||||
}
|
||||
|
||||
void generate_lookup_table(const StateMachine& machine, SourceGenerator& generator)
|
||||
{
|
||||
generator.append(R"~~~(
|
||||
static constexpr StateTransition STATE_TRANSITION_TABLE[][256] = {
|
||||
)~~~");
|
||||
|
||||
auto generate_for_state = [&](const State& s) {
|
||||
auto table_generator = generator.fork();
|
||||
table_generator.set("active_state", s.name);
|
||||
table_generator.append("/* @active_state@ */ { ");
|
||||
VERIFY(!s.name.is_empty());
|
||||
Vector<StateTransition> row;
|
||||
for (int i = 0; i < 256; i++)
|
||||
row.append({ s.name, "_Ignore" });
|
||||
for (auto action : s.actions) {
|
||||
for (int range_element = action.range.begin; range_element <= action.range.end; range_element++) {
|
||||
row[range_element] = { action.action.new_state, action.action.action };
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 256; ++i) {
|
||||
auto cell_generator = table_generator.fork();
|
||||
cell_generator.set("cell_new_state", row[i].new_state.value_or(s.name));
|
||||
cell_generator.set("cell_action", row[i].action.value_or("_Ignore"));
|
||||
cell_generator.append(" {State::@cell_new_state@, Action::@cell_action@}, ");
|
||||
}
|
||||
table_generator.append("},\n");
|
||||
};
|
||||
if (machine.anywhere.has_value()) {
|
||||
generate_for_state(machine.anywhere.value());
|
||||
}
|
||||
for (auto s : machine.states) {
|
||||
generate_for_state(s);
|
||||
}
|
||||
generator.append(R"~~~(
|
||||
};
|
||||
)~~~");
|
||||
}
|
||||
|
||||
void output_header(const StateMachine& machine, SourceGenerator& generator)
|
||||
{
|
||||
generator.set("class_name", machine.name);
|
||||
generator.set("initial_state", machine.initial_state);
|
||||
generator.set("state_count", String::number(machine.states.size() + 1));
|
||||
|
||||
generator.append(R"~~~(
|
||||
#pragma once
|
||||
|
||||
#include <AK/Function.h>
|
||||
#include <AK/Platform.h>
|
||||
#include <AK/Types.h>
|
||||
)~~~");
|
||||
if (machine.namespaces.has_value()) {
|
||||
generator.set("namespace", machine.namespaces.value());
|
||||
generator.append(R"~~~(
|
||||
namespace @namespace@ {
|
||||
)~~~");
|
||||
}
|
||||
generator.append(R"~~~(
|
||||
class @class_name@ {
|
||||
public:
|
||||
enum class Action : u8 {
|
||||
_Ignore,
|
||||
)~~~");
|
||||
for (auto a : actions(machine)) {
|
||||
if (a.is_empty())
|
||||
continue;
|
||||
auto action_generator = generator.fork();
|
||||
action_generator.set("action.name", a);
|
||||
action_generator.append(R"~~~(
|
||||
@action.name@,
|
||||
)~~~");
|
||||
}
|
||||
|
||||
generator.append(R"~~~(
|
||||
}; // end Action
|
||||
|
||||
using Handler = Function<void(Action, u8)>;
|
||||
|
||||
@class_name@(Handler handler)
|
||||
: m_handler(move(handler))
|
||||
{
|
||||
}
|
||||
|
||||
void advance(u8 byte)
|
||||
{
|
||||
auto next_state = lookup_state_transition(byte);
|
||||
bool state_will_change = next_state.new_state != m_state && next_state.new_state != State::_Anywhere;
|
||||
|
||||
// only run exit directive if state is being changed
|
||||
if (state_will_change) {
|
||||
switch (m_state) {
|
||||
)~~~");
|
||||
for (auto s : machine.states) {
|
||||
auto state_generator = generator.fork();
|
||||
if (s.exit_action.has_value()) {
|
||||
state_generator.set("state_name", s.name);
|
||||
state_generator.set("action", s.exit_action.value());
|
||||
state_generator.append(R"~~~(
|
||||
case State::@state_name@:
|
||||
m_handler(Action::@action@, byte);
|
||||
break;
|
||||
)~~~");
|
||||
}
|
||||
}
|
||||
generator.append(R"~~~(
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (next_state.action != Action::_Ignore)
|
||||
m_handler(next_state.action, byte);
|
||||
m_state = next_state.new_state;
|
||||
|
||||
// only run entry directive if state is being changed
|
||||
if (state_will_change)
|
||||
{
|
||||
switch (next_state.new_state)
|
||||
{
|
||||
)~~~");
|
||||
for (auto state : machine.states) {
|
||||
auto state_generator = generator.fork();
|
||||
if (state.entry_action.has_value()) {
|
||||
state_generator.set("state_name", state.name);
|
||||
state_generator.set("action", state.entry_action.value());
|
||||
state_generator.append(R"~~~(
|
||||
case State::@state_name@:
|
||||
m_handler(Action::@action@, byte);
|
||||
break;
|
||||
)~~~");
|
||||
}
|
||||
}
|
||||
generator.append(R"~~~(
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
enum class State : u8 {
|
||||
_Anywhere,
|
||||
)~~~");
|
||||
|
||||
int largest_state_value = 0;
|
||||
for (auto s : machine.states) {
|
||||
auto state_generator = generator.fork();
|
||||
state_generator.set("state.name", s.name);
|
||||
largest_state_value++;
|
||||
state_generator.append(R"~~~(
|
||||
@state.name@,
|
||||
)~~~");
|
||||
}
|
||||
generator.append(R"~~~(
|
||||
}; // end State
|
||||
|
||||
struct StateTransition {
|
||||
State new_state;
|
||||
Action action;
|
||||
};
|
||||
|
||||
State m_state { State::@initial_state@ };
|
||||
|
||||
Handler m_handler;
|
||||
|
||||
ALWAYS_INLINE StateTransition lookup_state_transition(u8 byte)
|
||||
{
|
||||
VERIFY((u8)m_state < @state_count@);
|
||||
)~~~");
|
||||
if (machine.anywhere.has_value()) {
|
||||
generator.append(R"~~~(
|
||||
auto anywhere_state = STATE_TRANSITION_TABLE[0][byte];
|
||||
if (anywhere_state.new_state != State::_Anywhere || anywhere_state.action != Action::_Ignore)
|
||||
return anywhere_state;
|
||||
else
|
||||
)~~~");
|
||||
}
|
||||
generator.append(R"~~~(
|
||||
return STATE_TRANSITION_TABLE[(u8)m_state][byte];
|
||||
}
|
||||
)~~~");
|
||||
|
||||
auto table_generator = generator.fork();
|
||||
generate_lookup_table(machine, table_generator);
|
||||
generator.append(R"~~~(
|
||||
}; // end @class_name@
|
||||
)~~~");
|
||||
|
||||
if (machine.namespaces.has_value()) {
|
||||
generator.append(R"~~~(
|
||||
} // end namespace
|
||||
)~~~");
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue