-
-
Notifications
You must be signed in to change notification settings - Fork 117
implement URLPattern #785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement URLPattern #785
Changes from 1 commit
80cb760
fc4b193
cd9df5e
09c3f42
6656757
686af7d
3c08805
66da95c
8377009
61f4b67
fe3af13
1262f8c
20691e3
eed6d80
f153e00
7dbf175
5f36b46
4619c9d
5cdb6db
5f28a37
20d7529
16aace3
14d217c
4a31b3f
f10d3b2
4c20080
74f72fd
969c87a
6276ce8
5f02b24
f55e3f5
cc73e97
ca60161
1a47532
a67fe01
37dc747
d4d843d
5c212d7
d33f228
530deb4
f1e04ce
a10ba16
b67580d
42d6c32
8d8acb2
ac0817e
8523594
096e159
f711faf
fc5b020
690a14a
4f1dc9b
21109d1
8929462
6759d37
e8897d9
4e96bbe
43c806d
029e17f
c4c373b
4b3f34d
8d4994c
5e6f934
71468e2
6a4c9a5
fd6d1d4
7dca1de
6d38085
abb2af0
a0df533
aeb4699
1eeab05
208c2ff
664ed1c
60c4015
5e989f0
5539349
04252cd
3eac233
3f7536c
602a565
fc3e76e
9407a49
6d8e960
67fb323
dbd003d
8619179
a4f0c42
099fb43
049dd11
6b29fed
61f45be
d2bcf67
e997a28
6e96857
188e171
5682bf1
67f9708
681bf67
3304dd0
40f85e3
fdb044e
8ee26f4
7f4acf2
505f526
6f284c4
d928625
64c6968
8090940
f204a8c
d7b92eb
fc884cb
77f44d3
ab71fa0
ca66004
b2d9e70
baeafc6
487582d
ffee76c
0100006
757683b
8dc937e
53ba80f
edbf6c0
5f74dd3
a5580c7
db7acf9
c60c2dc
385f554
dab41f6
cf69585
bd9655d
393f515
64f66c6
1f563d4
52c33b5
6ae710b
1b59155
dd20066
528027c
65fe0b6
613d60d
943f0aa
9bb11ad
5b1de58
1ec8ea0
c858831
36a7b72
ff2bf00
0feb9a6
57accd5
67f9988
9deaa41
d47ca13
14e6c53
6f2838f
89c8bea
e3f4fe2
8b8d5e6
a47d8c5
b620b09
36a9097
87def0a
61728b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -332,7 +332,7 @@ inline bool is_valid_name_code_point(char cp, bool first) { | |
| return true; | ||
| } | ||
|
|
||
| template <typename F> | ||
| template <url_pattern_encoding_callback F> | ||
| Token* url_pattern_parser<F>::try_consume_modifier_token() { | ||
| // Let token be the result of running try to consume a token given parser and | ||
| // "other-modifier". | ||
|
|
@@ -346,7 +346,7 @@ Token* url_pattern_parser<F>::try_consume_modifier_token() { | |
| return token; | ||
| } | ||
|
|
||
| template <typename F> | ||
| template <url_pattern_encoding_callback F> | ||
| Token* url_pattern_parser<F>::try_consume_regexp_or_wildcard_token( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recommend documenting who owns the pointer to a Token instance here. |
||
| Token* name_token) { | ||
| // Let token be the result of running try to consume a token given parser and | ||
|
|
@@ -361,7 +361,7 @@ Token* url_pattern_parser<F>::try_consume_regexp_or_wildcard_token( | |
| return token; | ||
| } | ||
|
|
||
| template <typename F> | ||
| template <url_pattern_encoding_callback F> | ||
| Token* url_pattern_parser<F>::try_consume_token(token_type type) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recommend documenting who owns the pointer to a Token instance here. |
||
| // Assert: parser’s index is less than parser’s token list size. | ||
| ADA_ASSERT_TRUE(index < tokens.size()); | ||
|
|
@@ -375,7 +375,7 @@ Token* url_pattern_parser<F>::try_consume_token(token_type type) { | |
| return &next_token; | ||
| } | ||
|
|
||
| template <typename F> | ||
| template <url_pattern_encoding_callback F> | ||
| std::string url_pattern_parser<F>::consume_text() { | ||
| // Let result be the empty string. | ||
| std::string result{}; | ||
|
|
@@ -396,7 +396,7 @@ std::string url_pattern_parser<F>::consume_text() { | |
| return result; | ||
| } | ||
|
|
||
| template <typename F> | ||
| template <url_pattern_encoding_callback F> | ||
| tl::expected<Token, url_pattern_errors> | ||
| url_pattern_parser<F>::consume_required_token(token_type type) { | ||
| // Let result be the result of running try to consume a token given parser and | ||
|
|
@@ -406,18 +406,17 @@ url_pattern_parser<F>::consume_required_token(token_type type) { | |
| if (!result) { | ||
| return tl::unexpected(url_pattern_errors::type_error); | ||
| } | ||
| return std::move(*result); | ||
| return *result; | ||
| } | ||
|
|
||
| template <typename F> | ||
| template <url_pattern_encoding_callback F> | ||
| std::optional<url_pattern_errors> | ||
| url_pattern_parser<F>::maybe_add_part_from_the_pending_fixed_value() { | ||
| // If parser’s pending fixed value is the empty string, then return. | ||
| if (pending_fixed_value.empty()) return std::nullopt; | ||
| // Let encoded value be the result of running parser’s encoding callback given | ||
| // parser’s pending fixed value. | ||
| tl::expected<std::string, url_pattern_errors> encoded_value = | ||
| encoding_callback(pending_fixed_value); | ||
| auto encoded_value = encoding_callback(pending_fixed_value); | ||
| if (!encoded_value) { | ||
| return encoded_value.error(); | ||
| } | ||
|
|
@@ -426,14 +425,14 @@ url_pattern_parser<F>::maybe_add_part_from_the_pending_fixed_value() { | |
| // Let part be a new part whose type is "fixed-text", value is encoded value, | ||
| // and modifier is "none". | ||
| url_pattern_part part{.type = url_pattern_part_type::FIXED_TEXT, | ||
| .value = std::move(encoded_value.value()), | ||
| .value = std::move(*encoded_value), | ||
| .modifier = url_pattern_part_modifier::NONE}; | ||
| // Append part to parser’s part list. | ||
| parts.push_back(std::move(part)); | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| template <typename F> | ||
| template <url_pattern_encoding_callback F> | ||
| std::optional<url_pattern_errors> url_pattern_parser<F>::add_part( | ||
| std::string_view prefix, Token* name_token, Token* regexp_or_wildcard_token, | ||
| std::string_view suffix, Token* modifier_token) { | ||
|
|
@@ -554,7 +553,7 @@ std::optional<url_pattern_errors> url_pattern_parser<F>::add_part( | |
| return std::nullopt; | ||
| } | ||
|
|
||
| template <typename F> | ||
| template <url_pattern_encoding_callback F> | ||
| bool url_pattern_parser<F>::is_duplicate_name(std::string_view name) { | ||
| // For each part of parser’s part list: | ||
| // If part’s name is name, then return true. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| #define ADA_URL_PATTERN_HELPERS_H | ||
|
|
||
| #include "ada/expected.h" | ||
| #include "ada/url_pattern.h" | ||
|
|
||
| #include <string> | ||
| #include <tuple> | ||
|
|
@@ -48,10 +49,10 @@ struct Token { | |
| }; | ||
|
|
||
| // @see https://urlpattern.spec.whatwg.org/#pattern-parser | ||
| template <typename url_pattern_encoding_callback> | ||
| template <url_pattern_encoding_callback F> | ||
| class url_pattern_parser { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instance of the template class url_pattern_parser appear to own tokens. We want to make sure that the token pointers never survive beyond the lifetime of their originator. |
||
| public: | ||
| url_pattern_parser(url_pattern_encoding_callback&& encoding_callback_, | ||
| url_pattern_parser(F&& encoding_callback_, | ||
| std::string_view segment_wildcard_regexp_) | ||
| : encoding_callback(encoding_callback_), | ||
| segment_wildcard_regexp(std::string(segment_wildcard_regexp_)) {} | ||
|
|
@@ -81,7 +82,7 @@ class url_pattern_parser { | |
| bool is_duplicate_name(std::string_view name); | ||
|
|
||
| std::vector<Token> tokens{}; | ||
| url_pattern_encoding_callback encoding_callback; | ||
| F encoding_callback; | ||
| std::string segment_wildcard_regexp; | ||
| std::vector<url_pattern_part> parts{}; | ||
| std::string pending_fixed_value{}; | ||
|
|
@@ -299,11 +300,11 @@ constexpr bool is_absolute_pathname(std::string_view input, | |
| std::string_view type) noexcept; | ||
|
|
||
| // @see https://urlpattern.spec.whatwg.org/#parse-a-pattern-string | ||
| template <typename url_pattern_encoding_callback> | ||
| template <url_pattern_encoding_callback F> | ||
| tl::expected<std::vector<url_pattern_part>, url_pattern_errors> | ||
| parse_pattern_string(std::string_view input, | ||
| url_pattern_compile_component_options& options, | ||
| url_pattern_encoding_callback&& encoding_callback); | ||
| F&& encoding_callback); | ||
|
|
||
| // @see https://urlpattern.spec.whatwg.org/#generate-a-pattern-string | ||
| std::string generate_pattern_string( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -873,23 +873,23 @@ constexpr bool is_absolute_pathname(std::string_view input, | |
| return false; | ||
| } | ||
|
|
||
| template <typename url_pattern_encoding_callback> | ||
| template <url_pattern_encoding_callback F> | ||
| tl::expected<std::vector<url_pattern_part>, url_pattern_errors> | ||
| parse_pattern_string(std::string_view input, | ||
| url_pattern_compile_component_options& options, | ||
| url_pattern_encoding_callback&& encoding_callback) { | ||
| F&& encoding_callback) { | ||
| // 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. | ||
| auto parser = url_pattern_parser<url_pattern_encoding_callback>( | ||
| auto parser = url_pattern_parser<F>( | ||
|
||
| encoding_callback, generate_segment_wildcard_regexp(options)); | ||
| // Set parser’s token list to the result of running tokenize given input and | ||
| // "strict". | ||
| auto tokenize_result = tokenize(input, token_policy::STRICT); | ||
| if (!tokenize_result) { | ||
| return tl::unexpected(tokenize_result.error()); | ||
| } | ||
| parser.tokens = std::move(tokenize_result.value<std::vector<Token>>()); | ||
| parser.tokens = std::move(*tokenize_result); | ||
|
|
||
| // While parser’s index is less than parser’s token list's size: | ||
| while (parser.index < parser.tokens.size()) { | ||
|
|
@@ -898,13 +898,13 @@ parse_pattern_string(std::string_view input, | |
| auto char_token = parser.try_consume_token(token_type::CHAR); | ||
| // Let name token be the result of running try to consume a token given | ||
| // parser and "name". | ||
| auto name_token_ = parser.try_consume_token(token_type::NAME); | ||
| auto name_token = parser.try_consume_token(token_type::NAME); | ||
| // Let regexp or wildcard token be the result of running try to consume a | ||
| // regexp or wildcard token given parser and name token. | ||
| auto regexp_or_wildcard_token_ = | ||
| parser.try_consume_token(token_type::REGEXP); | ||
| auto regexp_or_wildcard_token = | ||
| parser.try_consume_regexp_or_wildcard_token(name_token); | ||
| // If name token is not null or regexp or wildcard token is not null: | ||
| if (name_token_ || regexp_or_wildcard_token_) { | ||
| if (name_token || regexp_or_wildcard_token) { | ||
| // Let prefix be the empty string. | ||
| std::string prefix{}; | ||
| // If char token is not null then set prefix to char token’s value. | ||
|
|
@@ -922,12 +922,12 @@ parse_pattern_string(std::string_view input, | |
| } | ||
| // Let modifier token be the result of running try to consume a modifier | ||
| // token given parser. | ||
| auto modifier_token_ = parser.try_consume_modifier_token(); | ||
| auto modifier_token = parser.try_consume_modifier_token(); | ||
| // Run add a part given parser, prefix, name token, regexp or wildcard | ||
| // token, the empty string, and modifier token. | ||
| if (auto error = | ||
| parser.add_part(prefix, name_token_, regexp_or_wildcard_token_, | ||
| {}, modifier_token_)) { | ||
| parser.add_part(prefix, name_token, regexp_or_wildcard_token, {}, | ||
| modifier_token)) { | ||
| return tl::unexpected(*error); | ||
| } | ||
| // Continue. | ||
|
|
@@ -956,26 +956,25 @@ parse_pattern_string(std::string_view input, | |
| auto prefix_ = parser.consume_text(); | ||
| // Set name token to the result of running try to consume a token given | ||
| // parser and "name". | ||
| name_token_ = parser.try_consume_token(token_type::NAME); | ||
| name_token = parser.try_consume_token(token_type::NAME); | ||
| // 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_ = | ||
| parser.try_consume_regexp_or_wildcard_token(name_token_); | ||
| regexp_or_wildcard_token = | ||
| parser.try_consume_regexp_or_wildcard_token(name_token); | ||
| // Let suffix be the result of running consume text given parser. | ||
| auto suffix_ = parser.consume_text(); | ||
| // Run consume a required token given parser and "close". | ||
| auto required_token = parser.consume_required_token(token_type::CLOSE); | ||
| if (!required_token) { | ||
| if (!parser.consume_required_token(token_type::CLOSE)) { | ||
| return tl::unexpected(url_pattern_errors::type_error); | ||
| } | ||
| // Set modifier token to the result of running try to consume a modifier | ||
| // token given parser. | ||
| auto modifier_token_ = parser.try_consume_modifier_token(); | ||
| auto modifier_token = parser.try_consume_modifier_token(); | ||
| // Run add a part given parser, prefix, name token, regexp or wildcard | ||
| // token, suffix, and modifier token. | ||
| if (auto error = | ||
| parser.add_part(prefix_, name_token_, regexp_or_wildcard_token_, | ||
| suffix_, modifier_token_)) { | ||
| parser.add_part(prefix_, name_token, regexp_or_wildcard_token, | ||
| suffix_, modifier_token)) { | ||
| return tl::unexpected(*error); | ||
| } | ||
| // Continue. | ||
|
|
@@ -986,8 +985,7 @@ parse_pattern_string(std::string_view input, | |
| return tl::unexpected(*error); | ||
| } | ||
| // Run consume a required token given parser and "end". | ||
| auto required_token = parser.consume_required_token(token_type::END); | ||
| if (!required_token) { | ||
| if (!parser.consume_required_token(token_type::END)) { | ||
| return tl::unexpected(url_pattern_errors::type_error); | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are returning a pointer to an instance of Token here. Who owns the Token instance? I recommend documenting it.