@@ -3,9 +3,9 @@ import { assertGreaterOrEqual } from "@std/assert/greater-or-equal";
3
3
import { MemoizationCacheResult , memoize } from "@std/cache/memoize" ;
4
4
import { ArrayResult , ArrayResultError } from "../array_result.ts" ;
5
5
6
- type Source = Readonly < { source : string ; position : number } > ;
6
+ type Input = Readonly < { source : string ; position : number } > ;
7
7
type ParserResult < T > = ArrayResult < Readonly < { value : T ; length : number } > > ;
8
- type InnerParser < T > = ( input : Source ) => ParserResult < T > ;
8
+ type InnerParser < T > = ( input : Input ) => ParserResult < T > ;
9
9
10
10
let source = "" ;
11
11
const allMemo : Set < WeakRef < SourceMemo < unknown > > > = new Set ( ) ;
@@ -25,24 +25,24 @@ class SourceMemo<T> {
25
25
constructor ( ) {
26
26
allMemo . add ( new WeakRef ( this ) ) ;
27
27
}
28
- set ( key : Source , value : T ) : void {
28
+ set ( key : Input , value : T ) : void {
29
29
if ( source !== key . source ) {
30
30
source = key . source ;
31
31
clearCache ( ) ;
32
32
}
33
33
this . #map. set ( key . position , value ) ;
34
34
}
35
- get ( key : Source ) : undefined | T {
35
+ get ( key : Input ) : undefined | T {
36
36
if ( source === key . source ) {
37
37
return this . #map. get ( key . position ) ;
38
38
} else {
39
39
return undefined ;
40
40
}
41
41
}
42
- has ( key : Source ) : boolean {
42
+ has ( key : Input ) : boolean {
43
43
return source === key . source && this . #map. has ( key . position ) ;
44
44
}
45
- delete ( key : Source ) : void {
45
+ delete ( key : Input ) : void {
46
46
if ( source === key . source ) {
47
47
this . #map. delete ( key . position ) ;
48
48
}
@@ -56,41 +56,41 @@ export class Parser<T> {
56
56
constructor ( parser : InnerParser < T > ) {
57
57
this . rawParser = memoize <
58
58
InnerParser < T > ,
59
- Source ,
59
+ Input ,
60
60
SourceMemo < MemoizationCacheResult < ParserResult < T > > >
61
61
> (
62
- ( source ) => {
63
- assertGreaterOrEqual ( source . source . length , source . position ) ;
64
- return parser ( source ) ;
62
+ ( input ) => {
63
+ assertGreaterOrEqual ( input . source . length , input . position ) ;
64
+ return parser ( input ) ;
65
65
} ,
66
66
{ cache : new SourceMemo ( ) } ,
67
67
) ;
68
68
}
69
69
generateParser ( ) : ( source : string ) => ArrayResult < T > {
70
- return ( source ) =>
71
- this . rawParser ( { source, position : 0 } )
70
+ return ( input ) =>
71
+ this . rawParser ( { source : input , position : 0 } )
72
72
. map ( ( { value } ) => value ) ;
73
73
}
74
74
map < U > ( mapper : ( value : T ) => U ) : Parser < U > {
75
- return new Parser ( ( source ) =>
76
- this . rawParser ( source )
75
+ return new Parser ( ( input ) =>
76
+ this . rawParser ( input )
77
77
. map ( ( { value, length } ) => ( { value : mapper ( value ) , length } ) )
78
78
) ;
79
79
}
80
80
filter ( mapper : ( value : T ) => boolean ) : Parser < T > {
81
- return new Parser ( ( source ) =>
82
- this . rawParser ( source ) . filter ( ( { value } ) => mapper ( value ) )
81
+ return new Parser ( ( input ) =>
82
+ this . rawParser ( input ) . filter ( ( { value } ) => mapper ( value ) )
83
83
) ;
84
84
}
85
85
then < U > ( mapper : ( value : T ) => Parser < U > ) : Parser < U > {
86
- return new Parser ( ( source ) =>
86
+ return new Parser ( ( input ) =>
87
87
this
88
- . rawParser ( source )
88
+ . rawParser ( input )
89
89
. flatMap ( ( { value, length } ) =>
90
90
mapper ( value )
91
91
. rawParser ( {
92
- source : source . source ,
93
- position : source . position + length ,
92
+ source : input . source ,
93
+ position : input . position + length ,
94
94
} )
95
95
. map ( ( { value, length : addedLength } ) => ( {
96
96
value,
@@ -100,8 +100,8 @@ export class Parser<T> {
100
100
) ;
101
101
}
102
102
sort ( comparer : ( left : T , right : T ) => number ) : Parser < T > {
103
- return new Parser ( ( source ) =>
104
- this . rawParser ( source )
103
+ return new Parser ( ( input ) =>
104
+ this . rawParser ( input )
105
105
. sort ( ( left , right ) => comparer ( left . value , right . value ) )
106
106
) ;
107
107
}
@@ -136,22 +136,22 @@ export const nothing = new Parser(() =>
136
136
) ;
137
137
export const emptyArray = nothing . map ( ( ) => [ ] ) ;
138
138
export function lookAhead < T > ( parser : Parser < T > ) : Parser < T > {
139
- return new Parser ( ( source ) =>
140
- parser . rawParser ( source )
139
+ return new Parser ( ( input ) =>
140
+ parser . rawParser ( input )
141
141
. map ( ( { value } ) => ( { value, length : 0 } ) )
142
142
) ;
143
143
}
144
144
export function lazy < T > ( parser : ( ) => Parser < T > ) : Parser < T > {
145
- return new Parser ( ( source ) => parser ( ) . rawParser ( source ) ) ;
145
+ return new Parser ( ( input ) => parser ( ) . rawParser ( input ) ) ;
146
146
}
147
147
export function choice < T > ( ...choices : ReadonlyArray < Parser < T > > ) : Parser < T > {
148
148
assertGreater (
149
149
choices . length ,
150
150
1 ,
151
151
"`choice` called with less than 2 arguments" ,
152
152
) ;
153
- return new Parser ( ( source ) =>
154
- new ArrayResult ( choices ) . flatMap ( ( parser ) => parser . rawParser ( source ) )
153
+ return new Parser ( ( input ) =>
154
+ new ArrayResult ( choices ) . flatMap ( ( parser ) => parser . rawParser ( input ) )
155
155
) ;
156
156
}
157
157
export function choiceOnlyOne < T > (
@@ -164,10 +164,10 @@ export function choiceOnlyOne<T>(
164
164
) ;
165
165
return choices . reduceRight (
166
166
( right , left ) =>
167
- new Parser ( ( source ) => {
168
- const arrayResult = left . rawParser ( source ) ;
167
+ new Parser ( ( input ) => {
168
+ const arrayResult = left . rawParser ( input ) ;
169
169
if ( arrayResult . isError ( ) ) {
170
- return ArrayResult . concat ( arrayResult , right . rawParser ( source ) ) ;
170
+ return ArrayResult . concat ( arrayResult , right . rawParser ( input ) ) ;
171
171
} else {
172
172
return arrayResult ;
173
173
}
@@ -291,24 +291,24 @@ export const everything = new Parser(({ source, position }) =>
291
291
} ] )
292
292
) ;
293
293
export const character = match ( / ./ us, "character" ) ;
294
- export const end = new Parser ( ( source ) =>
295
- source . position === source . source . length
294
+ export const end = new Parser ( ( input ) =>
295
+ input . position === input . source . length
296
296
? new ArrayResult ( [ { value : null , length : 0 } ] )
297
297
: new ArrayResult (
298
298
new UnexpectedError (
299
- describeSource ( source . source . slice ( source . position ) ) ,
299
+ describeSource ( input . source . slice ( input . position ) ) ,
300
300
"end of text" ,
301
301
) ,
302
302
)
303
303
) ;
304
304
export function withSource < T > (
305
305
parser : Parser < T > ,
306
306
) : Parser < readonly [ value : T , source : string ] > {
307
- return new Parser ( ( source ) =>
308
- parser . rawParser ( source ) . map ( ( { value, length } ) => ( {
307
+ return new Parser ( ( input ) =>
308
+ parser . rawParser ( input ) . map ( ( { value, length } ) => ( {
309
309
value : [
310
310
value ,
311
- source . source . slice ( source . position , source . position + length ) ,
311
+ input . source . slice ( input . position , input . position + length ) ,
312
312
] as const ,
313
313
length,
314
314
} ) )
0 commit comments