Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file pb_option.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168(*
The MIT License (MIT)
Copyright (c) 2016 Maxime Ransan <maxime.ransan@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*)typeconstant=Pbrt_options.constant=|Constant_stringofstring|Constant_boolofbool|Constant_intofint|Constant_floatoffloat|Constant_literalofstringtypeoption_name=|Simple_nameofstring|Extension_nameofstringtypemessage_literal=(string*value)listandlist_literal=valuelistandvalue=Pbrt_options.value=|Scalar_valueofconstant|Message_literalofmessage_literal|List_literaloflist_literaltypet=option_name*valuetypeset=tlistletstringify_option_name=function|Simple_names->s|Extension_names->"("^s^")"letoption_name_equalab=matcha,bwith|Simple_namea,Simple_nameb->String.equalab|Extension_namea,Extension_nameb->String.equalab|_->falseletempty=[]letrecmerge_valuev1v2=matchv1,v2with|Message_literalml1,Message_literalml2->(* In this case, both the existing and new values are messages.
Iterate through the fields of the new value.
For each field, check if a field with the same name exists in the existing value.
If it does and both field values are messages, merge them recursively.
If it does not, add the new field to the existing message. *)letrecmerge_listslist1list2=matchlist2with|[]->list1|(field,value)::rest->letupdated_list,is_merged=List.fold_left(fun(acc,merged)(f,v)->ifString.equalffieldthen(matchvalue,vwith|Message_literal_,Message_literal_->(acc@[f,merge_valuevaluev],true(* recursively merges two message literals *))|_->acc@[f,value],merged)elseacc@[f,v],merged)([],false)list1inifis_mergedthen(* If the current field of list2 was found in list1 and the two
values merged, continue with the rest of list2. The current field of
list2 is not added to updated_list as its value has already been
included during the merge. *)merge_listsupdated_listrestelse(* If the current field of list2 was not found in list1, add it to
updated_list. *)merge_lists(updated_list@[field,value])restinMessage_literal(merge_listsml1ml2)|_->(* FIXME: This overrides the scalar value of an existing option with the
scalar value of a new option, which is not allowed as per Protocol Buffer
Language Specification. *)v2letaddoption_setoption_namevalue=matchList.partition(fun((name,_):t)->option_name_equalnameoption_name)option_setwith|[],_->(* If the option does not currently exist in the set, add it *)(option_name,value)::option_set|[(_,existing_value)],remainder->(* If the option already exists in the set, merge it's value with the new value *)letmerged_value=merge_valueexisting_valuevaluein(option_name,merged_value)::remainder|_->(* This is a sanity check. As we use an equality function, List.partition should
* always partition the list into two lists where the first list has at most one element.
* Hence, the condition that results in a call to failwith should never be satisfied. *)failwith"This should not happen, partition should result in at most single item \
in left component"letgettoption_name=matchList.find(fun(other,_)->option_name_equaloption_nameother)twith|_,c->Somec|exceptionNot_found->Noneletget_exttoption_name=gett(Extension_nameoption_name)letpp_constantppf=function|Constant_strings->Format.fprintfppf"%S"s|Constant_boolb->Format.fprintfppf"%B"b|Constant_inti->Format.fprintfppf"%d"i|Constant_floatf->Format.fprintfppf"%f"f|Constant_literall->Format.fprintfppf"`%s`"lletrecpp_valueppf=function|Scalar_valuec->pp_constantppfc|Message_literalml->pp_message_literalppfml|List_literalml->pp_list_literalppfmlandpp_message_literalppfml=Format.fprintfppf"{@[<v>%a@]}"(Format.pp_print_list~pp_sep:(funppf()->Format.fprintfppf",@,")pp_message_field)mlandpp_list_literalppfml=Format.fprintfppf"[@[<v>%a@]]"(Format.pp_print_list~pp_sep:(funppf()->Format.fprintfppf",@,")pp_value)mlandpp_message_fieldppf(field,value)=Format.fprintfppf"%S: %a"fieldpp_valuevalueletpp_tppf(name,value)=Format.fprintfppf"{@;<1 2>%S: %a@;<1 2>}"(stringify_option_namename)pp_valuevalueletpp_setppfset=Format.fprintfppf"[@[<v>%a@]]"(Format.pp_print_list~pp_sep:(funppf()->Format.fprintfppf",@,")pp_t)set