LibWeb: Add code generator for CSS transform functions

This first step just generates the TransformFunction enum, but more will
follow.
This commit is contained in:
Sam Atkins 2022-04-17 15:33:50 +01:00 committed by Andreas Kling
parent 807101e394
commit 5f3498d50f
Notes: sideshowbarker 2024-07-17 11:43:02 +09:00
4 changed files with 139 additions and 0 deletions

View file

@ -3,6 +3,7 @@ set(SOURCES "") # avoid pulling SOURCES from parent scope
lagom_tool(GenerateCSSEnums SOURCES GenerateCSSEnums.cpp LIBS LagomMain)
lagom_tool(GenerateCSSMediaFeatureID SOURCES GenerateCSSMediaFeatureID.cpp LIBS LagomMain)
lagom_tool(GenerateCSSPropertyID SOURCES GenerateCSSPropertyID.cpp LIBS LagomMain)
lagom_tool(GenerateCSSTransformFunctions SOURCES GenerateCSSTransformFunctions.cpp LIBS LagomMain)
lagom_tool(GenerateCSSValueID SOURCES GenerateCSSValueID.cpp LIBS LagomMain)
add_subdirectory(WrapperGenerator)

View file

@ -0,0 +1,92 @@
/*
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "GeneratorUtil.h"
#include <AK/SourceGenerator.h>
#include <AK/StringBuilder.h>
#include <LibCore/ArgsParser.h>
#include <LibMain/Main.h>
ErrorOr<void> generate_header_file(JsonObject& transforms_data, Core::Stream::File& file);
ErrorOr<void> generate_implementation_file(JsonObject& transforms_data, Core::Stream::File& file);
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
StringView generated_header_path;
StringView generated_implementation_path;
StringView identifiers_json_path;
Core::ArgsParser args_parser;
args_parser.add_option(generated_header_path, "Path to the TransformFunctions header file to generate", "generated-header-path", 'h', "generated-header-path");
args_parser.add_option(generated_implementation_path, "Path to the TransformFunctions implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path");
args_parser.add_option(identifiers_json_path, "Path to the JSON file to read from", "json-path", 'j', "json-path");
args_parser.parse(arguments);
auto json = TRY(read_entire_file_as_json(identifiers_json_path));
VERIFY(json.is_object());
auto transforms_data = json.as_object();
auto generated_header_file = TRY(Core::Stream::File::open(generated_header_path, Core::Stream::OpenMode::Write));
auto generated_implementation_file = TRY(Core::Stream::File::open(generated_implementation_path, Core::Stream::OpenMode::Write));
TRY(generate_header_file(transforms_data, *generated_header_file));
TRY(generate_implementation_file(transforms_data, *generated_implementation_file));
return 0;
}
static String title_casify_transform_function(StringView input)
{
// Transform function names look like `fooBar`, so we just have to make the first character uppercase.
StringBuilder builder;
builder.append(toupper(input[0]));
builder.append(input.substring_view(1));
return builder.to_string();
}
ErrorOr<void> generate_header_file(JsonObject& transforms_data, Core::Stream::File& file)
{
StringBuilder builder;
SourceGenerator generator { builder };
generator.append(R"~~~(
namespace Web::CSS {
)~~~");
generator.appendln("enum class TransformFunction {");
transforms_data.for_each_member([&](auto& name, auto&) {
auto member_generator = generator.fork();
member_generator.set("name:titlecase", title_casify_transform_function(name));
member_generator.appendln(" @name:titlecase@,");
});
generator.appendln("};");
generator.appendln("\n}");
TRY(file.write(generator.as_string_view().bytes()));
return {};
}
ErrorOr<void> generate_implementation_file(JsonObject& transforms_data, Core::Stream::File& file)
{
StringBuilder builder;
SourceGenerator generator { builder };
generator.append(R"~~~(
namespace Web::CSS {
)~~~");
transforms_data.for_each_member([&](auto& name, auto& value) {
(void)name;
(void)value;
});
generator.appendln("}");
TRY(file.write(generator.as_string_view().bytes()));
return {};
}

View file

@ -74,6 +74,7 @@ set(SOURCES
CSS/Supports.cpp
CSS/SyntaxHighlighter/SyntaxHighlighter.cpp
CSS/Time.cpp
CSS/TransformFunctions.cpp
CSS/ValueID.cpp
CSS/ValueID.h
Cookie/ParsedCookie.cpp
@ -647,6 +648,16 @@ invoke_generator(
arguments -j "${CMAKE_CURRENT_SOURCE_DIR}/CSS/Properties.json"
)
invoke_generator(
"TransformFunctions"
Lagom::GenerateCSSTransformFunctions
"${CMAKE_CURRENT_SOURCE_DIR}/CSS/TransformFunctions.json"
""
"CSS/TransformFunctions.h"
"CSS/TransformFunctions.cpp"
arguments -j "${CMAKE_CURRENT_SOURCE_DIR}/CSS/TransformFunctions.json"
)
invoke_generator(
"ValueID"
Lagom::GenerateCSSValueID

View file

@ -0,0 +1,35 @@
{
"matrix": {
"parameters": "<number>{6}"
},
"translate": {
"parameters": "<length-percentage>{1,2}"
},
"translateX": {
"parameters": "<length-percentage>"
},
"translateY": {
"parameters": "<length-percentage>"
},
"scale": {
"parameters": "<number>{1,2}"
},
"scaleX": {
"parameters": "<number>"
},
"scaleY": {
"parameters": "<number>"
},
"rotate": {
"parameters": "<angle>"
},
"skew": {
"parameters": "<angle>{1,2}"
},
"skewX": {
"parameters": "<angle>"
},
"skewY": {
"parameters": "<angle>"
}
}