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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
/*
* 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& media_feature_data, Core::File& file);
ErrorOr<void> generate_implementation_file(JsonObject& media_feature_data, Core::File& file);
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
StringView generated_header_path;
StringView generated_implementation_path;
StringView media_features_json_path;
Core::ArgsParser args_parser;
args_parser.add_option(generated_header_path, "Path to the MediaFeatureID header file to generate", "generated-header-path", 'h', "generated-header-path");
args_parser.add_option(generated_implementation_path, "Path to the MediaFeatureID implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path");
args_parser.add_option(media_features_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(media_features_json_path));
VERIFY(json.is_object());
auto media_feature_data = json.as_object();
auto generated_header_file = TRY(Core::File::open(generated_header_path, Core::File::OpenMode::Write));
auto generated_implementation_file = TRY(Core::File::open(generated_implementation_path, Core::File::OpenMode::Write));
TRY(generate_header_file(media_feature_data, *generated_header_file));
TRY(generate_implementation_file(media_feature_data, *generated_implementation_file));
return 0;
}
ErrorOr<void> generate_header_file(JsonObject& media_feature_data, Core::File& file)
{
StringBuilder builder;
SourceGenerator generator { builder };
generator.append(R"~~~(#pragma once
#include <AK/StringView.h>
#include <AK/Traits.h>
#include <LibWeb/CSS/ValueID.h>
namespace Web::CSS {
enum class MediaFeatureValueType {
Boolean,
Integer,
Length,
Ratio,
Resolution,
};
enum class MediaFeatureID {)~~~");
media_feature_data.for_each_member([&](auto& name, auto&) {
auto member_generator = generator.fork();
member_generator.set("name:titlecase", title_casify(name));
member_generator.append(R"~~~(
@name:titlecase@,)~~~");
});
generator.append(R"~~~(
};
Optional<MediaFeatureID> media_feature_id_from_string(StringView);
StringView string_from_media_feature_id(MediaFeatureID);
bool media_feature_type_is_range(MediaFeatureID);
bool media_feature_accepts_type(MediaFeatureID, MediaFeatureValueType);
bool media_feature_accepts_identifier(MediaFeatureID, ValueID);
}
)~~~");
// FIXME: This should write the entire span.
TRY(file.write_some(generator.as_string_view().bytes()));
return {};
}
ErrorOr<void> generate_implementation_file(JsonObject& media_feature_data, Core::File& file)
{
StringBuilder builder;
SourceGenerator generator { builder };
generator.append(R"~~~(
#include <LibWeb/CSS/MediaFeatureID.h>
#include <LibWeb/Infra/Strings.h>
namespace Web::CSS {
Optional<MediaFeatureID> media_feature_id_from_string(StringView string)
{)~~~");
media_feature_data.for_each_member([&](auto& name, auto&) {
auto member_generator = generator.fork();
member_generator.set("name", name);
member_generator.set("name:titlecase", title_casify(name));
member_generator.append(R"~~~(
if (Infra::is_ascii_case_insensitive_match(string, "@name@"sv))
return MediaFeatureID::@name:titlecase@;
)~~~");
});
generator.append(R"~~~(
return {};
}
StringView string_from_media_feature_id(MediaFeatureID media_feature_id)
{
switch (media_feature_id) {)~~~");
media_feature_data.for_each_member([&](auto& name, auto&) {
auto member_generator = generator.fork();
member_generator.set("name", name);
member_generator.set("name:titlecase", title_casify(name));
member_generator.append(R"~~~(
case MediaFeatureID::@name:titlecase@:
return "@name@"sv;)~~~");
});
generator.append(R"~~~(
}
VERIFY_NOT_REACHED();
}
bool media_feature_type_is_range(MediaFeatureID media_feature_id)
{
switch (media_feature_id) {)~~~");
media_feature_data.for_each_member([&](auto& name, auto& value) {
VERIFY(value.is_object());
auto& feature = value.as_object();
auto member_generator = generator.fork();
member_generator.set("name:titlecase", title_casify(name));
VERIFY(feature.has("type"sv));
auto feature_type = feature.get_deprecated_string("type"sv);
VERIFY(feature_type.has_value());
member_generator.set("is_range", feature_type.value() == "range" ? "true" : "false");
member_generator.append(R"~~~(
case MediaFeatureID::@name:titlecase@:
return @is_range@;)~~~");
});
generator.append(R"~~~(
}
VERIFY_NOT_REACHED();
}
bool media_feature_accepts_type(MediaFeatureID media_feature_id, MediaFeatureValueType value_type)
{
switch (media_feature_id) {)~~~");
media_feature_data.for_each_member([&](auto& name, auto& member) {
VERIFY(member.is_object());
auto& feature = member.as_object();
auto member_generator = generator.fork();
member_generator.set("name:titlecase", title_casify(name));
member_generator.append(R"~~~(
case MediaFeatureID::@name:titlecase@:)~~~");
bool have_output_value_type_switch = false;
if (feature.has("values"sv)) {
auto append_value_type_switch_if_needed = [&]() {
if (!have_output_value_type_switch) {
member_generator.append(R"~~~(
switch (value_type) {)~~~");
}
have_output_value_type_switch = true;
};
auto values = feature.get_array("values"sv);
VERIFY(values.has_value());
auto& values_array = values.value();
for (auto& type : values_array.values()) {
VERIFY(type.is_string());
auto type_name = type.as_string();
// Skip identifiers.
if (type_name[0] != '<')
continue;
if (type_name == "<mq-boolean>") {
append_value_type_switch_if_needed();
member_generator.append(R"~~~(
case MediaFeatureValueType::Boolean:
return true;)~~~");
} else if (type_name == "<integer>") {
append_value_type_switch_if_needed();
member_generator.append(R"~~~(
case MediaFeatureValueType::Integer:
return true;)~~~");
} else if (type_name == "<length>") {
append_value_type_switch_if_needed();
member_generator.append(R"~~~(
case MediaFeatureValueType::Length:
return true;)~~~");
} else if (type_name == "<ratio>") {
append_value_type_switch_if_needed();
member_generator.append(R"~~~(
case MediaFeatureValueType::Ratio:
return true;)~~~");
} else if (type_name == "<resolution>") {
append_value_type_switch_if_needed();
member_generator.append(R"~~~(
case MediaFeatureValueType::Resolution:
return true;)~~~");
} else {
warnln("Unrecognized media-feature value type: `{}`", type_name);
VERIFY_NOT_REACHED();
}
}
}
if (have_output_value_type_switch) {
member_generator.append(R"~~~(
default:
return false;
})~~~");
} else {
member_generator.append(R"~~~(
return false;)~~~");
}
});
generator.append(R"~~~(
}
VERIFY_NOT_REACHED();
}
bool media_feature_accepts_identifier(MediaFeatureID media_feature_id, ValueID identifier)
{
switch (media_feature_id) {)~~~");
media_feature_data.for_each_member([&](auto& name, auto& member) {
VERIFY(member.is_object());
auto& feature = member.as_object();
auto member_generator = generator.fork();
member_generator.set("name:titlecase", title_casify(name));
member_generator.append(R"~~~(
case MediaFeatureID::@name:titlecase@:)~~~");
bool have_output_identifier_switch = false;
if (feature.has("values"sv)) {
auto append_identifier_switch_if_needed = [&]() {
if (!have_output_identifier_switch) {
member_generator.append(R"~~~(
switch (identifier) {)~~~");
}
have_output_identifier_switch = true;
};
auto values = feature.get_array("values"sv);
VERIFY(values.has_value());
auto& values_array = values.value();
for (auto& identifier : values_array.values()) {
VERIFY(identifier.is_string());
auto identifier_name = identifier.as_string();
// Skip types.
if (identifier_name[0] == '<')
continue;
append_identifier_switch_if_needed();
auto ident_generator = member_generator.fork();
ident_generator.set("identifier:titlecase", title_casify(identifier_name));
ident_generator.append(R"~~~(
case ValueID::@identifier:titlecase@:
return true;)~~~");
}
}
if (have_output_identifier_switch) {
member_generator.append(R"~~~(
default:
return false;
})~~~");
} else {
member_generator.append(R"~~~(
return false;)~~~");
}
});
generator.append(R"~~~(
}
VERIFY_NOT_REACHED();
}
}
)~~~");
// FIXME: This should write the entire span.
TRY(file.write_some(generator.as_string_view().bytes()));
return {};
}
|