Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-foundation

Source of release 0.6.1

0.6.1 / src / web_url / pattern_parser.lucb

#==============================================================================================
#
#   pattern_parser - URLPattern: parsing a pattern string into a part list
#
#   DESCRIPTION:
#       Port of Libraries/LibURL/Pattern/PatternParser.cpp (lines 1-409) and
#       Libraries/LibURL/Pattern/PatternParser.h (lines 1-74). `Optional<Token const&>` is a
#       `const Token*?` into the parser's token list, which does not change after tokenizing.
#
#==============================================================================================

## Ported from Libraries/LibURL/Pattern/PatternParser.cpp:13 URL::Pattern::PatternParser::PatternParser.
pub func pattern_parser_construct(this: PatternParser*, encoding_callback: EncodingCallback, segment_wildcard_regexp: ak.String):
    var token_list: ak.Vector[Token]
    var part_list: ak.Vector[Part]
    var pending_fixed_value: ak.StringBuilder
    this.m_token_list = token_list
    this.m_encoding_callback = encoding_callback
    this.m_segment_wildcard_regexp = segment_wildcard_regexp
    this.m_part_list = part_list
    this.m_pending_fixed_value = pending_fixed_value
    ak.string_builder_construct(&this.m_pending_fixed_value)
    this.m_index = 0
    this.m_next_numeric_name = 0

## Ported from Libraries/LibURL/Pattern/PatternParser.cpp:20 URL::Pattern::PatternParser::consume_a_required_token.
# https://urlpattern.spec.whatwg.org/#consume-a-required-token
pub func pattern_parser_consume_a_required_token(this: PatternParser*, type_: TokenType) -> !:
    # 1. Let result be the result of running try to consume a token given parser and type.
    let result = pattern_parser_try_to_consume_a_token(this, type_)

    # 2. If result is null, then throw a TypeError.
    if result == none:
        var message: ak.StringBuilder
        ak.string_builder_construct(&message)
        append_literal(&message, "Missing required token '")
        ak.string_builder_append_string_view(&message, token_type_to_string(type_))
        append_literal(&message, "' in URL pattern")
        try pattern_throw_builder(&message)

# 3. Return result.
# NOTE: No caller actually needs the result, so we just ignore it.

## Ported from Libraries/LibURL/Pattern/PatternParser.cpp:35 URL::Pattern::PatternParser::consume_text.
# https://urlpattern.spec.whatwg.org/#consume-text
pub func pattern_parser_consume_text(this: PatternParser*) -> ak.String:
    # 1. Let result be the empty string.
    var result: ak.StringBuilder
    ak.string_builder_construct(&result)

    # 1. While true:
    while true:
        # 1. Let token be the result of running try to consume a token given parser and "char".
        var token = pattern_parser_try_to_consume_a_token(this, TokenType.char_)

        # 2. If token is null, then set token to the result of running try to consume a token given parser and "escaped-char".
        if token == none:
            token = pattern_parser_try_to_consume_a_token(this, TokenType.escaped_char)

        # 3. If token is null, then break.
        let consumed = token else break

        # 4. Append token’s value to the end of result.
        append_string(&result, consumed.value)

    # 2. Return result.
    return ak.string_builder_to_string_without_validation(&result)

# A new part (Part(Type, String value, Modifier)).
func new_part(type_: PartType, value: ak.String, modifier: PartModifier) -> Part:
    var part: Part
    part_construct(&part, type_, value, modifier)
    return part

## Ported from Libraries/LibURL/Pattern/PatternParser.cpp:62 URL::Pattern::PatternParser::maybe_add_a_part_from_the_pending_fixed_value.
# https://urlpattern.spec.whatwg.org/#maybe-add-a-part-from-the-pending-fixed-value
pub func pattern_parser_maybe_add_a_part_from_the_pending_fixed_value(this: PatternParser*) -> !:
    # 1. If parser’s pending fixed value is the empty string, then return.
    if ak.string_builder_is_empty(&this.m_pending_fixed_value):
        return

    # 2. Let encoded value be the result of running parser’s encoding callback given parser’s pending fixed value.
    let encoded_value = try this.m_encoding_callback(ak.string_builder_to_string_without_validation(&this.m_pending_fixed_value))

    # 3. Set parser’s pending fixed value to the empty string.
    ak.string_builder_clear(&this.m_pending_fixed_value)

    # 4. Let part be a new part whose type is "fixed-text", value is encoded value, and modifier is "none".
    # 5. Append part to parser’s part list.
    ak.vector_append_t(&this.m_part_list, new_part(PartType.fixed_text, encoded_value, PartModifier.none_))

