1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
let last_token = ref Parser.EOF
let token lexbuf =
let token = Lexer.token lexbuf in
last_token := token;
token
let rec skip_phrase lexbuf =
match token lexbuf with
| Parser.SEMISEMI | Parser.EOF -> ()
| _ -> skip_phrase lexbuf
| 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_phrase lexbuf
let maybe_skip_phrase lexbuf =
match !last_token with
| Parser.SEMISEMI | Parser.EOF -> ()
| _ -> skip_phrase lexbuf
type 'a parser = Lexing.position -> 'a Parser.MenhirInterpreter.checkpoint
let wrap (parser : 'a parser) lexbuf : 'a =
try
Docstrings.init ();
Lexer.init ();
let open Parser.MenhirInterpreter in
let rec fix_resume = function
| InputNeeded _ | Accepted _ | Rejected | HandlingError _ as cp -> cp
| Shifting (_, _, _) | AboutToReduce (_, _) as cp ->
fix_resume (resume ~strategy:`Simplified cp)
in
let rec offer_input lexbuf cp tok =
let ptok = Lexing.(tok, lexbuf.lex_start_p, lexbuf.lex_curr_p) in
match fix_resume (offer cp ptok) with
| InputNeeded _ as cp ->
offer_input lexbuf cp (token lexbuf)
| Accepted x -> Some x
| Rejected -> None
| Shifting (_, _, _) | AboutToReduce (_, _) ->
assert false
| HandlingError _ as cp' ->
match Lexer.try_disambiguate lexbuf tok with
| Some tok' -> offer_input lexbuf cp tok'
| None -> main_loop lexbuf cp'
and main_loop lexbuf = function
| InputNeeded _ as cp ->
offer_input lexbuf cp (token lexbuf)
| Accepted x -> Some x
| Rejected -> None
| Shifting (_, _, _) | AboutToReduce (_, _) | HandlingError _ as cp ->
main_loop lexbuf (resume ~strategy:`Simplified cp)
in
let ast =
match main_loop lexbuf (parser lexbuf.Lexing.lex_curr_p) with
| Some ast -> ast
| None -> raise Parsing.Parse_error
in
Parsing.clear_parser();
Docstrings.warn_bad_docstrings ();
last_token := Parser.EOF;
ast
with
| Lexer.Error(Lexer.Illegal_character _, _) as err
when !Location.input_name = "//toplevel//"->
skip_phrase lexbuf;
raise err
| Syntaxerr.Error _ as err
when !Location.input_name = "//toplevel//" ->
maybe_skip_phrase lexbuf;
raise err
| Parsing.Parse_error | Syntaxerr.Escape_error ->
let loc = Location.curr lexbuf in
if !Location.input_name = "//toplevel//"
then maybe_skip_phrase lexbuf;
raise(Syntaxerr.Error(Syntaxerr.Other loc))
let implementation = wrap Parser.Incremental.implementation
and interface = wrap Parser.Incremental.interface
and toplevel_phrase = wrap Parser.Incremental.toplevel_phrase
and use_file = wrap Parser.Incremental.use_file
and core_type = wrap Parser.Incremental.parse_core_type
and expression = wrap Parser.Incremental.parse_expression
and pattern = wrap Parser.Incremental.parse_pattern
let module_type = wrap Parser.Incremental.parse_module_type
let module_expr = wrap Parser.Incremental.parse_module_expr
let longident = wrap Parser.Incremental.parse_any_longident
let val_ident = wrap Parser.Incremental.parse_val_longident
let constr_ident= wrap Parser.Incremental.parse_constr_longident
let extended_module_path = wrap Parser.Incremental.parse_mod_ext_longident
let simple_module_path = wrap Parser.Incremental.parse_mod_longident
let type_ident = wrap Parser.Incremental.parse_mty_longident
let prepare_error err =
let open Syntaxerr in
match err with
| 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_path loc ->
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.tyvar var var
| Other loc ->
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" s
| Removed_string_set loc ->
Location.errorf ~loc
"Syntax error: strings are immutable, there is no assignment \
syntax for them.\n\
@{<hint>Hint@}: Mutable sequences of bytes are available in \
the Bytes module.\n\
@{<hint>Hint@}: Did you mean to use 'Bytes.set'?"
let () =
Location.register_error_of_exn
(function
| Syntaxerr.Error err -> Some (prepare_error err)
| _ -> None
)