123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107(* Parser for directives in C-like syntax, rewriting them into extensions,
like ones we would get from parsing OCaml file.
*)openPpxlibmoduleParsing=Caml.Parsingtypelexer=Lexing.lexbuf->Parser.token(* +---------------------------------------------------------------+
| Parsing of directives |
+---------------------------------------------------------------+ *)letlocatedxlexbuf={Location.txt=x;loc=Location.of_lexbuflexbuf};;letparseparsing_funlexerlexbuf=tryparsing_funlexerlexbufwithParsing.Parse_error|Syntaxerr.Escape_error->letloc=Location.of_lexbuflexbufinraise(Syntaxerr.Error(Syntaxerr.Otherloc));;letfetch_directive_argument(lexer:lexer)lexbuf=letrecloopacc(brackets:Parser.tokenlist)=matchlexerlexbuf,bracketswith|EOF,_|EOL,[]->locatedParser.EOFlexbuf::acc|(EOL|COMMENT_),_->loopaccbrackets|token,_->letacc=locatedtokenlexbuf::accinmatchtoken,bracketswith|BEGIN,_->loopacc(END::brackets)|DO,_->loopacc(DONE::brackets)|LPAREN,_->loopacc(RPAREN::brackets)|LBRACE,_->loopacc(RBRACE::brackets)|LBRACELESS,_->loopacc(GREATERRBRACE::brackets)|LBRACKETLESS,_->loopacc(GREATERRBRACKET::brackets)|LBRACKETBAR,_->loopacc(BARRBRACKET::brackets)|(LBRACKET|LBRACKETGREATER|LBRACKETPERCENT|LBRACKETPERCENTPERCENT|LBRACKETAT|LBRACKETATAT|LBRACKETATATAT),_->loopacc(RBRACKET::brackets)|_,closing::bracketswhentoken=closing->loopaccbrackets|_->loopaccbracketsinletstart_pos=Lexing.lexeme_end_plexbufinmatchloop[][]|>List.revwith|[]->None|tokens->lettokens=reftokensinletfake_lexer(lexbuf:Lexing.lexbuf):Parser.token=match!tokenswith|[]->EOF|token::rest->tokens:=rest;lexbuf.lex_start_p<-token.loc.loc_start;lexbuf.lex_curr_p<-token.loc.loc_end;token.txtinletfake_lexbuf=Lexing.from_function(fun__->assertfalse)infake_lexbuf.lex_curr_p<-start_pos;matchparseParser.implementationfake_lexerfake_lexbufwith|[]->None|[st]->assert_no_attributes_in#structure_itemst;Somest|_::st::_->Location.raise_errorf~loc:st.pstr_loc"optcomp: too many structure items";;letparse_directive(lexer:lexer)lexbuf:('aToken.t)=lettoken=located(lexerlexbuf)lexbufinletarg=fetch_directive_argumentlexerlexbufinletloc={token.locwithloc_end=Lexing.lexeme_end_plexbuf}inletpayload=matchargwith|Somest_item->PStr[st_item]|None->PStr[]inmatchtoken.txtwith|IF->Token.make_directive"if"locpayload|ELSE->Token.make_directive"else"locpayload|LIDENTs->Token.make_directiveslocpayload|_->Location.raise_errorf~loc"optcomp: unknown token"letparse_looplexbuf=letis_beginning_of_linelexbuf=letpos=Lexing.lexeme_start_plexbufinpos.pos_cnum=pos.pos_bolinletrecparse_loop_auxacc=matchLexer.token_with_commentslexbufwith|HASHwhenis_beginning_of_linelexbuf->letacc=parse_directiveLexer.token_with_commentslexbuf::accinparse_loop_auxacc|EOF->acc|_->parse_loop_auxaccinList.rev(parse_loop_aux[])