123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312(*
Copyright 2009, 2010, 2011, 2012, 2013, 2014 Anton Lavrik
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)(*
* Light syntax for Piqi DDL.
*)moduleC=Piqi_commonopenCopenIolistletgen_typenamex=iosx^^ios"()"letgen_name_typenametypename=matchname,typenamewith|Somen,None->iosn|None,Somet->gen_typenamet|Somen,Somet->iosn^^ios" :: "^^gen_typenamet|_->assertfalseletgen_default_astast=letstr=Piq_gen.to_stringast~nl:falseinifString.containsstr'\n'(* multiline? *)thenletlines=Piq_gen.split_textstrinletlines=List.mapioslinesiniol[ios" =";indent;iod"\n"lines;unindent;]elseiol[ios" = ";iosstr;]letgen_default=function|None->iol[]|Somedefault->letast=Piqobj.piq_of_piqi_anydefaultingen_default_astastletgen_field_mode=function|`required->"-"|`optional->"?"|`repeated->"*"letgen_fieldx=letopenFinletfield_mode=gen_field_modex.modeiniol[iosfield_mode;ios" ";gen_name_typex.namex.typename;gen_defaultx.default;]letgen_recordx=letopenRinletfields=List.mapgen_fieldx.fieldiniol[eol;ios" {";indent;iod"\n"fields;unindent;eol;ios" }";]letgen_optionx=letopenOiniol[ios"| ";gen_name_typex.namex.typename]letgen_enumx=letopenEinletoptions=List.mapgen_optionx.optioniniol[indent;iod"\n"options;(* XXX: print on the same line? *)unindent;]letgen_variantx=letopenVinletoptions=List.mapgen_optionx.optioniniol[indent;iod"\n"options;(* XXX: try to print on the same line? *)unindent;]letgen_listx=letopenLiniol[ios" [ ";gen_typenamex.typename;ios" ]"]letgen_aliasx=letopenAinlettypename=matchx.typenamewith|Somen->gen_typenamen|None->(* generate name for built-in types *)letpiqtype=((some_ofx.piqi_type):>T.piqtype)inios"."^^ios(C.piqi_typenamepiqtype)iniol[ios" ";typename;]letgen_typedef_repr=function|`recordt->gen_recordt|`variantt->gen_variantt|`enumt->gen_enumt|`listt->gen_listt|`aliast->gen_aliastletgen_defx=letname=typedef_namexinletrepr=gen_typedef_reprxiniol[ios"type ";iosname;ios" =";repr;]letgen_sepl=ifl<>[]thenios"\n\n"elseiol[]letgen_defs(defs:T.typedeflist)=letl=List.mapgen_defdefsiniol[iod"\n\n"l;gen_sepl]letgen_importx=letopenImportinletname=matchx.namewith|None->iol[]|Somex->ios" as "^^iosxiniol[ios"import ";iosx.modname;name;]letgen_importsl=letl=List.mapgen_importliniol[iod"\n"l;gen_sepl]letgen_includesl=letopenIncludinletl=List.map(funx->ios"include "^^iosx.modname)liniol[iod"\n"l;gen_sepl](* boot code *)letfield_def=if!Sys.interactivethenObj.magic1(* don't do anything in interactive (toplevel) mode *)elsePiqi.find_embedded_piqtype"field"letoption_def=if!Sys.interactivethenObj.magic1(* don't do anything in interactive (toplevel) mode *)elsePiqi.find_embedded_piqtype"option"letgen_extension_itemx=letast=Piqobj.piq_of_piqi_anyxin(* NOTE: recognizing and printing only fields and options *)matchastwith|`named{Piq_ast.Named.name="field";Piq_ast.Named.value=ast}->letx=Piqi.mlobj_of_astfield_defT.parse_fieldastinletres=gen_fieldxin[res]|`named{Piq_ast.Named.name="option";Piq_ast.Named.value=ast}->letx=Piqi.mlobj_of_astoption_defT.parse_optionastinletres=gen_optionxin[res]|_->[]letgen_extension_target=function|`typedefx|`namex->[x]|`fieldx->[](* "field=" ^ x *)|`optionx->[](* "option=" ^ x *)|`importx->[](* "import=" ^ x *)|`funcx->[](* "function=" ^ x *)letgen_extensionx=letopenExtendin(* TODO: break long list of extended names to several lines *)letnames=U.flatmap(funx->gen_extension_targetx)x.whatinletitems=U.flatmapgen_extension_item(x.piqi_with@x.quote)in(* don't print any extensions other than fields and options *)ifnames<>[]&&items<>[]thenletres=iol[ios"extend ";iod" "(List.mapiosnames);indent;iod"\n"items;unindent;]in[res]else[]letgen_extensionsl=letl=U.flatmapgen_extensionliniol[iod"\n\n"l;gen_sepl]letgen_paramname=function|None->[]|Somex->letrepr=matchxwith|`namex->ios" "^^gen_typenamex|(#T.typedefasx)->gen_typedef_reprxinletres=iol[iosname;ios" =";repr;]in[res]letgen_functionf=letopenT.Funcinletparams=List.concat[gen_param"input"f.input;gen_param"output"f.output;gen_param"error"f.error;]iniol[ios"function ";iosf.name;indent;iod"\n"params;unindent;]letgen_functionsl=letl=List.mapgen_functionliniol[iod"\n\n"l;gen_sepl]letgen_module=function|Somex->ios"module "^^iosx^^ios"\n\n"|None->iol[]letgen_piqich(piqi:T.piqi)=letopenPinletpiqi=some_ofpiqi.original_piqiinletcode=iol[(* XXX: gen_module _orig_piqi.modname; *)gen_modulepiqi.modname;gen_importspiqi.import;gen_includespiqi.includ;(* NOTE: can't use resolved or extended typedef here, because we are
* printing inludes and extensions separately *)gen_defspiqi.typedef;gen_extensionspiqi.extend;gen_functionspiqi.func;]inIolist.to_channelchcode