123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243(**************************************************************************)(* *)(* OCaml *)(* *)(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)(* *)(* Copyright 1996 Institut National de Recherche en Informatique et *)(* en Automatique. *)(* *)(* All rights reserved. This file is distributed under the terms of *)(* the GNU Lesser General Public License version 2.1, with the *)(* special exception on linking described in the file LICENSE. *)(* *)(**************************************************************************)(* The run-time library for lexers generated by camllex *)typeposition={pos_fname:string;pos_lnum:int;pos_bol:int;pos_cnum:int;}letdummy_pos={pos_fname="";pos_lnum=0;pos_bol=0;pos_cnum=-1;}typelexbuf={refill_buff:lexbuf->unit;mutablelex_buffer:bytes;mutablelex_buffer_len:int;mutablelex_abs_pos:int;mutablelex_start_pos:int;mutablelex_curr_pos:int;mutablelex_last_pos:int;mutablelex_last_action:int;mutablelex_eof_reached:bool;mutablelex_mem:intarray;mutablelex_start_p:position;mutablelex_curr_p:position;}typelex_tables={lex_base:string;lex_backtrk:string;lex_default:string;lex_trans:string;lex_check:string;lex_base_code:string;lex_backtrk_code:string;lex_default_code:string;lex_trans_code:string;lex_check_code:string;lex_code:string;}externalc_engine:lex_tables->int->lexbuf->int="caml_lex_engine"externalc_new_engine:lex_tables->int->lexbuf->int="caml_new_lex_engine"letenginetblstatebuf=letresult=c_enginetblstatebufinifresult>=0&&buf.lex_curr_p!=dummy_posthenbeginbuf.lex_start_p<-buf.lex_curr_p;buf.lex_curr_p<-{buf.lex_curr_pwithpos_cnum=buf.lex_abs_pos+buf.lex_curr_pos};end;resultletnew_enginetblstatebuf=letresult=c_new_enginetblstatebufinifresult>=0&&buf.lex_curr_p!=dummy_posthenbeginbuf.lex_start_p<-buf.lex_curr_p;buf.lex_curr_p<-{buf.lex_curr_pwithpos_cnum=buf.lex_abs_pos+buf.lex_curr_pos};end;resultletlex_refillread_funaux_bufferlexbuf=letread=read_funaux_buffer(Bytes.lengthaux_buffer)inletn=ifread>0thenreadelse(lexbuf.lex_eof_reached<-true;0)in(* Current state of the buffer:
<-------|---------------------|----------->
| junk | valid data | junk |
^ ^ ^ ^
0 start_pos buffer_end Bytes.length buffer
*)iflexbuf.lex_buffer_len+n>Bytes.lengthlexbuf.lex_bufferthenbegin(* There is not enough space at the end of the buffer *)iflexbuf.lex_buffer_len-lexbuf.lex_start_pos+n<=Bytes.lengthlexbuf.lex_bufferthenbegin(* But there is enough space if we reclaim the junk at the beginning
of the buffer *)Bytes.blitlexbuf.lex_bufferlexbuf.lex_start_poslexbuf.lex_buffer0(lexbuf.lex_buffer_len-lexbuf.lex_start_pos)endelsebegin(* We must grow the buffer. Doubling its size will provide enough
space since n <= String.length aux_buffer <= String.length buffer.
Watch out for string length overflow, though. *)letnewlen=Int.min(2*Bytes.lengthlexbuf.lex_buffer)Sys.max_string_lengthiniflexbuf.lex_buffer_len-lexbuf.lex_start_pos+n>newlenthenfailwith"Lexing.lex_refill: cannot grow buffer";letnewbuf=Bytes.createnewlenin(* Copy the valid data to the beginning of the new buffer *)Bytes.blitlexbuf.lex_bufferlexbuf.lex_start_posnewbuf0(lexbuf.lex_buffer_len-lexbuf.lex_start_pos);lexbuf.lex_buffer<-newbufend;(* Reallocation or not, we have shifted the data left by
start_pos characters; update the positions *)lets=lexbuf.lex_start_posinlexbuf.lex_abs_pos<-lexbuf.lex_abs_pos+s;lexbuf.lex_curr_pos<-lexbuf.lex_curr_pos-s;lexbuf.lex_start_pos<-0;lexbuf.lex_last_pos<-lexbuf.lex_last_pos-s;lexbuf.lex_buffer_len<-lexbuf.lex_buffer_len-s;lett=lexbuf.lex_meminfori=0toArray.lengtht-1doletv=t.(i)inifv>=0thent.(i)<-v-sdoneend;(* There is now enough space at the end of the buffer *)Bytes.blitaux_buffer0lexbuf.lex_bufferlexbuf.lex_buffer_lenn;lexbuf.lex_buffer_len<-lexbuf.lex_buffer_len+nletzero_pos={pos_fname="";pos_lnum=1;pos_bol=0;pos_cnum=0;}letfrom_function?(with_positions=true)f={refill_buff=lex_refillf(Bytes.create512);lex_buffer=Bytes.create1024;lex_buffer_len=0;lex_abs_pos=0;lex_start_pos=0;lex_curr_pos=0;lex_last_pos=0;lex_last_action=0;lex_mem=[||];lex_eof_reached=false;lex_start_p=ifwith_positionsthenzero_poselsedummy_pos;lex_curr_p=ifwith_positionsthenzero_poselsedummy_pos;}letfrom_channel?with_positionsic=from_function?with_positions(funbufn->inputicbuf0n)letfrom_string?(with_positions=true)s={refill_buff=(funlexbuf->lexbuf.lex_eof_reached<-true);lex_buffer=Bytes.of_strings;(* have to make a copy for compatibility
with unsafe-string mode *)lex_buffer_len=String.lengths;lex_abs_pos=0;lex_start_pos=0;lex_curr_pos=0;lex_last_pos=0;lex_last_action=0;lex_mem=[||];lex_eof_reached=true;lex_start_p=ifwith_positionsthenzero_poselsedummy_pos;lex_curr_p=ifwith_positionsthenzero_poselsedummy_pos;}letset_positionlexbufposition=lexbuf.lex_curr_p<-{positionwithpos_fname=lexbuf.lex_curr_p.pos_fname};lexbuf.lex_abs_pos<-position.pos_cnumletset_filenamelexbuffname=lexbuf.lex_curr_p<-{lexbuf.lex_curr_pwithpos_fname=fname}letwith_positionslexbuf=lexbuf.lex_curr_p!=dummy_posletlexemelexbuf=letlen=lexbuf.lex_curr_pos-lexbuf.lex_start_posinBytes.sub_stringlexbuf.lex_bufferlexbuf.lex_start_poslenletsub_lexemelexbufi1i2=letlen=i2-i1inBytes.sub_stringlexbuf.lex_bufferi1lenletsub_lexeme_optlexbufi1i2=ifi1>=0thenbeginletlen=i2-i1inSome(Bytes.sub_stringlexbuf.lex_bufferi1len)endelsebeginNoneendletsub_lexeme_charlexbufi=Bytes.getlexbuf.lex_bufferiletsub_lexeme_char_optlexbufi=ifi>=0thenSome(Bytes.getlexbuf.lex_bufferi)elseNoneletlexeme_charlexbufi=Bytes.getlexbuf.lex_buffer(lexbuf.lex_start_pos+i)letlexeme_startlexbuf=lexbuf.lex_start_p.pos_cnumletlexeme_endlexbuf=lexbuf.lex_curr_p.pos_cnumletlexeme_start_plexbuf=lexbuf.lex_start_pletlexeme_end_plexbuf=lexbuf.lex_curr_pletnew_linelexbuf=letlcp=lexbuf.lex_curr_piniflcp!=dummy_posthenlexbuf.lex_curr_p<-{lcpwithpos_lnum=lcp.pos_lnum+1;pos_bol=lcp.pos_cnum;}(* Discard data left in lexer buffer. *)letflush_inputlb=lb.lex_curr_pos<-0;lb.lex_abs_pos<-0;letlcp=lb.lex_curr_piniflcp!=dummy_posthenlb.lex_curr_p<-{zero_poswithpos_fname=lcp.pos_fname};lb.lex_buffer_len<-0;