@@ -60,11 +60,11 @@ export interface CompileOptions {
6060type TokenType =
6161 | "{"
6262 | "}"
63- | "WILDCARD "
64- | "PARAM "
65- | "CHAR "
66- | "ESCAPED "
67- | "END "
63+ | "wildcard "
64+ | "param "
65+ | "char "
66+ | "escape "
67+ | "end "
6868 // Reserved for use or ambiguous due to past use.
6969 | "("
7070 | ")"
@@ -197,29 +197,27 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
197197 let value = "" ;
198198
199199 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 ] ) ) ;
204203 } else if ( chars [ index ] === '"' ) {
205- let pos = index ;
204+ let quoteStart = index ;
206205
207- while ( index < chars . length ) {
208- if ( chars [ ++ index ] === '"' ) {
206+ while ( index ++ < chars . length ) {
207+ if ( chars [ index ] === '"' ) {
209208 index ++ ;
210- pos = 0 ;
209+ quoteStart = 0 ;
211210 break ;
212211 }
213212
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 ] ;
219217 }
220218
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 ) ;
223221 }
224222 }
225223
@@ -237,54 +235,50 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
237235 if ( type ) {
238236 tokens . push ( { type, index : index ++ , value } ) ;
239237 } else if ( value === "\\" ) {
240- tokens . push ( { type : "ESCAPED " , index : index ++ , value : chars [ index ++ ] } ) ;
238+ tokens . push ( { type : "escape " , index : index ++ , value : chars [ index ++ ] } ) ;
241239 } else if ( value === ":" ) {
242- tokens . push ( { type : "PARAM " , index : index ++ , value : name ( ) } ) ;
240+ tokens . push ( { type : "param " , index : index ++ , value : name ( ) } ) ;
243241 } else if ( value === "*" ) {
244- tokens . push ( { type : "WILDCARD " , index : index ++ , value : name ( ) } ) ;
242+ tokens . push ( { type : "wildcard " , index : index ++ , value : name ( ) } ) ;
245243 } else {
246- tokens . push ( { type : "CHAR " , index : index ++ , value } ) ;
244+ tokens . push ( { type : "char " , index : index ++ , value } ) ;
247245 }
248246 }
249247
250- tokens . push ( { type : "END " , index, value : "" } ) ;
248+ tokens . push ( { type : "end " , index, value : "" } ) ;
251249
252250 function consumeUntil ( endType : TokenType ) : Token [ ] {
253251 const output : Token [ ] = [ ] ;
254252
255253 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 ] ;
266264 }
267- output . push ( { type : "text" , value : encodePath ( path ) } ) ;
268- continue ;
269- }
270265
271- if ( type === "PARAM" ) {
272266 output . push ( {
273- type : "param " ,
274- name : value ,
267+ type : "text " ,
268+ value : encodePath ( path ) ,
275269 } ) ;
276270 continue ;
277271 }
278272
279- if ( type === "WILDCARD " ) {
273+ if ( token . type === "param" || token . type === "wildcard ") {
280274 output . push ( {
281- type : "wildcard" ,
282- name : value ,
275+ type : token . type ,
276+ name : token . value ,
283277 } ) ;
284278 continue ;
285279 }
286280
287- if ( type === "{" ) {
281+ if ( token . type === "{" ) {
288282 output . push ( {
289283 type : "group" ,
290284 tokens : consumeUntil ( "}" ) ,
@@ -293,15 +287,15 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
293287 }
294288
295289 throw new PathError (
296- `Unexpected ${ type } at index ${ index } , expected ${ endType } ` ,
290+ `Unexpected ${ token . type } at index ${ token . index } , expected ${ endType } ` ,
297291 str ,
298292 ) ;
299293 }
300294
301295 return output ;
302296 }
303297
304- return new TokenData ( consumeUntil ( "END " ) , str ) ;
298+ return new TokenData ( consumeUntil ( "end " ) , str ) ;
305299}
306300
307301/**
0 commit comments