@@ -122,51 +122,6 @@ function errorMessage(text: string, originalPath: string | undefined) {
122
122
return message ;
123
123
}
124
124
125
- class Iter {
126
- private _tokens : Array < LexToken > ;
127
- private _index = 0 ;
128
-
129
- constructor (
130
- tokens : Array < LexToken > ,
131
- private originalPath : string ,
132
- ) {
133
- this . _index = 0 ;
134
- this . _tokens = tokens ;
135
- }
136
-
137
- peek ( ) : LexToken {
138
- return this . _tokens [ this . _index ] ;
139
- }
140
-
141
- tryConsume ( type : TokenType ) : string | undefined {
142
- const token = this . peek ( ) ;
143
- if ( token . type !== type ) return ;
144
- this . _index ++ ;
145
- return token . value ;
146
- }
147
-
148
- consume ( type : TokenType ) : string {
149
- const value = this . tryConsume ( type ) ;
150
- if ( value !== undefined ) return value ;
151
- const { type : nextType , index } = this . peek ( ) ;
152
- throw new TypeError (
153
- errorMessage (
154
- `Unexpected ${ nextType } at index ${ index } , expected ${ type } ` ,
155
- this . originalPath ,
156
- ) ,
157
- ) ;
158
- }
159
-
160
- text ( ) : string {
161
- let result = "" ;
162
- let value : string | undefined ;
163
- while ( ( value = this . tryConsume ( "CHAR" ) || this . tryConsume ( "ESCAPED" ) ) ) {
164
- result += value ;
165
- }
166
- return result ;
167
- }
168
- }
169
-
170
125
/**
171
126
* Plain text.
172
127
*/
@@ -232,6 +187,7 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
232
187
const chars = [ ...str ] ;
233
188
const tokens : Array < LexToken > = [ ] ;
234
189
let i = 0 ;
190
+ let index = 0 ;
235
191
236
192
function name ( ) {
237
193
let value = "" ;
@@ -295,14 +251,46 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
295
251
296
252
tokens . push ( { type : "END" , index : i , value : "" } ) ;
297
253
298
- function consume ( it : Iter , endType : TokenType ) : Token [ ] {
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
+ }
285
+
286
+ function consumeUntil ( endType : TokenType ) : Token [ ] {
299
287
const tokens : Token [ ] = [ ] ;
300
288
301
289
while ( true ) {
302
- const path = it . text ( ) ;
290
+ const path = text ( ) ;
303
291
if ( path ) tokens . push ( { type : "text" , value : encodePath ( path ) } ) ;
304
292
305
- const param = it . tryConsume ( "PARAM" ) ;
293
+ const param = tryConsume ( "PARAM" ) ;
306
294
if ( param ) {
307
295
tokens . push ( {
308
296
type : "param" ,
@@ -311,7 +299,7 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
311
299
continue ;
312
300
}
313
301
314
- const wildcard = it . tryConsume ( "WILDCARD" ) ;
302
+ const wildcard = tryConsume ( "WILDCARD" ) ;
315
303
if ( wildcard ) {
316
304
tokens . push ( {
317
305
type : "wildcard" ,
@@ -320,22 +308,21 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
320
308
continue ;
321
309
}
322
310
323
- const open = it . tryConsume ( "{" ) ;
311
+ const open = tryConsume ( "{" ) ;
324
312
if ( open ) {
325
313
tokens . push ( {
326
314
type : "group" ,
327
- tokens : consume ( it , "}" ) ,
315
+ tokens : consumeUntil ( "}" ) ,
328
316
} ) ;
329
317
continue ;
330
318
}
331
319
332
- it . consume ( endType ) ;
320
+ consume ( endType ) ;
333
321
return tokens ;
334
322
}
335
323
}
336
324
337
- const it = new Iter ( tokens , str ) ;
338
- return new TokenData ( consume ( it , "END" ) , str ) ;
325
+ return new TokenData ( consumeUntil ( "END" ) , str ) ;
339
326
}
340
327
341
328
/**
0 commit comments