-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructor_type_parser.go
More file actions
457 lines (371 loc) · 11.7 KB
/
constructor_type_parser.go
File metadata and controls
457 lines (371 loc) · 11.7 KB
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package urlpattern
import (
"regexp"
"golang.org/x/exp/utf8string"
)
type state uint8
// https://urlpattern.spec.whatwg.org/#constructor-string-parsing
type constructorTypeParser struct {
input utf8string.String
tokenList []token
result URLPatternInit
componentStart int
tokenIndex int
tokenIncrement int
groupDepth int
hostnameIPv6BracketDepth int
protocolMatchesASpecialScheme bool
state state
}
// https://urlpattern.spec.whatwg.org/#constructor-string-parser-state
const (
stateInit state = iota
stateProtocol
sateAuthority
stateUsername
statePassword
stateHostname
statePort
statePathname
stateSearch
stateHash
stateDone
)
// https://urlpattern.spec.whatwg.org/#parse-a-constructor-string
func newConstructorTypeParser(input string, tokenList []token) constructorTypeParser {
return constructorTypeParser{
input: *utf8string.NewString(input),
tokenList: tokenList,
result: URLPatternInit{},
tokenIncrement: 1,
state: stateInit,
}
}
// https://urlpattern.spec.whatwg.org/#constructor-string-parsing
func parseConstructorString(input string) (*URLPatternInit, error) {
tl, err := tokenize(input, tokenizePolicyLenient)
if err != nil {
return nil, err
}
p := newConstructorTypeParser(input, tl)
tlLen := len(p.tokenList)
for p.tokenIndex < tlLen {
p.tokenIncrement = 1
if p.tokenList[p.tokenIndex].tType == tokenEnd {
if p.state == stateInit {
p.rewind()
if p.isHashPrefix() {
p.changeState(stateHash, 1)
} else if p.isSearchPrefix() {
p.changeState(stateSearch, 1)
//p.result.Hash = ""
} else {
p.changeState(statePathname, 0)
//p.result.Hash = ""
//p.result.Search = ""
}
p.tokenIndex += p.tokenIncrement
continue
}
if p.state == sateAuthority {
p.rewindAndSetState(stateHostname)
p.tokenIndex += p.tokenIncrement
continue
}
p.changeState(stateDone, 0)
break
}
if p.isGroupOpen() {
p.groupDepth++
p.tokenIndex += p.tokenIncrement
continue
}
if p.groupDepth > 0 {
if p.isGroupClose() {
p.groupDepth--
} else {
p.tokenIndex += p.tokenIncrement
continue
}
}
// Switch on parser’s state and run the associated steps:
switch p.state {
case stateInit:
if p.isProtocolSuffix() {
p.rewindAndSetState(stateProtocol)
}
case stateProtocol:
if p.isProtocolSuffix() {
if err := p.computeProtocolMatchesSpecialSchemeFlag(); err != nil {
return nil, err
}
nextState := statePathname
skip := 1
if p.nextIsAuthoritySlashes() {
nextState = sateAuthority
skip = 3
} else if p.protocolMatchesASpecialScheme {
nextState = sateAuthority
}
p.changeState(nextState, skip)
}
case sateAuthority:
if p.isIdentityTerminator() {
p.rewindAndSetState(stateUsername)
} else if p.isPathnameStart() ||
p.isSearchPrefix() ||
p.isHashPrefix() {
p.rewindAndSetState(stateHostname)
}
case stateUsername:
if p.isPasswordPrefix() {
p.changeState(statePassword, 1)
} else if p.isIdentityTerminator() {
p.changeState(stateHostname, 1)
}
case statePassword:
if p.isIdentityTerminator() {
p.changeState(stateHostname, 1)
}
case stateHostname:
if p.isIPV6Open() {
p.hostnameIPv6BracketDepth++
} else if p.isIPV6Close() {
p.hostnameIPv6BracketDepth--
} else if p.isPortPrefix() && p.hostnameIPv6BracketDepth == 0 {
p.changeState(statePort, 1)
} else if p.isPathnameStart() {
p.changeState(statePathname, 0)
} else if p.isSearchPrefix() {
p.changeState(stateSearch, 1)
} else if p.isHashPrefix() {
p.changeState(stateHash, 1)
}
case statePort:
if p.isPathnameStart() {
p.changeState(statePathname, 0)
} else if p.isSearchPrefix() {
p.changeState(stateSearch, 1)
} else if p.isHashPrefix() {
p.changeState(stateHash, 1)
}
case statePathname:
if p.isSearchPrefix() {
p.changeState(stateSearch, 1)
} else if p.isHashPrefix() {
p.changeState(stateHash, 1)
}
case stateSearch:
if p.isHashPrefix() {
p.changeState(stateHash, 1)
}
case stateHash:
// noop
case stateDone:
// Assert: This step is never reached.
panic("done state must never be reached")
}
p.tokenIndex += p.tokenIncrement
}
// If parser’s result contains "hostname" and not "port", then set parser’s result["port"] to the empty string.
if p.result.Hostname != nil && p.result.Port == nil {
es := ""
p.result.Port = &es
}
return &p.result, nil
}
// https://urlpattern.spec.whatwg.org/#rewind
func (p *constructorTypeParser) rewind() {
p.tokenIndex = p.componentStart
p.tokenIncrement = 0
}
// https://urlpattern.spec.whatwg.org/#rewind-and-set-state
func (p *constructorTypeParser) rewindAndSetState(s state) {
p.rewind()
p.state = s
}
// https://urlpattern.spec.whatwg.org/#is-a-hash-prefix
func (p *constructorTypeParser) isHashPrefix() bool {
return p.isNonSpecialPatternChar(p.tokenIndex, "#")
}
// https://urlpattern.spec.whatwg.org/#is-a-search-prefix
func (p *constructorTypeParser) isSearchPrefix() bool {
if p.isNonSpecialPatternChar(p.tokenIndex, "?") {
return true
}
if p.tokenList[p.tokenIndex].value != "?" {
return false
}
previousIndex := p.tokenIndex - 1
if previousIndex < 0 {
return true
}
previousToken := p.getSafeToken(previousIndex)
switch previousToken.tType {
case tokenName:
return false
case tokenRegexp:
return false
case tokenClose:
return false
case tokenAsterisk:
return false
}
return true
}
// https://urlpattern.spec.whatwg.org/#is-a-group-open
func (p *constructorTypeParser) isGroupOpen() bool {
return p.tokenList[p.tokenIndex].tType == tokenOpen
}
// https://urlpattern.spec.whatwg.org/#is-a-group-close
func (p *constructorTypeParser) isGroupClose() bool {
return p.tokenList[p.tokenIndex].tType == tokenClose
}
// https://urlpattern.spec.whatwg.org/#is-a-non-special-pattern-char
func (p *constructorTypeParser) isNonSpecialPatternChar(index int, value string) bool {
token := p.getSafeToken(index)
if token.value != value {
return false
}
return token.tType == tokenChar || token.tType == tokenEscapedChar || token.tType == tokenInvalidChar
}
// https://urlpattern.spec.whatwg.org/#get-a-safe-token
func (p *constructorTypeParser) getSafeToken(index int) token {
len := len(p.tokenList)
if index < len {
return p.tokenList[index]
}
// Assert: parser's token list's size is greater than or equal to 1.
return p.tokenList[len-1]
}
// https://urlpattern.spec.whatwg.org/#change-state
func (p *constructorTypeParser) changeState(newState state, skip int) {
v := p.makeComponentString()
// ignore sInit, authority and done
switch p.state {
case stateProtocol:
p.result.Protocol = &v
case stateUsername:
p.result.Username = &v
case statePassword:
p.result.Password = &v
case stateHostname:
p.result.Hostname = &v
case statePort:
p.result.Port = &v
case statePathname:
p.result.Pathname = &v
case stateSearch:
p.result.Search = &v
case stateHash:
p.result.Hash = &v
}
if p.state != stateInit && newState != stateDone {
es := ""
// If parser’s state is "protocol", "authority", "username", or "password"; new state is "port", "pathname", "search", or "hash"; and parser’s result["hostname"] does not exist, then set parser’s result["hostname"] to the empty string.
if p.result.Hostname == nil &&
(p.state == stateProtocol || p.state == sateAuthority || p.state == stateUsername || p.state == statePassword) &&
(newState == statePort || newState == statePathname || newState == stateSearch || newState == stateHash) {
p.result.Hostname = &es
}
if p.result.Pathname == nil &&
(p.state == stateProtocol || p.state == sateAuthority || p.state == stateUsername || p.state == statePassword || p.state == stateHostname || p.state == statePort) &&
(newState == stateSearch || newState == stateHash) {
if p.protocolMatchesASpecialScheme {
sl := "/"
p.result.Pathname = &sl
} else {
p.result.Pathname = &es
}
}
if p.result.Search == nil &&
(p.state == stateProtocol || p.state == sateAuthority || p.state == stateUsername || p.state == statePassword || p.state == stateHostname || p.state == statePort || p.state == statePathname) &&
(newState == stateHash) {
p.result.Search = &es
}
}
p.state = newState
p.tokenIndex = p.tokenIndex + skip
p.componentStart = p.tokenIndex
p.tokenIncrement = 0
}
// https://urlpattern.spec.whatwg.org/#make-a-component-string
func (p *constructorTypeParser) makeComponentString() string {
token := p.tokenList[p.tokenIndex]
componentStartToken := p.getSafeToken(int(p.componentStart))
componentStartInputIndex := componentStartToken.index
endIndex := token.index
return p.input.Slice(componentStartInputIndex, endIndex)
}
// https://urlpattern.spec.whatwg.org/#is-a-protocol-suffix
func (p *constructorTypeParser) isProtocolSuffix() bool {
return p.isNonSpecialPatternChar(p.tokenIndex, ":")
}
// https://urlpattern.spec.whatwg.org/#compute-protocol-matches-a-special-scheme-flag
func (p *constructorTypeParser) computeProtocolMatchesSpecialSchemeFlag() error {
protocol := p.makeComponentString()
protocolComponent, err := compileComponent(protocol, canonicalizeProtocol, options{})
if err != nil {
return err
}
if protocolComponent.protocolComponentMatchesSpecialScheme() {
p.protocolMatchesASpecialScheme = true
}
return nil
}
// https://urlpattern.spec.whatwg.org/#next-is-authority-slashes
func (p *constructorTypeParser) nextIsAuthoritySlashes() bool {
return p.isNonSpecialPatternChar(p.tokenIndex+1, "/") && p.isNonSpecialPatternChar(p.tokenIndex+2, "/")
}
// https://urlpattern.spec.whatwg.org/#is-an-identity-terminator
func (p *constructorTypeParser) isIdentityTerminator() bool {
return p.isNonSpecialPatternChar(p.tokenIndex, "@")
}
// https://urlpattern.spec.whatwg.org/#is-a-pathname-start
func (p *constructorTypeParser) isPathnameStart() bool {
return p.isNonSpecialPatternChar(p.tokenIndex, "/")
}
// https://urlpattern.spec.whatwg.org/#is-a-password-prefix
func (p *constructorTypeParser) isPasswordPrefix() bool {
return p.isNonSpecialPatternChar(p.tokenIndex, ":")
}
// https://urlpattern.spec.whatwg.org/#is-a-port-prefix
func (p *constructorTypeParser) isPortPrefix() bool {
return p.isNonSpecialPatternChar(p.tokenIndex, ":")
}
// https://urlpattern.spec.whatwg.org/#is-an-ipv6-open
func (p *constructorTypeParser) isIPV6Open() bool {
return p.isNonSpecialPatternChar(p.tokenIndex, "[")
}
// https://urlpattern.spec.whatwg.org/#is-an-ipv6-close
func (p *constructorTypeParser) isIPV6Close() bool {
return p.isNonSpecialPatternChar(p.tokenIndex, "]")
}
// https://urlpattern.spec.whatwg.org/#compile-a-component
func compileComponent(input string, encodencodingCallback encodingCallback, options options) (*component, error) {
partList, err := parsePatternString(input, options, encodencodingCallback)
if err != nil {
return nil, err
}
// Let (regular expression string, name list) be the result of running generate a regular expression and name list given part list and options.
regularExpressionString, nameList, err := partList.generateRegularExpressionAndNameList(options)
if err != nil {
return nil, err
}
regularExpression, err := regexp.Compile(regularExpressionString)
if err != nil {
return nil, err
}
patternString, err := partList.generatePatternString(options)
if err != nil {
return nil, err
}
var hasRegexpGroups bool
for _, part := range partList {
if part.pType == partRegexp {
hasRegexpGroups = true
break
}
}
return &component{patternString, regularExpression, nameList, hasRegexpGroups}, nil
}