123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463(* This file is part of the Catala compiler, a specification language for tax
and social benefits computation rules. Copyright (C) 2020 Inria,
contributors: Denis Merigoux <denis.merigoux@inria.fr>, Emile Rolley
<emile.rolley@tuta.io>
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License. *)(* Types used by flags & options *)typefile=stringtyperaw_file=filetypebackend_lang=En|Fr|Pltypewhen_enum=Auto|Always|Nevertypemessage_format_enum=Human|GNUtypeinput_src=FileNameoffile|Contentsofstring*file|Stdinoffile(** Associates a {!type: Cli.backend_lang} with its string represtation. *)letlanguages=["en",En;"fr",Fr;"pl",Pl]letlanguage_code=letrl=List.map(fun(a,b)->b,a)languagesinfunl->List.assoclrlletinput_src_file=functionFileNamef|Contents(_,f)|Stdinf->fletmessage_format_opt=["human",Human;"gnu",GNU]typeoptions={mutableinput_src:input_src;mutablelanguage:backend_langoption;mutabledebug:bool;mutablecolor:when_enum;mutablemessage_format:message_format_enum;mutabletrace:bool;mutableplugins_dirs:filelist;mutabledisable_warnings:bool;mutablemax_prec_digits:int;mutablepath_rewrite:raw_file->file;}(* Note: we force that the global options (ie options common to all commands)
and the options available through global refs are the same. While this is a
bit arbitrary, it makes some sense code-wise and provides some safeguard
against explosion of the number of global references. Reducing the number of
globals further would be nice though. *)letglobals={input_src=Stdin"-stdin-";language=None;debug=false;color=Auto;message_format=Human;trace=false;plugins_dirs=[];disable_warnings=false;max_prec_digits=20;path_rewrite=(fun_->assertfalse);}letenforce_globals?input_src?language?debug?color?message_format?trace?plugins_dirs?disable_warnings?max_prec_digits?path_rewrite()=Option.iter(funx->globals.input_src<-x)input_src;Option.iter(funx->globals.language<-x)language;Option.iter(funx->globals.debug<-x)debug;Option.iter(funx->globals.color<-x)color;Option.iter(funx->globals.message_format<-x)message_format;Option.iter(funx->globals.trace<-x)trace;Option.iter(funx->globals.plugins_dirs<-x)plugins_dirs;Option.iter(funx->globals.disable_warnings<-x)disable_warnings;Option.iter(funx->globals.max_prec_digits<-x)max_prec_digits;Option.iter(funx->globals.path_rewrite<-x)path_rewrite;globalsopenCmdliner(* Arg converters for our custom types *)letwhen_opt=Arg.enum["auto",Auto;"always",Always;"never",Never](* Some helpers for catala sources *)letextensions=[".catala_fr",Fr;".catala_en",En;".catala_pl",Pl]letfile_langfilename=List.assoc_opt(Filename.extensionfilename)extensions|>function|Somelang->lang|None->(matchglobals.languagewith|Somelang->lang|None->Format.kasprintffailwith"Could not infer language variant from the extension of \
@{<yellow>%s@}, and @{<bold>--language@} was not specified"filename)letreverse_path?(from_dir=Sys.getcwd())~to_dirf=ifFilename.is_relativefrom_dirtheninvalid_arg"File.with_reverse_path"elseifnot(Filename.is_relativef)thenfelseifnot(Filename.is_relativeto_dir)thenFilename.concatfrom_dirfelseletrecauxaccrbase=function|[]->acc|dir::p->(ifdir=Filename.parent_dir_namethenmatchrbasewith|base1::rbase->aux(base1::acc)rbasep|[]->auxacc[]pelsematchaccwith|dir1::accwhendir1=dir->auxaccrbasep|_->aux(Filename.parent_dir_name::acc)rbasep)inletpath_to_listpath=String.split_on_charFilename.dir_sep.[0]path|>List.filter(function""|"."->false|_->true)inletrbase=List.rev(path_to_listfrom_dir)inString.concatFilename.dir_sep(aux(path_to_listf)rbase(path_to_listto_dir))(** CLI flags and options *)moduleFlags=structopenCmdlineropenArgmoduleGlobal=structletinfo=info~docs:Manpage.s_common_optionsletinput_src=letconverter=conv~docv:"FILE"((funs->ifs="-"thenOk(Stdin"-stdin-")elseResult.map(funf->FileNamef)(conv_parsernon_dir_files)),funppf->function|Stdin_->Format.pp_print_stringppf"-"|FileNamef->conv_printernon_dir_fileppff|_->assertfalse)inrequired&pos~rev:true0(someconverter)None&Arg.info[]~docv:"FILE"~docs:Manpage.s_arguments~doc:"Catala master file to be compiled ($(b,-) for stdin)."letlanguage=value&opt(some(enumlanguages))None&info["l";"language"]~docv:"LANG"~doc:"Locale variant of the input language to use when it can not be \
inferred from the file extension."letdebug=value&flag&info["debug";"d"]~env:(Cmd.Env.info"CATALA_DEBUG")~doc:"Prints debug information."letcolor=letunstyled=value&flag&info["unstyled"]~doc:"Removes styling (colors, etc.) from terminal output."~deprecated:"Use $(b,--color=)$(i,never) instead"inletcolor=value&opt~vopt:Alwayswhen_optAuto&info["color"]~env:(Cmd.Env.info"CATALA_COLOR")~doc:"Allow output of colored and styled text. Use $(i,auto), to \
enable when the standard output is to a terminal, $(i,never) to \
disable."inTerm.(const(funcolorunstyled->ifunstyledthenNeverelsecolor)$color$unstyled)letmessage_format=value&opt(enummessage_format_opt)Human&info["message-format"]~doc:"Selects the format of error and warning messages emitted by the \
compiler. If set to $(i,human), the messages will be nicely \
displayed and meant to be read by a human. If set to $(i, gnu), \
the messages will be rendered according to the GNU coding \
standards."lettrace=value&flag&info["trace";"t"]~doc:"Displays a trace of the interpreter's computation or generates \
logging instructions in translate programs."letplugins_dirs=letdoc="Set the given directory to be searched for backend plugins."inletenv=Cmd.Env.info"CATALA_PLUGINS"inletdefault=let(/)=Filename.concatinletexec_dir=Filename.(dirnameSys.argv.(0))inletdev_plugin_dir=exec_dir/"plugins"inifSys.file_existsdev_plugin_dirthen(* When running tests in place, may need to lookup in _build/default
besides the exec *)[dev_plugin_dir]else(* Otherwise, assume a standard layout: "<prefix>/bin/catala" besides
"<prefix>/lib/catala" *)[Filename.(dirnameexec_dir)/"lib"/"catala"/"plugins"]invalue&opt_allstringdefault&info["plugin-dir"]~docv:"DIR"~env~docletdisable_warnings=value&flag&info["disable-warnings"]~doc:"Disable all the warnings emitted by the compiler."letmax_prec_digits=value&optint20&info["p";"max-digits-printed"]~docv:"NUM"~doc:"Maximum number of significant digits printed for decimal results."letname_flag=value&opt(somestring)None&info["name"]~docv:"FILE"~doc:"Treat the input as coming from a file with the given name. Useful \
e.g. when reading from stdin"letdirectory=value&opt(somedir)None&info["C";"directory"]~docv:"DIR"~doc:"Behave as if run from the given directory for file and error \
reporting. Does not affect resolution of files in arguments."letflags=letmakelanguagedebugcolormessage_formattraceplugins_dirsdisable_warningsmax_prec_digitsdirectory:options=ifdebugthenPrintexc.record_backtracetrue;letpath_rewrite=matchdirectorywith|None->funf->f|Someto_dir->(function"-"->"-"|f->reverse_path~to_dirf)in(* This sets some global refs for convenience, but most importantly
returns the options record. *)enforce_globals~language~debug~color~message_format~trace~plugins_dirs~disable_warnings~max_prec_digits~path_rewrite()inTerm.(constmake$language$debug$color$message_format$trace$plugins_dirs$disable_warnings$max_prec_digits$directory)letoptions=letmakeinput_srcnamedirectoryoptions:options=(* Set some global refs for convenience *)letinput_src=matchnamewith|None->input_src|Somename->(matchinput_srcwith|FileNamef->FileNamef|Contents(str,_)->Contents(str,name)|Stdin_->Stdinname)inletinput_src=matchinput_srcwith|FileNamef->FileName(options.path_rewritef)|Contents(str,f)->Contents(str,options.path_rewritef)|Stdinf->Stdin(options.path_rewritef)inletplugins_dirs=List.mapoptions.path_rewriteoptions.plugins_dirsinOption.iterSys.chdirdirectory;globals.input_src<-input_src;globals.plugins_dirs<-plugins_dirs;{optionswithinput_src;plugins_dirs}inTerm.(constmake$input_src$name_flag$directory$flags)endletinclude_dirs=value&opt_allstring[]&info["I";"include"]~docv:"DIR"~doc:"Include directory to lookup for compiled module files."letcheck_invariants=value&flag&info["check-invariants"]~doc:"Check structural invariants on the AST."letno_typing=value&flag&info["no-typing"]~doc:"Don't check the consistency of types"letwrap_weaved_output=value&flag&info["wrap";"w"]~doc:"Wraps literate programming output with a minimal preamble."letprint_only_law=value&flag&info["print-only-law"]~doc:"In literate programming output, skip all code and metadata sections \
and print only the text of the law."letex_scope=required&opt(somestring)None&info["s";"scope"]~docv:"SCOPE"~doc:"Scope to be focused on."letex_scope_opt=value&opt(somestring)None&info["s";"scope"]~docv:"SCOPE"~doc:"Scope to be focused on."letex_variable=required&opt(somestring)None&info["v";"variable"]~docv:"VARIABLE"~doc:"Variable to be focused on."letoutput=value&opt(somestring)None&info["output";"o"]~docv:"OUTPUT"~env:(Cmd.Env.info"CATALA_OUT")~doc:"$(i, OUTPUT) is the file that will contain the output of the \
compiler. Defaults to $(i,FILE).$(i,EXT) where $(i,EXT) depends on \
the chosen backend. Use $(b,-o -) for stdout."letoptimize=value&flag&info["optimize";"O"]~doc:"Run compiler optimizations."letavoid_exceptions=value&flag&info["avoid-exceptions"]~doc:"Compiles the default calculus without exceptions."letclosure_conversion=value&flag&info["closure-conversion"]~doc:"Performs closure conversion on the lambda calculus. Implies \
$(b,--avoid-exceptions)."letdisable_counterexamples=value&flag&info["disable-counterexamples"]~doc:"Disables the search for counterexamples. Useful when you want a \
deterministic output from the Catala compiler, since provers can \
have some randomness in them."end(* Retrieve current version from dune *)letversion=Version.vlets_plugins="INSTALLED PLUGINS"letinfo=letdoc="Compiler for Catala, a specification language for tax and social benefits \
computation rules."inletman=[`SManpage.s_synopsis;`P"$(mname) [$(i,COMMAND)] $(i,FILE) [$(i,OPTION)]…";`P"Use $(mname) [$(i,COMMAND)] $(b,--hel)p for documentation on a \
specific command";`SManpage.s_description;`P"Catala is a domain-specific language for deriving \
faithful-by-construction algorithms from legislative texts.";`SManpage.s_commands;`Ss_plugins;`SManpage.s_authors;`P"The authors are listed by alphabetical order:";`P"Nicolas Chataing <$(i,nicolas.chataing@ens.fr)>";`Noblank;`P"Alain Delaët-Tixeuil <$(i,alain.delaet--tixeuil@inria.fr)>";`Noblank;`P"Aymeric Fromherz <$(i,aymeric.fromherz@inria.fr)>";`Noblank;`P"Louis Gesbert <$(i,louis.gesbert@ocamlpro.com)>";`Noblank;`P"Denis Merigoux <$(i,denis.merigoux@inria.fr)>";`Noblank;`P"Emile Rolley <$(i,erolley@tutamail.com)>";`SManpage.s_examples;`Pre"catala Interpret -s Foo file.catala_en";`Pre"catala Ocaml -o target/file.ml file.catala_en";`SManpage.s_bugs;`P"Please file bug reports at https://github.com/CatalaLang/catala/issues";]inletexits=Cmd.Exit.defaults@[Cmd.Exit.info~doc:"on error."1]inCmd.info"catala"~version~doc~exits~manexceptionExit_withofint