AK: Remove fallible SourceGenerator::try_appendln()

And fall back to the infallible appendln().
This commit is contained in:
Andreas Kling 2023-08-21 16:06:29 +02:00
parent 8b936b5912
commit a1d694ccdc
Notes: sideshowbarker 2024-07-18 03:23:00 +09:00
7 changed files with 54 additions and 58 deletions

View file

@ -86,11 +86,10 @@ public:
return {};
}
ErrorOr<void> try_appendln(StringView pattern)
void appendln(StringView pattern)
{
TRY(try_append(pattern));
TRY(m_builder.try_append('\n'));
return {};
append(pattern);
m_builder.append('\n');
}
template<size_t N>
@ -112,9 +111,9 @@ public:
}
template<size_t N>
ErrorOr<void> try_appendln(char const (&pattern)[N])
void appendln(char const (&pattern)[N])
{
return try_appendln(StringView { pattern, N - 1 });
appendln(StringView { pattern, N - 1 });
}
// FIXME: These are deprecated.
@ -128,11 +127,8 @@ public:
set(StringView { key, N - 1 }, value);
}
void append(StringView pattern) { MUST(try_append(pattern)); }
void appendln(StringView pattern) { MUST(try_appendln(pattern)); }
template<size_t N>
void append(char const (&pattern)[N]) { MUST(try_append(pattern)); }
template<size_t N>
void appendln(char const (&pattern)[N]) { MUST(try_appendln(pattern)); }
private:
StringBuilder& m_builder;

View file

@ -55,17 +55,17 @@ namespace Web::CSS {
)~~~"));
TRY(generator.try_appendln("enum class EasingFunction {"));
generator.appendln("enum class EasingFunction {");
TRY(easing_data.try_for_each_member([&](auto& name, auto&) -> ErrorOr<void> {
auto member_generator = TRY(generator.fork());
member_generator.set("name:titlecase", TRY(title_casify(name)));
TRY(member_generator.try_appendln(" @name:titlecase@,"));
member_generator.appendln(" @name:titlecase@,");
return {};
}));
TRY(generator.try_appendln("};"));
generator.appendln("};");
TRY(generator.try_appendln("Optional<EasingFunction> easing_function_from_string(StringView);"));
TRY(generator.try_appendln("StringView to_string(EasingFunction);"));
generator.appendln("Optional<EasingFunction> easing_function_from_string(StringView);");
generator.appendln("StringView to_string(EasingFunction);");
TRY(generator.try_append(R"~~~(
enum class EasingFunctionParameterType {
@ -86,7 +86,7 @@ struct EasingFunctionMetadata {
EasingFunctionMetadata easing_function_metadata(EasingFunction);
)~~~"));
TRY(generator.try_appendln("\n}"));
generator.appendln("\n}");
TRY(file.write_until_depleted(generator.as_string_view().bytes()));
return {};
@ -212,7 +212,7 @@ EasingFunctionMetadata easing_function_metadata(EasingFunction easing_function)
}
)~~~"));
TRY(generator.try_appendln("\n}"));
generator.appendln("\n}");
TRY(file.write_until_depleted(generator.as_string_view().bytes()));
return {};

View file

@ -74,7 +74,7 @@ enum class ValueID;
enum_generator.set("enum_type", "u64"_string);
}
TRY(enum_generator.try_appendln("enum class @name:titlecase@ : @enum_type@ {"));
enum_generator.appendln("enum class @name:titlecase@ : @enum_type@ {");
for (auto& member : members.values()) {
auto member_name = member.to_deprecated_string();
@ -83,18 +83,18 @@ enum class ValueID;
continue;
auto member_generator = TRY(enum_generator.fork());
member_generator.set("member:titlecase", TRY(title_casify(member_name)));
TRY(member_generator.try_appendln(" @member:titlecase@,"));
member_generator.appendln(" @member:titlecase@,");
}
TRY(enum_generator.try_appendln("};"));
TRY(enum_generator.try_appendln("Optional<@name:titlecase@> value_id_to_@name:snakecase@(ValueID);"));
TRY(enum_generator.try_appendln("ValueID to_value_id(@name:titlecase@);"));
TRY(enum_generator.try_appendln("StringView to_string(@name:titlecase@);"));
enum_generator.appendln("};");
enum_generator.appendln("Optional<@name:titlecase@> value_id_to_@name:snakecase@(ValueID);");
enum_generator.appendln("ValueID to_value_id(@name:titlecase@);");
enum_generator.appendln("StringView to_string(@name:titlecase@);");
TRY(enum_generator.try_append("\n"));
return {};
}));
TRY(generator.try_appendln("}"));
generator.appendln("}");
TRY(file.write_until_depleted(generator.as_string_view().bytes()));
return {};
@ -199,7 +199,7 @@ StringView to_string(@name:titlecase@ value)
return {};
}));
TRY(generator.try_appendln("}"));
generator.appendln("}");
TRY(file.write_until_depleted(generator.as_string_view().bytes()));
return {};

