@@ -186,31 +186,31 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
186
186
const { encodePath = NOOP_VALUE } = options ;
187
187
const chars = [ ...str ] ;
188
188
const tokens : Array < LexToken > = [ ] ;
189
- let i = 0 ;
190
189
let index = 0 ;
190
+ let pos = 0 ;
191
191
192
192
function name ( ) {
193
193
let value = "" ;
194
194
195
- if ( ID_START . test ( chars [ ++ i ] ) ) {
196
- value += chars [ i ] ;
197
- while ( ID_CONTINUE . test ( chars [ ++ i ] ) ) {
198
- value += chars [ i ] ;
195
+ if ( ID_START . test ( chars [ index ] ) ) {
196
+ value += chars [ index ] ;
197
+ while ( ID_CONTINUE . test ( chars [ ++ index ] ) ) {
198
+ value += chars [ index ] ;
199
199
}
200
- } else if ( chars [ i ] === '"' ) {
201
- let pos = i ;
200
+ } else if ( chars [ index ] === '"' ) {
201
+ let pos = index ;
202
202
203
- while ( i < chars . length ) {
204
- if ( chars [ ++ i ] === '"' ) {
205
- i ++ ;
203
+ while ( index < chars . length ) {
204
+ if ( chars [ ++ index ] === '"' ) {
205
+ index ++ ;
206
206
pos = 0 ;
207
207
break ;
208
208
}
209
209
210
- if ( chars [ i ] === "\\" ) {
211
- value += chars [ ++ i ] ;
210
+ if ( chars [ index ] === "\\" ) {
211
+ value += chars [ ++ index ] ;
212
212
} else {
213
- value += chars [ i ] ;
213
+ value += chars [ index ] ;
214
214
}
215
215
}
216
216
@@ -223,103 +223,84 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
223
223
224
224
if ( ! value ) {
225
225
throw new TypeError (
226
- errorMessage ( `Missing parameter name at index ${ i } ` , str ) ,
226
+ errorMessage ( `Missing parameter name at index ${ index } ` , str ) ,
227
227
) ;
228
228
}
229
229
230
230
return value ;
231
231
}
232
232
233
- while ( i < chars . length ) {
234
- const value = chars [ i ] ;
233
+ while ( index < chars . length ) {
234
+ const value = chars [ index ] ;
235
235
const type = SIMPLE_TOKENS [ value ] ;
236
236
237
237
if ( type ) {
238
- tokens . push ( { type, index : i ++ , value } ) ;
238
+ tokens . push ( { type, index : index ++ , value } ) ;
239
239
} else if ( value === "\\" ) {
240
- tokens . push ( { type : "ESCAPED" , index : i ++ , value : chars [ i ++ ] } ) ;
240
+ tokens . push ( { type : "ESCAPED" , index : index ++ , value : chars [ index ++ ] } ) ;
241
241
} else if ( value === ":" ) {
242
- const value = name ( ) ;
243
- tokens . push ( { type : "PARAM" , index : i , value } ) ;
242
+ tokens . push ( { type : "PARAM" , index : index ++ , value : name ( ) } ) ;
244
243
} else if ( value === "*" ) {
245
- const value = name ( ) ;
246
- tokens . push ( { type : "WILDCARD" , index : i , value } ) ;
244
+ tokens . push ( { type : "WILDCARD" , index : index ++ , value : name ( ) } ) ;
247
245
} else {
248
- tokens . push ( { type : "CHAR" , index : i , value : chars [ i ++ ] } ) ;
246
+ tokens . push ( { type : "CHAR" , index : index ++ , value } ) ;
249
247
}
250
248
}
251
249
252
- tokens . push ( { type : "END" , index : i , value : "" } ) ;
253
-
254
- function peek ( ) : LexToken {
255
- return tokens [ index ] ;
256
- }
257
-
258
- function tryConsume ( type : TokenType ) : string | undefined {
259
- const token = peek ( ) ;
260
- if ( token . type !== type ) return ;
261
- index ++ ;
262
- return token . value ;
263
- }
264
-
265
- function consume ( type : TokenType ) : string {
266
- const value = tryConsume ( type ) ;
267
- if ( value !== undefined ) return value ;
268
- const { type : nextType , index } = peek ( ) ;
269
- throw new TypeError (
270
- errorMessage (
271
- `Unexpected ${ nextType } at index ${ index } , expected ${ type } ` ,
272
- str ,
273
- ) ,
274
- ) ;
275
- }
276
-
277
- function text ( ) : string {
278
- let result = "" ;
279
- let value : string | undefined ;
280
- while ( ( value = tryConsume ( "CHAR" ) || tryConsume ( "ESCAPED" ) ) ) {
281
- result += value ;
282
- }
283
- return result ;
284
- }
250
+ tokens . push ( { type : "END" , index, value : "" } ) ;
285
251
286
252
function consumeUntil ( endType : TokenType ) : Token [ ] {
287
- const tokens : Token [ ] = [ ] ;
253
+ const output : Token [ ] = [ ] ;
288
254
289
255
while ( true ) {
290
- const path = text ( ) ;
291
- if ( path ) tokens . push ( { type : "text" , value : encodePath ( path ) } ) ;
256
+ const { type, value, index } = tokens [ pos ++ ] ;
257
+ if ( type === endType ) break ;
258
+
259
+ if ( type === "CHAR" || type === "ESCAPED" ) {
260
+ let path = value ;
261
+ while ( true ) {
262
+ const next = tokens [ pos ] ;
263
+ if ( next . type !== "CHAR" && next . type !== "ESCAPED" ) break ;
264
+ pos ++ ;
265
+ path += next . value ;
266
+ }
267
+ output . push ( { type : "text" , value : encodePath ( path ) } ) ;
268
+ continue ;
269
+ }
292
270
293
- const param = tryConsume ( "PARAM" ) ;
294
- if ( param ) {
295
- tokens . push ( {
271
+ if ( type === "PARAM" ) {
272
+ output . push ( {
296
273
type : "param" ,
297
- name : param ,
274
+ name : value ,
298
275
} ) ;
299
276
continue ;
300
277
}
301
278
302
- const wildcard = tryConsume ( "WILDCARD" ) ;
303
- if ( wildcard ) {
304
- tokens . push ( {
279
+ if ( type === "WILDCARD" ) {
280
+ output . push ( {
305
281
type : "wildcard" ,
306
- name : wildcard ,
282
+ name : value ,
307
283
} ) ;
308
284
continue ;
309
285
}
310
286
311
- const open = tryConsume ( "{" ) ;
312
- if ( open ) {
313
- tokens . push ( {
287
+ if ( type === "{" ) {
288
+ output . push ( {
314
289
type : "group" ,
315
290
tokens : consumeUntil ( "}" ) ,
316
291
} ) ;
317
292
continue ;
318
293
}
319
294
320
- consume ( endType ) ;
321
- return tokens ;
295
+ throw new TypeError (
296
+ errorMessage (
297
+ `Unexpected ${ type } at index ${ index } , expected ${ endType } ` ,
298
+ str ,
299
+ ) ,
300
+ ) ;
322
301
}
302
+
303
+ return output ;
323
304
}
324
305
325
306
return new TokenData ( consumeUntil ( "END" ) , str ) ;
0 commit comments