@@ -42,141 +42,141 @@ const state = { reducers: new Set<string>() }
4242// ------------------------------------------------------------------
4343// Array
4444// ------------------------------------------------------------------
45- function FromArrayReducer ( name : string , parser : Runtime . IParser ) : string {
45+ function FromArrayReducer ( context : Runtime . IProperties , name : string , parser : Runtime . IParser ) : string {
4646 const reducer_name = `${ name } _${ state . reducers . size } `
47- const reducer = `export type ${ reducer_name } <Input extends string, Result extends unknown[] = []> = ${ FromParser ( reducer_name , parser ) } extends [infer _0, infer Input extends string] ? ${ reducer_name } <Input, [...Result, _0]> : [Result, Input]`
47+ const reducer = `export type ${ reducer_name } <Input extends string, Result extends unknown[] = []> = ${ FromParser ( context , reducer_name , parser ) } extends [infer _0, infer Input extends string] ? ${ reducer_name } <Input, [...Result, _0]> : [Result, Input]`
4848 state . reducers . add ( reducer )
4949 return reducer_name
5050}
51- function FromArray ( name : string , parser : Runtime . IParser ) : string {
52- const reducer_name = FromArrayReducer ( Name ( name ) , parser )
51+ function FromArray ( context : Runtime . IProperties , name : string , parser : Runtime . IParser ) : string {
52+ const reducer_name = FromArrayReducer ( context , Name ( name ) , parser )
5353 return `${ reducer_name } <Input>`
5454}
5555// ------------------------------------------------------------------
5656// Const
5757// ------------------------------------------------------------------
58- function FromConst ( name : string , const_ : string ) : string {
58+ function FromConst ( context : Runtime . IProperties , name : string , const_ : string ) : string {
5959 return `Token.TConst<'${ Escape ( const_ ) } ', Input>`
6060}
6161// ------------------------------------------------------------------
6262// BigInt
6363// ------------------------------------------------------------------
64- function FromBigInt ( name : string ) : string {
64+ function FromBigInt ( context : Runtime . IProperties , name : string ) : string {
6565 return `Token.TBigInt<Input>`
6666}
6767// ------------------------------------------------------------------
6868// Ident
6969// ------------------------------------------------------------------
70- function FromIdent ( name : string ) : string {
70+ function FromIdent ( context : Runtime . IProperties , name : string ) : string {
7171 return `Token.TIdent<Input>`
7272}
7373// ------------------------------------------------------------------
7474// Integer
7575// ------------------------------------------------------------------
76- function FromInteger ( name : string ) : string {
76+ function FromInteger ( context : Runtime . IProperties , name : string ) : string {
7777 return `Token.TInteger<Input>`
7878}
7979// ------------------------------------------------------------------
8080// Number
8181// ------------------------------------------------------------------
82- function FromNumber ( name : string ) : string {
82+ function FromNumber ( context : Runtime . IProperties , name : string ) : string {
8383 return `Token.TNumber<Input>`
8484}
8585// ------------------------------------------------------------------
8686// Optional
8787// ------------------------------------------------------------------
88- function FromOptional ( name : string , parser : Runtime . IOptional ) : string {
89- return `(${ FromParser ( name , parser . parser ) } extends [infer _0, infer Input extends string] ? [[_0], Input]: [[], Input])`
88+ function FromOptional ( context : Runtime . IProperties , name : string , parser : Runtime . IOptional ) : string {
89+ return `(${ FromParser ( context , name , parser . parser ) } extends [infer _0, infer Input extends string] ? [[_0], Input]: [[], Input])`
9090}
9191// ------------------------------------------------------------------
9292// Ref
9393// ------------------------------------------------------------------
94- function FromRef ( name : string , ref : string ) : string {
94+ function FromRef ( context : Runtime . IProperties , name : string , ref : string ) : string {
9595 return `${ Name ( ref ) } <Input>`
9696}
9797// ------------------------------------------------------------------
9898// Rest
9999// ------------------------------------------------------------------
100- function FromRest ( name : string ) : string {
100+ function FromRest ( context : Runtime . IProperties , name : string ) : string {
101101 return `Token.TRest<Input>`
102102}
103103// ------------------------------------------------------------------
104104// String
105105// ------------------------------------------------------------------
106- function FromString ( name : string , quotes : string [ ] ) : string {
106+ function FromString ( context : Runtime . IProperties , name : string , quotes : string [ ] ) : string {
107107 const _quotes = quotes . map ( ( option ) => `'${ Escape ( option ) } '` ) . join ( ', ' )
108108 return `Token.TString<[${ _quotes } ], Input>`
109109}
110110// ------------------------------------------------------------------
111111// Tuple
112112// ------------------------------------------------------------------
113- function FromTuple ( name : string , parsers : Runtime . IParser [ ] ) : string {
113+ function FromTuple ( context : Runtime . IProperties , name : string , parsers : Runtime . IParser [ ] ) : string {
114114 const initial = `[[${ parsers . map ( ( _ , index ) => `_${ index } ` ) . join ( ', ' ) } ], Input]`
115- return parsers . reduceRight ( ( result , right , index ) => `(${ FromParser ( name , right ) } extends [infer _${ index } , infer Input extends string] ? ${ result } : [])` , initial )
115+ return parsers . reduceRight ( ( result , right , index ) => `(${ FromParser ( context , name , right ) } extends [infer _${ index } , infer Input extends string] ? ${ result } : [])` , initial )
116116}
117117// ------------------------------------------------------------------
118118// Union
119119// ------------------------------------------------------------------
120- function FromUnion ( name : string , parsers : Runtime . IParser [ ] ) : string {
121- return parsers . length === 0 ? '[]' : `(${ parsers . reduceRight ( ( result , right ) => `${ FromParser ( name , right ) } extends [infer _0, infer Input extends string] ? [_0, Input] : ${ result } ` , '[]' ) } )`
120+ function FromUnion ( context : Runtime . IProperties , name : string , parsers : Runtime . IParser [ ] ) : string {
121+ return parsers . length === 0 ? '[]' : `(${ parsers . reduceRight ( ( result , right ) => `${ FromParser ( context , name , right ) } extends [infer _0, infer Input extends string] ? [_0, Input] : ${ result } ` , '[]' ) } )`
122122}
123123// ------------------------------------------------------------------
124124// UnsignedInteger
125125// ------------------------------------------------------------------
126- function FromUnsignedInteger ( name : string ) : string {
126+ function FromUnsignedInteger ( context : Runtime . IProperties , name : string ) : string {
127127 return `Token.TUnsignedInteger<Input>`
128128}
129129// ------------------------------------------------------------------
130130// UnsignedNumber
131131// ------------------------------------------------------------------
132- function FromUnsignedNumber ( name : string ) : string {
132+ function FromUnsignedNumber ( context : Runtime . IProperties , name : string ) : string {
133133 return `Token.TUnsignedNumber<Input>`
134134}
135135// ------------------------------------------------------------------
136136// Until_1
137137// ------------------------------------------------------------------
138- function FromUntil_1 ( name : string , end : string [ ] ) : string {
138+ function FromUntil_1 ( context : Runtime . IProperties , name : string , end : string [ ] ) : string {
139139 const escaped = end . map ( value => `'${ Escape ( value ) } '` )
140140 return `Token.TUntil_1<[${ escaped . join ( ', ' ) } ], Input>`
141141}
142142// ------------------------------------------------------------------
143143// Until
144144// ------------------------------------------------------------------
145- function FromUntil ( name : string , end : string [ ] ) : string {
145+ function FromUntil ( context : Runtime . IProperties , name : string , end : string [ ] ) : string {
146146 const escaped = end . map ( value => `'${ Escape ( value ) } '` )
147147 return `Token.TUntil<[${ escaped . join ( ', ' ) } ], Input>`
148148}
149149// ------------------------------------------------------------------
150150// Parser
151151// ------------------------------------------------------------------
152- function FromParser ( name : string , parser : Runtime . IParser ) : string {
152+ function FromParser ( context : Runtime . IProperties , name : string , parser : Runtime . IParser ) : string {
153153 return (
154- Runtime . IsArray ( parser ) ? FromArray ( name , parser . parser ) :
155- Runtime . IsBigInt ( parser ) ? FromBigInt ( name ) :
156- Runtime . IsConst ( parser ) ? FromConst ( name , parser . const ) :
157- Runtime . IsIdent ( parser ) ? FromIdent ( name ) :
158- Runtime . IsInteger ( parser ) ? FromInteger ( name ) :
159- Runtime . IsNumber ( parser ) ? FromNumber ( name ) :
160- Runtime . IsOptional ( parser ) ? FromOptional ( name , parser ) :
161- Runtime . IsRef ( parser ) ? FromRef ( name , parser . ref ) :
162- Runtime . IsRest ( parser ) ? FromRest ( name ) :
163- Runtime . IsString ( parser ) ? FromString ( name , parser . quotes ) :
164- Runtime . IsTuple ( parser ) ? FromTuple ( name , parser . parsers ) :
165- Runtime . IsUnion ( parser ) ? FromUnion ( name , parser . parsers ) :
166- Runtime . IsUnsignedInteger ( parser ) ? FromUnsignedInteger ( name ) :
167- Runtime . IsUnsignedNumber ( parser ) ? FromUnsignedNumber ( name ) :
168- Runtime . IsUntil_1 ( parser ) ? FromUntil_1 ( name , parser . end ) :
169- Runtime . IsUntil ( parser ) ? FromUntil ( name , parser . end ) :
154+ Runtime . IsArray ( parser ) ? FromArray ( context , name , parser . parser ) :
155+ Runtime . IsBigInt ( parser ) ? FromBigInt ( context , name ) :
156+ Runtime . IsConst ( parser ) ? FromConst ( context , name , parser . const ) :
157+ Runtime . IsIdent ( parser ) ? FromIdent ( context , name ) :
158+ Runtime . IsInteger ( parser ) ? FromInteger ( context , name ) :
159+ Runtime . IsNumber ( parser ) ? FromNumber ( context , name ) :
160+ Runtime . IsOptional ( parser ) ? FromOptional ( context , name , parser ) :
161+ Runtime . IsRef ( parser ) ? FromRef ( context , name , parser . ref ) :
162+ Runtime . IsRest ( parser ) ? FromRest ( context , name ) :
163+ Runtime . IsString ( parser ) ? FromString ( context , name , parser . quotes ) :
164+ Runtime . IsTuple ( parser ) ? FromTuple ( context , name , parser . parsers ) :
165+ Runtime . IsUnion ( parser ) ? FromUnion ( context , name , parser . parsers ) :
166+ Runtime . IsUnsignedInteger ( parser ) ? FromUnsignedInteger ( context , name ) :
167+ Runtime . IsUnsignedNumber ( parser ) ? FromUnsignedNumber ( context , name ) :
168+ Runtime . IsUntil_1 ( parser ) ? FromUntil_1 ( context , name , parser . end ) :
169+ Runtime . IsUntil ( parser ) ? FromUntil ( context , name , parser . end ) :
170170 Unreachable ( parser )
171171 )
172172}
173173// ------------------------------------------------------------------
174174// Type
175175// ------------------------------------------------------------------
176- export function BuildStaticParse ( name : string , parser : Runtime . IParser ) : string {
176+ export function BuildStaticParse ( context : Runtime . IProperties , name : string , parser : Runtime . IParser ) : string {
177177 state . reducers . clear ( )
178178 const semanticsType = `S.${ Name ( name ) } Mapping`
179- const type = `export type ${ Name ( name ) } <Input extends string> = ${ FromParser ( name , parser ) } extends [infer _0 extends ${ Infer ( parser ) } , infer Input extends string] ? [${ semanticsType } <_0>, Input] : []`
179+ const type = `export type ${ Name ( name ) } <Input extends string> = ${ FromParser ( context , name , parser ) } extends [infer _0 extends ${ Infer ( parser ) } , infer Input extends string] ? [${ semanticsType } <_0>, Input] : []`
180180 const reducers = [ ...state . reducers ]
181181 return [ ...reducers , type ] . join ( '\n' )
182182}
0 commit comments