Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / css / parser / syntax_parsing.lucb

#==============================================================================================
#
#   syntax_parsing - parsing a CSS <syntax> definition, and parsing values with one
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/Parser/SyntaxParsing.cpp (lines 1-355): <syntax-single-component>,
#       <syntax-multiplier>, <syntax-component>, <syntax-combinator>, parse_as_syntax, and
#       "parse with a <syntax>" (the free function and the Parser members). The
#       LimitSingleComponentIdentToCustomIdent enum of SyntaxParsing.h is in
#       types_syntax_parsing.
#
#       Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Parsing a <syntax> ======================================================================

## Ported from Libraries/LibWeb/CSS/Parser/SyntaxParsing.cpp:23 Web::CSS::Parser::parse_syntax_single_component.
func css_parse_syntax_single_component(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, limit_single_component_ident_to_custom_ident: CssLimitSingleComponentIdentToCustomIdent) -> CssSyntaxNode*?:
    # <syntax-single-component> = '<' <syntax-type-name> '>' | <ident>
    # <syntax-type-name> = angle | color | custom-ident | image | integer
    #                    | length | length-percentage | number
    #                    | percentage | resolution | string | time
    #                    | url | transform-function

    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)
    css_syntax.token_stream_discard_whitespace(tokens)

    # <ident>
    let next = css_syntax.token_stream_next_token(tokens)
    if css_syntax.component_value_is(&next, .ident):
        let ident_token = css_syntax.token_stream_consume_a_token(tokens)
        let ident = css_component_value_ident(&ident_token)

        # AD-HOC: Some users (i.e. the @property syntax descriptor) only allow custom idents here,
        #         https://github.com/w3c/csswg-drafts/issues/13614
        var no_blacklist: const ak.StringView[]
        if limit_single_component_ident_to_custom_ident == CssLimitSingleComponentIdentToCustomIdent.yes and not is_valid_custom_ident(ident, no_blacklist):
            return none

        css_syntax.state_transaction_commit(&transaction)
        var case_sensitivity = ak.CaseSensitivity.case_insensitive
        if limit_single_component_ident_to_custom_ident == CssLimitSingleComponentIdentToCustomIdent.yes:
            case_sensitivity = ak.CaseSensitivity.case_sensitive
        return css_ident_syntax_node_create(ident, case_sensitivity).css_syntax_node()

    # '<' <syntax-type-name> '>'
    if css_syntax.component_value_is_delim(&next, (u32)'<'):
        css_syntax.token_stream_discard_a_token(tokens)  # '<'
        let type_name = css_syntax.token_stream_consume_a_token(tokens)
        let end_token = css_syntax.token_stream_consume_a_token(tokens)

        if css_syntax.component_value_is_delim(&end_token, (u32)'>') and css_syntax.component_value_is(&type_name, .ident):
            let name = css_component_value_ident(&type_name)
            if css_fly_string_is_one_of(&name, ["angle", "color", "custom-ident", "image", "integer", "length", "length-percentage", "number",
                                                "percentage", "resolution", "string", "time", "url", "transform-function"]):
                css_syntax.state_transaction_commit(&transaction)
                return css_type_syntax_node_create(name).css_syntax_node()

    return none

## Ported from Libraries/LibWeb/CSS/Parser/SyntaxParsing.cpp:77 Web::CSS::Parser::parse_syntax_multiplier.
func css_parse_syntax_multiplier(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> u8?:
    # <syntax-multiplier> = [ '#' | '+' ]
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)

    let delim = css_syntax.token_stream_consume_a_token(tokens)
    if css_syntax.component_value_is_delim(&delim, (u32)'#') or css_syntax.component_value_is_delim(&delim, (u32)'+'):
        css_syntax.state_transaction_commit(&transaction)
        let token = css_syntax.component_value_token(&delim)
        return (u8)css_syntax.token_delim(&token)

    return none

