Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-css

Source of release 0.5.1

0.5.1 / src / css_syntax / types.lucb

#==============================================================================================
#
#   types - the syntax-level structures: rules, declarations, blocks, functions
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/Parser/Types.h (lines 1-111) and LibWeb/CSS/Parser/Types.cpp (lines
#       1-190), in the header's order with the .cpp bodies in place of their declarations.
#
#       The rule-list walkers report misplaced children with the engine's
#       `ErrorReporter::the().report(InvalidRuleLocationError { ... })`. The ErrorReporter
#       lives in the engine (DESIGN.md §4.1.3: Parser/ErrorReporter stays there), so the engine
#       installs `invalid_rule_location_reporter`, which this module calls with the error's
#       two names; until it is installed nothing is reported (only a debug log and the parse
#       error table observe reports).
#
#==============================================================================================

## The engine's `ErrorReporter::the().report(InvalidRuleLocationError { .outer_rule_name,
## .inner_rule_name })`, installed by the engine (the one cut of this module, DESIGN.md §4.1.3).
pub var invalid_rule_location_reporter: (func(ak.FlyString, ak.FlyString) -> unit)?

## `ErrorReporter::the().report(InvalidRuleLocationError { outer_rule_name, inner_rule_name })`.
func report_invalid_rule_location(outer_rule_name: ak.FlyString, inner_rule_name: ak.FlyString):
    if let report = invalid_rule_location_reporter:
        report(outer_rule_name, inner_rule_name)

## `MUST(String::formatted("@{}", name))` as the FlyString an InvalidRuleLocationError holds.
func at_rule_name_for_error(name: ak.FlyString) -> ak.FlyString:
    return ak.fly_string_from_string(string_formatted("@{}", [ak.arg_string_view(ak.fly_string_bytes_as_string_view(&name))]))

# mark: AtRule ==================================================================================

## Ported from Libraries/LibWeb/CSS/Parser/Types.cpp:61 Web::CSS::Parser::AtRule::for_each.
pub func at_rule_for_each(this: const AtRule*, visit_at_rule: ak.Function1[const AtRule*, unit], visit_qualified_rule: ak.Function1[const QualifiedRule*, unit], visit_declaration: ak.Function1[const Declaration*, unit]):
    for child in ak.vector_span_const(&this.child_rules_and_lists_of_declarations):
        match child:
            .rule(rule):
                match rule:
                    .at_rule(at_rule): ak.function1_call(&visit_at_rule, &at_rule)
                    .qualified_rule(qualified_rule): ak.function1_call(&visit_qualified_rule, &qualified_rule)
            .vector_declaration(declarations):
                let list = ak.vector_span_const(&declarations)
                for declaration in &list:
                    ak.function1_call(&visit_declaration, declaration)

## The captures of AtRule::for_each_as_*'s error-reporting lambdas: `[this]`.
struct AtRuleForEachReportCaptures:
    var this: const AtRule*

## The lambda that reports a nested at-rule: `@name` inside `@at_rule.name`.
func at_rule_for_each_report_at_rule(context: void*?, at_rule: const AtRule*):
    let captures = (AtRuleForEachReportCaptures*)(context else trap("no captures"))
    report_invalid_rule_location(at_rule_name_for_error(captures.this.name), at_rule_name_for_error(at_rule.name))

## The lambda that reports a nested qualified rule.
func at_rule_for_each_report_qualified_rule(context: void*?, qualified_rule: const QualifiedRule*):
    _ = qualified_rule
    let captures = (AtRuleForEachReportCaptures*)(context else trap("no captures"))
    report_invalid_rule_location(at_rule_name_for_error(captures.this.name), ak.fly_string_from_str("qualified-rule"))

## The lambda that reports a nested list of declarations.
func at_rule_for_each_report_declaration(context: void*?, declaration: const Declaration*):
    _ = declaration
    let captures = (AtRuleForEachReportCaptures*)(context else trap("no captures"))
    report_invalid_rule_location(at_rule_name_for_error(captures.this.name), ak.fly_string_from_str("list-of-declarations"))

## Ported from Libraries/LibWeb/CSS/Parser/Types.cpp:78 Web::CSS::Parser::AtRule::for_each_as_declaration_list.
# https://drafts.csswg.org/css-syntax/#typedef-declaration-list
pub func at_rule_for_each_as_declaration_list(this: const AtRule*, visit: ak.Function1[const Declaration*, unit]):
    # <declaration-list>: only declarations are allowed; at-rules and qualified rules are automatically invalid.
    var captures = AtRuleForEachReportCaptures(this = this)
    at_rule_for_each(this,
                     ak.Function1[const AtRule*, unit](call = at_rule_for_each_report_at_rule, context = &captures),
                     ak.Function1[const QualifiedRule*, unit](call = at_rule_for_each_report_qualified_rule, context = &captures),
                     visit)

## Ported from Libraries/LibWeb/CSS/Parser/Types.cpp:98 Web::CSS::Parser::AtRule::for_each_as_qualified_rule_list.
# https://drafts.csswg.org/css-syntax/#typedef-qualified-rule-list
pub func at_rule_for_each_as_qualified_rule_list(this: const AtRule*, visit: ak.Function1[const QualifiedRule*, unit]):
    # <qualified-rule-list>: only qualified rules are allowed; declarations and at-rules are automatically invalid.
    var captures = AtRuleForEachReportCaptures(this = this)
    at_rule_for_each(this,
                     ak.Function1[const AtRule*, unit](call = at_rule_for_each_report_at_rule, context = &captures),
                     visit,
                     ak.Function1[const Declaration*, unit](call = at_rule_for_each_report_declaration, context = &captures))

## Ported from Libraries/LibWeb/CSS/Parser/Types.cpp:118 Web::CSS::Parser::AtRule::for_each_as_at_rule_list.
# https://drafts.csswg.org/css-syntax/#typedef-at-rule-list
pub func at_rule_for_each_as_at_rule_list(this: const AtRule*, visit: ak.Function1[const AtRule*, unit]):
    # <at-rule-list>: only at-rules are allowed; declarations and qualified rules are automatically invalid.
    var captures = AtRuleForEachReportCaptures(this = this)
    at_rule_for_each(this,
                     visit,
                     ak.Function1[const QualifiedRule*, unit](call = at_rule_for_each_report_qualified_rule, context = &captures),
                     ak.Function1[const Declaration*, unit](call = at_rule_for_each_report_declaration, context = &captures))

## Ported from Libraries/LibWeb/CSS/Parser/Types.cpp:138 Web::CSS::Parser::AtRule::for_each_as_declaration_rule_list.
# https://drafts.csswg.org/css-syntax/#typedef-declaration-rule-list
pub func at_rule_for_each_as_declaration_rule_list(this: const AtRule*, visit_at_rule: ak.Function1[const AtRule*, unit], visit_declaration: ak.Function1[const Declaration*, unit]):
    # <declaration-rule-list>: declarations and at-rules are allowed; qualified rules are automatically invalid.
    var captures = AtRuleForEachReportCaptures(this = this)
    at_rule_for_each(this,
                     visit_at_rule,
                     ak.Function1[const QualifiedRule*, unit](call = at_rule_for_each_report_qualified_rule, context = &captures),
                     visit_declaration)

## Ported from Libraries/LibWeb/CSS/Parser/Types.cpp:153 Web::CSS::Parser::AtRule::for_each_as_rule_list.
# https://drafts.csswg.org/css-syntax/#typedef-rule-list
pub func at_rule_for_each_as_rule_list(this: const AtRule*, visit: ak.Function1[const Rule*, unit]):
    # <rule-list>: qualified rules and at-rules are allowed; declarations are automatically invalid.
    for child in ak.vector_span_const(&this.child_rules_and_lists_of_declarations):
        match child:
            .rule(rule):
                ak.function1_call(&visit, &rule)
            .vector_declaration(_):
                report_invalid_rule_location(at_rule_name_for_error(this.name), ak.fly_string_from_str("list-of-declarations"))

# mark: QualifiedRule ===========================================================================

## Ported from Libraries/LibWeb/CSS/Parser/Types.cpp:169 Web::CSS::Parser::QualifiedRule::for_each_as_declaration_list.
# https://drafts.csswg.org/css-syntax/#typedef-declaration-list
pub func qualified_rule_for_each_as_declaration_list(this: const QualifiedRule*, rule_name: ak.FlyString, visit: ak.Function1[const Declaration*, unit]):
    # <declaration-list>: only declarations are allowed; at-rules and qualified rules are automatically invalid.
    let own_declarations = ak.vector_span_const(&this.declarations)
    for declaration in &own_declarations:
        ak.function1_call(&visit, declaration)

    for child in ak.vector_span_const(&this.child_rules):
        match child:
            .rule(_):
                report_invalid_rule_location(rule_name, ak.fly_string_from_str("qualified-rule"))
            .vector_declaration(declarations):
                let child_declarations = ak.vector_span_const(&declarations)
                for declaration in &child_declarations:
                    ak.function1_call(&visit, declaration)

# mark: SubstitutionFunctionsPresence ===========================================================

## Ported from Libraries/LibWeb/CSS/Parser/Types.h:70 Web::CSS::Parser::SubstitutionFunctionsPresence::has_any.
pub func substitution_functions_presence_has_any(this: const SubstitutionFunctionsPresence*) -> bool:
    return this.attr or this.env or this.if_ or this.inherit or this.var_

# mark: SimpleBlock =============================================================================

## Ported from Libraries/LibWeb/CSS/Parser/Types.h:79 Web::CSS::Parser::SimpleBlock::is_curly.
pub func simple_block_is_curly(this: const SimpleBlock*) -> bool:
    return token_is(&this.token, TokenType.open_curly)

## Ported from Libraries/LibWeb/CSS/Parser/Types.h:80 Web::CSS::Parser::SimpleBlock::is_paren.
pub func simple_block_is_paren(this: const SimpleBlock*) -> bool:
    return token_is(&this.token, TokenType.open_paren)

## Ported from Libraries/LibWeb/CSS/Parser/Types.h:81 Web::CSS::Parser::SimpleBlock::is_square.
pub func simple_block_is_square(this: const SimpleBlock*) -> bool:
    return token_is(&this.token, TokenType.open_square)

## Ported from Libraries/LibWeb/CSS/Parser/Types.cpp:15 Web::CSS::Parser::SimpleBlock::to_string.
pub func simple_block_to_string(this: const SimpleBlock*) -> ak.String:
    var builder = ak.string_builder_new()

    ak.string_builder_append_string_view(&builder, token_bracket_string(&this.token))
    # builder.join(""sv, value): each component value through Formatter<ComponentValue>.
    let values = ak.vector_span_const(&this.value)
    for component_value in &values:
        let string = component_value_to_string(component_value)
        string_builder_append_string(&builder, &string)
    ak.string_builder_append_string_view(&builder, token_bracket_mirror_string(&this.token))

    return ak.string_builder_to_string_without_validation(&builder)

## Ported from Libraries/LibWeb/CSS/Parser/Types.cpp:26 Web::CSS::Parser::SimpleBlock::original_source_text.
pub func simple_block_original_source_text(this: const SimpleBlock*) -> ak.String:
    var builder = ak.string_builder_new()
    string_builder_append_string(&builder, &this.token.m_original_source_text)
    let values = ak.vector_span_const(&this.value)
    for component_value in &values:
        let text = component_value_original_source_text(component_value)
        string_builder_append_string(&builder, &text)
    string_builder_append_string(&builder, &this.end_token.m_original_source_text)
    return ak.string_builder_to_string_without_validation(&builder)

## Ported from Libraries/LibWeb/CSS/Parser/Types.h:86 Web::CSS::Parser::SimpleBlock::operator==.
pub func simple_block_eq(this: const SimpleBlock*, other: SimpleBlock) -> bool:
    return token_eq(&this.token, other.token) and component_values_eq(&this.value, &other.value)

# mark: Function ================================================================================

## Ported from Libraries/LibWeb/CSS/Parser/Types.cpp:37 Web::CSS::Parser::Function::to_string.
pub func function_to_string(this: const Function*) -> ak.String:
    var builder = ak.string_builder_new()

    serialize_an_identifier_string_builder(&builder, ak.fly_string_bytes_as_string_view(&this.name))
    ak.string_builder_append_u8(&builder, '(')
    let values = ak.vector_span_const(&this.value)
    for item in &values:
        let string = component_value_to_string(item)
        string_builder_append_string(&builder, &string)
    ak.string_builder_append_u8(&builder, ')')

    return ak.string_builder_to_string_without_validation(&builder)

## Ported from Libraries/LibWeb/CSS/Parser/Types.cpp:50 Web::CSS::Parser::Function::original_source_text.
pub func function_original_source_text(this: const Function*) -> ak.String:
    var builder = ak.string_builder_new()
    string_builder_append_string(&builder, &this.name_token.m_original_source_text)
    let values = ak.vector_span_const(&this.value)
    for component_value in &values:
        let text = component_value_original_source_text(component_value)
        string_builder_append_string(&builder, &text)
    string_builder_append_string(&builder, &this.end_token.m_original_source_text)
    return ak.string_builder_to_string_without_validation(&builder)

## Ported from Libraries/LibWeb/CSS/Parser/Types.h:99 Web::CSS::Parser::Function::operator==.
pub func function_eq(this: const Function*, other: Function) -> bool:
    return ak.fly_string_eq_fly_string(&this.name, &other.name) and component_values_eq(&this.value, &other.value)

# mark: GuaranteedInvalidValue ==================================================================

## Ported from Libraries/LibWeb/CSS/Parser/Types.h:105 Web::CSS::Parser::GuaranteedInvalidValue::to_string.
pub func guaranteed_invalid_value_to_string(this: const GuaranteedInvalidValue*) -> ak.String:
    _ = this
    return ak.string_new()

## Ported from Libraries/LibWeb/CSS/Parser/Types.h:106 Web::CSS::Parser::GuaranteedInvalidValue::original_source_text.
pub func guaranteed_invalid_value_original_source_text(this: const GuaranteedInvalidValue*) -> ak.String:
    _ = this
    return ak.string_new()