Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / css / parser / error_reporter.lucb

#==============================================================================================
#
#   error_reporter - CSS parsing errors: their hashes, their messages and the ErrorReporter
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/Parser/ErrorReporter.h (lines 1-152: the error structs' hash()
#       members, the defaulted operator== of the ParsingError variant and its Traits) and
#       ErrorReporter.cpp (lines 1-76).
#
#       The reporter is a function-local static in C++ that owns its memory. Here it is a module
#       variable, which the collector does not scan, so its table and the Strings of the errors
#       it keeps are allocated in the C heap (`memory.heap`); see report().
#
#       Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## AK/Debug.h's CSS_PARSER_DEBUG (0 in the donor's build).
let css_parser_debug: bool = false

# mark: The errors' hashes =======================================================================

## Ported from Libraries/LibWeb/CSS/Parser/ErrorReporter.h:20 Web::CSS::Parser::UnknownPropertyError::hash.
pub func css_unknown_property_error_hash(this: const CssUnknownPropertyError*) -> u32:
    return ak.pair_int_hash(ak.fly_string_hash(&this.rule_name), ak.fly_string_hash(&this.property_name))

## Ported from Libraries/LibWeb/CSS/Parser/ErrorReporter.h:29 Web::CSS::Parser::UnknownRuleError::hash.
pub func css_unknown_rule_error_hash(this: const CssUnknownRuleError*) -> u32:
    return ak.fly_string_hash(&this.rule_name)

## Ported from Libraries/LibWeb/CSS/Parser/ErrorReporter.h:35 Web::CSS::Parser::UnknownMediaFeatureError::hash.
pub func css_unknown_media_feature_error_hash(this: const CssUnknownMediaFeatureError*) -> u32:
    return ak.fly_string_hash(&this.media_feature_name)

## Ported from Libraries/LibWeb/CSS/Parser/ErrorReporter.h:42 Web::CSS::Parser::UnknownPseudoClassOrElementError::hash.
pub func css_unknown_pseudo_class_or_element_error_hash(this: const CssUnknownPseudoClassOrElementError*) -> u32:
    return ak.pair_int_hash(ak.fly_string_hash(&this.rule_name), ak.fly_string_hash(&this.name))

## Ported from Libraries/LibWeb/CSS/Parser/ErrorReporter.h:54 Web::CSS::Parser::InvalidPropertyError::hash.
pub func css_invalid_property_error_hash(this: const CssInvalidPropertyError*) -> u32:
    return ak.pair_int_hash(ak.fly_string_hash(&this.rule_name), ak.pair_int_hash(ak.fly_string_hash(&this.property_name), ak.pair_int_hash(ak.string_hash_value(&this.value_string), ak.string_hash_value(&this.description))))

## Ported from Libraries/LibWeb/CSS/Parser/ErrorReporter.h:65 Web::CSS::Parser::InvalidValueError::hash.
pub func css_invalid_value_error_hash(this: const CssInvalidValueError*) -> u32:
    return ak.pair_int_hash(ak.fly_string_hash(&this.value_type), ak.pair_int_hash(ak.string_hash_value(&this.value_string), ak.string_hash_value(&this.description)))

## Ported from Libraries/LibWeb/CSS/Parser/ErrorReporter.h:76 Web::CSS::Parser::InvalidRuleError::hash.
pub func css_invalid_rule_error_hash(this: const CssInvalidRuleError*) -> u32:
    return ak.pair_int_hash(ak.fly_string_hash(&this.rule_name), ak.pair_int_hash(ak.string_hash_value(&this.prelude), ak.string_hash_value(&this.description)))

## Ported from Libraries/LibWeb/CSS/Parser/ErrorReporter.h:87 Web::CSS::Parser::InvalidQueryError::hash.
pub func css_invalid_query_error_hash(this: const CssInvalidQueryError*) -> u32:
    return ak.pair_int_hash(ak.fly_string_hash(&this.query_type), ak.pair_int_hash(ak.string_hash_value(&this.value_string), ak.string_hash_value(&this.description)))

## Ported from Libraries/LibWeb/CSS/Parser/ErrorReporter.h:98 Web::CSS::Parser::InvalidSelectorError::hash.
pub func css_invalid_selector_error_hash(this: const CssInvalidSelectorError*) -> u32:
    return ak.pair_int_hash(ak.fly_string_hash(&this.rule_name), ak.pair_int_hash(ak.string_hash_value(&this.value_string), ak.string_hash_value(&this.description)))

## Ported from Libraries/LibWeb/CSS/Parser/ErrorReporter.h:109 Web::CSS::Parser::InvalidPseudoClassOrElementError::hash.
pub func css_invalid_pseudo_class_or_element_error_hash(this: const CssInvalidPseudoClassOrElementError*) -> u32:
    return ak.pair_int_hash(ak.fly_string_hash(&this.name), ak.pair_int_hash(ak.string_hash_value(&this.value_string), ak.string_hash_value(&this.description)))

## Ported from Libraries/LibWeb/CSS/Parser/ErrorReporter.h:119 Web::CSS::Parser::InvalidRuleLocationError::hash.
pub func css_invalid_rule_location_error_hash(this: const CssInvalidRuleLocationError*) -> u32:
    return ak.pair_int_hash(ak.fly_string_hash(&this.outer_rule_name), ak.fly_string_hash(&this.inner_rule_name))

## Ported from Libraries/LibWeb/CSS/Parser/ErrorReporter.h:146 AK::Traits<Web::CSS::Parser::ParsingError>::hash:
## `error.visit([](auto const& it) { return it.hash(); })`.
pub func css_parsing_error_hash(error_: const CssParsingError*) -> u32:
    match *error_:
        .css_unknown_property_error(it): return css_unknown_property_error_hash(&it)
        .css_unknown_rule_error(it): return css_unknown_rule_error_hash(&it)
        .css_unknown_media_feature_error(it): return css_unknown_media_feature_error_hash(&it)
        .css_unknown_pseudo_class_or_element_error(it): return css_unknown_pseudo_class_or_element_error_hash(&it)
        .css_invalid_property_error(it): return css_invalid_property_error_hash(&it)
        .css_invalid_value_error(it): return css_invalid_value_error_hash(&it)
        .css_invalid_rule_error(it): return css_invalid_rule_error_hash(&it)
        .css_invalid_query_error(it): return css_invalid_query_error_hash(&it)
        .css_invalid_selector_error(it): return css_invalid_selector_error_hash(&it)
        .css_invalid_pseudo_class_or_element_error(it): return css_invalid_pseudo_class_or_element_error_hash(&it)
        .css_invalid_rule_location_error(it): return css_invalid_rule_location_error_hash(&it)

## The ParsingError variant's operator== (AK::Variant: the same alternative, and that
## alternative's defaulted `operator==`, member by member).
pub func css_parsing_error_eq(a: const CssParsingError*, b: const CssParsingError*) -> bool:
    match *a:
        .css_unknown_property_error(x):
            match *b:
                .css_unknown_property_error(y): return fly_strings_eq(&x.rule_name, &y.rule_name) and fly_strings_eq(&x.property_name, &y.property_name)
                _: return false
        .css_unknown_rule_error(x):
            match *b:
                .css_unknown_rule_error(y): return fly_strings_eq(&x.rule_name, &y.rule_name)
                _: return false
        .css_unknown_media_feature_error(x):
            match *b:
                .css_unknown_media_feature_error(y): return fly_strings_eq(&x.media_feature_name, &y.media_feature_name)
                _: return false
        .css_unknown_pseudo_class_or_element_error(x):
            match *b:
                .css_unknown_pseudo_class_or_element_error(y): return fly_strings_eq(&x.rule_name, &y.rule_name) and fly_strings_eq(&x.name, &y.name)
                _: return false
        .css_invalid_property_error(x):
            match *b:
                .css_invalid_property_error(y):
                    return (fly_strings_eq(&x.rule_name, &y.rule_name) and fly_strings_eq(&x.property_name, &y.property_name) and ak.string_equals(&x.value_string, &y.value_string) and ak.string_equals(&x.description, &y.description))
                _: return false
        .css_invalid_value_error(x):
            match *b:
                .css_invalid_value_error(y):
                    return (fly_strings_eq(&x.value_type, &y.value_type) and ak.string_equals(&x.value_string, &y.value_string) and ak.string_equals(&x.description, &y.description))
                _: return false
        .css_invalid_rule_error(x):
            match *b:
                .css_invalid_rule_error(y):
                    return (fly_strings_eq(&x.rule_name, &y.rule_name) and ak.string_equals(&x.prelude, &y.prelude) and ak.string_equals(&x.description, &y.description))
                _: return false
        .css_invalid_query_error(x):
            match *b:
                .css_invalid_query_error(y):
                    return (fly_strings_eq(&x.query_type, &y.query_type) and ak.string_equals(&x.value_string, &y.value_string) and ak.string_equals(&x.description, &y.description))
                _: return false
        .css_invalid_selector_error(x):
            match *b:
                .css_invalid_selector_error(y):
                    return (fly_strings_eq(&x.rule_name, &y.rule_name) and ak.string_equals(&x.value_string, &y.value_string) and ak.string_equals(&x.description, &y.description))
                _: return false
        .css_invalid_pseudo_class_or_element_error(x):
            match *b:
                .css_invalid_pseudo_class_or_element_error(y):
                    return (fly_strings_eq(&x.name, &y.name) and ak.string_equals(&x.value_string, &y.value_string) and ak.string_equals(&x.description, &y.description))
                _: return false
        .css_invalid_rule_location_error(x):
            match *b:
                .css_invalid_rule_location_error(y): return fly_strings_eq(&x.outer_rule_name, &y.outer_rule_name) and fly_strings_eq(&x.inner_rule_name, &y.inner_rule_name)
                _: return false

## FlyString's operator== (identity of the interned string).
func fly_strings_eq(a: const ak.FlyString*, b: const ak.FlyString*) -> bool:
    return ak.fly_string_eq_fly_string(a, b)

# mark: serialize_parsing_error =================================================================

## Ported from Libraries/LibWeb/CSS/Parser/ErrorReporter.cpp:12 Web::CSS::Parser::serialize_parsing_error.
pub func css_serialize_parsing_error(error_: const CssParsingError*) -> ak.String:
    match *error_:
        .css_unknown_property_error(it):
            return css_string_formatted("Unknown property '{}' in {} rule.", [css_arg_fly_string(&it.property_name), css_arg_fly_string(&it.rule_name)])
        .css_unknown_rule_error(it):
            return css_string_formatted("Unknown rule '{}'.", [css_arg_fly_string(&it.rule_name)])
        .css_unknown_media_feature_error(it):
            return css_string_formatted("Unknown media feature '{}'.", [css_arg_fly_string(&it.media_feature_name)])
        .css_unknown_pseudo_class_or_element_error(it):
            return css_string_formatted("Unknown pseudo class or element '{}' in {} selector.", [css_arg_fly_string(&it.name), css_arg_fly_string(&it.rule_name)])
        .css_invalid_property_error(it):
            return css_string_formatted("Property '{}' in {} rule has invalid value `{}`.", [css_arg_fly_string(&it.property_name), css_arg_fly_string(&it.rule_name), css_arg_string(&it.value_string)])
        .css_invalid_value_error(it):
            return css_string_formatted("Unable to parse {} from `{}`: {}", [css_arg_fly_string(&it.value_type), css_arg_string(&it.value_string), css_arg_string(&it.description)])
        .css_invalid_rule_error(it):
            return css_string_formatted("'{}' rule with prelude `{}` is invalid: {}", [css_arg_fly_string(&it.rule_name), css_arg_string(&it.prelude), css_arg_string(&it.description)])
        .css_invalid_query_error(it):
            return css_string_formatted("'{}' query `{}` is invalid: {}", [css_arg_fly_string(&it.query_type), css_arg_string(&it.value_string), css_arg_string(&it.description)])
        .css_invalid_selector_error(it):
            return css_string_formatted("{} selector `{}` is invalid: {}", [css_arg_fly_string(&it.rule_name), css_arg_string(&it.value_string), css_arg_string(&it.description)])
        .css_invalid_pseudo_class_or_element_error(it):
            return css_string_formatted("Pseudo '{}' value `{}` is invalid: {}", [css_arg_fly_string(&it.name), css_arg_string(&it.value_string), css_arg_string(&it.description)])
        .css_invalid_rule_location_error(it):
            return css_string_formatted("'{}' rule is invalid inside {}", [css_arg_fly_string(&it.inner_rule_name), css_arg_fly_string(&it.outer_rule_name)])

# mark: ErrorReporter ===========================================================================

## `static ErrorReporter s_error_reporter {}` of ErrorReporter::the().
var css_error_reporter_s_error_reporter: CssErrorReporter

## Ported from Libraries/LibWeb/CSS/Parser/ErrorReporter.cpp:50 Web::CSS::Parser::ErrorReporter::the.
pub func css_error_reporter_the() -> CssErrorReporter*:
    return &css_error_reporter_s_error_reporter

## Ported from Libraries/LibWeb/CSS/Parser/ErrorReporter.cpp:56 Web::CSS::Parser::ErrorReporter::report.
pub func css_error_reporter_report(this: CssErrorReporter*, error_: CssParsingError):
    if let existing = ak.hash_map_get_k(&this.m_errors, error_):
        existing.occurrences += 1
        return

    if css_parser_debug:
        let message = css_serialize_parsing_error(&error_)
        ak.dbgln(f"CSS parsing error: {ak.string_view_text(ak.string_bytes_as_string_view(&message))}")
    # The table lives as long as the program, as the C++ static does: its storage and the error's
    # Strings are made in the C heap, which the collector neither scans nor sweeps, with ak's
    # atomic allocator unset (the Strings' bytes would otherwise go to the newest VM's heap).
    let saved_atomic_allocator = ak.atomic_allocator
    ak.atomic_allocator = none
    defer ak.atomic_allocator = saved_atomic_allocator
    with memory.heap:
        _ = ak.hash_map_set(&this.m_errors, css_parsing_error_copy(&error_), CssErrorReporterMetadata(occurrences = 1))

## A copy of `error` whose Strings are made with the current allocator (the C++ moves the error
## into the reporter's table, which owns it).
func css_parsing_error_copy(error_: const CssParsingError*) -> CssParsingError:
    match *error_:
        .css_invalid_property_error(it):
            return CssParsingError.css_invalid_property_error(CssInvalidPropertyError(rule_name = it.rule_name, property_name = it.property_name,
                                                                                      value_string = css_string_copy(&it.value_string), description = css_string_copy(&it.description)))
        .css_invalid_value_error(it):
            return CssParsingError.css_invalid_value_error(CssInvalidValueError(value_type = it.value_type,
                                                                                value_string = css_string_copy(&it.value_string), description = css_string_copy(&it.description)))
        .css_invalid_rule_error(it):
            return CssParsingError.css_invalid_rule_error(CssInvalidRuleError(rule_name = it.rule_name,
                                                                              prelude = css_string_copy(&it.prelude), description = css_string_copy(&it.description)))
        .css_invalid_query_error(it):
            return CssParsingError.css_invalid_query_error(CssInvalidQueryError(query_type = it.query_type,
                                                                                value_string = css_string_copy(&it.value_string), description = css_string_copy(&it.description)))
        .css_invalid_selector_error(it):
            return CssParsingError.css_invalid_selector_error(CssInvalidSelectorError(rule_name = it.rule_name,
                                                                                      value_string = css_string_copy(&it.value_string), description = css_string_copy(&it.description)))
        .css_invalid_pseudo_class_or_element_error(it):
            return CssParsingError.css_invalid_pseudo_class_or_element_error(CssInvalidPseudoClassOrElementError(name = it.name,
                                                                                                                 value_string = css_string_copy(&it.value_string), description = css_string_copy(&it.description)))
        _:
            # The other errors hold only FlyStrings, which are interned.
            return *error_

## A String with the same bytes, made with the current allocator.
func css_string_copy(string: const ak.String*) -> ak.String:
    return ak.string_from_utf8(ak.string_bytes_as_string_view(string)) else trap("MUST: String::from_utf8")

## Ported from Libraries/LibWeb/CSS/Parser/ErrorReporter.cpp:67 Web::CSS::Parser::ErrorReporter::dump.
pub func css_error_reporter_dump(this: const CssErrorReporter*):
    # TODO: Organise this in some way?
    ak.dbgln(f"{ak.hash_map_size(&this.m_errors)} CSS errors reported:")
    for entry in ak.hash_map_begin_const(&this.m_errors):
        let message = css_serialize_parsing_error(&entry.key)
        ak.dbgln(f"- {ak.string_view_text(ak.string_bytes_as_string_view(&message))} ({entry.value.occurrences} occurrences)")

## `ErrorReporter::the().report(InvalidRuleLocationError { .outer_rule_name, .inner_rule_name })`
## for css_syntax's rule walkers (its `invalid_rule_location_reporter` hook, DESIGN.md ยง4.1.3).
func css_report_invalid_rule_location(outer_rule_name: ak.FlyString, inner_rule_name: ak.FlyString):
    css_error_reporter_report(css_error_reporter_the(), CssParsingError.css_invalid_rule_location_error(CssInvalidRuleLocationError(outer_rule_name = outer_rule_name, inner_rule_name = inner_rule_name)))