LibWeb/CSS: Use ErrorReporter for value-parsing errors

Also remove some redundant reporting for `<urange>` parsing errors.
This commit is contained in:
Sam Atkins 2025-07-23 11:04:17 +01:00
parent 3b7aa736e7
commit cebdcd9f69
Notes: github-actions[bot] 2025-08-04 09:52:05 +00:00
5 changed files with 239 additions and 61 deletions

View file

@ -87,8 +87,8 @@ ErrorOr<void> generate_implementation_file(JsonObject& functions_data, Core::Fil
generator.append(R"~~~(
// This file is generated by GenerateCSSMathFunctions.cpp
#include <AK/Debug.h>
#include <LibWeb/CSS/MathFunctions.h>
#include <LibWeb/CSS/Parser/ErrorReporter.h>
#include <LibWeb/CSS/Parser/Parser.h>
#include <LibWeb/CSS/Enums.h>
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
@ -144,13 +144,21 @@ RefPtr<CalculationNode const> Parser::parse_math_function(Function const& functi
for (auto& argument : arguments) {
auto calculation_node = parse_a_calculation(argument, context);
if (!calculation_node) {
dbgln_if(CSS_PARSER_DEBUG, "@name:lowercase@() argument #{} is not a valid calculation", parsed_arguments.size());
ErrorReporter::the().report(InvalidValueError {
.value_type = "@name:lowercase@()"_fly_string,
.value_string = stream.dump_string(),
.description = MUST(String::formatted("Argument #{} is not a valid calculation.", parsed_arguments.size())),
});
return nullptr;
}
auto maybe_argument_type = calculation_node->numeric_type();
if (!maybe_argument_type.has_value()) {
dbgln_if(CSS_PARSER_DEBUG, "@name:lowercase@() argument #{} couldn't determine its type", parsed_arguments.size());
ErrorReporter::the().report(InvalidValueError {
.value_type = "@name:lowercase@()"_fly_string,
.value_string = stream.dump_string(),
.description = MUST(String::formatted("Argument #{} couldn't determine its type.", parsed_arguments.size())),
});
return nullptr;
}
auto argument_type = maybe_argument_type.release_value();
@ -163,7 +171,11 @@ RefPtr<CalculationNode const> Parser::parse_math_function(Function const& functi
function_generator.set("type_check", generate_calculation_type_check("argument_type"sv, parameter_type_string));
function_generator.append(R"~~~(
if (!(@type_check@)) {
dbgln_if(CSS_PARSER_DEBUG, "@name:lowercase@() argument #{} type ({}) is not an accepted type", parsed_arguments.size(), argument_type.dump());
ErrorReporter::the().report(InvalidValueError {
.value_type = "@name:lowercase@()"_fly_string,
.value_string = stream.dump_string(),
.description = MUST(String::formatted("Argument #{} type ({}) is not an accepted type.", parsed_arguments.size(), argument_type.dump())),
});
return nullptr;
}
@ -174,7 +186,11 @@ RefPtr<CalculationNode const> Parser::parse_math_function(Function const& functi
if (requires_same_parameters) {
function_generator.append(R"~~~(
if (determined_argument_type != argument_type) {
dbgln_if(CSS_PARSER_DEBUG, "@name:lowercase@() argument #{} type ({}) doesn't match type of previous arguments ({})", parsed_arguments.size(), argument_type.dump(), determined_argument_type->dump());
ErrorReporter::the().report(InvalidValueError {
.value_type = "@name:lowercase@()"_fly_string,
.value_string = stream.dump_string(),
.description = MUST(String::formatted("Argument #{} type ({}) doesn't match type of previous arguments ({}).", parsed_arguments.size(), argument_type.dump(), determined_argument_type->dump())),
});
return nullptr;
}
)~~~");
@ -183,7 +199,11 @@ RefPtr<CalculationNode const> Parser::parse_math_function(Function const& functi
if (auto consistent_type = determined_argument_type->consistent_type(argument_type); consistent_type.has_value()) {
determined_argument_type = consistent_type.release_value();
} else {
dbgln_if(CSS_PARSER_DEBUG, "@name:lowercase@() argument #{} type ({}) is not consistent with type of previous arguments ({})", parsed_arguments.size(), argument_type.dump(), determined_argument_type->dump());
ErrorReporter::the().report(InvalidValueError {
.value_type = "@name:lowercase@()"_fly_string,
.value_string = stream.dump_string(),
.description = MUST(String::formatted("Argument #{} type ({}) is not consistent with type of previous arguments ({}).", parsed_arguments.size(), argument_type.dump(), determined_argument_type->dump())),
});
return nullptr;
}
)~~~");
@ -212,7 +232,11 @@ RefPtr<CalculationNode const> Parser::parse_math_function(Function const& functi
function_generator.append(R"~~~(
if (arguments.size() < @min_argument_count@ || arguments.size() > @max_argument_count@) {
dbgln_if(CSS_PARSER_DEBUG, "@name:lowercase@() has wrong number of arguments {}, expected between @min_argument_count@ and @max_argument_count@ inclusive", arguments.size());
ErrorReporter::the().report(InvalidValueError {
.value_type = "@name:lowercase@()"_fly_string,
.value_string = stream.dump_string(),
.description = MUST(String::formatted("Wrong number of arguments {}, expected between @min_argument_count@ and @max_argument_count@ inclusive.", arguments.size())),
});
return nullptr;
}
size_t argument_index = 0;
@ -265,7 +289,11 @@ RefPtr<CalculationNode const> Parser::parse_math_function(Function const& functi
if (parameter_required) {
parameter_generator.append(R"~~~(
if (argument_index >= arguments.size()) {
dbgln_if(CSS_PARSER_DEBUG, "@name:lowercase@() missing required argument '@parameter_name@'");
ErrorReporter::the().report(InvalidValueError {
.value_type = "@name:lowercase@()"_fly_string,
.value_string = stream.dump_string(),
.description = "Missing required argument '@parameter_name@'."_string,
});
return nullptr;
} else {
)~~~");
@ -284,7 +312,11 @@ RefPtr<CalculationNode const> Parser::parse_math_function(Function const& functi
if (parameter_required) {
parameter_generator.append(R"~~~(
} else {
dbgln_if(CSS_PARSER_DEBUG, "@name:lowercase@() required argument '@parameter_name@' failed to parse");
ErrorReporter::the().report(InvalidValueError {
.value_type = "@name:lowercase@()"_fly_string,
.value_string = stream.dump_string(),
.description = "Failed to parse required argument '@parameter_name@'."_string,
});
return nullptr;
)~~~");
}
@ -299,13 +331,21 @@ RefPtr<CalculationNode const> Parser::parse_math_function(Function const& functi
parameter_generator.append(R"~~~(
auto maybe_argument_type_@parameter_index@ = parameter_@parameter_index@->numeric_type();
if (!maybe_argument_type_@parameter_index@.has_value()) {
dbgln_if(CSS_PARSER_DEBUG, "@name:lowercase@() argument '@parameter_name@' couldn't determine its type");
ErrorReporter::the().report(InvalidValueError {
.value_type = "@name:lowercase@()"_fly_string,
.value_string = stream.dump_string(),
.description = "Argument '@parameter_name@' couldn't determine its type."_string,
});
return nullptr;
}
auto argument_type_@parameter_index@ = maybe_argument_type_@parameter_index@.release_value();
if (!(@type_check@)) {
dbgln_if(CSS_PARSER_DEBUG, "@name:lowercase@() argument '@parameter_name@' type ({}) is not an accepted type", argument_type_@parameter_index@.dump());
ErrorReporter::the().report(InvalidValueError {
.value_type = "@name:lowercase@()"_fly_string,
.value_string = stream.dump_string(),
.description = MUST(String::formatted("Argument '@parameter_name@' type ({}) is not an accepted type.", argument_type_@parameter_index@.dump())),
});
return nullptr;
}
@ -317,7 +357,11 @@ RefPtr<CalculationNode const> Parser::parse_math_function(Function const& functi
if (requires_same_parameters) {
parameter_generator.append(R"~~~(
if (determined_argument_type != argument_type_@parameter_index@) {
dbgln_if(CSS_PARSER_DEBUG, "@name:lowercase@() argument '@parameter_name@' type ({}) doesn't match type of previous arguments ({})", argument_type_@parameter_index@.dump(), determined_argument_type->dump());
ErrorReporter::the().report(InvalidValueError {
.value_type = "@name:lowercase@()"_fly_string,
.value_string = stream.dump_string(),
.description = MUST(String::formatted("Argument '@parameter_name@' type ({}) doesn't match type of previous arguments ({}).", argument_type_@parameter_index@.dump(), determined_argument_type->dump())),
});
return nullptr;
}
)~~~");
@ -326,7 +370,11 @@ RefPtr<CalculationNode const> Parser::parse_math_function(Function const& functi
if (auto consistent_type = determined_argument_type->consistent_type(argument_type_@parameter_index@); consistent_type.has_value()) {
determined_argument_type = consistent_type.release_value();
} else {
dbgln_if(CSS_PARSER_DEBUG, "@name:lowercase@() argument '@parameter_name@' type ({}) is not consistent with type of previous arguments ({})", argument_type_@parameter_index@.dump(), determined_argument_type->dump());
ErrorReporter::the().report(InvalidValueError {
.value_type = "@name:lowercase@()"_fly_string,
.value_string = stream.dump_string(),
.description = MUST(String::formatted("Argument '@parameter_name@' type ({}) is not consistent with type of previous arguments ({}).", argument_type_@parameter_index@.dump(), determined_argument_type->dump())),
});
return nullptr;
}
)~~~");