Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file ast_polyvar.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108(* 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. *)openImportletis_enum_polyvar=letis_enumrow_fields=List.for_all~f:(fun(x:row_field)->matchx.prf_descwithRtag(_label,true,[])->true|_->false)row_fieldsinfun(ty:type_declaration)->matchty.ptype_manifestwith|Some{ptyp_desc=Ptyp_variant(row_fields,Closed,None);_}whenis_enumrow_fields->Somerow_fields|_->Noneletmap_row_fields_into_ints(row_fields:row_fieldlist)~loc~allow_no_payload=let_,acc=List.fold_left~f:(fun(i,acc)rtag->matchrtag.prf_descwith|Rtag({txt;_},true,[])->leti=matchAst_attributes.iter_process_mel_int_asrtag.prf_attributeswith|Somei->i|None->iin(i+1,(txt,i)::acc)|Rtag({txt;_},_,_)whenallow_no_payload->leti=matchAst_attributes.iter_process_mel_int_asrtag.prf_attributeswith|Somei->i|None->iin(i+1,(txt,i)::acc)|_->Error.err~locInvalid_mel_int_type)~init:(0,[])row_fieldsinMelange_ffi.External_arg_spec.Int(List.revacc)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,name)(* It also check in-consistency of cases like
{[ [`a | `c of int ] ]} *)letmap_row_fields_into_strings(row_fields:row_fieldlist)~loc~allow_no_payload=lethas_mel_as=reffalseinletcase,result=List.fold_right~f:(funtag(nullary,acc)->match(nullary,tag.prf_desc)with|(`Nothing|`Null),Rtag({txt;_},true,[])->(`Null,process_mel_astag~txt~has_mel_as::acc)|`NonNull,Rtag({txt;_},true,[])whenallow_no_payload->(`Null,process_mel_astag~txt~has_mel_as::acc)|(`Nothing|`NonNull),Rtag({txt;_},false,[_])->(`NonNull,process_mel_astag~txt~has_mel_as::acc)|_->Error.err~locInvalid_mel_string_type)row_fields~init:(`Nothing,[])inmatchcasewith|`Nothing->Error.err~locInvalid_mel_string_type|`Null|`NonNull->(lethas_payload=case=`NonNullinletdescr=if!has_mel_asthenSomeresultelseNoneinmatch(has_payload,descr)with|false,None->Mel_ast_invariant.warn~locRedundant_mel_string;Melange_ffi.External_arg_spec.Nothing|false,Somedescr->Poly_var_string{descr}|true,_->Poly_var{descr})