View file

@ -56,7 +56,7 @@ enum class MathFunction {
TRY(functions_data.try_for_each_member([&](auto& name, auto&) -> ErrorOr<void> {
auto member_generator = TRY(generator.fork());
member_generator.set("name:titlecase", TRY(title_casify(name)));
TRY(member_generator.try_appendln(" @name:titlecase@,"sv));
member_generator.appendln(" @name:titlecase@,"sv);
return {};
}));
@ -157,7 +157,7 @@ OwnPtr<CalculationNode> Parser::parse_math_function(PropertyID property_id, Func
auto function_generator = TRY(generator.fork());
function_generator.set("name:lowercase", TRY(String::from_deprecated_string(name)));
function_generator.set("name:titlecase", TRY(title_casify(name)));
TRY(function_generator.try_appendln(" if (function.name().equals_ignoring_ascii_case(\"@name:lowercase@\"sv)) {"));
function_generator.appendln(" if (function.name().equals_ignoring_ascii_case(\"@name:lowercase@\"sv)) {");
if (function_data.get_bool("is-variadic"sv).value_or(false)) {
// Variadic function
TRY(function_generator.try_append(R"~~~(

View file

@ -270,7 +270,7 @@ 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()) {
TRY(property_generator.try_appendln("true;"));
property_generator.appendln("true;");
break;
}
@ -302,9 +302,9 @@ bool property_accepts_@css_type_name@(PropertyID property_id, [[maybe_unused]] @
TRY(property_generator.try_append(" && "));
if (!max_value_string.is_empty())
TRY(output_check(max_value_string, "<="sv));
TRY(property_generator.try_appendln(";"));
property_generator.appendln(";");
} else {
TRY(property_generator.try_appendln("true;"));
property_generator.appendln("true;");
}
break;
}
@ -616,39 +616,39 @@ bool property_accepts_type(PropertyID property_id, ValueType value_type)
continue;
if (type_name == "angle") {
TRY(property_generator.try_appendln(" case ValueType::Angle:"));
property_generator.appendln(" case ValueType::Angle:");
} else if (type_name == "color") {
TRY(property_generator.try_appendln(" case ValueType::Color:"));
property_generator.appendln(" case ValueType::Color:");
} else if (type_name == "custom-ident") {
TRY(property_generator.try_appendln(" case ValueType::CustomIdent:"));
property_generator.appendln(" case ValueType::CustomIdent:");
} else if (type_name == "easing-function") {
TRY(property_generator.try_appendln(" case ValueType::EasingFunction:"));
property_generator.appendln(" case ValueType::EasingFunction:");
} else if (type_name == "frequency") {
TRY(property_generator.try_appendln(" case ValueType::Frequency:"));
property_generator.appendln(" case ValueType::Frequency:");
} else if (type_name == "image") {
TRY(property_generator.try_appendln(" case ValueType::Image:"));
property_generator.appendln(" case ValueType::Image:");
} else if (type_name == "integer") {
TRY(property_generator.try_appendln(" case ValueType::Integer:"));
property_generator.appendln(" case ValueType::Integer:");
} else if (type_name == "length") {
TRY(property_generator.try_appendln(" case ValueType::Length:"));
property_generator.appendln(" case ValueType::Length:");
} else if (type_name == "number") {
TRY(property_generator.try_appendln(" case ValueType::Number:"));
property_generator.appendln(" case ValueType::Number:");
} else if (type_name == "paint") {
TRY(property_generator.try_appendln(" case ValueType::Paint:"));
property_generator.appendln(" case ValueType::Paint:");
} else if (type_name == "percentage") {
TRY(property_generator.try_appendln(" case ValueType::Percentage:"));
property_generator.appendln(" case ValueType::Percentage:");
} else if (type_name == "ratio") {
TRY(property_generator.try_appendln(" case ValueType::Ratio:"));
property_generator.appendln(" case ValueType::Ratio:");
} else if (type_name == "rect") {
TRY(property_generator.try_appendln(" case ValueType::Rect:"));
property_generator.appendln(" case ValueType::Rect:");
} else if (type_name == "resolution") {
TRY(property_generator.try_appendln(" case ValueType::Resolution:"));
property_generator.appendln(" case ValueType::Resolution:");
} else if (type_name == "string") {
TRY(property_generator.try_appendln(" case ValueType::String:"));
property_generator.appendln(" case ValueType::String:");
} else if (type_name == "time") {
TRY(property_generator.try_appendln(" case ValueType::Time:"));
property_generator.appendln(" case ValueType::Time:");
} else if (type_name == "url") {
TRY(property_generator.try_appendln(" case ValueType::Url:"));
property_generator.appendln(" case ValueType::Url:");
} else {
VERIFY_NOT_REACHED();
}
@ -656,7 +656,7 @@ bool property_accepts_type(PropertyID property_id, ValueType value_type)
}
if (did_output_accepted_type)
TRY(property_generator.try_appendln(" return true;"));
property_generator.appendln(" return true;");
TRY(property_generator.try_append(R"~~~(
default:
@ -683,15 +683,15 @@ bool property_accepts_identifier(PropertyID property_id, ValueID identifier)
auto property_generator = TRY(generator.fork());
property_generator.set("name:titlecase", TRY(title_casify(name)));
TRY(property_generator.try_appendln(" case PropertyID::@name:titlecase@: {"));
property_generator.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()) {
TRY(property_generator.try_appendln(" switch (identifier) {"));
property_generator.appendln(" switch (identifier) {");
auto& valid_identifiers = maybe_valid_identifiers.value();
for (auto& identifier : valid_identifiers.values()) {
auto identifier_generator = TRY(generator.fork());
identifier_generator.set("identifier:titlecase", TRY(title_casify(identifier.as_string())));
TRY(identifier_generator.try_appendln(" case ValueID::@identifier:titlecase@:"));
identifier_generator.appendln(" case ValueID::@identifier:titlecase@:");
}
TRY(property_generator.try_append(R"~~~(
return true;

View file

@ -57,7 +57,7 @@ enum class PseudoClass {
auto member_generator = TRY(generator.fork());
member_generator.set("name:titlecase", TRY(title_casify(name)));
TRY(member_generator.try_appendln(" @name:titlecase@,"));
member_generator.appendln(" @name:titlecase@,");
return {};
}));
TRY(generator.try_append(R"~~~(

View file

@ -64,17 +64,17 @@ namespace Web::CSS {
)~~~"));
TRY(generator.try_appendln("enum class TransformFunction {"));
generator.appendln("enum class TransformFunction {");
TRY(transforms_data.try_for_each_member([&](auto& name, auto&) -> ErrorOr<void> {
auto member_generator = TRY(generator.fork());
member_generator.set("name:titlecase", TRY(title_casify_transform_function(name)));
TRY(member_generator.try_appendln(" @name:titlecase@,"));
member_generator.appendln(" @name:titlecase@,");
return {};
}));
TRY(generator.try_appendln("};"));
generator.appendln("};");
TRY(generator.try_appendln("Optional<TransformFunction> transform_function_from_string(StringView);"));
TRY(generator.try_appendln("StringView to_string(TransformFunction);"));
generator.appendln("Optional<TransformFunction> transform_function_from_string(StringView);");
generator.appendln("StringView to_string(TransformFunction);");
TRY(generator.try_append(R"~~~(
enum class TransformFunctionParameterType {
@ -95,7 +95,7 @@ struct TransformFunctionMetadata {
TransformFunctionMetadata transform_function_metadata(TransformFunction);
)~~~"));
TRY(generator.try_appendln("\n}"));
generator.appendln("\n}");
TRY(file.write_until_depleted(generator.as_string_view().bytes()));
return {};
@ -208,7 +208,7 @@ TransformFunctionMetadata transform_function_metadata(TransformFunction transfor
}
)~~~"));
TRY(generator.try_appendln("\n}"));
generator.appendln("\n}");
TRY(file.write_until_depleted(generator.as_string_view().bytes()));
return {};