123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198(*
Copyright 2013-2018 RIKEN
Copyright 2018-2025 Chiba Institude of Technology
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)(* Author: Masatomo Hashimoto <m.hashimoto@stair.center> *)[%%prepare_logger]moduleAstloc=Langs_common.AstlocmoduleParserlib_base=Langs_common.Parserlib_basemoduleLoc=AstlocmoduleAux=Parser_auxmodulePB=Parserlib_basemoduleTB=TokenbuffermoduleSF=Common.SourceFormmoduleTk=Tokens_[%%capture_pathmoduleF(Stat:Aux.STATE_T)=structmoduleTBF=Tokenbuffer.F(Stat)moduleT=Trunk.F(Stat)modulePP=Tokenpp.F(Stat)moduleU=Ulexer.F(Stat)moduleA=Aux.F(Stat)typebufs={b_ppbuf:PP.buffer;b_trunkbuf:T.buffer;b_tokensrc:T.tokensource;}letmkbufsppbuftrunkbuftokensrc={b_ppbuf=ppbuf;b_trunkbuf=trunkbuf;b_tokensrc=tokensrc;}exceptionEmptyclassc(env:Aux.env)(partial_parser_selector:Context.t->TB.partial_parserlist)=object(self)inherit[Tk.token]PB.scannervalmutablecurrent_bufs=Nonevalmutablecurrent_src=Nonevalmutablelast_token=PB.make_token(Tk.EOFNone)Loc.dummy_lexposLoc.dummy_lexposvalmutableset_ignored_regions=fun()->()methodlast_loc=Token.to_loc~cache:(Someenv#fname_ext_cache)last_tokenmethodprivatecurrent_bufs=matchcurrent_bufswith|Somebufs->bufs|None->raiseEmptymethodprivatecurrent_src=matchcurrent_srcwith|Somesrc->src|None->raiseEmptymethodget_token()=letcurrent_bufs=self#current_bufsinletppbuf=current_bufs.b_ppbufinlettrunkbuf=current_bufs.b_trunkbufinlettokensrc=current_bufs.b_tokensrcinifenv#context_enter_flagthenbeginenv#clear_context_enter_flag;env#set_last_active_ofsstrunkbuf#get_last_offsetsend;ifenv#context_activate_flagthenbeginenv#clear_context_activate_flag;env#set_last_active_ofsstrunkbuf#get_prev_offsetsend;letcurrent_src=self#current_srcinifcurrent_src#eof_reachedthenbeginmatchcurrent_src#eof_locwith|Someloc->Common.fail_to_parse~head:(Loc.to_string~prefix:"["~suffix:"]"loc)"syntax error"|None->assertfalseendelsebegin[%debug_log"calling pp"];let_qtoken=ppbuf#pp()inletqtoken=TBF.hack_token(tokensrc:>Tokensource.c)_qtokeninlettok,loc=qtokeninlettoken=PB.qtoken_to_tokenqtokenin[%debug_log"token --------------------> %s"(Token.to_string~show_ext:truetoken)];(*Printf.printf "%s\n%!" (Token.to_string token);*)last_token<-token;beginmatchtokwith|Tk.EOF_->begincurrent_src#set_eof_reached;current_src#set_eof_loclocend|Tk.PP_DEFINE__IDENT__BODY_|Tk.PP_UNDEF__IDENT_|Tk.PP_ISSUE__MESG_|Tk.PP_INCLUDE__FILE_|Tk.PP_UNKNOWN__REST_->()|_->()end;tokenendmethodenter_sourcesrc=[%debug_log"source=\"%s\""src#filename];current_src<-Somesrc;letulexbuf=ifsrc#filename="<stdin>"thenbeginsrc#get_ulexbuf_from_stdinendelsebeginself#guess_source_formsrc;src#get_ulexbufendinlettrunkbuf=newT.bufferpartial_parser_selectorinlettokensrc=newT.tokensourcetrunkbufulexbufinletppbuf=newPP.buffer0partial_parser_selector(tokensrc:>Tokensource.c)inletbufs=mkbufsppbuftrunkbuftokensrcincurrent_bufs<-Somebufs;env#set_enter_source_callbacktokensrc#enter_source;set_ignored_regions<-(fun()->env#ignored_regions#add_regionsppbuf#ignored_regions);ulexbufmethodset_ignored_regions=set_ignored_regions()(*
method exit_source =
[%debug_log "called"];
let poped = Stack.pop stack in
env#ignored_regions#add_regions (poped.b_ppbuf#ignored_regions);
let bufs_opt =
try
Some (Stack.top stack)
with
Stack.Empty -> None
in
current_bufs <- bufs_opt
*)methodguess_source_formsrc=letfile=src#fileinletform=U.guess_source_formfileinenv#verbose_msg"setting source form of \"%s\" to %s"file#path(SF.to_stringform);src#set_source_formforminitializerenv#set_enter_source_callbackself#enter_sourceend(* of class Scanner.F.c *)end(* of functor Scanner.F *)]