Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file lines.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232openCFStreamtypeitem=Line.t[@@derivingsexp]moduleMakeIO(Future:Future.S)=structopenFutureletreadr=Reader.linesr|>Pipe.map~f:Line.of_string_unsafeletwritewpipe_r=Pipe.iterpipe_r~f:(funitem->Writer.write_linew(item:item:>string))letwrite_file?perm?appendfilepipe_r=Writer.with_file?perm?appendfile~f:(funw->writewpipe_r)endincludeMakeIO(Future_unix)letof_char_streamcstr=letf_=matchStream.peekcstrwith|None->None|Some_->letans=Buffer.create100inletrecloop()=matchStream.nextcstrwith|Somec->ifc<>'\n'then(Buffer.add_charansc;loop())|None->()inloop();Some(Buffer.contentsans|>Line.of_string_unsafe)inStream.fromfletof_channelcin=letf_=In_channel.input_linecin|>Option.map~f:Line.of_string_unsafeinStream.fromfletof_strings=letn=String.lengthsinletfpos=ifpos>=nthenNoneelseifs.[pos]='\n'thenSome(Line.of_string_unsafe"",pos+1)elseletsub,new_pos=matchString.lfindi~poss~f:(fun_c->c='\n')with|Somepos'->String.subs~pos~len:(pos'-pos),pos'+1|None->String.suffixs(n-pos),ninSome(Line.of_string_unsafesub,new_pos)inStream.unfold0~fletto_channelxsoc=Stream.iterxs~f:(funl->Out_channel.output_stringoc(l:item:>string);Out_channel.newlineoc)letwith_filefn~f=In_channel.with_filefn~f:(funic->f(readic))moduleBuffer=structtypet={mutableunfinished_line:stringoption;lines:itemQueue.t;mutableparsed_lines:int;filename:stringoption;}letmake?filename()={unfinished_line=None;lines=Queue.create();parsed_lines=0;filename}letfeed_lineps=Queue.enqueuep.linessletfeed_stringps=letlines=String.splits~on:'\n'inletrecfaux=function|[]->assertfalse|[""]->(* last char was a "\n" *)()|[s]->(* there is a partial line at the end *)p.unfinished_line<-Somes;|h::t->Queue.enqueuep.lines(Line.of_string_unsafeh);fauxtinmatchp.unfinished_line,lineswith|_,[]->assertfalse|_,[""]->()|None,l->fauxl|Somes,h::t->p.unfinished_line<-None;faux((s^h)::t)letqueued_linesp=Queue.lengthp.linesletpeek_linep=Queue.peekp.linesletnext_linep=letl=Queue.dequeuep.linesinifl<>Nonethen(p.parsed_lines<-p.parsed_lines+1;);lexceptionNo_next_lineletnext_line_exnp=matchnext_linepwith|Somes->s|None->raiseNo_next_lineletcurrent_positionp=Pos.make?source:p.filename~line:p.parsed_lines()letis_emptyp=Queue.is_emptyp.lines&&p.unfinished_line=Noneletcontentsp=Queue.to_listp.lines,p.unfinished_lineletemptyp=(Queue.clearp.lines;p.unfinished_line<-None)endmoduleTransform=structletstring_to_item()=letbuf=Buffer.make()inTfxm.make~name:"string_to_lines"~feed:(Buffer.feed_stringbuf)~next:(function|true->(matchBuffer.next_linebufwith|Someline->`outputline|None->(matchBuffer.contentsbufwith|[],None->`end_of_stream|[],Someunfinished_line->(Buffer.emptybuf;`output(Line.of_string_unsafeunfinished_line))|_->assertfalse))|false->(matchBuffer.next_linebufwith|None->`not_ready|Someline->`outputline))()letitem_to_string?(buffer:[`clearofint|`resetofint]=`reset1024)()=letmoduleBuffer=Caml.Bufferinletbuffer,clear_buffer=matchbufferwith|`clears->(Buffer.creates,Buffer.clear)|`resets->(Buffer.creates,Buffer.reset)inTfxm.make~name:"lines_to_string"()~feed:(funl->Buffer.add_stringbuffer(l:Line.t:>string);Buffer.add_charbuffer'\n')~next:(funstopped->matchBuffer.contentsbufferwith|""->ifstoppedthen`end_of_streamelse`not_ready|s->clear_bufferbuffer;`outputs)letgroup2()=letqueue:(item*item)Queue.t=Queue.create()inletitem1=refNoneinTfxm.make~name:"group2"~feed:(functionitem->match!item1with|Someitem1'->(Queue.enqueuequeue(item1',item);item1:=None)|None->item1:=Someitem)~next:(funstopped->matchQueue.dequeuequeuewith|Someij->`output(Okij)|None->ifnotstoppedthen`not_readyelse(match!item1with|None->`end_of_stream|Some_->`output(Error`premature_end_of_input)))()letmake?name?filename~next~on_error()=letlo_parser=Buffer.make?filename()inTfxm.make?name()~feed:(Buffer.feed_stringlo_parser)~next:(funstopped->matchnextlo_parserwith|`output(Okr)->`output(Okr)|`output(Errorr)->`output(Error(on_error(`nextr)))|`not_ready->ifstoppedthen(ifBuffer.is_emptylo_parserthen`end_of_streamelseletl,o=Buffer.contentslo_parserin`output(Error(on_error(`incomplete_input(Buffer.current_positionlo_parser,(l:>stringlist),o)))))else`not_ready)letmake_merge_error=make~on_error:(function|`nexte->e|`incomplete_inpute->`incomplete_inpute)end