@@ -44,20 +44,6 @@ function FromContext(options: Options, name: string, left: Runtime.IParser, righ
4444 return `If(${ FromParser ( options , name , left ) } , ([_0, input]) => ${ FromParser ( options , name , right ) . replace ( 'context' , `_0 as ${ options . contextType } ` ) } , () => [])`
4545}
4646// ------------------------------------------------------------------
47- // Tuple
48- // ------------------------------------------------------------------
49- function FromTuple ( options : Options , name : string , parsers : Runtime . IParser [ ] ) : string {
50- const parameters = `[${ parsers . map ( ( _ , index ) => `_${ index } ` ) . join ( ', ' ) } ]`
51- const initial = `[${ parameters } , input]`
52- return parsers . reduceRight ( ( result , right , index ) => `If(${ FromParser ( options , name , right ) } , ([_${ index } , input]) => ${ result } )` , initial )
53- }
54- // ------------------------------------------------------------------
55- // Union
56- // ------------------------------------------------------------------
57- function FromUnion ( options : Options , name : string , parsers : Runtime . IParser [ ] ) : string {
58- return parsers . length === 0 ? '[]' : parsers . reduceRight ( ( result , right ) => `If(${ FromParser ( options , name , right ) } , ([_0, input]) => [_0, input], () => ${ result } )` , '[]' )
59- }
60- // ------------------------------------------------------------------
6147// Array
6248// ------------------------------------------------------------------
6349function FromArrayReducer ( options : Options , name : string , parser : Runtime . IParser ) : string {
@@ -71,25 +57,12 @@ function FromArray(options: Options, name: string, parser: Runtime.IParser): str
7157 return `${ reducer_name } (input, context)`
7258}
7359// ------------------------------------------------------------------
74- // Optional
75- // ------------------------------------------------------------------
76- function FromOptional ( options : Options , name : string , parser : Runtime . IOptional ) : string {
77- return `If(${ FromParser ( options , name , parser . parser ) } , ([_0, input]) => [[_0], input], () => [[], input])`
78- }
79- // ------------------------------------------------------------------
8060// Const
8161// ------------------------------------------------------------------
8262function FromConst ( options : Options , name : string , value : string ) : string {
8363 return `Runtime.Token.Const('${ Escape ( value ) } ', input)`
8464}
8565// ------------------------------------------------------------------
86- // Const
87- // ------------------------------------------------------------------
88- function FromUntil ( options : Options , name : string , values : string [ ] ) : string {
89- const escaped = values . map ( value => `'${ Escape ( value ) } '` )
90- return `Runtime.Token.Until([${ escaped . join ( ', ' ) } ], input)`
91- }
92- // ------------------------------------------------------------------
9366// Ident
9467// ------------------------------------------------------------------
9568function FromIdent ( options : Options , name : string ) : string {
@@ -102,34 +75,69 @@ function FromNumber(options: Options, name: string): string {
10275 return `Runtime.Token.Number(input)`
10376}
10477// ------------------------------------------------------------------
78+ // Optional
79+ // ------------------------------------------------------------------
80+ function FromOptional ( options : Options , name : string , parser : Runtime . IOptional ) : string {
81+ return `If(${ FromParser ( options , name , parser . parser ) } , ([_0, input]) => [[_0], input], () => [[], input])`
82+ }
83+ // ------------------------------------------------------------------
84+ // Ref
85+ // ------------------------------------------------------------------
86+ function FromRef ( options : Options , name : string , ref : string ) : string {
87+ return `${ ref } (input, context)`
88+ }
89+ // ------------------------------------------------------------------
10590// String
10691// ------------------------------------------------------------------
10792function FromString ( options : Options , name : string , string_options : string [ ] ) : string {
10893 const _options = string_options . map ( ( option ) => `'${ Escape ( option ) } '` ) . join ( ', ' )
10994 return `Runtime.Token.String([${ _options } ], input)`
11095}
11196// ------------------------------------------------------------------
112- // Ref
97+ // Tuple
11398// ------------------------------------------------------------------
114- function FromRef ( options : Options , name : string , ref : string ) : string {
115- return `${ ref } (input, context)`
99+ function FromTuple ( options : Options , name : string , parsers : Runtime . IParser [ ] ) : string {
100+ const parameters = `[${ parsers . map ( ( _ , index ) => `_${ index } ` ) . join ( ', ' ) } ]`
101+ const initial = `[${ parameters } , input]`
102+ return parsers . reduceRight ( ( result , right , index ) => `If(${ FromParser ( options , name , right ) } , ([_${ index } , input]) => ${ result } )` , initial )
116103}
117104// ------------------------------------------------------------------
118- // Ref
105+ // Union
106+ // ------------------------------------------------------------------
107+ function FromUnion ( options : Options , name : string , parsers : Runtime . IParser [ ] ) : string {
108+ return parsers . length === 0 ? '[]' : parsers . reduceRight ( ( result , right ) => `If(${ FromParser ( options , name , right ) } , ([_0, input]) => [_0, input], () => ${ result } )` , '[]' )
109+ }
110+ // ------------------------------------------------------------------
111+ // Until
112+ // ------------------------------------------------------------------
113+ function FromUntil ( options : Options , name : string , values : string [ ] ) : string {
114+ const escaped = values . map ( value => `'${ Escape ( value ) } '` )
115+ return `Runtime.Token.Until([${ escaped . join ( ', ' ) } ], input)`
116+ }
117+ // ------------------------------------------------------------------
118+ // UntilNonEmpty
119+ // ------------------------------------------------------------------
120+ function FromUntilNonEmpty ( options : Options , name : string , values : string [ ] ) : string {
121+ const escaped = values . map ( value => `'${ Escape ( value ) } '` )
122+ return `Runtime.Token.UntilNonEmpty([${ escaped . join ( ', ' ) } ], input)`
123+ }
124+ // ------------------------------------------------------------------
125+ // Parser
119126// ------------------------------------------------------------------
120127function FromParser ( options : Options , name : string , parser : Runtime . IParser ) : string {
121128 return (
122- Runtime . IsContext ( parser ) ? FromContext ( options , name , parser . left , parser . right ) :
123- Runtime . IsTuple ( parser ) ? FromTuple ( options , name , parser . parsers ) :
124- Runtime . IsUnion ( parser ) ? FromUnion ( options , name , parser . parsers ) :
125129 Runtime . IsArray ( parser ) ? FromArray ( options , name , parser . parser ) :
126- Runtime . IsOptional ( parser ) ? FromOptional ( options , name , parser ) :
127- Runtime . IsString ( parser ) ? FromString ( options , name , parser . options ) :
130+ Runtime . IsContext ( parser ) ? FromContext ( options , name , parser . left , parser . right ) :
128131 Runtime . IsConst ( parser ) ? FromConst ( options , name , parser . value ) :
129- Runtime . IsUntil ( parser ) ? FromUntil ( options , name , parser . values ) :
130- Runtime . IsRef ( parser ) ? FromRef ( options , name , parser . ref ) :
131132 Runtime . IsIdent ( parser ) ? FromIdent ( options , name ) :
132133 Runtime . IsNumber ( parser ) ? FromNumber ( options , name ) :
134+ Runtime . IsOptional ( parser ) ? FromOptional ( options , name , parser ) :
135+ Runtime . IsRef ( parser ) ? FromRef ( options , name , parser . ref ) :
136+ Runtime . IsString ( parser ) ? FromString ( options , name , parser . options ) :
137+ Runtime . IsTuple ( parser ) ? FromTuple ( options , name , parser . parsers ) :
138+ Runtime . IsUnion ( parser ) ? FromUnion ( options , name , parser . parsers ) :
139+ Runtime . IsUntil ( parser ) ? FromUntil ( options , name , parser . values ) :
140+ Runtime . IsUntilNonEmpty ( parser ) ? FromUntilNonEmpty ( options , name , parser . values ) :
133141 Unreachable ( parser )
134142 )
135143}
0 commit comments