|
| 1 | +(**************************************************************************) |
| 2 | +(* *) |
| 3 | +(* OCaml *) |
| 4 | +(* *) |
| 5 | +(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *) |
| 6 | +(* *) |
| 7 | +(* Copyright 1996 Institut National de Recherche en Informatique et *) |
| 8 | +(* en Automatique. *) |
| 9 | +(* *) |
| 10 | +(* All rights reserved. This file is distributed under the terms of *) |
| 11 | +(* the GNU Lesser General Public License version 2.1, with the *) |
| 12 | +(* special exception on linking described in the file LICENSE. *) |
| 13 | +(* *) |
| 14 | +(**************************************************************************) |
| 15 | + |
| 16 | +(* Entry points in the parser *) |
| 17 | + |
| 18 | +(* Skip tokens to the end of the phrase *) |
| 19 | + |
| 20 | +let rec skip_phrase lexbuf = |
| 21 | + try |
| 22 | + match Lexer.token lexbuf with |
| 23 | + Parser.SEMISEMI | Parser.EOF -> () |
| 24 | + | _ -> skip_phrase lexbuf |
| 25 | + with |
| 26 | + | Lexer.Error (Lexer.Unterminated_comment _, _) |
| 27 | + | Lexer.Error (Lexer.Unterminated_string, _) |
| 28 | + | Lexer.Error (Lexer.Unterminated_string_in_comment _, _) |
| 29 | + | Lexer.Error (Lexer.Illegal_character _, _) -> skip_phrase lexbuf |
| 30 | +;; |
| 31 | + |
| 32 | +let maybe_skip_phrase lexbuf = |
| 33 | + if Parsing.is_current_lookahead Parser.SEMISEMI |
| 34 | + || Parsing.is_current_lookahead Parser.EOF |
| 35 | + then () |
| 36 | + else skip_phrase lexbuf |
| 37 | + |
| 38 | +let wrap parsing_fun lexbuf = |
| 39 | + try |
| 40 | + Docstrings.init (); |
| 41 | + Lexer.init (); |
| 42 | + let ast = parsing_fun Lexer.token lexbuf in |
| 43 | + Parsing.clear_parser(); |
| 44 | + Docstrings.warn_bad_docstrings (); |
| 45 | + ast |
| 46 | + with |
| 47 | + | Lexer.Error(Lexer.Illegal_character _, _) as err |
| 48 | + when !Location.input_name = "//toplevel//"-> |
| 49 | + skip_phrase lexbuf; |
| 50 | + raise err |
| 51 | + | Syntaxerr.Error _ as err |
| 52 | + when !Location.input_name = "//toplevel//" -> |
| 53 | + maybe_skip_phrase lexbuf; |
| 54 | + raise err |
| 55 | + | Parsing.Parse_error | Syntaxerr.Escape_error -> |
| 56 | + let loc = Location.curr lexbuf in |
| 57 | + if !Location.input_name = "//toplevel//" |
| 58 | + then maybe_skip_phrase lexbuf; |
| 59 | + raise(Syntaxerr.Error(Syntaxerr.Other loc)) |
| 60 | + |
| 61 | +let implementation = wrap Parser.implementation |
| 62 | +and interface = wrap Parser.interface |
| 63 | +and toplevel_phrase = wrap Parser.toplevel_phrase |
| 64 | +and use_file = wrap Parser.use_file |
| 65 | +and core_type = wrap Parser.parse_core_type |
| 66 | +and expression = wrap Parser.parse_expression |
| 67 | +and pattern = wrap Parser.parse_pattern |
0 commit comments