123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152(*
Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015 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.
*)moduleC=Piqi_commonopenCletprettyprint_astchast=Piq_gen.to_channelchastletrecprettyprint_listchast_list=letrecaux=function|[]->()|[x]->prettyprint_astchx|h::t->prettyprint_astchh;output_charch'\n';auxtinauxast_list(* simplify piqi ast: *)letsimplify_piqi_ast(ast:piq_ast)=lettr=Piq_ast.transform_astin(* map typedef.x -> x *)letrm_typedef=tr[](function|`named{Piq_ast.Named.name="typedef";Piq_ast.Named.value=v}->[v]|x->[x])(* del .../mode.required *)(* map .../field/mode x -> x *)andtr_field_modepath=trpath(function|`named{Piq_ast.Named.name="mode";Piq_ast.Named.value=`name"required"}->[]|`named{Piq_ast.Named.name="mode";Piq_ast.Named.value=(`name_)asx}->[x]|x->[x])(* map extend/.piqi-any x -> x *)(* TODO: this method of specifying extensions will be eventually deprecated *)andtr_extend_piq_any=tr["extend"](function|`named{Piq_ast.Named.name="piqi-any";Piq_ast.Named.value=v}->[v]|x->[x])(* map extend/.what x -> x *)andtr_extend_what=tr["extend"](function|`named{Piq_ast.Named.name="what";Piq_ast.Named.value=v}->[v]|x->[x])(* map ../record.x -> x *)(* map ../name.x -> x *)andrm_param_extrapath=trpath(function|`named{Piq_ast.Named.name="record";Piq_ast.Named.value=v}->[v]|`named{Piq_ast.Named.name="name";Piq_ast.Named.value=v}->[v]|x->[x])(* strip :<type> from a field's default value *)andtr_field_default_valuepath=trpath(function|`typed{Piq_ast.Typed.value=v}->[v]|x->[x])inlet(|>)af=fainletsimplify_function_paramparamast=ast|>rm_param_extra["function";param]|>tr_field_mode["function";param;"field"]|>tr_field_default_value["function";param;"field";"default"]inast|>rm_typedef|>tr_field_mode["record";"field"]|>tr_field_default_value["record";"field";"default"]|>tr_extend_piq_any|>tr_extend_what(* functions *)|>simplify_function_param"input"|>simplify_function_param"output"|>simplify_function_param"error"letcompare_piqi_itemsab=letname_of=function|`namex->x|`namedx->x.Piq_ast.Named.name|`typenamex->x|`typedx->x.Piq_ast.Typed.typename|_->assertfalseinletrankx=matchname_ofxwith|"module"->0|"protobuf-package"->1|"include"->2|"import"->3|"typedef"->4|"function"->5|"extend"->6|_->100inranka-rankbletsort_piqi_items(ast:piq_ast)=matchastwith|`listl->letl=List.stable_sortcompare_piqi_itemslin`listl|_->assertfalseletprettify_piqi_astast=letast=sort_piqi_itemsastinsimplify_piqi_astastletprettyprint_piqi_astchast=letast=prettify_piqi_astastinmatchastwith|`listl->prettyprint_listchl|_->assertfalseletprettyprint_piqich(piqi:T.piqi)=letast=Piqi.piqi_to_astpiqiinprettyprint_piqi_astchast