@@ -38,6 +38,11 @@ export interface InputFieldDeclaration {
3838 argumentContainer : InputFieldArgumentContainer ;
3939}
4040
41+ export interface Template {
42+ identifier : string ;
43+ template : InputFieldDeclaration ;
44+ }
45+
4146export class InputFieldDeclarationParser {
4247 static roundBracesPair : EnclosingPair = new EnclosingPair ( '(' , ')' ) ;
4348 static squareBracesPair : EnclosingPair = new EnclosingPair ( '[' , ']' ) ;
@@ -48,15 +53,29 @@ export class InputFieldDeclarationParser {
4853 InputFieldDeclarationParser . curlyBracesPair ,
4954 ] ;
5055
51- static templates : Record < string , InputFieldDeclaration > = { } ;
56+ static templates : Template [ ] = [ ] ;
5257
5358
54- static parse ( fullDeclaration : string , templateName ?: string ) : InputFieldDeclaration {
59+ static parse ( fullDeclaration : string ) : InputFieldDeclaration {
5560 let inputFieldDeclaration : InputFieldDeclaration = { } as InputFieldDeclaration ;
5661
62+ let useTemplate = false ;
63+ let templateName = '' ;
64+
5765 // declaration
5866 inputFieldDeclaration . fullDeclaration = fullDeclaration ;
59- inputFieldDeclaration . declaration = ParserUtils . getInBetween ( fullDeclaration , InputFieldDeclarationParser . squareBracesPair ) as string ;
67+ const temp = ParserUtils . getInBetween ( fullDeclaration , InputFieldDeclarationParser . squareBracesPair ) ;
68+ if ( Array . isArray ( temp ) ) {
69+ if ( temp . length === 2 ) {
70+ useTemplate = true ;
71+ templateName = temp [ 0 ] ;
72+ inputFieldDeclaration . declaration = temp [ 1 ] ;
73+ } else {
74+ throw new MetaBindParsingError ( 'invalid input field declaration' ) ;
75+ }
76+ } else {
77+ inputFieldDeclaration . declaration = temp ;
78+ }
6079
6180 // declaration parts
6281 const declarationParts : string [ ] = ParserUtils . split ( inputFieldDeclaration . declaration , ':' , InputFieldDeclarationParser . squareBracesPair ) ;
@@ -67,51 +86,65 @@ export class InputFieldDeclarationParser {
6786
6887 // input field type and arguments
6988 const inputFieldTypeWithArguments : string = declarationParts [ 0 ] ;
70- // input field type
71- const inputFieldTypeString = ParserUtils . removeInBetween ( inputFieldTypeWithArguments , InputFieldDeclarationParser . roundBracesPair ) ;
72- inputFieldDeclaration . inputFieldType = InputFieldDeclarationParser . getInputFieldType ( inputFieldTypeString ) ;
73-
74- // arguments
75- const inputFieldArgumentsString : string = ParserUtils . getInBetween ( inputFieldTypeWithArguments , InputFieldDeclarationParser . roundBracesPair ) as string ;
76- // console.log(inputFieldArgumentsString);
77- if ( inputFieldArgumentsString ) {
78- inputFieldDeclaration . argumentContainer = InputFieldDeclarationParser . parseArguments ( inputFieldArgumentsString , inputFieldDeclaration . inputFieldType ) ;
89+ if ( inputFieldTypeWithArguments ) {
90+ // input field type
91+ const inputFieldTypeString = ParserUtils . removeInBetween ( inputFieldTypeWithArguments , InputFieldDeclarationParser . roundBracesPair ) ;
92+ inputFieldDeclaration . inputFieldType = InputFieldDeclarationParser . getInputFieldType ( inputFieldTypeString ) ;
93+
94+ // arguments
95+ const inputFieldArgumentsString : string = ParserUtils . getInBetween ( inputFieldTypeWithArguments , InputFieldDeclarationParser . roundBracesPair ) as string ;
96+ // console.log(inputFieldArgumentsString);
97+ if ( inputFieldArgumentsString ) {
98+ inputFieldDeclaration . argumentContainer = InputFieldDeclarationParser . parseArguments ( inputFieldArgumentsString , inputFieldDeclaration . inputFieldType ) ;
99+ } else {
100+ inputFieldDeclaration . argumentContainer = new InputFieldArgumentContainer ( ) ;
101+ }
79102 } else {
103+ inputFieldDeclaration . inputFieldType = InputFieldType . INVALID ;
80104 inputFieldDeclaration . argumentContainer = new InputFieldArgumentContainer ( ) ;
81105 }
82106
83107
84- if ( templateName ) {
85- const template = InputFieldDeclarationParser . templates [ templateName ] ;
108+ if ( useTemplate ) {
109+ console . log ( templateName ) ;
110+ const template = InputFieldDeclarationParser . templates . filter ( x => x . identifier === templateName ) . first ( ) ?. template ;
111+ console . log ( template ) ;
86112 if ( template ) {
87113 inputFieldDeclaration . bindTarget = inputFieldDeclaration . bindTarget || template . bindTarget ;
88114 inputFieldDeclaration . isBound = inputFieldDeclaration . isBound || template . isBound ;
89115 inputFieldDeclaration . inputFieldType = inputFieldDeclaration . inputFieldType === InputFieldType . INVALID ? template . inputFieldType : ( inputFieldDeclaration . inputFieldType || template . inputFieldType ) ;
90116 inputFieldDeclaration . argumentContainer = template . argumentContainer . mergeByOverride ( inputFieldDeclaration . argumentContainer ) ;
117+ } else {
118+ throw new MetaBindParsingError ( `unknown template name \'${ templateName } \'` ) ;
91119 }
92120 }
93121
94122 if ( inputFieldDeclaration . inputFieldType === InputFieldType . INVALID ) {
95- throw new MetaBindParsingError ( `unknown input field type \' ${ inputFieldTypeString } \' ` ) ;
123+ throw new MetaBindParsingError ( `unknown input field type` ) ;
96124 }
97125
98126 return inputFieldDeclaration ;
99127 }
100128
101129 static parseTemplates ( templates : string ) : void {
102130 let templateDeclarations = ParserUtils . split ( templates , '\n' , InputFieldDeclarationParser . squareBracesPair ) ;
103- templateDeclarations . map ( x => x . trim ( ) ) . filter ( x => x . length > 0 ) ;
131+ templateDeclarations = templateDeclarations . map ( x => x . trim ( ) ) . filter ( x => x . length > 0 ) ;
104132
105133 for ( const templateDeclaration of templateDeclarations ) {
106- const templateDeclarationParts : string [ ] = ParserUtils . split ( templateDeclaration , '->' , InputFieldDeclarationParser . squareBracesPair ) ;
107- templateDeclarationParts . map ( x => x . trim ( ) ) ;
134+ let templateDeclarationParts : string [ ] = ParserUtils . split ( templateDeclaration , '->' , InputFieldDeclarationParser . squareBracesPair ) ;
135+ templateDeclarationParts = templateDeclarationParts . map ( x => x . trim ( ) ) ;
108136
109137 if ( templateDeclarationParts . length === 1 ) {
110138 throw new MetaBindParsingError ( 'Invalid template syntax' ) ;
111139 } else if ( templateDeclarationParts . length === 2 ) {
112- InputFieldDeclarationParser . templates [ templateDeclarationParts [ 0 ] ] = InputFieldDeclarationParser . parse ( templateDeclarationParts [ 1 ] ) ;
140+ InputFieldDeclarationParser . templates . push ( {
141+ identifier : templateDeclarationParts [ 0 ] ,
142+ template : InputFieldDeclarationParser . parse ( templateDeclarationParts [ 1 ] ) ,
143+ } )
113144 }
114145 }
146+
147+ console . log ( InputFieldDeclarationParser . templates ) ;
115148 }
116149
117150 static parseArguments ( inputFieldArgumentsString : string , inputFieldType : InputFieldType ) : InputFieldArgumentContainer {
0 commit comments