## Ported from Libraries/LibURL/Pattern/PatternParser.cpp:82 URL::Pattern::PatternParser::is_a_duplicate_name.
# https://urlpattern.spec.whatwg.org/#is-a-duplicate-name
pub func pattern_parser_is_a_duplicate_name(this: const PatternParser*, name: ak.String) -> bool:
    # 1. For each part of parser’s part list:
    for index in 0..<ak.vector_size(&this.m_part_list):
        # 1. If part’s name is name, then return true.
        if string_equals(ak.vector_at_const(&this.m_part_list, index).name, name):
            return true

    # 2. Return false.
    return false

## Ported from Libraries/LibURL/Pattern/PatternParser.cpp:96 URL::Pattern::PatternParser::add_a_part.
# https://urlpattern.spec.whatwg.org/#add-a-part
pub func pattern_parser_add_a_part(this: PatternParser*, prefix: ak.String, name_token: const Token*?, regexp_or_wildcard_token: const Token*?, suffix: ak.String, modifier_token: const Token*?) -> !:
    # 1. Let modifier be "none".
    var modifier = PartModifier.none_

    # 2. If modifier token is not null:
    if let token = modifier_token:
        # 1. If modifier token’s value is "?" then set modifier to "optional".
        if string_is(token.value, "?"):
            modifier = PartModifier.optional
        # 2. Otherwise if modifier token’s value is "*" then set modifier to "zero-or-more".
        elif string_is(token.value, "*"):
            modifier = PartModifier.zero_or_more
        # 3. Otherwise if modifier token’s value is "+" then set modifier to "one-or-more".
        elif string_is(token.value, "+"):
            modifier = PartModifier.one_or_more

    # 3. If name token is null and regexp or wildcard token is null and modifier is "none":
    # NOTE: This was a "{foo}" grouping. We add this to the pending fixed value so that it will be combined with
    #       any previous or subsequent text.
    if name_token == none and regexp_or_wildcard_token == none and modifier == PartModifier.none_:
        # 1. Append prefix to the end of parser’s pending fixed value.
        append_string(&this.m_pending_fixed_value, prefix)

        # 2. Return.
        return

    # 4. Run maybe add a part from the pending fixed value given parser.
    try pattern_parser_maybe_add_a_part_from_the_pending_fixed_value(this)

    # 5. If name token is null and regexp or wildcard token is null:
    # NOTE: This was a "{foo}?" grouping. The modifier means we cannot combine it with other text. Therefore we
    #       add it as a part immediately.
    if name_token == none and regexp_or_wildcard_token == none:
        # 1. Assert: suffix is the empty string.
        assert(ak.string_is_empty(&suffix))

        # 2. If prefix is the empty string, then return.
        if ak.string_is_empty(&prefix):
            return

        # 3. Let encoded value be the result of running parser’s encoding callback given prefix.
        let encoded_value = try this.m_encoding_callback(prefix)

        # 4. Let part be a new part whose type is "fixed-text", value is encoded value, and modifier is modifier.
        # 5. Append part to parser’s part list.
        ak.vector_append_t(&this.m_part_list, new_part(PartType.fixed_text, encoded_value, modifier))

        # 6. Return.
        return

    # 6. Let regexp value be the empty string.
    # NOTE: Next, we convert the regexp or wildcard token into a regular expression.
    var regexp_value = empty_string()

    # 7. If regexp or wildcard token is null, then set regexp value to parser’s segment wildcard regexp.
    if regexp_or_wildcard_token == none:
        regexp_value = this.m_segment_wildcard_regexp
    # 8. Otherwise if regexp or wildcard token’s type is "asterisk", then set regexp value to the full wildcard regexp value.
    elif (regexp_or_wildcard_token else trap("unreachable")).type_ == TokenType.asterisk:
        regexp_value = ak.string_from_utf8(sv(pattern_full_wildcard_regexp_value)) else trap("MUST: String::from_utf8")
    # 9. Otherwise set regexp value to regexp or wildcard token’s value.
    else:
        regexp_value = (regexp_or_wildcard_token else trap("unreachable")).value

    # 10. Let type be "regexp".
    # NOTE: Next, we convert regexp value into a part type. We make sure to go to a regular expression first so
    #       that an equivalent "regexp" token will be treated the same as a "name" or "asterisk" token.
    var type_ = PartType.regexp

    # 11. If regexp value is parser’s segment wildcard regexp:
    if string_equals(regexp_value, this.m_segment_wildcard_regexp):
        # 1. Set type to "segment-wildcard".
        type_ = PartType.segment_wildcard

        # 2. Set regexp value to the empty string.
        regexp_value = empty_string()
    # 12. Otherwise if regexp value is the full wildcard regexp value:
    elif string_is(regexp_value, pattern_full_wildcard_regexp_value):
        # 1. Set type to "full-wildcard".
        type_ = PartType.full_wildcard

        # 2. Set regexp value to the empty string.
        regexp_value = empty_string()

    # 13. Let name be the empty string.
    # NOTE: Next, we determine the part name. This can be explicitly provided by a "name" token or be automatically assigned.
    var name = empty_string()

    # 14. If name token is not null, then set name to name token’s value.
    if let token = name_token:
        name = token.value
    # 15. Otherwise if regexp or wildcard token is not null:
    elif regexp_or_wildcard_token != none:
        # 1. Set name to parser’s next numeric name, serialized.
        name = ak.string_number_u64((u64)this.m_next_numeric_name)

        # 2. Increment parser’s next numeric name by 1.
        this.m_next_numeric_name += 1

    # 16. If the result of running is a duplicate name given parser and name is true, then throw a TypeError.
    if pattern_parser_is_a_duplicate_name(this, name):
        var message: ak.StringBuilder
        ak.string_builder_construct(&message)
        append_literal(&message, "Duplicate name '")
        append_string(&message, name)
        append_literal(&message, "' provided in URL pattern")
        try pattern_throw_builder(&message)

    # 17. Let encoded prefix be the result of running parser’s encoding callback given prefix.
    # NOTE: Finally, we encode the fixed text values and create the part.
    let encoded_prefix = try this.m_encoding_callback(prefix)

    # 18. Let encoded suffix be the result of running parser’s encoding callback given suffix.
    let encoded_suffix = try this.m_encoding_callback(suffix)

    # 19. Let part be a new part whose type is type, value is regexp value, modifier is modifier, name is name, prefix
    #     is encoded prefix, and suffix is encoded suffix.
    # 20. Append part to parser’s part list.
    var part: Part
    part_construct_string(&part, type_, regexp_value, modifier, name, encoded_prefix, encoded_suffix)
    ak.vector_append_t(&this.m_part_list, part)

