123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121(*
* Copyright (c) 2018 Thomas Gazagnaire <thomas@gazagnaire.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)letsrc=Logs.Src.create"ocaml-mdx"moduleLog=(valLogs.src_logsrc:Logs.LOG)moduleOutput=OutputmoduleCram=CrammoduleToplevel=ToplevelmoduleBlock=BlockmoduleMigrate_ast=Migrate_astmoduleCompat=Compattypeline=|Sectionof(int*string)|Textofstring|BlockofBlock.ttypet=linelistletdump_lineppf(l:line)=matchlwith|Blockb->Fmt.pfppf"Block %a"Block.dumpb|Section(d,s)->Fmt.pfppf"Section (%d, %S)"ds|Texts->Fmt.pfppf"Text %S"sletdump=Fmt.Dump.listdump_lineletpp_line?syntaxppf(l:line)=matchlwith|Blockb->Fmt.pfppf"%a\n"(Block.pp?syntax)b|Section(d,s)->Fmt.pfppf"%s %s\n"(String.maked'#')s|Texts->Fmt.pfppf"%s\n"sletpp?syntaxppft=Fmt.pfppf"%a\n"Fmt.(list~sep:(unit"\n")(pp_line?syntax))tletto_string=Fmt.to_to_stringppletsection_of_line=function|Sections->Somes|Text_->None|Blockb->b.sectionletfilter_sectionre(t:t)=matchList.filter(funl->matchsection_of_linelwith|None->false|Some(_,s)->Re.execpres)twith|[]->None|l->Somelletparsel=List.map(function|`Textt->Textt|`Sections->Sections|`Blockb->Blockb)ltypesyntax=Syntax.t=|Normal|Cramletparse_lexbufsyntaxl=parse(Lexer.tokensyntaxl)letparse_filesyntaxf=parse(Lexer.tokensyntax(snd(Misc.initf)))letof_stringsyntaxs=parse_lexbufsyntax(Lexing.from_strings)leteval=function|Section_|Text_asx->x|Blocktasx->lett'=Block.evaltinift==t'thenxelseBlockt'typeexpect_result=|Identical|Differsletrun_str~syntax~ffile=letfile_contents,lexbuf=Misc.initfileinletitems=parse_lexbufsyntaxlexbufinletitems=List.mapevalitemsinLog.debug(funl->l"run @[%a@]"dumpitems);letcorrected=ffile_contentsitemsinletresult=ifcorrected<>file_contentsthenDifferselseIdenticalin(result,corrected)letwrite_file~outfilecontent=letoc=open_out_binoutfileinoutput_stringoccontent;close_outocletrun_to_stdout?(syntax=Normal)~finfile=let(_,corrected)=run_str~syntax~finfileinprint_stringcorrectedletrun_to_file?(syntax=Normal)~f~outfileinfile=let(_,corrected)=run_str~syntax~finfileinwrite_file~outfilecorrectedletrun?(syntax=Normal)?(force_output=false)~finfile=letoutfile=infile^".corrected"inlet(test_result,corrected)=run_str~syntax~finfileinmatchforce_output,test_resultwith|true,_|false,Differs->write_file~outfilecorrected|false,Identical->ifSys.file_existsoutfilethenSys.removeoutfile