123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149(* TyXML
* http://www.ocsigen.org/tyxml
* Copyright (C) 2016 Anton Bachin
*
* 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, with linking exception;
* either version 2.1 of the License, or (at your option) any 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., 51 Franklin Street, Suite 500, Boston, MA 02111-1307, USA.
*)openAst_helpermoduleLabel=Ast_convenience.Label(** Lang utilities *)typelang=Html|Svglethtml_implementation=ref"Html"letsvg_implementation=ref"Svg"letimplemenentation_ref=function|Html->html_implementation|Svg->svg_implementationletset_implementationlangs=(implemenentation_reflang):=sletimplementationlang=!(implemenentation_reflang)letlang=function|Html->"HTML"|Svg->"SVG"letmake_lid~locis=Location.mkloc(Longident.parse@@implementationi^"."^s)locletmake~locis=Exp.ident~loc@@make_lid~locis(** Generic *)letfindfl=trySome(List.findfl)withNot_found->Noneletwith_loclocfx=with_default_locloc@@fun()->fxleterror_prefix:_format6="Error: "leterrorlocppf=(* Originally written by @Drup in 24d87befcc505a9e3a1b081849b12560ce38028f. *)(* We use a custom implementation because the type of Location.raise_errorf
changed in 4.03 *)letbuf=Buffer.create17inletfmt=Format.formatter_of_bufferbufinFormat.kfprintf(fun_->Format.pp_print_flushfmt();Location.raise_errorf~loc"%s@."(Buffer.contentsbuf))fmt(error_prefix^^ppf)(** Ast manipulation *)letintloc=with_loclocAst_convenience.intletfloatloc=with_loclocAst_convenience.floatletstringloc=with_loclocAst_convenience.strletadd_constraints~listlange=letloc={e.Parsetree.pexp_locwithloc_ghost=true}inletelt=make_lid~loclang"elt"inletwrap=iflistthenmake_lid~loclang"list_wrap"elsemake_lid~loclang"wrap"inletty=Typ.(constr~locwrap[constr~locelt[any~loc()]])inExp.constraint_~locetytype'avalue=|Valof'a|AntiquotofParsetree.expressionletvaluex=Valxletantiquote=Antiquoteletmap_valuef=function|Valx->Val(fx)|Antiquotx->Antiquotxletlist_genconsappendnill=letfacc=function|Valx->consaccx|Antiquote->appendaccein(l|>List.rev|>List.fold_leftfnil)letlistlocl=letnil=[%expr[]][@metalocloc]inletconsaccx=[%expr[%ex]::[%eacc]][@metalocloc]inletappendaccx=[%expr[%ex]@[%eacc]][@metalocloc]inlist_genconsappendnil@@List.map(funx->Valx)lletlist_wrap_valuelangloc=let(!!)=make~loclanginletnil=[%expr[%e!!"Xml.W.nil"]()][@metalocloc]inletconsaccx=[%expr[%e!!"Xml.W.cons"]([%e!!"Xml.W.return"][%ex])[%eacc]][@metalocloc]inletappendaccx=[%expr[%e!!"Xml.W.append"][%eadd_constraints~list:truelangx][%eacc]][@metalocloc]inlist_genconsappendnilletlist_wraplanglocl=list_wrap_valuelangloc@@List.map(funx->Valx)lletwrapimplementationloce=[%expr[%emake~locimplementation"Xml.W.return"][%ee]][@metalocloc]letwrap_valuelangloc=function|Valx->wraplanglocx|Antiquote->add_constraints~list:falselange