## Ported from Libraries/LibWeb/CSS/Parser/SyntaxParsing.cpp:91 Web::CSS::Parser::parse_syntax_component.
pub func css_parse_syntax_component_token_stream(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, limit_single_component_ident_to_custom_ident: CssLimitSingleComponentIdentToCustomIdent = CssLimitSingleComponentIdentToCustomIdent.no) -> CssSyntaxNode*?:
    # <syntax-component> = <syntax-single-component> <syntax-multiplier>?
    #                    | '<' transform-list '>'

    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)

    css_syntax.token_stream_discard_whitespace(tokens)

    # '<' transform-list '>'
    let next = css_syntax.token_stream_next_token(tokens)
    if css_syntax.component_value_is_delim(&next, (u32)'<'):
        var transform_list_transaction = css_syntax.state_transaction_create_child(&transaction)
        defer css_syntax.state_transaction_destroy(&transform_list_transaction)
        css_syntax.token_stream_discard_a_token(tokens)  # '<'
        let ident_token = css_syntax.token_stream_consume_a_token(tokens)
        let end_token = css_syntax.token_stream_consume_a_token(tokens)

        if css_syntax.component_value_is_ident(&ident_token, ak.sv("transform-list")) and css_syntax.component_value_is_delim(&end_token, (u32)'>'):
            css_syntax.state_transaction_commit(&transform_list_transaction)
            return css_type_syntax_node_create(ak.fly_string_from_str("transform-list")).css_syntax_node()

    # <syntax-single-component> <syntax-multiplier>?
    let syntax_single_component = css_parse_syntax_single_component(tokens, limit_single_component_ident_to_custom_ident) else return none

    let maybe_multiplier = css_parse_syntax_multiplier(tokens)
    if maybe_multiplier == none:
        css_syntax.state_transaction_commit(&transaction)
        return syntax_single_component

    match (maybe_multiplier else trap("VERIFY: a multiplier")):
        '#':
            css_syntax.state_transaction_commit(&transaction)
            return css_comma_separated_multiplier_syntax_node_create(syntax_single_component).css_syntax_node()
        '+':
            css_syntax.state_transaction_commit(&transaction)
            return css_multiplier_syntax_node_create(syntax_single_component).css_syntax_node()
        _:
            return none

## Ported from Libraries/LibWeb/CSS/Parser/SyntaxParsing.cpp:136 Web::CSS::Parser::parse_syntax_combinator.
func css_parse_syntax_combinator(tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> u8?:
    # <syntax-combinator> = '|'
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)
    css_syntax.token_stream_discard_whitespace(tokens)

    let delim = css_syntax.token_stream_consume_a_token(tokens)
    if css_syntax.component_value_is_delim(&delim, (u32)'|'):
        css_syntax.state_transaction_commit(&transaction)
        let token = css_syntax.component_value_token(&delim)
        return (u8)css_syntax.token_delim(&token)

    return none

