123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130(* Copyright (C) 2017 Hongbo Zhang, Authors of ReScript
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)openImportmoduleExternal_arg_spec=Melange_ffi.External_arg_specletis_enum_polyvar=letis_enumrow_fields=List.for_allrow_fields~f:(function|{prf_desc=Rtag(_,true,[]);_}->true|{prf_desc=Rtag_|Rinherit_;_}->false)infunction|{ptype_manifest=Some{ptyp_desc=Ptyp_variant(row_fields,Closed,None);_};_;}whenis_enumrow_fields->Somerow_fields|{ptype_manifest=Some_|None;_}->Noneletmap_row_fields_into_ints=letprocess_mel_as~attrsi=Option.value(Ast_attributes.iter_process_mel_int_asattrs)~default:iinfun(row_fields:row_fieldlist)~loc->letcase,_,result=List.fold_leftrow_fields~init:(`Nothing,0,[])~f:(fun(nullary,i,acc){prf_desc;prf_attributes;_}->letnullary,txt=match(nullary,prf_desc)with|(`Nothing|`Null),Rtag({txt;_},true,[])->(`Null,txt)|(`Nothing|`NonNull),Rtag({txt;_},false,[_])->(`NonNull,txt)|_->Error.err~locInvalid_mel_int_typeinleti=process_mel_as~attrs:prf_attributesiin(nullary,i+1,(txt,External_arg_spec.Arg_cst.Inti)::acc))inmatchcasewith|`Nothing->assertfalse|`Null->External_arg_spec.Int{descr=result;spread=false}|`NonNull->Int{descr=result;spread=true}(* It also check in-consistency of cases like
{[ [`a | `c of int ] ]} *)letmap_row_fields_into_strings=letprocess_mel_astag~txt~has_mel_as=letname=matchAst_attributes.iter_process_mel_string_astag.prf_attributeswith|Somename->has_mel_as:=true;name|None->txtin(txt,External_arg_spec.Arg_cst.Strname)infun(row_fields:row_fieldlist)~loc->lethas_mel_as=reffalseinletcase,result=List.fold_right~f:(funtag(nullary,acc)->letnullary,txt=match(nullary,tag.prf_desc)with|(`Nothing|`Null),Rtag({txt;_},true,[])->(`Null,txt)|(`Nothing|`NonNull),Rtag({txt;_},false,[_])->(`NonNull,txt)|_->Error.err~locInvalid_mel_string_typein(nullary,process_mel_astag~txt~has_mel_as::acc))row_fields~init:(`Nothing,[])inmatch(case,!has_mel_as)with|`Nothing,_->Error.err~locInvalid_mel_string_type|`Null,false->Mel_ast_invariant.warn~locRedundant_mel_string;External_arg_spec.Nothing|`Null,true->Poly_var{descr=result;spread=false}|`NonNull,has_mel_as->Poly_var{descr=(ifhas_mel_asthenresultelse[]);spread=true}letmap_row_fields_into_spread(row_fields:row_fieldlist)~loc=letresult=List.maprow_fields~f:(function|{prf_desc=Rtag({txt;_},false,[_]);prf_attributes;_}->(txt,matchAst_attributes.iter_process_mel_as_cstprf_attributeswith|Somex->x|None->Strtxt)|_->Error.err~locInvalid_mel_spread_type)inExternal_arg_spec.Poly_var{descr=result;spread=true}letinfer_mel_as~locrow_fields=lethas_mel_as=reffalseinletresult=List.filter_maprow_fields~f:(function|{prf_desc=Rtag({txt;_},_,_);prf_attributes;_}->(matchAst_attributes.iter_process_mel_as_cstprf_attributeswith|Somex->has_mel_as:=true;Some(txt,x)|None->None)|_->Error.err~locInvalid_mel_spread_type)inmatch!has_mel_aswith|true->External_arg_spec.Poly_var{descr=result;spread=false}|false->Nothing