summaryrefslogtreecommitdiff
path: root/Meta/Lagom
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2022-04-13 17:46:47 +0100
committerAndreas Kling <kling@serenityos.org>2022-04-14 14:54:06 +0200
commit3f61f869c898a30d96e8bdd60d428c699895560a (patch)
tree1c8ed9252f5f143f100330747cebf6e09a0fe0d7 /Meta/Lagom
parent823d67bfc185ed5fbda62c773d51d3150559c621 (diff)
downloadserenity-3f61f869c898a30d96e8bdd60d428c699895560a.zip
LibWeb: Generate ValueID-to-enum conversion functions
Diffstat (limited to 'Meta/Lagom')
-rw-r--r--Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSEnums.cpp47
-rw-r--r--Meta/Lagom/Tools/CodeGenerators/LibWeb/GeneratorUtil.h5
2 files changed, 49 insertions, 3 deletions
diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSEnums.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSEnums.cpp
index 5672208d19..75c37ddf6b 100644
--- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSEnums.cpp
+++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSEnums.cpp
@@ -46,6 +46,8 @@ ErrorOr<void> generate_header_file(JsonObject& enums_data, Core::Stream::File& f
generator.append(R"~~~(
#pragma once
+#include <AK/Optional.h>
+
namespace Web::CSS {
enum class ValueID;
@@ -58,6 +60,8 @@ enum class ValueID;
auto enum_generator = generator.fork();
enum_generator.set("name:titlecase", title_casify(name));
+ enum_generator.set("name:snakecase", snake_casify(name));
+
enum_generator.appendln("enum class @name:titlecase@ {");
for (auto& member : members.values()) {
@@ -70,7 +74,9 @@ enum class ValueID;
member_generator.appendln(" @member:titlecase@,");
}
- enum_generator.appendln("};\n");
+ enum_generator.appendln("};");
+ enum_generator.appendln("Optional<@name:titlecase@> value_id_to_@name:snakecase@(ValueID);");
+ enum_generator.append("\n");
});
generator.appendln("}");
@@ -86,12 +92,47 @@ ErrorOr<void> generate_implementation_file(JsonObject& enums_data, Core::Stream:
generator.append(R"~~~(
#include <LibWeb/CSS/Enums.h>
+#include <LibWeb/CSS/ValueID.h>
namespace Web::CSS {
)~~~");
- // TODO: Generate some things!
- (void)enums_data;
+ enums_data.for_each_member([&](auto& name, auto& value) {
+ VERIFY(value.is_array());
+ auto& members = value.as_array();
+
+ auto enum_generator = generator.fork();
+ enum_generator.set("name:titlecase", title_casify(name));
+ enum_generator.set("name:snakecase", snake_casify(name));
+
+ enum_generator.append(R"~~~(
+Optional<@name:titlecase@> value_id_to_@name:snakecase@(ValueID value_id)
+{
+ switch (value_id) {)~~~");
+
+ for (auto& member : members.values()) {
+ auto member_generator = enum_generator.fork();
+ auto member_name = member.to_string();
+ if (member_name.contains('=')) {
+ auto parts = member_name.split_view('=');
+ member_generator.set("valueid:titlecase", title_casify(parts[0]));
+ member_generator.set("member:titlecase", title_casify(parts[1]));
+ } else {
+ member_generator.set("valueid:titlecase", title_casify(member_name));
+ member_generator.set("member:titlecase", title_casify(member_name));
+ }
+ member_generator.append(R"~~~(
+ case ValueID::@valueid:titlecase@:
+ return @name:titlecase@::@member:titlecase@;)~~~");
+ }
+
+ enum_generator.append(R"~~~(
+ default:
+ return {};
+ }
+}
+)~~~");
+ });
generator.appendln("}");
diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GeneratorUtil.h b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GeneratorUtil.h
index 7d68df56ce..150f868796 100644
--- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GeneratorUtil.h
+++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GeneratorUtil.h
@@ -49,6 +49,11 @@ String camel_casify(StringView dashy_name)
return builder.to_string();
}
+String snake_casify(String const& dashy_name)
+{
+ return dashy_name.replace("-", "_", true);
+}
+
ErrorOr<JsonValue> read_entire_file_as_json(StringView filename)
{
auto file = TRY(Core::Stream::File::open(filename, Core::Stream::OpenMode::Read));