## Ported from Libraries/LibURL/Pattern/PatternParser.cpp:234 URL::Pattern::PatternParser::try_to_consume_a_modifier_token.
# https://urlpattern.spec.whatwg.org/#try-to-consume-a-modifier-token
pub func pattern_parser_try_to_consume_a_modifier_token(this: PatternParser*) -> const Token*?:
    # 1. Let token be the result of running try to consume a token given parser and "other-modifier".
    var token = pattern_parser_try_to_consume_a_token(this, TokenType.other_modifier)

    # 2. If token is not null, then return token.
    if token != none:
        return token

    # 3. Set token to the result of running try to consume a token given parser and "asterisk".
    token = pattern_parser_try_to_consume_a_token(this, TokenType.asterisk)

    # 4. Return token.
    return token

## Ported from Libraries/LibURL/Pattern/PatternParser.cpp:251 URL::Pattern::PatternParser::try_to_consume_a_regexp_or_wildcard_token.
# https://urlpattern.spec.whatwg.org/#try-to-consume-a-regexp-or-wildcard-token
pub func pattern_parser_try_to_consume_a_regexp_or_wildcard_token(this: PatternParser*, name_token: const Token*?) -> const Token*?:
    # 1. Let token be the result of running try to consume a token given parser and "regexp".
    var token = pattern_parser_try_to_consume_a_token(this, TokenType.regexp)

    # 2. If name token is null and token is null, then set token to the result of running try to consume a token given
    #    parser and "asterisk".
    if name_token == none and token == none:
        token = pattern_parser_try_to_consume_a_token(this, TokenType.asterisk)

    # 3. Return token.
    return token

## Ported from Libraries/LibURL/Pattern/PatternParser.cpp:266 URL::Pattern::PatternParser::try_to_consume_a_token.
# https://urlpattern.spec.whatwg.org/#try-to-consume-a-token
pub func pattern_parser_try_to_consume_a_token(this: PatternParser*, type_: TokenType) -> const Token*?:
    # 1. Assert: parser’s index is less than parser’s token list size.
    assert(this.m_index < ak.vector_size(&this.m_token_list))

    # 2. Let next token be parser’s token list[parser’s index].
    let next_token = ak.vector_at_const(&this.m_token_list, this.m_index)

    # 3. If next token’s type is not type return null.
    if next_token.type_ != type_:
        return none

    # 4. Increment parser’s index by 1.
    this.m_index += 1

    # 5. Return next token.
    return next_token

