summaryrefslogtreecommitdiff
path: root/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSEnums.cpp
blob: 75c37ddf6ba8350799753140bf9776ac8b29fba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * 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

#include <AK/Optional.h>

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.set("name:snakecase", snake_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("};");
        enum_generator.appendln("Optional<@name:titlecase@> value_id_to_@name:snakecase@(ValueID);");
        enum_generator.append("\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>
#include <LibWeb/CSS/ValueID.h>

namespace Web::CSS {
)~~~");

    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("}");

    TRY(file.write(generator.as_string_view().bytes()));
    return {};
}