Skip to content

Commit 0f815a9

Browse files
authored
improve some error messages (#776)
* improve error message when require/open unqualified identifier * add KO test for require without root_path prefix * improve error message when a pattern is applied to an explicit term * improve error message in case of invalid character
1 parent e69fd43 commit 0f815a9

File tree

6 files changed

+25
-8
lines changed

6 files changed

+25
-8
lines changed

src/common/error.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
open Lplib
44
open Base
55

6+
(** Exception raised by the parser when an unqualified identifier is given
7+
instead of a qualified one. *)
8+
exception NotQualified
9+
610
(** [err_fmt] warning/error output formatter. *)
711
let err_fmt = Stdlib.ref Format.err_formatter
812

src/parsing/lpLexer.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ let fail : Sedlexing.lexbuf -> string -> 'a = fun lb msg ->
2121
raise (SyntaxError (make_pos (lexing_positions lb) msg))
2222

2323
let invalid_character : Sedlexing.lexbuf -> 'a = fun lb ->
24-
fail lb ("Invalid character: \"" ^ Utf8.lexeme lb ^ "\".")
24+
fail lb "Invalid character"
2525

2626
(** Tokens. *)
2727
type token =

src/parsing/lpParser.mly

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ qid:
141141
| s=UID { make_pos $sloc ([], s) }
142142
| p=QID { qid_of_path $sloc p }
143143

144-
path: p=QID { make_pos $sloc (List.rev p) }
144+
path:
145+
| UID { raise Error.NotQualified }
146+
| p=QID { make_pos $sloc (List.rev p) }
145147

146148
qid_expl:
147149
| s=UID_EXPL { make_pos $sloc ([], s) }
@@ -279,8 +281,8 @@ command:
279281
{ make_pos $sloc (P_require(true,l)) }
280282
| REQUIRE l=list(path) SEMICOLON
281283
{ make_pos $sloc (P_require(false,l)) }
282-
| REQUIRE i=path AS a=uid SEMICOLON
283-
{ make_pos $sloc (P_require_as(i,a)) }
284+
| REQUIRE p=path AS i=uid SEMICOLON
285+
{ make_pos $sloc (P_require_as(p,i)) }
284286
| OPEN l=list(path) SEMICOLON
285287
{ make_pos $sloc (P_open l) }
286288
| ms=modifier* SYMBOL s=uid al=param_list* COLON a=term

src/parsing/parser.ml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,14 @@ module Lp : PARSER = struct
6060
| End_of_file -> Option.iter close_in inchan; None
6161
| LpLexer.SyntaxError {pos=None; _} -> assert false
6262
| LpLexer.SyntaxError {pos=Some pos; elt} -> parser_fatal pos "%s" elt
63+
| Error.NotQualified ->
64+
let pos = Pos.locate (Sedlexing.lexing_positions lb) in
65+
parser_fatal pos
66+
"Unqualified identifier (the root_path is missing)."
6367
| LpParser.Error ->
6468
let pos = Pos.locate (Sedlexing.lexing_positions lb) in
65-
parser_fatal pos "Unexpected token: %s" (Sedlexing.Utf8.lexeme lb)
69+
parser_fatal pos "Unexpected token: \"%s\"."
70+
(Sedlexing.Utf8.lexeme lb)
6671
in
6772
Stream.from generator
6873

@@ -105,7 +110,7 @@ module Dk : PARSER = struct
105110
| End_of_file -> Option.iter close_in inchan; None
106111
| DkParser.Error ->
107112
let pos = Pos.locate (Lexing.(lb.lex_start_p, lb.lex_curr_p)) in
108-
parser_fatal pos "Unexpected token [%s]." (Lexing.lexeme lb)
113+
parser_fatal pos "Unexpected token \"%s\"." (Lexing.lexeme lb)
109114
in
110115
Stream.from generator
111116

src/parsing/scope.ml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,12 @@ and scope_parsed : int -> mode -> sig_state -> env -> p_term -> tbox =
200200
begin
201201
match p_head.elt, md with
202202
| P_Patt _, M_LHS _ when args <> [] ->
203-
fatal t.pos "Pattern variables cannot be applied."
203+
begin match args with
204+
| [{elt=P_Expl _;_}] ->
205+
fatal t.pos "Explicit terms are forbidden in rule LHS. \
206+
You perhaps forgot to write a dot before?"
207+
| _ -> fatal t.pos "Pattern variables cannot be applied."
208+
end
204209
| _ -> ()
205210
end;
206211
(* Scope the head and obtain the implicitness of arguments. *)
@@ -305,7 +310,7 @@ and scope_binder : ?warn:bool -> int -> mode -> sig_state ->
305310
cons a (Bindlib.bind_var v t)
306311
| Some {elt=id;pos}::idopts ->
307312
if is_invalid_bindlib_id id then
308-
fatal pos "%s: Escaped identifiers or regular identifiers \
313+
fatal pos "\"%s\": Escaped identifiers or regular identifiers \
309314
having an integer suffix with leading zeros \
310315
are not allowed for bound variable names." id;
311316
let v = new_tvar id in

tests/KO/req_without_root_path.lp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require bool;

0 commit comments

Comments
 (0)