123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248(*
* BNF3: BNF tree representation.
* Copyright (C) 2008
* Dmitri Boulytchev, St.Petersburg State University
*
* This software is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License version 2, as published by the Free Software Foundation.
*
* This software 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 Library General Public License version 2 for more details
* (enclosed in the file COPYING).
*)openPrintfmoduletypePrintHelper=sigtypetvalstr:string->stringvalterm:string->stringvalnt:string->stringvalaster:string->stringvalplus:string->stringvalalt:stringlist->stringvalseq:stringlist->stringvalgroup:string->stringvalcustom:(t->string)->[`Sofstring|`Toft]list->stringvalapply:string->stringlist->stringvalopt:string->stringvallist:(t->string)->tlist->stringlistvalrule:string->string->stringvalprule:string->stringlist->string->stringendletconcatWithsfxy=(ifx=""then""elsex^s)^(fy)letconcatfxy=concatWith", "fxyletidx=xletconcat'xy=concatidxyletfoldfx=List.fold_leftf""xmodulerecExpr:sigtypet=Stringofstring|Termofstring|Nontermofstring|Applyoft*tlist|Staroft|Plusoft|Optoft|Altoftlist|Seqoftlist|Groupoft|Customof[`Sofstring|`Toft]listvalstring:string->tvalterm:string->tvalnonterm:string->tvalapply:t->tlist->tvalstar:t->tvalplus:t->tvalopt:t->tvalalt:tlist->tvalseq:tlist->tvalgroup:t->tvalcustom:[`Sofstring|`Toft]list->tvaltoTeX:t->stringvaltoTree:t->stringend=structtypet=Stringofstring|Termofstring|Nontermofstring|Applyoft*tlist|Staroft|Plusoft|Optoft|Altoftlist|Seqoftlist|Groupoft|Customof[`Sofstring|`Toft]listletalt=function|[x]->x|x->Altxletseq=function|[x]->x|x->Seqxletstringx=Stringxletterms=Termsletnonterms=Nontermsletapplyxy=Apply(x,y)letstarx=Starxletplusx=Plusxletopt=function|Groupx->Optx|Optx->Optx|x->Optxletgroup=function|((Seq_)asx)|((Alt_)asx)->Groupx|x->xletcustomx=CustomxmodulePrinter(X:PrintHelperwithtypet=t)=structletrecprint=function|Strings->X.strs|Termt->X.termt|Nontermn->X.ntn|Stare->X.aster(printe)|Pluse->X.plus(printe)|Opte->X.opt(printe)|Altl->X.alt(List.mapprintl)|Seql->X.seq(List.mapprintl)|Groupe->X.group(printe)|Customs->X.customprints|Apply(x,y)->X.apply(printx)(X.listprinty)endmoduleTeXPrinter=Printer(TeXHelper)moduleTreePrinter=Printer(TreeHelper)lettoTree=TreePrinter.printlettoTeX=TeXPrinter.printendandTreeHelper:PrintHelperwithtypet=Expr.t=structtypet=Expr.tletoptstr=sprintf"Opt (%s)"strletplusstr=sprintf"Plus (%s)"strletasterstr=sprintf"Aster (%s)"strletgroupstr=sprintf"Group (%s)"strletntstr=sprintf"Nonterm %s"strletaltlst=sprintf"Alt (%s)"(foldconcat'lst)letseqlst=sprintf"Seq (%s)"(foldconcat'lst)letlistfx=List.mapfxlettermstr=sprintf"Term %s"strletstrarg=sprintf"String %s"argletrulexy=sprintf"%s :: %s"xyletprulexyz=lety=List.fold_left(funaccy->acc^"["^y^"]")""yinsprintf"%s%s :: %s"xyzletapplyxy=sprintf"Apply (%s, [%s])"x(fold(concatid)y)letcustomfx=sprintf"Custom (%s)"(fold(concatWith""(function`Ss->s|`Tt->ft))x)endandTeXHelper:PrintHelperwithtypet=Expr.t=structtypet=Expr.tletquotes=letbuf=Buffer.create(String.lengths*2)infori=0toString.lengths-1doBuffer.add_stringbuf(matchs.[i]with|'"'->"\\\""|'{'->"\\{"|'}'->"\\}"|'$'->"\\$"|'&'->"\\&"|'#'->"\\#"|'%'->"\\%"|'_'->"\\_"|'~'->"$\\tilde{}$"|'\\'->"$\\backslash$"|'<'->"$<$"|'>'->"$>$"|'|'->"$|$"|'^'->"$\\hat{}$"|c->String.make1c)done;Buffer.contentsbufletoptstr=sprintf"\\osropt{%s}"strletplusstr=sprintf"\\osrplus{%s}"strletasterstr=sprintf"\\osraster{%s}"strletgroupstr=sprintf"\\osrgroup{%s}"strletntstr=sprintf"\\osrnonterm{%s}"(quotestr)letaltlst="\\osfralt "^(fold(concatWith"\\osralt "id)lst)letseqlst=sprintf"\\osrblock{%s}"(fold(concatWith"\\osbr "id)lst)letlistfx=List.mapfxlettermstr=sprintf"\\osrterm{%s}"(quotestr)letstrx=letf=reftrueinfori=0toString.lengthx-1doletc=x.[i]inf:=!f&&((c>='a'&&c<='z')||(c>='A'&&c<='Z')||(c>='0'&&c<='9'))done;if!fthensprintf"\\osrterm{%s}"(quotex)elsesprintf"\\osrterm{``%s''}"(quotex)letrulexy=sprintf"\\osrule{%s}{%s}\n"xyletprulexyz=lety=List.fold_left(funaccyi->acc^"["^yi^"]")""yinsprintf"\\osprule{%s}{%s}{%s}\n"xyzletcustomfx=fold(concatWith""(function`Ss->quotes|`Tt->ft))xletapplyxy=sprintf"%s%s"x(fold(funaccx->acc^sprintf"\\osrargs{%s}"x)y)endmoduleDef=structtypet=string*stringlist*Expr.tletmakename=functionExpr.Groupx->name,[],x|x->name,[],xletmakePnameargs=functionExpr.Groupx->name,args,x|x->name,args,xlettoTeX(name,args,expr)=matchargswith|[]->TeXHelper.rulename(Expr.toTeXexpr)|args->TeXHelper.prulenameargs(Expr.toTeXexpr)end