@@ -10,7 +10,7 @@ type Cache<T> = Map<number, MemoizationCacheResult<ParserResult<T>>>;
10
10
let currentSource = "" ;
11
11
const allCache : Set < WeakRef < Cache < unknown > > > = new Set ( ) ;
12
12
13
- export class Parser < T > {
13
+ export class Parser < const T > {
14
14
readonly rawParser : InnerParser < T > ;
15
15
constructor ( parser : InnerParser < T > ) {
16
16
const cache : Cache < T > = new Map ( ) ;
@@ -32,13 +32,13 @@ export class Parser<T> {
32
32
}
33
33
return this . rawParser ( 0 ) . map ( ( { value } ) => value ) ;
34
34
}
35
- map < U > ( mapper : ( value : T ) => U ) : Parser < U > {
35
+ map < const U > ( mapper : ( value : T ) => U ) : Parser < U > {
36
36
return new Parser ( ( input ) =>
37
37
this . rawParser ( input )
38
38
. map ( ( { value, length } ) => ( { value : mapper ( value ) , length } ) )
39
39
) ;
40
40
}
41
- mapWithPositionedError < U > ( mapper : ( value : T ) => U ) : Parser < U > {
41
+ mapWithPositionedError < const U > ( mapper : ( value : T ) => U ) : Parser < U > {
42
42
return withPosition ( this )
43
43
. map ( ( value ) => withPositionedError ( ( ) => mapper ( value . value ) , value ) ) ;
44
44
}
@@ -52,7 +52,7 @@ export class Parser<T> {
52
52
. filter ( ( value ) => withPositionedError ( ( ) => mapper ( value . value ) , value ) )
53
53
. map ( ( { value } ) => value ) ;
54
54
}
55
- then < U > ( mapper : ( value : T ) => Parser < U > ) : Parser < U > {
55
+ then < const U > ( mapper : ( value : T ) => Parser < U > ) : Parser < U > {
56
56
return new Parser ( ( position ) =>
57
57
this . rawParser ( position )
58
58
. flatMap ( ( { value, length } ) =>
@@ -74,10 +74,10 @@ export class Parser<T> {
74
74
sortBy ( mapper : ( value : T ) => number ) : Parser < T > {
75
75
return this . sort ( ( left , right ) => mapper ( left ) - mapper ( right ) ) ;
76
76
}
77
- with < U > ( parser : Parser < U > ) : Parser < U > {
77
+ with < const U > ( parser : Parser < U > ) : Parser < U > {
78
78
return sequence ( this , parser ) . map ( ( [ _ , arrayResult ] ) => arrayResult ) ;
79
79
}
80
- skip < U > ( parser : Parser < U > ) : Parser < T > {
80
+ skip < const U > ( parser : Parser < U > ) : Parser < T > {
81
81
return sequence ( this , parser ) . map ( ( [ arrayResult ] ) => arrayResult ) ;
82
82
}
83
83
}
@@ -90,7 +90,7 @@ export class PositionedError extends ArrayResultError {
90
90
this . name = "PositionedError" ;
91
91
}
92
92
}
93
- function withPositionedError < T > ( fn : ( ) => T , position : Position ) {
93
+ function withPositionedError < const T > ( fn : ( ) => T , position : Position ) {
94
94
try {
95
95
return fn ( ) ;
96
96
} catch ( error ) {
@@ -124,16 +124,18 @@ export const nothing: Parser<null> = new Parser(() =>
124
124
new ArrayResult ( [ { value : null , length : 0 } ] )
125
125
) ;
126
126
export const emptyArray : Parser < ReadonlyArray < never > > = nothing . map ( ( ) => [ ] ) ;
127
- export function lookAhead < T > ( parser : Parser < T > ) : Parser < T > {
127
+ export function lookAhead < const T > ( parser : Parser < T > ) : Parser < T > {
128
128
return new Parser ( ( input ) =>
129
129
parser . rawParser ( input )
130
130
. map ( ( { value } ) => ( { value, length : 0 } ) )
131
131
) ;
132
132
}
133
- export function lazy < T > ( parser : ( ) => Parser < T > ) : Parser < T > {
133
+ export function lazy < const T > ( parser : ( ) => Parser < T > ) : Parser < T > {
134
134
return new Parser ( ( input ) => parser ( ) . rawParser ( input ) ) ;
135
135
}
136
- export function choice < T > ( ...choices : ReadonlyArray < Parser < T > > ) : Parser < T > {
136
+ export function choice < const T > (
137
+ ...choices : ReadonlyArray < Parser < T > >
138
+ ) : Parser < T > {
137
139
assertGreater (
138
140
choices . length ,
139
141
1 ,
@@ -143,7 +145,7 @@ export function choice<T>(...choices: ReadonlyArray<Parser<T>>): Parser<T> {
143
145
new ArrayResult ( choices ) . flatMap ( ( parser ) => parser . rawParser ( input ) )
144
146
) ;
145
147
}
146
- export function choiceOnlyOne < T > (
148
+ export function choiceOnlyOne < const T > (
147
149
...choices : ReadonlyArray < Parser < T > >
148
150
) : Parser < T > {
149
151
assertGreater (
@@ -164,13 +166,13 @@ export function choiceOnlyOne<T>(
164
166
empty ,
165
167
) ;
166
168
}
167
- export function optional < T > ( parser : Parser < T > ) : Parser < null | T > {
169
+ export function optional < const T > ( parser : Parser < T > ) : Parser < null | T > {
168
170
return choice ( parser , nothing ) ;
169
171
}
170
- export function optionalAll < T > ( parser : Parser < T > ) : Parser < null | T > {
172
+ export function optionalAll < const T > ( parser : Parser < T > ) : Parser < null | T > {
171
173
return choiceOnlyOne ( parser , nothing ) ;
172
174
}
173
- export function sequence < T extends ReadonlyArray < unknown > > (
175
+ export function sequence < const T extends ReadonlyArray < unknown > > (
174
176
...sequence :
175
177
& Readonly < { [ I in keyof T ] : Parser < T [ I ] > } >
176
178
& Readonly < { length : T [ "length" ] } >
@@ -187,27 +189,33 @@ export function sequence<T extends ReadonlyArray<unknown>>(
187
189
emptyArray ,
188
190
) as Parser < any > ;
189
191
}
190
- export const many = memoize ( < T > ( parser : Parser < T > ) : Parser < ReadonlyArray < T > > =>
192
+ export const many = memoize ( < const T > (
193
+ parser : Parser < T > ,
194
+ ) : Parser < ReadonlyArray < T > > =>
191
195
choice < ReadonlyArray < T > > (
192
196
sequence ( parser , lazy ( lazyEval ( ( ) => many ( parser ) ) ) )
193
197
. map ( ( [ first , rest ] ) => [ first , ...rest ] ) ,
194
198
emptyArray ,
195
199
)
196
200
) ;
197
- export function manyAtLeastOnce < T > (
201
+ export function manyAtLeastOnce < const T > (
198
202
parser : Parser < T > ,
199
203
) : Parser < ReadonlyArray < T > > {
200
204
return sequence ( parser , many ( parser ) )
201
205
. map ( ( [ first , rest ] ) => [ first , ...rest ] ) ;
202
206
}
203
- export const all = memoize ( < T > ( parser : Parser < T > ) : Parser < ReadonlyArray < T > > =>
207
+ export const all = memoize ( < const T > (
208
+ parser : Parser < T > ,
209
+ ) : Parser < ReadonlyArray < T > > =>
204
210
choiceOnlyOne < ReadonlyArray < T > > (
205
211
sequence ( parser , lazy ( lazyEval ( ( ) => all ( parser ) ) ) )
206
212
. map ( ( [ first , rest ] ) => [ first , ...rest ] ) ,
207
213
emptyArray ,
208
214
)
209
215
) ;
210
- export function allAtLeastOnce < T > ( parser : Parser < T > ) : Parser < ReadonlyArray < T > > {
216
+ export function allAtLeastOnce < const T > (
217
+ parser : Parser < T > ,
218
+ ) : Parser < ReadonlyArray < T > > {
211
219
return sequence ( parser , all ( parser ) )
212
220
. map ( ( [ first , rest ] ) => [ first , ...rest ] ) ;
213
221
}
@@ -300,7 +308,7 @@ export const notEnd: Parser<null> = new Parser((position) =>
300
308
) ,
301
309
] )
302
310
) ;
303
- export function withSource < T > (
311
+ export function withSource < const T > (
304
312
parser : Parser < T > ,
305
313
) : Parser < readonly [ value : T , source : string ] > {
306
314
return new Parser ( ( position ) =>
@@ -313,7 +321,7 @@ export function withSource<T>(
313
321
} ) )
314
322
) ;
315
323
}
316
- export function withPosition < T > (
324
+ export function withPosition < const T > (
317
325
parser : Parser < T > ,
318
326
) : Parser < Readonly < { value : T } > & Position > {
319
327
return new Parser ( ( position ) =>
@@ -323,12 +331,12 @@ export function withPosition<T>(
323
331
} ) )
324
332
) ;
325
333
}
326
- export class CheckedParser < T > {
334
+ export class CheckedParser < const T > {
327
335
constructor ( public check : Parser < unknown > , public parser : Parser < T > ) { }
328
- map < U > ( mapper : ( value : T ) => U ) : CheckedParser < U > {
336
+ map < const U > ( mapper : ( value : T ) => U ) : CheckedParser < U > {
329
337
return new CheckedParser ( this . check , this . parser . map ( mapper ) ) ;
330
338
}
331
- mapWithPositionedError < U > ( mapper : ( value : T ) => U ) : CheckedParser < U > {
339
+ mapWithPositionedError < const U > ( mapper : ( value : T ) => U ) : CheckedParser < U > {
332
340
return new CheckedParser (
333
341
this . check ,
334
342
this . parser . mapWithPositionedError ( mapper ) ,
@@ -344,16 +352,16 @@ export class CheckedParser<T> {
344
352
) ;
345
353
}
346
354
}
347
- export function checkedSequence < T , U > (
355
+ export function checkedSequence < const T , const U > (
348
356
check : Parser < T > ,
349
357
rest : Parser < U > ,
350
358
) : CheckedParser < readonly [ T , U ] > {
351
359
return new CheckedParser ( check , sequence ( check , rest ) ) ;
352
360
}
353
- export function checkedAsWhole < T > ( parser : Parser < T > ) : CheckedParser < T > {
361
+ export function checkedAsWhole < const T > ( parser : Parser < T > ) : CheckedParser < T > {
354
362
return new CheckedParser ( parser , parser ) ;
355
363
}
356
- export function choiceWithCheck < T > (
364
+ export function choiceWithCheck < const T > (
357
365
...choices : ReadonlyArray < CheckedParser < T > >
358
366
) : Parser < T > {
359
367
return new Parser ( ( position ) => {
@@ -369,13 +377,13 @@ export function choiceWithCheck<T>(
369
377
return ArrayResult . errors ( errors ) ;
370
378
} ) ;
371
379
}
372
- export function optionalWithCheck < T > (
380
+ export function optionalWithCheck < const T > (
373
381
parser : CheckedParser < T > ,
374
382
) : Parser < null | T > {
375
383
return choiceWithCheck ( parser , checkedAsWhole ( nothing ) ) ;
376
384
}
377
385
export const allWithCheck = memoize (
378
- < T > ( parser : CheckedParser < T > ) : Parser < ReadonlyArray < T > > =>
386
+ < const T > ( parser : CheckedParser < T > ) : Parser < ReadonlyArray < T > > =>
379
387
choiceWithCheck < ReadonlyArray < T > > (
380
388
new CheckedParser (
381
389
parser . check ,
@@ -385,7 +393,7 @@ export const allWithCheck = memoize(
385
393
checkedAsWhole ( emptyArray ) ,
386
394
) ,
387
395
) ;
388
- export function allAtLeastOnceWithCheck < T > (
396
+ export function allAtLeastOnceWithCheck < const T > (
389
397
parser : CheckedParser < T > ,
390
398
) : Parser < ReadonlyArray < T > > {
391
399
return sequence ( parser . parser , allWithCheck ( parser ) )
0 commit comments