/* * Copyright (c) 2025, Shannon Booth * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include namespace URL::Pattern { // https://urlpattern.spec.whatwg.org/#pattern-parser class PatternParser { public: // https://urlpattern.spec.whatwg.org/#encoding-callback // An encoding callback is an abstract algorithm that takes a given string input. The input will be a simple text // piece of a pattern string. An implementing algorithm will validate and encode the input. It must return the // encoded string or throw an exception. using EncodingCallback = Function(String const&)>; static PatternErrorOr> parse(Utf8View const& input, Options const&, EncodingCallback); private: PatternParser(EncodingCallback, String segment_wildcard_regexp); Optional try_to_consume_a_token(Token::Type); Optional try_to_consume_a_modifier_token(); Optional try_to_consume_a_regexp_or_wildcard_token(Optional name_token); PatternErrorOr consume_a_required_token(Token::Type); String consume_text(); PatternErrorOr maybe_add_a_part_from_the_pending_fixed_value(); PatternErrorOr add_a_part(String const& prefix, Optional name_token, Optional regexp_or_wildcard_token, String const& suffix, Optional modifier_token); bool is_a_duplicate_name(String const&) const; // https://urlpattern.spec.whatwg.org/#pattern-parser-token-list // A pattern parser has an associated token list, a token list, initially an empty list. Vector m_token_list; // https://urlpattern.spec.whatwg.org/#pattern-parser-encoding-callback // A pattern parser has an associated encoding callback, a encoding callback, that must be set upon creation. EncodingCallback m_encoding_callback; // https://urlpattern.spec.whatwg.org/#pattern-parser-segment-wildcard-regexp // A pattern parser has an associated segment wildcard regexp, a string, that must be set upon creation. String m_segment_wildcard_regexp; // https://urlpattern.spec.whatwg.org/#pattern-parser-part-list // A pattern parser has an associated part list, a part list, initially an empty list. Vector m_part_list; // https://urlpattern.spec.whatwg.org/#pattern-parser-pending-fixed-value // A pattern parser has an associated pending fixed value, a string, initially the empty string. StringBuilder m_pending_fixed_value; // https://urlpattern.spec.whatwg.org/#pattern-parser-index // A pattern parser has an associated index, a number, initially 0. size_t m_index { 0 }; // https://urlpattern.spec.whatwg.org/#pattern-parser-next-numeric-name // A pattern parser has an associated next numeric name, a number, initially 0. size_t m_next_numeric_name { 0 }; }; }