Skip to content

Commit 9152995

Browse files
committed
Revu les erreurs de syntaxe sur les parentheses pas fermees, etc
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@1760 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
1 parent 91f7a19 commit 9152995

File tree

7 files changed

+31
-19
lines changed

7 files changed

+31
-19
lines changed

boot/ocamllex

12.9 KB
Binary file not shown.

parsing/location.ml

+5-5
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ let setup_terminal_info() =
6060

6161
let num_loc_lines = ref 0 (* number of lines already printed after input *)
6262

63-
let rec highlight_location loc =
63+
let rec highlight_locations loc1 loc2 =
6464
match !status with
6565
Unknown ->
66-
setup_terminal_info(); highlight_location loc
66+
setup_terminal_info(); highlight_locations loc1 loc2
6767
| Bad_term ->
6868
false
6969
| Good_term ->
@@ -90,9 +90,9 @@ let rec highlight_location loc =
9090
print_string "# ";
9191
for pos = 0 to String.length lb.lex_buffer - pos0 - 1 do
9292
if !bol then (print_string " "; bol := false);
93-
if pos = loc.loc_start then
93+
if pos = loc1.loc_start || pos = loc2.loc_start then
9494
Terminfo.puts stdout !start_standout 1;
95-
if pos = loc.loc_end then
95+
if pos = loc1.loc_end || pos = loc2.loc_end then
9696
Terminfo.puts stdout !end_standout 1;
9797
let c = lb.lex_buffer.[pos + pos0] in
9898
print_char c;
@@ -122,7 +122,7 @@ let (msg_file, msg_line, msg_chars, msg_to, msg_colon, warn_head) =
122122

123123
let print loc =
124124
if String.length !input_name = 0 then
125-
if highlight_location loc then () else begin
125+
if highlight_locations loc none then () else begin
126126
print_string "Characters ";
127127
print_int loc.loc_start; print_string "-";
128128
print_int loc.loc_end; print_string ":";

parsing/location.mli

+2
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ val print: t -> unit
2727
val print_warning: t -> string -> unit
2828
val echo_eof: unit -> unit
2929
val reset: unit -> unit
30+
31+
val highlight_locations: t -> t -> bool

parsing/parse.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ let wrap parsing_fun lexbuf =
4747
| Syntaxerr.Error _ as err ->
4848
if !Location.input_name = "" then maybe_skip_phrase lexbuf;
4949
raise err
50-
| Parsing.Parse_error ->
50+
| Parsing.Parse_error | Syntaxerr.Escape_error ->
5151
let loc = { loc_start = Lexing.lexeme_start lexbuf;
5252
loc_end = Lexing.lexeme_end lexbuf } in
5353
if !Location.input_name = ""

parsing/parser.mly

+9-2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ let rec mkrangepat c1 c2 =
105105
mkpat(Ppat_or(mkpat(Ppat_constant(Const_char c1)),
106106
mkrangepat (Char.chr(Char.code c1 + 1)) c2))
107107

108+
let syntax_error () =
109+
raise Syntaxerr.Escape_error
110+
108111
let unclosed opening_name opening_num closing_name closing_num =
109112
raise(Syntaxerr.Error(Syntaxerr.Unclosed(rhs_loc opening_num, opening_name,
110113
rhs_loc closing_num, closing_name)))
@@ -419,6 +422,8 @@ expr:
419422
{ mkexp(Pexp_apply($1, List.rev $2)) }
420423
| LET rec_flag let_bindings IN seq_expr %prec prec_let
421424
{ mkexp(Pexp_let($2, List.rev $3, $5)) }
425+
| LET rec_flag let_bindings IN error %prec prec_let
426+
{ syntax_error() }
422427
| LET rec_flag let_bindings error %prec prec_let
423428
{ unclosed "let" 1 "in" 4 }
424429
| PARSER opt_pat opt_bar parser_cases %prec prec_fun
@@ -433,6 +438,8 @@ expr:
433438
{ mkexp(Pexp_apply(Pstream.cparser ($5, List.rev $7), [$2])) }
434439
| TRY seq_expr WITH opt_bar match_cases %prec prec_try
435440
{ mkexp(Pexp_try($2, List.rev $5)) }
441+
| TRY seq_expr WITH error %prec prec_try
442+
{ syntax_error() }
436443
| TRY seq_expr error %prec prec_try
437444
{ unclosed "try" 1 "with" 3 }
438445
| expr_comma_list
@@ -517,8 +524,6 @@ simple_expr:
517524
{ unclosed "begin" 1 "end" 3 }
518525
| LPAREN seq_expr type_constraint RPAREN
519526
{ let (t, t') = $3 in mkexp(Pexp_constraint($2, t, t')) }
520-
| LPAREN seq_expr type_constraint error
521-
{ unclosed "(" 1 ")" 4 }
522527
| simple_expr DOT label_longident
523528
{ mkexp(Pexp_field($1, $3)) }
524529
| simple_expr DOT LPAREN seq_expr RPAREN
@@ -670,6 +675,8 @@ type_constraint:
670675
COLON core_type { (Some $2, None) }
671676
| COLON core_type COLONGREATER core_type { (Some $2, Some $4) }
672677
| COLONGREATER core_type { (None, Some $2) }
678+
| COLON error { syntax_error() }
679+
| COLONGREATER error { syntax_error() }
673680
;
674681

675682
/* Patterns */

parsing/syntaxerr.ml

+13-11
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,27 @@ type error =
2020
| Other of Location.t
2121

2222
exception Error of error
23+
exception Escape_error
2324

2425
let report_error = function
2526
Unclosed(opening_loc, opening, closing_loc, closing) ->
26-
if String.length !Location.input_name > 0 then begin
27-
Location.print closing_loc;
28-
print_string "Syntax error: missing '";
27+
if String.length !Location.input_name = 0
28+
&& Location.highlight_locations opening_loc closing_loc
29+
then begin
30+
print_string "Syntax error: '";
2931
print_string closing;
30-
print_string "'"; force_newline();
31-
Location.print opening_loc;
32-
print_string "This is the location of the unmatched '";
32+
print_string "' expected, the highlighted '";
3333
print_string opening;
34-
print_string "'"
34+
print_string "' might be unmatched"
3535
end else begin
36+
Location.print closing_loc;
37+
print_string "Syntax error: '";
38+
print_string closing;
39+
print_string "' expected"; force_newline();
3640
Location.print opening_loc;
37-
print_string "Syntax error: this '";
41+
print_string "This '";
3842
print_string opening;
39-
print_string "' has no matching '";
40-
print_string closing;
41-
print_string "'"
43+
print_string "' might be unmatched"
4244
end
4345
| Other loc ->
4446
Location.print loc;

parsing/syntaxerr.mli

+1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ type error =
1818
| Other of Location.t
1919

2020
exception Error of error
21+
exception Escape_error
2122

2223
val report_error: error -> unit

0 commit comments

Comments
 (0)