@@ -60,11 +60,11 @@ export interface CompileOptions {
60
60
type TokenType =
61
61
| "{"
62
62
| "}"
63
- | "WILDCARD "
64
- | "PARAM "
65
- | "CHAR "
66
- | "ESCAPED "
67
- | "END "
63
+ | "wildcard "
64
+ | "param "
65
+ | "char "
66
+ | "escape "
67
+ | "end "
68
68
// Reserved for use or ambiguous due to past use.
69
69
| "("
70
70
| ")"
@@ -197,29 +197,27 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
197
197
let value = "" ;
198
198
199
199
if ( ID_START . test ( chars [ index ] ) ) {
200
- value += chars [ index ] ;
201
- while ( ID_CONTINUE . test ( chars [ ++ index ] ) ) {
202
- value += chars [ index ] ;
203
- }
200
+ do {
201
+ value += chars [ index ++ ] ;
202
+ } while ( ID_CONTINUE . test ( chars [ index ] ) ) ;
204
203
} else if ( chars [ index ] === '"' ) {
205
- let pos = index ;
204
+ let quoteStart = index ;
206
205
207
- while ( index < chars . length ) {
208
- if ( chars [ ++ index ] === '"' ) {
206
+ while ( index ++ < chars . length ) {
207
+ if ( chars [ index ] === '"' ) {
209
208
index ++ ;
210
- pos = 0 ;
209
+ quoteStart = 0 ;
211
210
break ;
212
211
}
213
212
214
- if ( chars [ index ] === "\\" ) {
215
- value += chars [ ++ index ] ;
216
- } else {
217
- value += chars [ index ] ;
218
- }
213
+ // Increment over escape characters.
214
+ if ( chars [ index ] === "\\" ) index ++ ;
215
+
216
+ value += chars [ index ] ;
219
217
}
220
218
221
- if ( pos ) {
222
- throw new PathError ( `Unterminated quote at index ${ pos } ` , str ) ;
219
+ if ( quoteStart ) {
220
+ throw new PathError ( `Unterminated quote at index ${ quoteStart } ` , str ) ;
223
221
}
224
222
}
225
223
@@ -237,54 +235,50 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
237
235
if ( type ) {
238
236
tokens . push ( { type, index : index ++ , value } ) ;
239
237
} else if ( value === "\\" ) {
240
- tokens . push ( { type : "ESCAPED " , index : index ++ , value : chars [ index ++ ] } ) ;
238
+ tokens . push ( { type : "escape " , index : index ++ , value : chars [ index ++ ] } ) ;
241
239
} else if ( value === ":" ) {
242
- tokens . push ( { type : "PARAM " , index : index ++ , value : name ( ) } ) ;
240
+ tokens . push ( { type : "param " , index : index ++ , value : name ( ) } ) ;
243
241
} else if ( value === "*" ) {
244
- tokens . push ( { type : "WILDCARD " , index : index ++ , value : name ( ) } ) ;
242
+ tokens . push ( { type : "wildcard " , index : index ++ , value : name ( ) } ) ;
245
243
} else {
246
- tokens . push ( { type : "CHAR " , index : index ++ , value } ) ;
244
+ tokens . push ( { type : "char " , index : index ++ , value } ) ;
247
245
}
248
246
}
249
247
250
- tokens . push ( { type : "END " , index, value : "" } ) ;
248
+ tokens . push ( { type : "end " , index, value : "" } ) ;
251
249
252
250
function consumeUntil ( endType : TokenType ) : Token [ ] {
253
251
const output : Token [ ] = [ ] ;
254
252
255
253
while ( true ) {
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 ;
254
+ const token = tokens [ pos ++ ] ;
255
+ if ( token . type === endType ) break ;
256
+
257
+ if ( token . type === "char " || token . type === "escape " ) {
258
+ let path = token . value ;
259
+ let cur = tokens [ pos ] ;
260
+
261
+ while ( cur . type === "char" || cur . type === "escape ") {
262
+ path += cur . value ;
263
+ cur = tokens [ ++ pos ] ;
266
264
}
267
- output . push ( { type : "text" , value : encodePath ( path ) } ) ;
268
- continue ;
269
- }
270
265
271
- if ( type === "PARAM" ) {
272
266
output . push ( {
273
- type : "param " ,
274
- name : value ,
267
+ type : "text " ,
268
+ value : encodePath ( path ) ,
275
269
} ) ;
276
270
continue ;
277
271
}
278
272
279
- if ( type === "WILDCARD " ) {
273
+ if ( token . type === "param" || token . type === "wildcard ") {
280
274
output . push ( {
281
- type : "wildcard" ,
282
- name : value ,
275
+ type : token . type ,
276
+ name : token . value ,
283
277
} ) ;
284
278
continue ;
285
279
}
286
280
287
- if ( type === "{" ) {
281
+ if ( token . type === "{" ) {
288
282
output . push ( {
289
283
type : "group" ,
290
284
tokens : consumeUntil ( "}" ) ,
@@ -293,15 +287,15 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
293
287
}
294
288
295
289
throw new PathError (
296
- `Unexpected ${ type } at index ${ index } , expected ${ endType } ` ,
290
+ `Unexpected ${ token . type } at index ${ token . index } , expected ${ endType } ` ,
297
291
str ,
298
292
) ;
299
293
}
300
294
301
295
return output ;
302
296
}
303
297
304
- return new TokenData ( consumeUntil ( "END " ) , str ) ;
298
+ return new TokenData ( consumeUntil ( "end " ) , str ) ;
305
299
}
306
300
307
301
/**
0 commit comments