## Ported from Libraries/LibWeb/CSS/Parser/SyntaxParsing.cpp:152 Web::CSS::Parser::parse_as_syntax.
# https://drafts.csswg.org/css-values-5/#typedef-syntax
pub func css_parse_as_syntax(component_values: ak.Vector[css_syntax.ComponentValue], limit_single_component_ident_to_custom_ident: CssLimitSingleComponentIdentToCustomIdent = CssLimitSingleComponentIdentToCustomIdent.no) -> CssSyntaxNode*?:
    # <syntax> = '*' | <syntax-component> [ <syntax-combinator> <syntax-component> ]* | <syntax-string>
    # <syntax-component> = <syntax-single-component> <syntax-multiplier>?
    #                    | '<' transform-list '>'
    # <syntax-single-component> = '<' <syntax-type-name> '>' | <ident>
    # <syntax-type-name> = angle | color | custom-ident | image | integer
    #                    | length | length-percentage | number
    #                    | percentage | resolution | string | time
    #                    | url | transform-function
    # <syntax-combinator> = '|'
    # <syntax-multiplier> = [ '#' | '+' ]
    #
    # <syntax-string> = <string>
    # FIXME: Eventually, extend this to also parse *any* CSS grammar, not just for the <syntax> type.

    var tokens = css_syntax.token_stream_create(component_values)
    css_syntax.token_stream_discard_whitespace(&tokens)

    # '*'
    let next = css_syntax.token_stream_next_token(&tokens)
    if css_syntax.component_value_is_delim(&next, (u32)'*'):
        css_syntax.token_stream_discard_a_token(&tokens)  # '*'
        css_syntax.token_stream_discard_whitespace(&tokens)
        if css_syntax.token_stream_has_next_token(&tokens):
            return none
        return css_universal_syntax_node_create().css_syntax_node()

    # <syntax-string> = <string>
    # A <syntax-string> is a <string> whose value successfully parses as a <syntax>, and represents the same value as
    # that <syntax> would.
    # NB: For now, this is the only time a string is allowed in a <syntax>.
    if css_syntax.component_value_is(&next, .string):
        let string_value = css_syntax.token_stream_consume_a_token(&tokens)
        let string_token = css_syntax.component_value_token(&string_value)
        let string = css_syntax.token_string(&string_token)
        css_syntax.token_stream_discard_whitespace(&tokens)
        if css_syntax.token_stream_has_next_token(&tokens):
            return none

        var parsing_params: CssParsingParams = ---
        css_parsing_params_construct_css_parsing_mode(&parsing_params)
        var parser = css_parser_create(parsing_params, ak.fly_string_bytes_as_string_view(&string))
        let child_component_values = css_parser_parse_as_list_of_component_values(&parser)
        return css_parse_as_syntax(child_component_values, limit_single_component_ident_to_custom_ident)

    # <syntax-component> [ <syntax-combinator> <syntax-component> ]*
    let first = css_parse_syntax_component_token_stream(&tokens, limit_single_component_ident_to_custom_ident) else return none
    var syntax_components = ak.Vector[CssSyntaxNode*]()
    ak.vector_append_t(&syntax_components, first)

    css_syntax.token_stream_discard_whitespace(&tokens)
    while css_syntax.token_stream_has_next_token(&tokens):
        let combinator = css_parse_syntax_combinator(&tokens)
        css_syntax.token_stream_discard_whitespace(&tokens)
        let component = css_parse_syntax_component_token_stream(&tokens, limit_single_component_ident_to_custom_ident)
        css_syntax.token_stream_discard_whitespace(&tokens)
        if combinator == none or component == none:
            # dbgln("Failed parsing syntax portion, combinator = `{}`, component = `{}`", combinator, component): AK
            # formats the Optional<char> as its character or "None", and the OwnPtr as its address.
            let combinator_text = "None" if combinator == none else "|"
            var component_address: usize = 0
            if let parsed_component = component:
                component_address = (usize)parsed_component
            ak.dbgln(f"Failed parsing syntax portion, combinator = `{combinator_text}`, component = `0x{component_address:x}`")
            return none

        # FIXME: Make this logic smarter once we have more than one type of combinator.
        # For now, assume we're always making an AlternativesSyntaxNode.
        assert(combinator == '|')

        ak.vector_append_t(&syntax_components, component else trap("VERIFY: a component"))

    if ak.vector_size(&syntax_components) == 1:
        return ak.vector_take_first(&syntax_components)
    return css_alternatives_syntax_node_create(syntax_components).css_syntax_node()

# mark: Parsing with a <syntax> =================================================================

## Ported from Libraries/LibWeb/CSS/Parser/SyntaxParsing.cpp:224 Web::CSS::Parser::parse_with_a_syntax.
pub func css_parse_with_a_syntax(parsing_params: CssParsingParams, input: ak.Vector[css_syntax.ComponentValue], syntax: CssSyntaxNode*) -> CssStyleValue*:
    var parser = css_parser_create(parsing_params, ak.sv(""))
    return css_parser_parse_with_a_syntax(&parser, input, syntax)

