LibWeb: Add a new code generator for CSS enums

Alias values are represented by "alias-name=real-name".

We have a lot of repetitive code for converting between ValueID and
property-specific enums. Let's see if we can generate it. :^)

This first step just produces the enums, from a JSON file. The values in
there are a duplication of what's in Properties.json, but eventually
those will go away.
This commit is contained in:
Sam Atkins 2022-04-13 16:48:03 +01:00 committed by Andreas Kling
parent 66170ff632
commit a97944e483
Notes: sideshowbarker 2024-07-17 11:49:17 +09:00
4 changed files with 328 additions and 0 deletions

View file

@ -1,5 +1,6 @@
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(GenerateCSSValueID SOURCES GenerateCSSValueID.cpp LIBS LagomMain)

View file

@ -0,0 +1,100 @@
/*
* 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& enums_data, Core::Stream::File& file);
ErrorOr<void> generate_implementation_file(JsonObject& enums_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 Enums header file to generate", "generated-header-path", 'h', "generated-header-path");
args_parser.add_option(generated_implementation_path, "Path to the Enums 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 enums_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(enums_data, *generated_header_file));
TRY(generate_implementation_file(enums_data, *generated_implementation_file));
return 0;
}
ErrorOr<void> generate_header_file(JsonObject& enums_data, Core::Stream::File& file)
{
StringBuilder builder;
SourceGenerator generator { builder };
generator.append(R"~~~(
#pragma once
namespace Web::CSS {
enum class ValueID;
)~~~");
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.appendln("enum class @name:titlecase@ {");
for (auto& member : members.values()) {
auto member_name = member.to_string();
// Don't include aliases in the enum.
if (member_name.contains('='))
continue;
auto member_generator = enum_generator.fork();
member_generator.set("member:titlecase", title_casify(member_name));
member_generator.appendln(" @member:titlecase@,");
}
enum_generator.appendln("};\n");
});
generator.appendln("}");
TRY(file.write(generator.as_string_view().bytes()));
return {};
}
ErrorOr<void> generate_implementation_file(JsonObject& enums_data, Core::Stream::File& file)
{
StringBuilder builder;
SourceGenerator generator { builder };
generator.append(R"~~~(
#include <LibWeb/CSS/Enums.h>
namespace Web::CSS {
)~~~");
// TODO: Generate some things!
(void)enums_data;
generator.appendln("}");
TRY(file.write(generator.as_string_view().bytes()));
return {};
}

View file

@ -36,6 +36,7 @@ set(SOURCES
CSS/CSSSupportsRule.cpp
CSS/DefaultStyleSheetSource.cpp
CSS/Display.cpp
CSS/Enums.cpp
CSS/FontFace.cpp
CSS/Frequency.cpp
CSS/Length.cpp
@ -616,6 +617,16 @@ libweb_js_wrapper(XHR/ProgressEvent)
libweb_js_wrapper(XHR/XMLHttpRequest)
libweb_js_wrapper(XHR/XMLHttpRequestEventTarget)
invoke_generator(
"Enums"
Lagom::GenerateCSSEnums
"${CMAKE_CURRENT_SOURCE_DIR}/CSS/Enums.json"
""
"CSS/Enums.h"
"CSS/Enums.cpp"
arguments -j "${CMAKE_CURRENT_SOURCE_DIR}/CSS/Enums.json"
)
invoke_generator(
"MediaFeatureID"
Lagom::GenerateCSSMediaFeatureID

View file

@ -0,0 +1,216 @@
{
"align-items": [
"flex-start",
"flex-end",
"center",
"baseline",
"stretch"
],
"background-attachment": [
"fixed",
"local",
"scroll"
],
"background-box": [
"border-box",
"content-box",
"padding-box"
],
"box-sizing": [
"border-box",
"content-box"
],
"clear": [
"none",
"left",
"right",
"both"
],
"cursor": [
"auto",
"default",
"none",
"context-menu",
"help",
"pointer",
"progress",
"wait",
"cell",
"crosshair",
"text",
"vertical-text",
"alias",
"copy",
"move",
"no-drop",
"not-allowed",
"grab",
"grabbing",
"e-resize",
"n-resize",
"ne-resize",
"nw-resize",
"s-resize",
"se-resize",
"sw-resize",
"w-resize",
"ew-resize",
"ns-resize",
"nesw-resize",
"nwse-resize",
"col-resize",
"row-resize",
"all-scroll",
"zoom-in",
"zoom-out"
],
"flex-direction": [
"row",
"row-reverse",
"column",
"column-reverse"
],
"flex-wrap": [
"nowrap",
"wrap",
"wrap-reverse"
],
"float": [
"none",
"left",
"right"
],
"font-variant": [
"normal",
"small-caps"
],
"image-rendering": [
"auto",
"crisp-edges",
"high-quality",
"pixelated",
"smooth"
],
"justify-content": [
"flex-start",
"flex-end",
"center",
"space-between",
"space-around"
],
"line-style": [
"none",
"hidden",
"dotted",
"dashed",
"solid",
"double",
"groove",
"ridge",
"inset",
"outset"
],
"list-style-type": [
"circle",
"decimal",
"decimal-leading-zero",
"disc",
"lower-alpha",
"lower-latin",
"lower-roman",
"none",
"square",
"upper-alpha",
"upper-latin",
"upper-roman"
],
"overflow": [
"auto",
"clip",
"hidden",
"scroll",
"visible"
],
"pointer-events": [
"auto",
"all",
"none"
],
"position": [
"absolute",
"fixed",
"relative",
"static",
"sticky"
],
"position-edge": [
"left",
"right",
"top",
"bottom"
],
"repeat": [
"no-repeat",
"repeat",
"round",
"space"
],
"text-align": [
"center",
"justify",
"left",
"right",
"-libweb-center"
],
"text-decoration-line": [
"blink",
"line-through",
"none",
"overline",
"underline"
],
"text-decoration-style": [
"dashed",
"dotted",
"double",
"solid",
"wavy"
],
"text-justify": [
"auto",
"none",
"inter-word",
"inter-character",
"distribute=inter-character"
],
"text-transform": [
"capitalize",
"full-size-kana",
"full-width",
"lowercase",
"none",
"uppercase"
],
"vertical-align": [
"baseline",
"bottom",
"middle",
"sub",
"super",
"text-bottom",
"text-top",
"top"
],
"visibility": [
"collapse",
"hidden",
"visible"
],
"white-space": [
"normal",
"nowrap",
"pre",
"pre-line",
"pre-wrap"
]
}