@@ -122,78 +122,6 @@ function errorMessage(text: string, originalPath: string | undefined) {
122
122
return message ;
123
123
}
124
124
125
- /**
126
- * Tokenize input string.
127
- */
128
- function lexer ( str : string ) : Iter {
129
- const chars = [ ...str ] ;
130
- const tokens : Array < LexToken > = [ ] ;
131
- let i = 0 ;
132
-
133
- function name ( ) {
134
- let value = "" ;
135
-
136
- if ( ID_START . test ( chars [ ++ i ] ) ) {
137
- value += chars [ i ] ;
138
- while ( ID_CONTINUE . test ( chars [ ++ i ] ) ) {
139
- value += chars [ i ] ;
140
- }
141
- } else if ( chars [ i ] === '"' ) {
142
- let pos = i ;
143
-
144
- while ( i < chars . length ) {
145
- if ( chars [ ++ i ] === '"' ) {
146
- i ++ ;
147
- pos = 0 ;
148
- break ;
149
- }
150
-
151
- if ( chars [ i ] === "\\" ) {
152
- value += chars [ ++ i ] ;
153
- } else {
154
- value += chars [ i ] ;
155
- }
156
- }
157
-
158
- if ( pos ) {
159
- throw new TypeError (
160
- errorMessage ( `Unterminated quote at index ${ pos } ` , str ) ,
161
- ) ;
162
- }
163
- }
164
-
165
- if ( ! value ) {
166
- throw new TypeError (
167
- errorMessage ( `Missing parameter name at index ${ i } ` , str ) ,
168
- ) ;
169
- }
170
-
171
- return value ;
172
- }
173
-
174
- while ( i < chars . length ) {
175
- const value = chars [ i ] ;
176
- const type = SIMPLE_TOKENS [ value ] ;
177
-
178
- if ( type ) {
179
- tokens . push ( { type, index : i ++ , value } ) ;
180
- } else if ( value === "\\" ) {
181
- tokens . push ( { type : "ESCAPED" , index : i ++ , value : chars [ i ++ ] } ) ;
182
- } else if ( value === ":" ) {
183
- const value = name ( ) ;
184
- tokens . push ( { type : "PARAM" , index : i , value } ) ;
185
- } else if ( value === "*" ) {
186
- const value = name ( ) ;
187
- tokens . push ( { type : "WILDCARD" , index : i , value } ) ;
188
- } else {
189
- tokens . push ( { type : "CHAR" , index : i , value : chars [ i ++ ] } ) ;
190
- }
191
- }
192
-
193
- tokens . push ( { type : "END" , index : i , value : "" } ) ;
194
- return new Iter ( tokens , str ) ;
195
- }
196
-
197
125
class Iter {
198
126
private _tokens : Array < LexToken > ;
199
127
private _index = 0 ;
@@ -301,9 +229,73 @@ export class TokenData {
301
229
*/
302
230
export function parse ( str : string , options : ParseOptions = { } ) : TokenData {
303
231
const { encodePath = NOOP_VALUE } = options ;
304
- const it = lexer ( str ) ;
232
+ const chars = [ ...str ] ;
233
+ const tokens : Array < LexToken > = [ ] ;
234
+ let i = 0 ;
235
+
236
+ function name ( ) {
237
+ let value = "" ;
238
+
239
+ if ( ID_START . test ( chars [ ++ i ] ) ) {
240
+ value += chars [ i ] ;
241
+ while ( ID_CONTINUE . test ( chars [ ++ i ] ) ) {
242
+ value += chars [ i ] ;
243
+ }
244
+ } else if ( chars [ i ] === '"' ) {
245
+ let pos = i ;
246
+
247
+ while ( i < chars . length ) {
248
+ if ( chars [ ++ i ] === '"' ) {
249
+ i ++ ;
250
+ pos = 0 ;
251
+ break ;
252
+ }
253
+
254
+ if ( chars [ i ] === "\\" ) {
255
+ value += chars [ ++ i ] ;
256
+ } else {
257
+ value += chars [ i ] ;
258
+ }
259
+ }
260
+
261
+ if ( pos ) {
262
+ throw new TypeError (
263
+ errorMessage ( `Unterminated quote at index ${ pos } ` , str ) ,
264
+ ) ;
265
+ }
266
+ }
267
+
268
+ if ( ! value ) {
269
+ throw new TypeError (
270
+ errorMessage ( `Missing parameter name at index ${ i } ` , str ) ,
271
+ ) ;
272
+ }
273
+
274
+ return value ;
275
+ }
276
+
277
+ while ( i < chars . length ) {
278
+ const value = chars [ i ] ;
279
+ const type = SIMPLE_TOKENS [ value ] ;
280
+
281
+ if ( type ) {
282
+ tokens . push ( { type, index : i ++ , value } ) ;
283
+ } else if ( value === "\\" ) {
284
+ tokens . push ( { type : "ESCAPED" , index : i ++ , value : chars [ i ++ ] } ) ;
285
+ } else if ( value === ":" ) {
286
+ const value = name ( ) ;
287
+ tokens . push ( { type : "PARAM" , index : i , value } ) ;
288
+ } else if ( value === "*" ) {
289
+ const value = name ( ) ;
290
+ tokens . push ( { type : "WILDCARD" , index : i , value } ) ;
291
+ } else {
292
+ tokens . push ( { type : "CHAR" , index : i , value : chars [ i ++ ] } ) ;
293
+ }
294
+ }
295
+
296
+ tokens . push ( { type : "END" , index : i , value : "" } ) ;
305
297
306
- function consume ( endType : TokenType ) : Token [ ] {
298
+ function consume ( it : Iter , endType : TokenType ) : Token [ ] {
307
299
const tokens : Token [ ] = [ ] ;
308
300
309
301
while ( true ) {
@@ -332,7 +324,7 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
332
324
if ( open ) {
333
325
tokens . push ( {
334
326
type : "group" ,
335
- tokens : consume ( "}" ) ,
327
+ tokens : consume ( it , "}" ) ,
336
328
} ) ;
337
329
continue ;
338
330
}
@@ -342,8 +334,8 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
342
334
}
343
335
}
344
336
345
- const tokens = consume ( "END" ) ;
346
- return new TokenData ( tokens , str ) ;
337
+ const it = new Iter ( tokens , str ) ;
338
+ return new TokenData ( consume ( it , "END" ) , str ) ;
347
339
}
348
340
349
341
/**
0 commit comments