## The captures of parse_according_to_syntax_node's `[&](auto& tokens) { return parse_according_to_syntax_node(tokens, multiplier_node.child()); }`.
struct CssParserParseAccordingToSyntaxNodeChild:
    var this: CssParser*
    var child: CssSyntaxNode*

## The lambda: parse one item of a comma-separated multiplier according to its child node.
func css_parser_parse_according_to_syntax_node_child(context: void*?, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*) -> CssStyleValue*?:
    let captures = (CssParserParseAccordingToSyntaxNodeChild*)(context else trap("no captures"))
    return css_parser_parse_according_to_syntax_node(captures.this, tokens, captures.child)

## Ported from Libraries/LibWeb/CSS/Parser/SyntaxParsing.cpp:229 Web::CSS::Parser::Parser::parse_according_to_syntax_node.
pub func css_parser_parse_according_to_syntax_node(this: CssParser*, tokens: css_syntax.TokenStream[css_syntax.ComponentValue]*, syntax_node: CssSyntaxNode*) -> CssStyleValue*?:
    var transaction = css_syntax.token_stream_begin_transaction(tokens)
    defer css_syntax.state_transaction_destroy(&transaction)

    match css_syntax_node_type(syntax_node):
        .universal:
            if let declaration_value = css_parser_parse_declaration_value(tokens):
                var substitution_functions_presence = css_syntax.SubstitutionFunctionsPresence()
                css_parser_collect_arbitrary_substitution_function_presence_vector(&declaration_value, &substitution_functions_presence) catch:
                    return none

                css_syntax.state_transaction_commit(&transaction)
                return css_unresolved_style_value_create(declaration_value, substitution_functions_presence).css_style_value()
            return none
        .ident:
            let ident_node = as_css_ident_syntax_node(syntax_node)
            css_syntax.token_stream_discard_whitespace(tokens)
            let token = css_syntax.token_stream_consume_a_token(tokens)

            if not css_syntax.component_value_is(&token, .ident):
                return none

            let ident = css_component_value_ident(&token)
            let node_ident = css_ident_syntax_node_ident(ident_node)

            var matches = false
            if css_ident_syntax_node_case_sensitivity(ident_node) == ak.CaseSensitivity.case_sensitive:
                matches = ak.fly_string_eq_fly_string(&ident, &node_ident)
            else:
                matches = ak.fly_string_equals_ignoring_ascii_case_fly_string(&ident, &node_ident)
            if matches:
                css_syntax.state_transaction_commit(&transaction)
                if let keyword = css_data.keyword_from_string(ak.fly_string_bytes_as_string_view(&node_ident)):
                    return css_keyword_style_value_create(keyword).css_style_value()
                return css_custom_ident_style_value_create(node_ident).css_style_value()
            return none
        .type_:
            let type_node = as_css_type_syntax_node(syntax_node)
            let type_name = css_type_syntax_node_type_name(type_node)
            if let value_type = css_value_type_from_string(ak.fly_string_bytes_as_string_view(&type_name)):
                if let result = css_parser_parse_value(this, value_type, tokens):
                    css_syntax.state_transaction_commit(&transaction)
                    return result
                return none

            css_error_reporter_report(css_error_reporter_the(), CssParsingError.css_invalid_value_error(CssInvalidValueError(
                value_type = ak.fly_string_from_string(css_string_formatted("<{}>", [css_arg_fly_string(&type_name)])),
                value_string = css_syntax.token_stream_dump_string(tokens),
                description = ak.string_from_str("Unknown type in <syntax>."),
            )))
            return none
        .multiplier:
            let multiplier_node = as_css_multiplier_syntax_node(syntax_node)
            var values = ak.Vector[CssStyleValue*]()
            css_syntax.token_stream_discard_whitespace(tokens)
            while css_syntax.token_stream_has_next_token(tokens):
                let parsed_child = css_parser_parse_according_to_syntax_node(this, tokens, css_multiplier_syntax_node_child(multiplier_node)) else break
                ak.vector_append_t(&values, parsed_child)
                css_syntax.token_stream_discard_whitespace(tokens)
            if ak.vector_is_empty(&values):
                return none
            css_syntax.state_transaction_commit(&transaction)
            return css_style_value_list_create(values, CssStyleValueListSeparator.space).css_style_value()
        .comma_separated_multiplier:
            let multiplier_node = as_css_comma_separated_multiplier_syntax_node(syntax_node)
            var captures = CssParserParseAccordingToSyntaxNodeChild(this = this, child = css_comma_separated_multiplier_syntax_node_child(multiplier_node))
            let result = css_parser_parse_comma_separated_value_list(this, tokens,
                                                                     ak.Function1[css_syntax.TokenStream[css_syntax.ComponentValue]*, CssStyleValue*?](
                call = css_parser_parse_according_to_syntax_node_child, context = &captures)) else return none
            css_syntax.state_transaction_commit(&transaction)
            return result.css_style_value()
        .alternatives:
            let alternatives_node = as_css_alternatives_syntax_node(syntax_node)
            for child in css_alternatives_syntax_node_children_2(alternatives_node):
                var alternative_transaction = css_syntax.state_transaction_create_child(&transaction)
                defer css_syntax.state_transaction_destroy(&alternative_transaction)
                let result = css_parser_parse_according_to_syntax_node(this, tokens, child)
                css_syntax.token_stream_discard_whitespace(tokens)

                if result != none and css_syntax.token_stream_is_empty(tokens):
                    css_syntax.state_transaction_commit(&alternative_transaction)
                    return result
            return none
        _:
            trap("VERIFY_NOT_REACHED")

