Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file parse.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179(**************************************************************************)(* *)(* 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. *)(* *)(**************************************************************************)(* Entry points in the parser *)(* Skip tokens to the end of the phrase *)letlast_token=refParser.EOFlettokenlexbuf=lettoken=Lexer.tokenlexbufinlast_token:=token;tokenletrecskip_phraselexbuf=matchtokenlexbufwith|Parser.SEMISEMI|Parser.EOF->()|_->skip_phraselexbuf|exception(Lexer.Error(Lexer.Unterminated_comment_,_)|Lexer.Error(Lexer.Unterminated_string,_)|Lexer.Error(Lexer.Reserved_sequence_,_)|Lexer.Error(Lexer.Unterminated_string_in_comment_,_)|Lexer.Error(Lexer.Illegal_character_,_))->skip_phraselexbufletmaybe_skip_phraselexbuf=match!last_tokenwith|Parser.SEMISEMI|Parser.EOF->()|_->skip_phraselexbuftype'aparser=Lexing.position->'aParser.MenhirInterpreter.checkpointletwrap(parser:'aparser)lexbuf:'a=tryDocstrings.init();Lexer.init();letopenParser.MenhirInterpreterinletrecfix_resume=function|InputNeeded_|Accepted_|Rejected|HandlingError_ascp->cp|Shifting(_,_,_)|AboutToReduce(_,_)ascp->fix_resume(resume~strategy:`Simplifiedcp)inletrecoffer_inputlexbufcptok=letptok=Lexing.(tok,lexbuf.lex_start_p,lexbuf.lex_curr_p)inmatchfix_resume(offercpptok)with|InputNeeded_ascp->offer_inputlexbufcp(tokenlexbuf)|Acceptedx->Somex|Rejected->None|Shifting(_,_,_)|AboutToReduce(_,_)->assertfalse|HandlingError_ascp'->matchLexer.try_disambiguatelexbuftokwith|Sometok'->offer_inputlexbufcptok'|None->main_looplexbufcp'andmain_looplexbuf=function|InputNeeded_ascp->offer_inputlexbufcp(tokenlexbuf)|Acceptedx->Somex|Rejected->None|Shifting(_,_,_)|AboutToReduce(_,_)|HandlingError_ascp->main_looplexbuf(resume~strategy:`Simplifiedcp)inletast=matchmain_looplexbuf(parserlexbuf.Lexing.lex_curr_p)with|Someast->ast|None->raiseParsing.Parse_errorinParsing.clear_parser();Docstrings.warn_bad_docstrings();last_token:=Parser.EOF;astwith|Lexer.Error(Lexer.Illegal_character_,_)aserrwhen!Location.input_name="//toplevel//"->skip_phraselexbuf;raiseerr|Syntaxerr.Error_aserrwhen!Location.input_name="//toplevel//"->maybe_skip_phraselexbuf;raiseerr|Parsing.Parse_error|Syntaxerr.Escape_error->letloc=Location.currlexbufinif!Location.input_name="//toplevel//"thenmaybe_skip_phraselexbuf;raise(Syntaxerr.Error(Syntaxerr.Otherloc))(* We pass [--strategy simplified] to Menhir, which means that we wish to use
its "simplified" strategy for handling errors. When a syntax error occurs,
the current token is replaced with an [error] token. The parser then
continues shifting and reducing, as far as possible. After (possibly)
shifting the [error] token, though, the parser remains in error-handling
mode, and does not request the next token, so the current token remains
[error].
In OCaml's grammar, the [error] token always appears at the end of a
production, and this production always raises an exception. In such
a situation, the strategy described above means that:
- either the parser will not be able to shift [error],
and will raise [Parser.Error];
- or it will be able to shift [error] and will then reduce
a production whose semantic action raises an exception.
In either case, the parser will not attempt to read one token past
the syntax error. *)letimplementation=wrapParser.Incremental.implementationandinterface=wrapParser.Incremental.interfaceandtoplevel_phrase=wrapParser.Incremental.toplevel_phraseanduse_file=wrapParser.Incremental.use_fileandcore_type=wrapParser.Incremental.parse_core_typeandexpression=wrapParser.Incremental.parse_expressionandpattern=wrapParser.Incremental.parse_patternletmodule_type=wrapParser.Incremental.parse_module_typeletmodule_expr=wrapParser.Incremental.parse_module_exprletlongident=wrapParser.Incremental.parse_any_longidentletval_ident=wrapParser.Incremental.parse_val_longidentletconstr_ident=wrapParser.Incremental.parse_constr_longidentletextended_module_path=wrapParser.Incremental.parse_mod_ext_longidentletsimple_module_path=wrapParser.Incremental.parse_mod_longidentlettype_ident=wrapParser.Incremental.parse_mty_longident(* Error reporting for Syntaxerr *)(* The code has been moved here so that one can reuse Pprintast.tyvar *)letprepare_errorerr=letopenSyntaxerrinmatcherrwith|Unclosed(opening_loc,opening,closing_loc,closing)->Location.errorf~loc:closing_loc~sub:[Location.msg~loc:opening_loc"This '%s' might be unmatched"opening]"Syntax error: '%s' expected"closing|Expecting(loc,nonterm)->Location.errorf~loc"Syntax error: %s expected."nonterm|Not_expecting(loc,nonterm)->Location.errorf~loc"Syntax error: %s not expected."nonterm|Applicative_pathloc->Location.errorf~loc"Syntax error: applicative paths of the form F(X).t \
are not supported when the option -no-app-func is set."|Variable_in_scope(loc,var)->Location.errorf~loc"In this scoped type, variable %a \
is reserved for the local type %s."Pprintast.tyvarvarvar|Otherloc->Location.errorf~loc"Syntax error"|Ill_formed_ast(loc,s)->Location.errorf~loc"broken invariant in parsetree: %s"s|Invalid_package_type(loc,s)->Location.errorf~loc"invalid package type: %s"slet()=Location.register_error_of_exn(function|Syntaxerr.Errorerr->Some(prepare_errorerr)|_->None)