Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file i18n_ppx_rewriter.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103(*
* Copyright (C) 2015 BeSport, Julien Sagot
*
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)open!Ppxlibletdefault_module_name=ref""letmodule_prefix=ref""letmodule_suffix=ref""letmk_identi=if!default_module_name=""theninvalid_arg"Missing default module name";letmodule_name,safe_ident=matchiwith(* CASE 1: [%i18n ident ... ] *)|Lident_->!default_module_name,i(* CASE 2: [%i18n S.ident ... ] *)|Ldot(Lident"S",_)->!default_module_name,i(* CASE 3: [%i18n OtherMod.ident ...] *)|Ldot(Lidentmodule_name,s)->!module_prefix^module_name^!module_suffix,Lidents(* CASE 4 : [%i18n OtherMod.S.ident ...] *)|Ldot(Ldot(Lidentmodule_name,"S"),s)->!module_prefix^module_name^!module_suffix,Ldot(Lident"S",s)(* CASE X : [%i18n OtherModA.OtherModB.ident ...]. Illegal. *)|i->leterr_msg=Format.asprintf"%s is not a valid i18n expression"(Longident.namei)infailwitherr_msginletrecprefixi=matchiwith|Lidentid->Ldot(Ldot(Lidentmodule_name,"Tr"),id)|Ldot(i,nm)->Ldot(prefixi,nm)|Lapply_->leterr_msg=Format.asprintf"%s is not a valid i18n expression"(Longident.namei)infailwitherr_msginprefixsafe_identletunitloc=letopenAst_builder.Defaultin(Nolabel,pexp_construct~loc(Located.mk~loc(Lident"()"))None)letlang_args=function|((Labelled("?lang"),_)aslang)::args|((Labelled("~lang"),_)aslang)::args->([lang],args)|x->([],x)letidentlocid=letopenAst_builder.Defaultinpexp_ident~loc(Located.mk~loc(mk_identid))letapplylocidargs=let(lang,args)=lang_argsargsinletopenAst_builder.Defaultinpexp_apply~loc(identlocid)(lang@[unitloc]@args@[unitloc])let()=List.iter(fun(key,spec,doc)->Ppxlib.Driver.add_argkeyspec~doc)["--prefix",Arg.Set_stringmodule_prefix,"PREFIX The prefix added to module names";"--suffix",Arg.Set_stringmodule_suffix,"SUFFIX The suffix added to module names";"--default-module",Arg.Set_stringdefault_module_name,"MODULE Name of the default module"]letexpand~loc:loc0~path:_expr=Ppxlib.Ast_pattern.(parse(pexp_ident__|>map1~f:(funid->applyloc0id[])|||(pexp_apply(pexp_ident__)(many__)|>map2~f:(applyloc0))))loc0expr(funx->x)letextension=Ppxlib.Extension.declare"i18n"Ppxlib.Extension.Context.expressionPpxlib.Ast_pattern.(single_expr_payload__)expandletrule=Ppxlib.Context_free.Rule.extensionextensionlet()=Ppxlib.Driver.register_transformation~rules:[rule]"i18n"