## Ported from Libraries/LibWeb/CSS/Parser/SyntaxParsing.cpp:326 Web::CSS::Parser::Parser::parse_with_a_syntax.
# https://drafts.csswg.org/css-values-5/#parse-with-a-syntax
pub func css_parser_parse_with_a_syntax(this: CssParser*, input: ak.Vector[css_syntax.ComponentValue], syntax: CssSyntaxNode*) -> CssStyleValue*:
    # 1. Parse a list of component values from values, and let raw parse be the result.
    # NB: Already done before this point.

    # FIXME: 2. If el was given, substitute arbitrary substitution functions in raw parse, and set raw parse to that result.
    # NB: This is currently a no-op because our only caller already substitutes ASFs in the input before calling us.
    # FIXME: Move substitute_arbitrary_substitution_functions() into the Parser, and keep the guarded contexts there,
    #        so we don't have this awkward situation of needing to pass that to random other functions.

    # 3. parse values according to syntax, with a * value treated as <declaration-value>?, and let parsed result be
    #    the result.
    #    If syntax used a | combinator, let parsed result be the parse result from the first matching clause.
    var tokens = css_syntax.token_stream_create(input)
    let parsed_result = css_parser_parse_according_to_syntax_node(this, &tokens, syntax)
    css_syntax.token_stream_discard_whitespace(&tokens)

    # 4. If parsed result is failure, return the guaranteed-invalid value.
    if parsed_result == none or css_syntax.token_stream_has_next_token(&tokens):
        return css_guaranteed_invalid_style_value_create().css_style_value()

    # 5. Assert: parsed result is now a well-defined list of one or more CSS values, since each branch of a <syntax>
    #    defines an unambiguous parse result (or the * syntax is unambiguous on its own).
    # NB: Nothing to do.

    # 6. Return parsed result.
    return parsed_result else trap("VERIFY: a parsed result")