Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file pp.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180type+'at=|Nop|Seqof'at*'at|Concatof'atlist|Boxofint*'at|Vboxofint*'at|Hboxof'at|Hvboxofint*'at|Hovboxofint*'at|Intofint|Stringofstring|Charofchar|List:'bt*('a->'bt)*'alist->'bt|Space|Cut|Newline|Textofstring|Tagof'a*'atmoduletypeTag=sigtypetmoduleHandler:sigtypetag=ttypetvalinit:tvalhandle:t->tag->string*t*stringendwithtypetag:=tendmoduleRenderer=structmoduletypeS=sigmoduleTag:Tagvalstring:unit->(?margin:int->?tag_handler:Tag.Handler.t->Tag.tt->string)Staged.tvalchannel:out_channel->(?margin:int->?tag_handler:Tag.Handler.t->Tag.tt->unit)Staged.tendmoduleMake(Tag:Tag)=structopenFormatmoduleTag=Tag(* The format interface only support string for tags, so we embed
then as follow:
- length of opening string on 16 bits
- opening string
- closing string
*)externalget16:string->int->int="%caml_string_get16"externalset16:bytes->int->int->unit="%caml_string_set16"letembed_tag~opening~closing=letopening_len=String.lengthopeninginletclosing_len=String.lengthclosinginassert(opening_len<=0xffff);letbuf=Bytes.create(2+opening_len+closing_len)inset16buf0opening_len;Bytes.blit_string~src:opening~src_pos:0~dst:buf~dst_pos:2~len:opening_len;Bytes.blit_string~src:closing~src_pos:0~dst:buf~dst_pos:(2+opening_len)~len:closing_len;Bytes.unsafe_to_stringbufletextract_opening_tags=letopen_len=get16s0inString.subs~pos:2~len:open_lenletextract_closing_tags=letpos=2+get16s0inString.dropsposletrecppthppft=matchtwith|Nop->()|Seq(a,b)->ppthppfa;ppthppfb|Concatl->List.iterl~f:(ppthppf)|Box(indent,t)->pp_open_boxppfindent;ppthppft;pp_close_boxppf()|Vbox(indent,t)->pp_open_vboxppfindent;ppthppft;pp_close_boxppf()|Hboxt->pp_open_hboxppf();ppthppft;pp_close_boxppf()|Hvbox(indent,t)->pp_open_hvboxppfindent;ppthppft;pp_close_boxppf()|Hovbox(indent,t)->pp_open_hovboxppfindent;ppthppft;pp_close_boxppf()|Intx->pp_print_intppfx|Stringx->pp_print_stringppfx|Charx->pp_print_charppfx|List(sep,f,l)->pp_print_list(funppfx->ppthppf(fx))ppfl~pp_sep:(funppf()->ppthppfsep)|Space->pp_print_spaceppf()|Cut->pp_print_cutppf()|Newline->pp_force_newlineppf()|Texts->pp_print_textppfs|Tag(tag,t)->letopening,th,closing=Tag.Handler.handlethtaginpp_open_tagppf(embed_tag~opening~closing);ppthppft;pp_close_tagppf()letsetupppf=letfuncs=pp_get_formatter_tag_functionsppf()inpp_set_mark_tagsppftrue;pp_set_formatter_tag_functionsppf{funcswithmark_open_tag=extract_opening_tag;mark_close_tag=extract_closing_tag}letstring()=letbuf=Buffer.create1024inletppf=formatter_of_bufferbufinsetupppf;Staged.stage(fun?(margin=80)?(tag_handler=Tag.Handler.init)t->pp_set_marginppfmargin;pptag_handlerppft;pp_print_flushppf();lets=Buffer.contentsbufinBuffer.clearbuf;s)letchanneloc=letppf=formatter_of_out_channelocinsetupppf;Staged.stage(fun?(margin=80)?(tag_handler=Tag.Handler.init)t->pp_set_marginppfmargin;pptag_handlerppft;pp_print_flushppf())endendmoduleRender=Renderer.Make(structtypet=unitmoduleHandler=structtypet=unitletinit=()lethandle()()="",(),""endend)letppppft=Render.pp()ppftletnop=Nopletseqab=Seq(a,b)letconcatl=Concatlletbox?(indent=0)l=Box(indent,Concatl)letvbox?(indent=0)l=Vbox(indent,Concatl)lethboxl=Hbox(Concatl)lethvbox?(indent=0)l=Hvbox(indent,Concatl)lethovbox?(indent=0)l=Hovbox(indent,Concatl)letintx=Intxletstringx=Stringxletcharx=Charxletlist?(sep=Cut)l~f=List(sep,f,l)letspace=Spaceletcut=Cutletnewline=Newlinelettexts=Textslettagt~tag=Tag(tag,t)