forked from denoland/rust-urlpattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.rs
441 lines (409 loc) · 12.2 KB
/
parser.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
use crate::error::ParserError;
use crate::tokenizer::Token;
use crate::tokenizer::TokenType;
use crate::Error;
// Ref: https://wicg.github.io/urlpattern/#full-wildcard-regexp-value
pub const FULL_WILDCARD_REGEXP_VALUE: &str = ".*";
/// The regexp syntax that should be used.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RegexSyntax {
/// Compile regexes to rust-regex syntax. This is the default.
Rust,
/// Compile regexes to ECMAScript syntax. This should be used with the
/// [crate::quirks::component_regex].
///
/// NOTE: enabling this syntax kind, means the regex syntax will NOT be
/// validated during parsing.
EcmaScript,
}
// Ref: https://wicg.github.io/urlpattern/#options-header
#[derive(Debug, Clone)]
pub struct Options {
pub delimiter_code_point: String, // TODO: It must contain one ASCII code point or the empty string. maybe Option<char>?
pub prefix_code_point: String, // TODO: It must contain one ASCII code point or the empty string. maybe Option<char>?
pub regex_syntax: RegexSyntax,
pub ignore_case: bool,
}
impl std::default::Default for Options {
// Ref: https://wicg.github.io/urlpattern/#default-options
#[inline]
fn default() -> Self {
Options {
delimiter_code_point: String::new(),
prefix_code_point: String::new(),
regex_syntax: RegexSyntax::Rust,
ignore_case: false,
}
}
}
impl Options {
// Ref: https://wicg.github.io/urlpattern/#hostname-options
#[inline]
pub fn hostname() -> Self {
Options {
delimiter_code_point: String::from("."),
prefix_code_point: String::new(),
regex_syntax: RegexSyntax::Rust,
ignore_case: false,
}
}
// Ref: https://wicg.github.io/urlpattern/#pathname-options
#[inline]
pub fn pathname() -> Self {
Options {
delimiter_code_point: String::from("/"),
prefix_code_point: String::from("/"),
regex_syntax: RegexSyntax::Rust,
ignore_case: false,
}
}
// Ref: https://wicg.github.io/urlpattern/#escape-a-regexp-string
pub fn escape_regexp_string(&self, input: &str) -> String {
assert!(input.is_ascii());
let mut result = String::new();
for char in input.chars() {
if matches!(
char,
'.'
| '+'
| '*'
| '?'
| '^'
| '$'
| '{'
| '}'
| '('
| ')'
| '['
| ']'
| '|'
// | '/': deviation from spec, rust regexp crate does not handle '\/' as a valid escape sequence
| '\\'
) || (char == '/' && self.regex_syntax == RegexSyntax::EcmaScript)
{
result.push('\\');
}
result.push(char);
}
result
}
// Ref: https://wicg.github.io/urlpattern/#generate-a-segment-wildcard-regexp
#[inline]
pub fn generate_segment_wildcard_regexp(&self) -> String {
// NOTE: this is a deliberate deviation from the spec. In rust-regex, you
// can not have a negative character class without specifying any
// characters.
if self.delimiter_code_point.is_empty() {
".+?".to_owned()
} else {
format!(
"[^{}]+?",
self.escape_regexp_string(&self.delimiter_code_point)
)
}
}
}
// Ref: https://wicg.github.io/urlpattern/#part-type
#[derive(Debug, Eq, PartialEq)]
pub enum PartType {
FixedText,
Regexp,
SegmentWildcard,
FullWildcard,
}
// Ref: https://wicg.github.io/urlpattern/#part-modifier
#[derive(Debug, Eq, PartialEq)]
pub enum PartModifier {
None,
Optional,
ZeroOrMore,
OneOrMore,
}
impl std::fmt::Display for PartModifier {
// Ref: https://wicg.github.io/urlpattern/#convert-a-modifier-to-a-string
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
PartModifier::None => "",
PartModifier::Optional => "?",
PartModifier::ZeroOrMore => "*",
PartModifier::OneOrMore => "+",
})
}
}
// Ref: https://wicg.github.io/urlpattern/#part
#[derive(Debug)]
pub struct Part {
pub kind: PartType,
pub value: String,
pub modifier: PartModifier,
pub name: String,
pub prefix: String,
pub suffix: String,
}
impl Part {
fn new(kind: PartType, value: String, modifier: PartModifier) -> Self {
Part {
kind,
value,
modifier,
name: String::new(),
prefix: String::new(),
suffix: String::new(),
}
}
}
// Ref: https://wicg.github.io/urlpattern/#pattern-parser
struct PatternParser<F>
where
F: Fn(&str) -> Result<String, Error>,
{
token_list: Vec<Token>,
encoding_callback: F,
segment_wildcard_regexp: String,
part_list: Vec<Part>,
pending_fixed_value: String,
index: usize,
next_numeric_name: usize,
}
impl<F> PatternParser<F>
where
F: Fn(&str) -> Result<String, Error>,
{
// Ref: https://wicg.github.io/urlpattern/#try-to-consume-a-token
fn try_consume_token(&mut self, kind: TokenType) -> Option<Token> {
assert!(self.index < self.token_list.len());
let next_token = self.token_list[self.index].clone();
if next_token.kind != kind {
None
} else {
self.index += 1;
Some(next_token)
}
}
// Ref: https://wicg.github.io/urlpattern/#try-to-consume-a-regexp-or-wildcard-token
#[inline]
fn try_consume_regexp_or_wildcard_token(
&mut self,
name_token_is_none: bool,
) -> Option<Token> {
let token = self.try_consume_token(TokenType::Regexp);
if name_token_is_none && token.is_none() {
self.try_consume_token(TokenType::Asterisk)
} else {
token
}
}
// Ref: https://wicg.github.io/urlpattern/#try-to-consume-a-modifier-token
#[inline]
fn try_consume_modifier_token(&mut self) -> Option<Token> {
self
.try_consume_token(TokenType::OtherModifier)
.or_else(|| self.try_consume_token(TokenType::Asterisk))
}
// Ref: https://wicg.github.io/urlpattern/#maybe-add-a-part-from-the-pending-fixed-value
#[inline]
fn maybe_add_part_from_pending_fixed_value(&mut self) -> Result<(), Error> {
if self.pending_fixed_value.is_empty() {
return Ok(());
}
let encoded_value = (self.encoding_callback)(&self.pending_fixed_value)?;
self.pending_fixed_value = String::new();
self.part_list.push(Part::new(
PartType::FixedText,
encoded_value,
PartModifier::None,
));
Ok(())
}
// Ref: https://wicg.github.io/urlpattern/#add-a-part
fn add_part(
&mut self,
prefix: &str,
name_token: Option<Token>,
regexp_or_wildcard_token: Option<Token>,
suffix: &str,
modifier_token: Option<Token>,
) -> Result<(), Error> {
let mut modifier = PartModifier::None;
if let Some(modifier_token) = modifier_token {
modifier = match modifier_token.value.as_ref() {
"?" => PartModifier::Optional,
"*" => PartModifier::ZeroOrMore,
"+" => PartModifier::OneOrMore,
_ => unreachable!(),
};
}
if name_token.is_none()
&& regexp_or_wildcard_token.is_none()
&& modifier == PartModifier::None
{
self.pending_fixed_value.push_str(prefix);
return Ok(());
}
self.maybe_add_part_from_pending_fixed_value()?;
if name_token.is_none() && regexp_or_wildcard_token.is_none() {
assert!(suffix.is_empty());
if prefix.is_empty() {
return Ok(());
}
let encoded_value = (self.encoding_callback)(prefix)?;
self.part_list.push(Part::new(
PartType::FixedText,
encoded_value,
modifier,
));
return Ok(());
}
let mut regexp_value = match ®exp_or_wildcard_token {
None => self.segment_wildcard_regexp.to_owned(),
Some(regexp_or_wildcard_token) => {
if regexp_or_wildcard_token.kind == TokenType::Asterisk {
FULL_WILDCARD_REGEXP_VALUE.to_string()
} else {
regexp_or_wildcard_token.value.to_owned()
}
}
};
let mut kind = PartType::Regexp;
if regexp_value == self.segment_wildcard_regexp {
kind = PartType::SegmentWildcard;
regexp_value = String::new();
} else if regexp_value == FULL_WILDCARD_REGEXP_VALUE {
kind = PartType::FullWildcard;
regexp_value = String::new();
}
let mut name = String::new();
if let Some(name_token) = name_token {
name = name_token.value;
} else if regexp_or_wildcard_token.is_some() {
name = self.next_numeric_name.to_string();
self.next_numeric_name += 1;
}
if self.is_duplicate_name(&name) {
return Err(Error::Parser(ParserError::DuplicateName(name)));
}
let encoded_prefix = (self.encoding_callback)(prefix)?;
let encoded_suffix = (self.encoding_callback)(suffix)?;
self.part_list.push(Part {
kind,
value: regexp_value,
modifier,
name,
prefix: encoded_prefix,
suffix: encoded_suffix,
});
Ok(())
}
// Ref: https://wicg.github.io/urlpattern/#is-a-duplicate-name
fn is_duplicate_name(&self, name: &str) -> bool {
self.part_list.iter().any(|p| p.name == name)
}
// Ref: https://wicg.github.io/urlpattern/#consume-text
fn consume_text(&mut self) -> String {
let mut result = String::new();
loop {
let mut token = self.try_consume_token(TokenType::Char);
if token.is_none() {
token = self.try_consume_token(TokenType::EscapedChar);
}
if token.is_none() {
break;
}
result.push_str(&token.unwrap().value);
}
result
}
// Ref: https://wicg.github.io/urlpattern/#consume-a-required-token
#[inline]
fn consume_required_token(
&mut self,
kind: TokenType,
) -> Result<Token, Error> {
self.try_consume_token(kind.clone()).ok_or_else(|| {
Error::Parser(ParserError::ExpectedToken(
kind,
self.token_list[self.index].kind.clone(),
self.token_list[self.index].value.clone(),
))
})
}
}
// Ref: https://wicg.github.io/urlpattern/#parse-a-pattern-string
pub fn parse_pattern_string<F>(
input: &str,
options: &Options,
encoding_callback: F,
) -> Result<Vec<Part>, Error>
where
F: Fn(&str) -> Result<String, Error>,
{
let token_list = crate::tokenizer::tokenize(
input,
crate::tokenizer::TokenizePolicy::Strict,
)?;
let mut parser = PatternParser {
token_list,
encoding_callback,
segment_wildcard_regexp: options.generate_segment_wildcard_regexp(),
part_list: vec![],
pending_fixed_value: String::new(),
index: 0,
next_numeric_name: 0,
};
while parser.index < parser.token_list.len() {
let char_token = parser.try_consume_token(TokenType::Char);
let mut name_token = parser.try_consume_token(TokenType::Name);
let mut regexp_or_wildcard_token =
parser.try_consume_regexp_or_wildcard_token(name_token.is_none());
if name_token.is_some() || regexp_or_wildcard_token.is_some() {
let mut prefix = String::new();
if let Some(char_token) = char_token {
prefix = char_token.value.to_owned();
}
if !prefix.is_empty() && prefix != options.prefix_code_point {
parser.pending_fixed_value.push_str(&prefix);
prefix = String::new();
}
parser.maybe_add_part_from_pending_fixed_value()?;
let modifier_token = parser.try_consume_modifier_token();
parser.add_part(
&prefix,
name_token,
regexp_or_wildcard_token,
"",
modifier_token,
)?;
continue;
}
let mut fixed_token = char_token;
if fixed_token.is_none() {
fixed_token = parser.try_consume_token(TokenType::EscapedChar);
}
if let Some(fixed_token) = fixed_token {
parser.pending_fixed_value.push_str(&fixed_token.value);
continue;
}
let open_token = parser.try_consume_token(TokenType::Open);
if open_token.is_some() {
let prefix = parser.consume_text();
name_token = parser.try_consume_token(TokenType::Name);
regexp_or_wildcard_token =
parser.try_consume_regexp_or_wildcard_token(name_token.is_none());
let suffix = parser.consume_text();
parser.consume_required_token(TokenType::Close)?;
let modifier_token = parser.try_consume_modifier_token();
parser.add_part(
&prefix,
name_token,
regexp_or_wildcard_token,
&suffix,
modifier_token,
)?;
continue;
}
parser.maybe_add_part_from_pending_fixed_value()?;
parser.consume_required_token(TokenType::End)?;
}
Ok(parser.part_list)
}