With @aminata-dev, implemented first error message improvement!

This commit is contained in:
Denis Merigoux 2023-06-19 16:18:30 +02:00 committed by Louis Gesbert
parent 67f69561c2
commit 4cdd2fce0f
4 changed files with 36 additions and 38 deletions

View File

@ -173,7 +173,7 @@ let emit_content (content : Content.t) (target : content_type) : unit =
Format.pp_print_cut ppf ();
Format.pp_print_cut ppf ();
Option.iter
(fun msg -> Format.fprintf ppf "%t@, compréhension ??" msg)
(fun msg -> Format.fprintf ppf "%t@," msg)
pos.pos_message;
Pos.format_loc_text ppf pos.pos)
ppf l)

View File

@ -120,52 +120,25 @@ let translate_unop (op : Surface.Ast.unop) pos : Ast.expr boxed =
"This operator doesn't exist, dates can't be negative"
| S.KDuration -> TLit TDuration)
(* Three-way minimum *)
let minimum a b c = min a (min b c)
let levenshtein_distance (s : string) (t : string) : int =
let m = String.length s and n = String.length t in
(* for all i and j, d.(i).(j) will hold the Levenshtein distance between the
first i characters of s and the first j characters of t *)
let d = Array.make_matrix (m + 1) (n + 1) 0 in
for i = 0 to m do
d.(i).(0) <- i
(* the distance of any first string to an empty second string *)
done;
for j = 0 to n do
d.(0).(j) <- j
(* the distance of any second string to an empty first string *)
done;
for j = 1 to n do
for i = 1 to m do
if s.[i - 1] = t.[j - 1] then d.(i).(j) <- d.(i - 1).(j - 1)
(* no operation required *)
else
d.(i).(j) <-
minimum
(d.(i - 1).(j) + 1) (* a deletion *)
(d.(i).(j - 1) + 1) (* an insertion *)
(d.(i - 1).(j - 1) + 1) (* a substitution *)
done
done;
d.(m).(n)
let raise_error_cons_not_found
(ctxt : Name_resolution.context)
(constructor : string Mark.pos) =
let constructors =
List.map (fun (s, _) -> s) (Ident.Map.bindings ctxt.constructor_idmap)
in
let _closest_constructor =
Myown.suggestion_minimum_levenshtein_distance_association constructors
let closest_constructors =
Suggestions.suggestion_minimum_levenshtein_distance_association constructors
(Mark.remove constructor)
in
Message.raise_spanned_error (Mark.get constructor)
"The name of this constructor has not been defined before, maybe it is a \
typo? Maybe you wanted to use : %s"
"blah" (* closest_constructor *)
typo?%a"
(fun fmt closest_constructors ->
match closest_constructors with
| [] -> Format.fprintf fmt ""
| hd :: _ ->
Format.fprintf fmt " Maybe you wanted to say @{<yellow>\"%s\"@}?" hd)
closest_constructors
let disambiguate_constructor
(ctxt : Name_resolution.context)

View File

@ -17,7 +17,7 @@ scope A:
```catala-test-inline
$ catala Interpret -s A
[ERROR] The name of this constructor has not been defined before, maybe it is a typo?
[ERROR] The name of this constructor has not been defined before, maybe it is a typo? Maybe you wanted to say "Case1"?
┌─⯈ tests/test_enum/bad/quick_pattern_fail.catala_en:15.38-15.43:
└──┐

View File

@ -0,0 +1,25 @@
## Article
```catala
declaration enumeration E:
-- Case1
declaration scope A:
context e content E
scope A:
definition e equals Case2
```
```catala-test-inline
$ catala Typecheck
[ERROR] The name of this constructor has not been defined before, maybe it is a typo? Maybe you wanted to say "Case1"?
┌─⯈ tests/test_enum/bad/wrong_cons.catala_en:12.23-12.28:
└──┐
12 │ definition e equals Case2
│ ‾‾‾‾‾
└─ Article
#return code 255#
```