## Ported from Libraries/LibURL/Pattern/PatternParser.cpp:286 URL::Pattern::PatternParser::parse.
# https://urlpattern.spec.whatwg.org/#parse-a-pattern-string
pub func pattern_parser_parse(input: ak.Utf8View, options: Options, encoding_callback: EncodingCallback) -> ak.Vector[Part]!:
    # 1. Let parser be a new pattern parser whose encoding callback is encoding callback and segment wildcard regexp
    #    is the result of running generate a segment wildcard regexp given options.
    var parser: PatternParser = ---
    pattern_parser_construct(&parser, encoding_callback, pattern_generate_a_segment_wildcard_regexp(options))

    # 2. Set parser’s token list to the result of running tokenize given input and "strict".
    parser.m_token_list = try tokenizer_tokenize(input, TokenizerPolicy.strict)

    # 3. While parser’s index is less than parser’s token list's size:
    while parser.m_index < ak.vector_size(&parser.m_token_list):
        # 1. Let char token be the result of running try to consume a token given parser and "char".
        let char_token = pattern_parser_try_to_consume_a_token(&parser, TokenType.char_)

        # 2. Let name token be the result of running try to consume a token given parser and "name".
        var name_token = pattern_parser_try_to_consume_a_token(&parser, TokenType.name)

        # 3. Let regexp or wildcard token be the result of running try to consume a regexp or wildcard token given
        #    parser and name token.
        var regexp_or_wildcard_token = pattern_parser_try_to_consume_a_regexp_or_wildcard_token(&parser, name_token)

        # 4. If name token is not null or regexp or wildcard token is not null:
        # NOTE: If there is a matching group, we need to add the part immediately.
        if name_token != none or regexp_or_wildcard_token != none:
            # 1. Let prefix be the empty string.
            var prefix = empty_string()

            # 2. If char token is not null then set prefix to char token’s value.
            if let token = char_token:
                prefix = token.value

            # 3. If prefix is not the empty string and not options’s prefix code point:
            if (not ak.string_is_empty(&prefix) and (options.prefix_code_point == none or not string_equals(prefix, ak.string_from_code_point(options.prefix_code_point else trap("unreachable"))))):
                # 1. Append prefix to the end of parser’s pending fixed value.
                append_string(&parser.m_pending_fixed_value, prefix)

                # 2. Set prefix to the empty string.
                prefix = empty_string()

            # 4. Run maybe add a part from the pending fixed value given parser.
            try pattern_parser_maybe_add_a_part_from_the_pending_fixed_value(&parser)

            # 5. Let modifier token be the result of running try to consume a modifier token given parser.
            let modifier_token = pattern_parser_try_to_consume_a_modifier_token(&parser)

            # 6. Run add a part given parser, prefix, name token, regexp or wildcard token, the empty string,
            #    and modifier token.
            try pattern_parser_add_a_part(&parser, prefix, name_token, regexp_or_wildcard_token, empty_string(), modifier_token)

            # 7. Continue.
            continue

        # 5. Let fixed token be char token.
        # NOTE: If there was no matching group, then we need to buffer any fixed text. We want to collect as
        #       much text as possible before adding it as a "fixed-text" part.
        var fixed_token = char_token

        # 6. If fixed token is null, then set fixed token to the result of running try to consume a token given
        #     parser and "escaped-char".
        if fixed_token == none:
            fixed_token = pattern_parser_try_to_consume_a_token(&parser, TokenType.escaped_char)

        # 7. If fixed token is not null:
        if let token = fixed_token:
            # 1. Append fixed token’s value to parser’s pending fixed value.
            append_string(&parser.m_pending_fixed_value, token.value)

            # 2. Continue.
            continue

        # 8. Let open token be the result of running try to consume a token given parser and "open".
        let open_token = pattern_parser_try_to_consume_a_token(&parser, TokenType.open)

        # 9. If open token is not null:
        if open_token != none:
            # 1. Let prefix be the result of running consume text given parser.
            let prefix = pattern_parser_consume_text(&parser)

            # 2. Set name token to the result of running try to consume a token given parser and "name".
            name_token = pattern_parser_try_to_consume_a_token(&parser, TokenType.name)

            # 3. Set regexp or wildcard token to the result of running try to consume a regexp or wildcard token
            #    given parser and name token.
            regexp_or_wildcard_token = pattern_parser_try_to_consume_a_regexp_or_wildcard_token(&parser, name_token)

            # 4. Let suffix be the result of running consume text given parser.
            let suffix = pattern_parser_consume_text(&parser)

            # 5. Run consume a required token given parser and "close".
            try pattern_parser_consume_a_required_token(&parser, TokenType.close)

            # 6. Let modifier token to the result of running try to consume a modifier token given parser.
            let modifier_token = pattern_parser_try_to_consume_a_modifier_token(&parser)

            # 7. Run add a part given parser, prefix, name token, regexp or wildcard token, suffix, and modifier token.
            try pattern_parser_add_a_part(&parser, prefix, name_token, regexp_or_wildcard_token, suffix, modifier_token)

            # 8. Continue.
            continue

        # 10. Run maybe add a part from the pending fixed value given parser.
        try pattern_parser_maybe_add_a_part_from_the_pending_fixed_value(&parser)

        # 11. Run consume a required token given parser and "end".
        try pattern_parser_consume_a_required_token(&parser, TokenType.end)

    # 4. Return parser’s part list.
    return parser.m_part_list