123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126(*
Tools
*)(* (@@) is too strong *)external(&):('a->'b)->'a->'b="%apply"let(!!%)=Format.eprintfletflipfxy=fyxletflip2fxyz=fzxymoduleFormat=structincludeFormattypet=formatterletsprintffmt=letbuf=Buffer.create100inletppf=formatter_of_bufferbufinkfprintf(funppf->pp_print_flushppf();Buffer.contentsbuf)ppffmtletksprintfffmt=letbuf=Buffer.create100inletppf=formatter_of_bufferbufinkfprintf(funppf->pp_print_flushppf();f(Buffer.contentsbuf))ppffmtletwrapfleftrightppffmt=leftppf;kfprintfrightppffmtendmoduleOption=structletmapf=function|None->None|Somev->Some(fv)openFormatletformatfppf=function|None->pp_print_stringppf"None"|Somev->fprintfppf"@[<2>Some@ (@[%a@])@]"fvletto_list=function|Somex->[x]|None->[]endmoduleList=structincludeListletrecfilter_mapf=function|[]->[]|x::xs->matchfxwith|None->filter_mapfxs|Somey->y::filter_mapfxsletconcat_mapfxs=concat(mapfxs)letassoc_optxxs=trySome(assocxxs)with_->Noneletpartition_mapfxs=letrecpartleftright=function|[]->revleft,revright|x::xs->matchfxwith|`Leftv->part(v::left)rightxs|`Rightv->partleft(v::right)xsinpart[][]xsopenFormatletrecformat(sep:(unit,formatter,unit)format)fppf=function|[]->()|[x]->fppfx|x::xs->fprintfppf"@[%a@]%t%a"fx(funppf->fprintfppfsep)(formatsepf)xsletfrom_toft=(* CR jfuruse: we should build from 'to' *)letrecfrom_tostft=iff>tthenrevstelsefrom_to(f::st)(f+1)tinfrom_to[]ftendmoduleString=structincludeStringletis_prefixps=trysubs0(lengthp)=pwith_->falseendmoduleHashtbl=structincludeHashtblletto_listtbl=Hashtbl.fold(funkvst->(k,v)::st)tbl[]endmoduleFilename=structincludeFilenameletsplit_extensions=letopenStringintryletpos=rindexs'.'insubs0pos,subspos(lengths-pos)with|_->s,""endletprotectf=tryOk(f())withe->Erroreletunprotect=function|Okv->v|Errore->raiseeletwarnffmt=letopenFormatinwrapf(funppf->fprintfppf"@[<2>Warning: ")(funppf->fprintfppf"@]@.")err_formatterfmtletraise_errorf=Location.raise_errorf