diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2022-04-17 15:33:50 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-04-18 14:16:28 +0200 |
commit | 5f3498d50f5b0d29b1c8b4f2043202c45ef7e081 (patch) | |
tree | 209db5b3409ae71e011d69c51b97aa76c7137cb9 /Meta/Lagom/Tools | |
parent | 807101e394d08833c2e63ad41f1dfe7217af3079 (diff) | |
download | serenity-5f3498d50f5b0d29b1c8b4f2043202c45ef7e081.zip |
LibWeb: Add code generator for CSS transform functions
This first step just generates the TransformFunction enum, but more will
follow.
Diffstat (limited to 'Meta/Lagom/Tools')
-rw-r--r-- | Meta/Lagom/Tools/CodeGenerators/LibWeb/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSTransformFunctions.cpp | 92 |
2 files changed, 93 insertions, 0 deletions
diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/CMakeLists.txt b/Meta/Lagom/Tools/CodeGenerators/LibWeb/CMakeLists.txt index 7b3e7904e7..df45d28635 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/CMakeLists.txt +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/CMakeLists.txt @@ -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) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSTransformFunctions.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSTransformFunctions.cpp new file mode 100644 index 0000000000..31f6c16e80 --- /dev/null +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSTransformFunctions.cpp @@ -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 {}; +} |