CodeGenerators: Port GenerateCSSPropertyID to new SourceGenerator API

This commit is contained in:
Sam Atkins 2023-06-17 14:23:59 +01:00 committed by Sam Atkins
parent e7dd5e5b05
commit d197b4b051
Notes: sideshowbarker 2024-07-17 01:00:06 +09:00

View file

@ -88,7 +88,7 @@ ErrorOr<void> generate_header_file(JsonObject& properties, Core::File& file)
{
StringBuilder builder;
SourceGenerator generator { builder };
generator.append(R"~~~(
TRY(generator.try_append(R"~~~(
#pragma once
#include <AK/NonnullRefPtr.h>
@ -102,7 +102,7 @@ namespace Web::CSS {
enum class PropertyID {
Invalid,
Custom,
)~~~");
)~~~"));
Vector<DeprecatedString> shorthand_property_ids;
Vector<DeprecatedString> longhand_property_ids;
@ -122,18 +122,18 @@ enum class PropertyID {
auto member_generator = TRY(generator.fork());
TRY(member_generator.set("name:titlecase", TRY(title_casify(name))));
member_generator.append(R"~~~(
TRY(member_generator.try_append(R"~~~(
@name:titlecase@,
)~~~");
)~~~"));
}
for (auto& name : longhand_property_ids) {
auto member_generator = TRY(generator.fork());
TRY(member_generator.set("name:titlecase", TRY(title_casify(name))));
member_generator.append(R"~~~(
TRY(member_generator.try_append(R"~~~(
@name:titlecase@,
)~~~");
)~~~"));
}
TRY(generator.set("first_property_id", TRY(title_casify(first_property_id))));
@ -145,7 +145,7 @@ enum class PropertyID {
TRY(generator.set("first_longhand_property_id", TRY(title_casify(longhand_property_ids.first()))));
TRY(generator.set("last_longhand_property_id", TRY(title_casify(longhand_property_ids.last()))));
generator.append(R"~~~(
TRY(generator.try_append(R"~~~(
};
Optional<PropertyID> property_id_from_camel_case_string(StringView);
@ -217,7 +217,7 @@ struct Traits<Web::CSS::PropertyID> : public GenericTraits<Web::CSS::PropertyID>
static unsigned hash(Web::CSS::PropertyID property_id) { return int_hash((unsigned)property_id); }
};
} // namespace AK
)~~~");
)~~~"));
TRY(file.write_until_depleted(generator.as_string_view().bytes()));
return {};
@ -226,14 +226,14 @@ struct Traits<Web::CSS::PropertyID> : public GenericTraits<Web::CSS::PropertyID>
ErrorOr<void> generate_bounds_checking_function(JsonObject& properties, SourceGenerator& parent_generator, StringView css_type_name, StringView type_name, Optional<StringView> default_unit_name, Optional<StringView> value_getter)
{
auto generator = TRY(parent_generator.fork());
generator.set("css_type_name", css_type_name);
generator.set("type_name", type_name);
TRY(generator.set("css_type_name", TRY(String::from_utf8(css_type_name))));
TRY(generator.set("type_name", TRY(String::from_utf8(type_name))));
generator.append(R"~~~(
TRY(generator.try_append(R"~~~(
bool property_accepts_@css_type_name@(PropertyID property_id, [[maybe_unused]] @type_name@ const& value)
{
switch (property_id) {
)~~~");
)~~~"));
TRY(properties.try_for_each_member([&](auto& name, JsonValue const& value) -> ErrorOr<void> {
VERIFY(value.is_object());
@ -246,9 +246,9 @@ bool property_accepts_@css_type_name@(PropertyID property_id, [[maybe_unused]] @
auto property_generator = TRY(generator.fork());
TRY(property_generator.set("property_name:titlecase", TRY(title_casify(name))));
property_generator.append(R"~~~(
TRY(property_generator.try_append(R"~~~(
case PropertyID::@property_name:titlecase@:
return )~~~");
return )~~~"));
if (type_and_range.size() > 1) {
auto range = type_and_range[1];
@ -264,16 +264,16 @@ bool property_accepts_@css_type_name@(PropertyID property_id, [[maybe_unused]] @
max_value_string = {};
if (min_value_string.is_empty() && max_value_string.is_empty()) {
property_generator.appendln("true;");
TRY(property_generator.try_appendln("true;"));
break;
}
auto output_check = [&](auto& value_string, StringView comparator) -> ErrorOr<void> {
if (value_getter.has_value()) {
property_generator.set("value_number", value_string);
property_generator.set("value_getter", value_getter.value());
property_generator.set("comparator", comparator);
property_generator.append("@value_getter@ @comparator@ @value_number@");
TRY(property_generator.set("value_number", TRY(String::from_utf8(value_string))));
TRY(property_generator.set("value_getter", TRY(String::from_utf8(value_getter.value()))));
TRY(property_generator.set("comparator", TRY(String::from_utf8(comparator))));
TRY(property_generator.try_append("@value_getter@ @comparator@ @value_number@"));
return {};
}
@ -283,22 +283,22 @@ bool property_accepts_@css_type_name@(PropertyID property_id, [[maybe_unused]] @
if (value_unit.is_empty())
value_unit = default_unit_name.value();
VERIFY(lexer.is_eof());
property_generator.set("value_number", value_number);
TRY(property_generator.set("value_number", TRY(String::from_utf8(value_number))));
TRY(property_generator.set("value_unit", TRY(title_casify(value_unit))));
property_generator.set("comparator", comparator);
property_generator.append("value @comparator@ @type_name@(@value_number@, @type_name@::Type::@value_unit@)");
TRY(property_generator.set("comparator", TRY(String::from_utf8(comparator))));
TRY(property_generator.try_append("value @comparator@ @type_name@(@value_number@, @type_name@::Type::@value_unit@)"));
return {};
};
if (!min_value_string.is_empty())
TRY(output_check(min_value_string, ">="sv));
if (!min_value_string.is_empty() && !max_value_string.is_empty())
property_generator.append(" && ");
TRY(property_generator.try_append(" && "));
if (!max_value_string.is_empty())
TRY(output_check(max_value_string, "<="sv));
property_generator.appendln(";");
TRY(property_generator.try_appendln(";"));
} else {
property_generator.appendln("true;");
TRY(property_generator.try_appendln("true;"));
}
break;
}
@ -306,12 +306,12 @@ bool property_accepts_@css_type_name@(PropertyID property_id, [[maybe_unused]] @
return {};
}));
generator.append(R"~~~(
TRY(generator.try_append(R"~~~(
default:
return false;
}
}
)~~~");
)~~~"));
return {};
}
@ -320,7 +320,7 @@ ErrorOr<void> generate_implementation_file(JsonObject& properties, Core::File& f
StringBuilder builder;
SourceGenerator generator { builder };
generator.append(R"~~~(
TRY(generator.try_append(R"~~~(
#include <AK/Assertions.h>
#include <LibWeb/CSS/Enums.h>
#include <LibWeb/CSS/Parser/Parser.h>
@ -334,65 +334,65 @@ namespace Web::CSS {
Optional<PropertyID> property_id_from_camel_case_string(StringView string)
{
)~~~");
)~~~"));
TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
VERIFY(value.is_object());
auto member_generator = TRY(generator.fork());
member_generator.set("name", name);
TRY(member_generator.set("name", TRY(String::from_deprecated_string(name))));
TRY(member_generator.set("name:titlecase", TRY(title_casify(name))));
TRY(member_generator.set("name:camelcase", TRY(camel_casify(name))));
member_generator.append(R"~~~(
TRY(member_generator.try_append(R"~~~(
if (string.equals_ignoring_ascii_case("@name:camelcase@"sv))
return PropertyID::@name:titlecase@;
)~~~");
)~~~"));
return {};
}));
generator.append(R"~~~(
TRY(generator.try_append(R"~~~(
return {};
}
Optional<PropertyID> property_id_from_string(StringView string)
{
)~~~");
)~~~"));
TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
VERIFY(value.is_object());
auto member_generator = TRY(generator.fork());
member_generator.set("name", name);
TRY(member_generator.set("name", TRY(String::from_deprecated_string(name))));
TRY(member_generator.set("name:titlecase", TRY(title_casify(name))));
member_generator.append(R"~~~(
TRY(member_generator.try_append(R"~~~(
if (Infra::is_ascii_case_insensitive_match(string, "@name@"sv))
return PropertyID::@name:titlecase@;
)~~~");
)~~~"));
return {};
}));
generator.append(R"~~~(
TRY(generator.try_append(R"~~~(
return {};
}
StringView string_from_property_id(PropertyID property_id) {
switch (property_id) {
)~~~");
)~~~"));
TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
VERIFY(value.is_object());
auto member_generator = TRY(generator.fork());
member_generator.set("name", name);
TRY(member_generator.set("name", TRY(String::from_deprecated_string(name))));
TRY(member_generator.set("name:titlecase", TRY(title_casify(name))));
member_generator.append(R"~~~(
TRY(member_generator.try_append(R"~~~(
case PropertyID::@name:titlecase@:
return "@name@"sv;
)~~~");
)~~~"));
return {};
}));
generator.append(R"~~~(
TRY(generator.try_append(R"~~~(
default:
return "(invalid CSS::PropertyID)"sv;
}
@ -401,7 +401,7 @@ StringView string_from_property_id(PropertyID property_id) {
bool is_inherited_property(PropertyID property_id)
{
switch (property_id) {
)~~~");
)~~~"));
TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
VERIFY(value.is_object());
@ -416,15 +416,15 @@ bool is_inherited_property(PropertyID property_id)
if (inherited) {
auto member_generator = TRY(generator.fork());
TRY(member_generator.set("name:titlecase", TRY(title_casify(name))));
member_generator.append(R"~~~(
TRY(member_generator.try_append(R"~~~(
case PropertyID::@name:titlecase@:
return true;
)~~~");
)~~~"));
}
return {};
}));
generator.append(R"~~~(
TRY(generator.try_append(R"~~~(
default:
return false;
}
@ -433,7 +433,7 @@ bool is_inherited_property(PropertyID property_id)
bool property_affects_layout(PropertyID property_id)
{
switch (property_id) {
)~~~");
)~~~"));
TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
VERIFY(value.is_object());
@ -445,14 +445,14 @@ bool property_affects_layout(PropertyID property_id)
if (affects_layout) {
auto member_generator = TRY(generator.fork());
TRY(member_generator.set("name:titlecase", TRY(title_casify(name))));
member_generator.append(R"~~~(
TRY(member_generator.try_append(R"~~~(
case PropertyID::@name:titlecase@:
)~~~");
)~~~"));
}
return {};
}));
generator.append(R"~~~(
TRY(generator.try_append(R"~~~(
return true;
default:
return false;
@ -462,7 +462,7 @@ bool property_affects_layout(PropertyID property_id)
bool property_affects_stacking_context(PropertyID property_id)
{
switch (property_id) {
)~~~");
)~~~"));
TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
VERIFY(value.is_object());
@ -474,14 +474,14 @@ bool property_affects_stacking_context(PropertyID property_id)
if (affects_stacking_context) {
auto member_generator = TRY(generator.fork());
TRY(member_generator.set("name:titlecase", TRY(title_casify(name))));
member_generator.append(R"~~~(
TRY(member_generator.try_append(R"~~~(
case PropertyID::@name:titlecase@:
)~~~");
)~~~"));
}
return {};
}));
generator.append(R"~~~(
TRY(generator.try_append(R"~~~(
return true;
default:
return false;
@ -500,7 +500,7 @@ ErrorOr<NonnullRefPtr<StyleValue>> property_initial_value(JS::Realm& context_rea
Parser::ParsingContext parsing_context(context_realm);
switch (property_id) {
)~~~");
)~~~"));
auto output_initial_value_code = [&](auto& name, auto& object) -> ErrorOr<void> {
if (!object.has("initial"sv)) {
@ -513,8 +513,8 @@ ErrorOr<NonnullRefPtr<StyleValue>> property_initial_value(JS::Realm& context_rea
auto member_generator = TRY(generator.fork());
TRY(member_generator.set("name:titlecase", TRY(title_casify(name))));
member_generator.set("initial_value_string", initial_value_string);
member_generator.append(
TRY(member_generator.set("initial_value_string", TRY(String::from_deprecated_string(initial_value_string))));
TRY(member_generator.try_append(
R"~~~( case PropertyID::@name:titlecase@:
{
auto parsed_value = TRY(parse_css_value(parsing_context, "@initial_value_string@"sv, PropertyID::@name:titlecase@));
@ -523,7 +523,7 @@ ErrorOr<NonnullRefPtr<StyleValue>> property_initial_value(JS::Realm& context_rea
initial_values[to_underlying(PropertyID::@name:titlecase@)] = initial_value;
return initial_value;
}
)~~~");
)~~~"));
return {};
};
@ -533,7 +533,7 @@ ErrorOr<NonnullRefPtr<StyleValue>> property_initial_value(JS::Realm& context_rea
return {};
}));
generator.append(
TRY(generator.try_append(
R"~~~( default: VERIFY_NOT_REACHED();
}
VERIFY_NOT_REACHED();
@ -542,7 +542,7 @@ ErrorOr<NonnullRefPtr<StyleValue>> property_initial_value(JS::Realm& context_rea
bool property_has_quirk(PropertyID property_id, Quirk quirk)
{
switch (property_id) {
)~~~");
)~~~"));
TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
VERIFY(value.is_object());
@ -554,31 +554,31 @@ bool property_has_quirk(PropertyID property_id, Quirk quirk)
if (!quirks.is_empty()) {
auto property_generator = TRY(generator.fork());
TRY(property_generator.set("name:titlecase", TRY(title_casify(name))));
property_generator.append(R"~~~(
TRY(property_generator.try_append(R"~~~(
case PropertyID::@name:titlecase@: {
switch (quirk) {
)~~~");
)~~~"));
for (auto& quirk : quirks.values()) {
VERIFY(quirk.is_string());
auto quirk_generator = TRY(property_generator.fork());
TRY(quirk_generator.set("quirk:titlecase", TRY(title_casify(quirk.as_string()))));
quirk_generator.append(R"~~~(
TRY(quirk_generator.try_append(R"~~~(
case Quirk::@quirk:titlecase@:
return true;
)~~~");
)~~~"));
}
property_generator.append(R"~~~(
TRY(property_generator.try_append(R"~~~(
default:
return false;
}
}
)~~~");
)~~~"));
}
}
return {};
}));
generator.append(R"~~~(
TRY(generator.try_append(R"~~~(
default:
return false;
}
@ -587,7 +587,7 @@ bool property_has_quirk(PropertyID property_id, Quirk quirk)
bool property_accepts_type(PropertyID property_id, ValueType value_type)
{
switch (property_id) {
)~~~");
)~~~"));
TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
VERIFY(value.is_object());
auto& object = value.as_object();
@ -595,10 +595,10 @@ bool property_accepts_type(PropertyID property_id, ValueType value_type)
auto& valid_types = maybe_valid_types.value();
auto property_generator = TRY(generator.fork());
TRY(property_generator.set("name:titlecase", TRY(title_casify(name))));
property_generator.append(R"~~~(
TRY(property_generator.try_append(R"~~~(
case PropertyID::@name:titlecase@: {
switch (value_type) {
)~~~");
)~~~"));
bool did_output_accepted_type = false;
for (auto& type : valid_types.values()) {
@ -608,37 +608,37 @@ bool property_accepts_type(PropertyID property_id, ValueType value_type)
continue;
if (type_name == "angle") {
property_generator.appendln(" case ValueType::Angle:");
TRY(property_generator.try_appendln(" case ValueType::Angle:"));
} else if (type_name == "color") {
property_generator.appendln(" case ValueType::Color:");
TRY(property_generator.try_appendln(" case ValueType::Color:"));
} else if (type_name == "custom-ident") {
property_generator.appendln(" case ValueType::CustomIdent:");
TRY(property_generator.try_appendln(" case ValueType::CustomIdent:"));
} else if (type_name == "frequency") {
property_generator.appendln(" case ValueType::Frequency:");
TRY(property_generator.try_appendln(" case ValueType::Frequency:"));
} else if (type_name == "image") {
property_generator.appendln(" case ValueType::Image:");
TRY(property_generator.try_appendln(" case ValueType::Image:"));
} else if (type_name == "integer") {
property_generator.appendln(" case ValueType::Integer:");
TRY(property_generator.try_appendln(" case ValueType::Integer:"));
} else if (type_name == "length") {
property_generator.appendln(" case ValueType::Length:");
TRY(property_generator.try_appendln(" case ValueType::Length:"));
} else if (type_name == "number") {
property_generator.appendln(" case ValueType::Number:");
TRY(property_generator.try_appendln(" case ValueType::Number:"));
} else if (type_name == "paint") {
property_generator.appendln(" case ValueType::Paint:");
TRY(property_generator.try_appendln(" case ValueType::Paint:"));
} else if (type_name == "percentage") {
property_generator.appendln(" case ValueType::Percentage:");
TRY(property_generator.try_appendln(" case ValueType::Percentage:"));
} else if (type_name == "ratio") {
property_generator.appendln(" case ValueType::Ratio:");
TRY(property_generator.try_appendln(" case ValueType::Ratio:"));
} else if (type_name == "rect") {
property_generator.appendln(" case ValueType::Rect:");
TRY(property_generator.try_appendln(" case ValueType::Rect:"));
} else if (type_name == "resolution") {
property_generator.appendln(" case ValueType::Resolution:");
TRY(property_generator.try_appendln(" case ValueType::Resolution:"));
} else if (type_name == "string") {
property_generator.appendln(" case ValueType::String:");
TRY(property_generator.try_appendln(" case ValueType::String:"));
} else if (type_name == "time") {
property_generator.appendln(" case ValueType::Time:");
TRY(property_generator.try_appendln(" case ValueType::Time:"));
} else if (type_name == "url") {
property_generator.appendln(" case ValueType::Url:");
TRY(property_generator.try_appendln(" case ValueType::Url:"));
} else {
VERIFY_NOT_REACHED();
}
@ -646,18 +646,18 @@ bool property_accepts_type(PropertyID property_id, ValueType value_type)
}
if (did_output_accepted_type)
property_generator.appendln(" return true;");
TRY(property_generator.try_appendln(" return true;"));
property_generator.append(R"~~~(
TRY(property_generator.try_append(R"~~~(
default:
return false;
}
}
)~~~");
)~~~"));
}
return {};
}));
generator.append(R"~~~(
TRY(generator.try_append(R"~~~(
default:
return false;
}
@ -666,29 +666,29 @@ bool property_accepts_type(PropertyID property_id, ValueType value_type)
bool property_accepts_identifier(PropertyID property_id, ValueID identifier)
{
switch (property_id) {
)~~~");
)~~~"));
TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
VERIFY(value.is_object());
auto& object = value.as_object();
auto property_generator = TRY(generator.fork());
TRY(property_generator.set("name:titlecase", TRY(title_casify(name))));
property_generator.appendln(" case PropertyID::@name:titlecase@: {");
TRY(property_generator.try_appendln(" case PropertyID::@name:titlecase@: {"));
if (auto maybe_valid_identifiers = object.get_array("valid-identifiers"sv); maybe_valid_identifiers.has_value() && !maybe_valid_identifiers->is_empty()) {
property_generator.appendln(" switch (identifier) {");
TRY(property_generator.try_appendln(" switch (identifier) {"));
auto& valid_identifiers = maybe_valid_identifiers.value();
for (auto& identifier : valid_identifiers.values()) {
auto identifier_generator = TRY(generator.fork());
TRY(identifier_generator.set("identifier:titlecase", TRY(title_casify(identifier.as_string()))));
identifier_generator.appendln(" case ValueID::@identifier:titlecase@:");
TRY(identifier_generator.try_appendln(" case ValueID::@identifier:titlecase@:"));
}
property_generator.append(R"~~~(
TRY(property_generator.try_append(R"~~~(
return true;
default:
break;
}
)~~~");
)~~~"));
}
if (auto maybe_valid_types = object.get_array("valid-types"sv); maybe_valid_types.has_value() && !maybe_valid_types->is_empty()) {
@ -700,19 +700,19 @@ bool property_accepts_identifier(PropertyID property_id, ValueID identifier)
auto type_generator = TRY(generator.fork());
TRY(type_generator.set("type_name:snakecase", TRY(snake_casify(type_name))));
type_generator.append(R"~~~(
TRY(type_generator.try_append(R"~~~(
if (value_id_to_@type_name:snakecase@(identifier).has_value())
return true;
)~~~");
)~~~"));
}
}
property_generator.append(R"~~~(
TRY(property_generator.try_append(R"~~~(
return false;
}
)~~~");
)~~~"));
return {};
}));
generator.append(R"~~~(
TRY(generator.try_append(R"~~~(
default:
return false;
}
@ -721,7 +721,7 @@ bool property_accepts_identifier(PropertyID property_id, ValueID identifier)
size_t property_maximum_value_count(PropertyID property_id)
{
switch (property_id) {
)~~~");
)~~~"));
TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
VERIFY(value.is_object());
@ -730,20 +730,20 @@ size_t property_maximum_value_count(PropertyID property_id)
VERIFY(max_values.has_value() && max_values->is_number() && !max_values->is_double());
auto property_generator = TRY(generator.fork());
TRY(property_generator.set("name:titlecase", TRY(title_casify(name))));
property_generator.set("max_values", max_values->to_deprecated_string());
property_generator.append(R"~~~(
TRY(property_generator.set("max_values", TRY(String::from_deprecated_string(max_values->to_deprecated_string()))));
TRY(property_generator.try_append(R"~~~(
case PropertyID::@name:titlecase@:
return @max_values@;
)~~~");
)~~~"));
}
return {};
}));
generator.append(R"~~~(
TRY(generator.try_append(R"~~~(
default:
return 1;
}
})~~~");
})~~~"));
TRY(generate_bounds_checking_function(properties, generator, "angle"sv, "Angle"sv, "Deg"sv));
TRY(generate_bounds_checking_function(properties, generator, "frequency"sv, "Frequency"sv, "Hertz"sv));
@ -754,11 +754,11 @@ size_t property_maximum_value_count(PropertyID property_id)
TRY(generate_bounds_checking_function(properties, generator, "resolution"sv, "Resolution"sv, "Dpi"sv));
TRY(generate_bounds_checking_function(properties, generator, "time"sv, "Time"sv, "S"sv));
generator.append(R"~~~(
TRY(generator.try_append(R"~~~(
Vector<PropertyID> longhands_for_shorthand(PropertyID property_id)
{
switch (property_id) {
)~~~");
)~~~"));
TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
if (value.as_object().has("longhands"sv)) {
auto longhands = value.as_object().get("longhands"sv);
@ -772,30 +772,30 @@ Vector<PropertyID> longhands_for_shorthand(PropertyID property_id)
if (first)
first = false;
else
builder.append(", "sv);
builder.appendff("PropertyID::{}", TRY(title_casify(longhand.to_deprecated_string())));
TRY(builder.try_append(", "sv));
TRY(builder.try_appendff("PropertyID::{}", TRY(title_casify(longhand.to_deprecated_string()))));
return IterationDecision::Continue;
}));
property_generator.set("longhands", builder.to_deprecated_string());
property_generator.append(R"~~~(
TRY(property_generator.try_append(R"~~~(
case PropertyID::@name:titlecase@:
return { @longhands@ };
)~~~");
)~~~"));
}
return {};
}));
generator.append(R"~~~(
TRY(generator.try_append(R"~~~(
default:
return { };
}
}
)~~~");
)~~~"));
generator.append(R"~~~(
TRY(generator.try_append(R"~~~(
} // namespace Web::CSS
)~~~");
)~~~"));
TRY(file.write_until_depleted(generator.as_string_view().bytes()));
return {};