Match the compiler code to the new type of error message content

This commit is contained in:
Aminata-Dev 2023-06-26 16:30:08 +02:00 committed by Louis Gesbert
parent 50113586e3
commit 75b6251b43
8 changed files with 67 additions and 97 deletions

View File

@ -41,7 +41,4 @@ Related modules:
Related modules: Related modules:
{!modules: Catala_utils.File Catala_utils.Mark Catala_utils.Cli Catala_utils.String} {!modules: Catala_utils.File Catala_utils.Mark Catala_utils.Cli Catala_utils.String Catala_utils.Suggestions}
(*description à compléter*)
{!modules: Catala_utils.Myown}

View File

@ -197,16 +197,13 @@ let raise_spanned_error
?(suggestion : Content.message option) ?(suggestion : Content.message option)
(span : Pos.t) (span : Pos.t)
format = format =
Format.kdprintf let continuation (message : Format.formatter -> unit) =
(fun message -> raise
raise (CompilerError
(CompilerError ([MainMessage message; Position { pos_message = span_msg; pos = span }]
([ @ match suggestion with None -> [] | Some sug -> [Suggestion sug]))
MainMessage message; in
Position { pos_message = span_msg; pos = span }; Format.kdprintf continuation format
]
@ match suggestion with None -> [] | Some sug -> [Suggestion sug])))
format
let raise_multispanned_error_full let raise_multispanned_error_full
(spans : (Content.message option * Pos.t) list) (spans : (Content.message option * Pos.t) list)

View File

@ -15,7 +15,7 @@
License for the specific language governing permissions and limitations under License for the specific language governing permissions and limitations under
the License. *) the License. *)
(* Three-way minimum *) (** Three-way minimum *)
let minimum a b c = min a (min b c) let minimum a b c = min a (min b c)
(** Computes the levenshtein distance between two strings, used to provide error (** Computes the levenshtein distance between two strings, used to provide error
@ -50,37 +50,44 @@ let levenshtein_distance (s : string) (t : string) : int =
d.(m).(n) d.(m).(n)
(*On crée la liste des distances minimales, c'est à dire tous les couples (>=1) (*We're creating a string list composed by those who satisfy the following rule
qui partagent la distance minimale*) : they share the same levenshtein distance, which is the minimal distance
found between the reference word "keyword" and all the strings in
"string_list".*)
let suggestion_minimum_levenshtein_distance_association let suggestion_minimum_levenshtein_distance_association
(l : string list) (string_list : string list)
(mot : string) : string list = (keyword : string) : string list =
let rec insertion ((x, y) : int * string) (l : (int * string) list) : let rec insertion ((new_x, new_y) : int * 'a) (n_tuple_list : (int * 'a) list)
(int * string) list = : (int * 'a) list =
match l with match n_tuple_list with
| (current_x, current_y) :: t -> | (current_x, current_y) :: tail ->
if x <= current_x then (x, y) :: l if new_x <= current_x then (new_x, new_y) :: n_tuple_list
(*égalité car insertion du dernier au premier élément*) (*= to satisfy first-come first-served basis (because the last element
else (current_x, current_y) :: insertion (x, y) t is inserted first (see levenshtein_distance_association))*)
| [] -> l @ [x, y] else (current_x, current_y) :: insertion (new_x, new_y) tail
| [] -> [new_x, new_y]
in in
(*on associe à chaque string de l sa distance de levenshtein avec un mot (*Here we associate each elements of "string_list'" with its levenshtein
commun. La liste en sortie est triée (principe premier arrivé, premier distance with "keyword'"*)
inscrit)*) (*It returns a 2-tuple list with the following format (levenshein_distance,
(*sauf accumulateur*) word_from_string_list). 2-tuples are sorted on the first-come first-served
let rec levenshtein_distance_association (l' : string list) (mot' : string) : basis*)
(int * string) list = let rec levenshtein_distance_association
match l' with (string_list' : string list)
(keyword' : string) : (int * string) list =
match string_list' with
| h :: t -> | h :: t ->
insertion insertion
(levenshtein_distance h mot', h) (levenshtein_distance h keyword', h)
(levenshtein_distance_association t mot') (levenshtein_distance_association t keyword')
| [] -> [] | [] -> []
in in
let final_list = levenshtein_distance_association l mot in let final_list = levenshtein_distance_association string_list keyword in
match final_list with match final_list with
| h :: _ -> | h :: _ ->
(*on filtre les minimums et on récupère les strings*) (*We collect the strings from "string_list" with the minimum levenshtein
distance found (i.e. the distance of the first element of the sorted
list*)
List.map snd (List.filter (fun (x, _) -> x == fst h) final_list) List.map snd (List.filter (fun (x, _) -> x == fst h) final_list)
(*< impossible car déjà la liste est déjà triée*) (*< impossible because the list is already sorted in ascending order*)
| [] -> [] | [] -> []

View File

@ -130,15 +130,18 @@ let raise_error_cons_not_found
Suggestions.suggestion_minimum_levenshtein_distance_association constructors Suggestions.suggestion_minimum_levenshtein_distance_association constructors
(Mark.remove constructor) (Mark.remove constructor)
in in
Message.raise_spanned_error (Mark.get constructor) let print_string_list (ppf : Format.formatter) (string_list : string list) =
"The name of this constructor has not been defined before, maybe it is a \ Format.pp_print_list
typo?%a" ~pp_sep:(fun ppf () -> Format.pp_print_string ppf " or ")
(fun fmt closest_constructors -> (fun ppf str -> Format.fprintf ppf "\"@{<yellow>%s@}\"" str)
match closest_constructors with ppf string_list
| [] -> Format.fprintf fmt "" in
| hd :: _ -> Message.raise_spanned_error
Format.fprintf fmt " Maybe you wanted to say @{<yellow>\"%s\"@}?" hd) ~span_msg:(fun ppf -> Format.fprintf ppf "Here is your code :")
closest_constructors ~suggestion:(fun ppf -> print_string_list ppf closest_constructors)
(Mark.get constructor)
"The name of this constructor has not been defined before (it's probably a \
typographical error)."
let disambiguate_constructor let disambiguate_constructor
(ctxt : Name_resolution.context) (ctxt : Name_resolution.context)

View File

@ -21,43 +21,6 @@
open Sedlexing open Sedlexing
open Catala_utils open Catala_utils
(** {1 Internal functions} *)
(** Three-way minimum *)
let minimum a b c = min a (min b c)
(** Computes the levenshtein distance between two strings, used to provide error
messages suggestions *)
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)
(** After parsing, heading structure is completely flat because of the (** After parsing, heading structure is completely flat because of the
[source_file_item] rule. We need to tree-i-fy the flat structure, by looking [source_file_item] rule. We need to tree-i-fy the flat structure, by looking
at the precedence of the law headings. *) at the precedence of the law headings. *)
@ -174,8 +137,8 @@ module ParserAux (LocalisedLexer : Lexer_common.LocalisedLexer) = struct
String.sub y 0 (String.length wrong_token) String.sub y 0 (String.length wrong_token)
else y else y
in in
let levx = levenshtein_distance truncated_x wrong_token in let levx = Suggestions.levenshtein_distance truncated_x wrong_token in
let levy = levenshtein_distance truncated_y wrong_token in let levy = Suggestions.levenshtein_distance truncated_y wrong_token in
if levx = levy then String.length x - String.length y else levx - levy) if levx = levy then String.length x - String.length y else levx - levy)
acceptable_tokens acceptable_tokens
in in
@ -187,7 +150,7 @@ module ParserAux (LocalisedLexer : Lexer_common.LocalisedLexer) = struct
(fun ppf -> (fun ppf ->
Format.fprintf ppf "did you mean %a?" Format.fprintf ppf "did you mean %a?"
(Format.pp_print_list (Format.pp_print_list
~pp_sep:(fun ppf () -> Format.fprintf ppf ",@ or@ maybe@ ") ~pp_sep:(fun ppf () -> Format.fprintf ppf ",@ or@ maybe@ ")
(fun ppf (ts, _) -> pp_hint ppf ts)) (fun ppf (ts, _) -> pp_hint ppf ts))
tokens) tokens)
in in

View File

@ -31,7 +31,7 @@ $ catala Interpret -s Test1
[ERROR] [ERROR]
Syntax error at token "scope" Syntax error at token "scope"
Message: expected either 'condition', or 'content' followed by the expected variable type Message: expected either 'condition', or 'content' followed by the expected variable type
Autosuggestion: did you mean "content", or maybe "condition"? Autosuggestion: did you mean "content", or maybe "condition"?
Error token: Error token:
┌─⯈ examples/NSW_community_gaming/tests/test_nsw_social_housie.catala_en:11.21-11.26: ┌─⯈ examples/NSW_community_gaming/tests/test_nsw_social_housie.catala_en:11.21-11.26:
└──┐ └──┐
@ -74,7 +74,7 @@ $ catala Interpret -s Test2
[ERROR] [ERROR]
Syntax error at token "scope" Syntax error at token "scope"
Message: expected either 'condition', or 'content' followed by the expected variable type Message: expected either 'condition', or 'content' followed by the expected variable type
Autosuggestion: did you mean "content", or maybe "condition"? Autosuggestion: did you mean "content", or maybe "condition"?
Error token: Error token:
┌─⯈ examples/NSW_community_gaming/tests/test_nsw_social_housie.catala_en:11.21-11.26: ┌─⯈ examples/NSW_community_gaming/tests/test_nsw_social_housie.catala_en:11.21-11.26:
└──┐ └──┐
@ -117,7 +117,7 @@ $ catala Interpret -s Test3
[ERROR] [ERROR]
Syntax error at token "scope" Syntax error at token "scope"
Message: expected either 'condition', or 'content' followed by the expected variable type Message: expected either 'condition', or 'content' followed by the expected variable type
Autosuggestion: did you mean "content", or maybe "condition"? Autosuggestion: did you mean "content", or maybe "condition"?
Error token: Error token:
┌─⯈ examples/NSW_community_gaming/tests/test_nsw_social_housie.catala_en:11.21-11.26: ┌─⯈ examples/NSW_community_gaming/tests/test_nsw_social_housie.catala_en:11.21-11.26:
└──┐ └──┐
@ -162,7 +162,7 @@ $ catala Interpret -s Test4
[ERROR] [ERROR]
Syntax error at token "scope" Syntax error at token "scope"
Message: expected either 'condition', or 'content' followed by the expected variable type Message: expected either 'condition', or 'content' followed by the expected variable type
Autosuggestion: did you mean "content", or maybe "condition"? Autosuggestion: did you mean "content", or maybe "condition"?
Error token: Error token:
┌─⯈ examples/NSW_community_gaming/tests/test_nsw_social_housie.catala_en:11.21-11.26: ┌─⯈ examples/NSW_community_gaming/tests/test_nsw_social_housie.catala_en:11.21-11.26:
└──┐ └──┐

View File

@ -18,11 +18,13 @@ scope A:
```catala-test-inline ```catala-test-inline
$ catala Interpret -s A $ catala Interpret -s A
[ERROR] [ERROR]
The name of this constructor has not been defined before, maybe it is a typo? Maybe you wanted to say "Case1"? The name of this constructor has not been defined before (it's probably a typographical error).
Here is your code :
┌─⯈ tests/test_enum/bad/quick_pattern_fail.catala_en:15.38-15.43: ┌─⯈ tests/test_enum/bad/quick_pattern_fail.catala_en:15.38-15.43:
└──┐ └──┐
15 │ definition y equals x with pattern Case3 15 │ definition y equals x with pattern Case3
│ ‾‾‾‾‾ │ ‾‾‾‾‾
└─ Article └─ Article
Maybe you wanted to write "Case1" or "Case2"
#return code 123# #return code 123#
``` ```

View File

@ -4,7 +4,6 @@
declaration enumeration E: declaration enumeration E:
-- Case1 -- Case1
declaration scope A: declaration scope A:
context e content E context e content E
@ -15,11 +14,13 @@ scope A:
```catala-test-inline ```catala-test-inline
$ catala Typecheck $ catala Typecheck
[ERROR] [ERROR]
The name of this constructor has not been defined before, maybe it is a typo? Maybe you wanted to say "Case1"? The name of this constructor has not been defined before (it's probably a typographical error).
┌─⯈ tests/test_enum/bad/wrong_cons.catala_en:12.23-12.28: Here is your code :
┌─⯈ tests/test_enum/bad/wrong_cons.catala_en:11.23-11.28:
└──┐ └──┐
12 │ definition e equals Case2 11 │ definition e equals Case2
│ ‾‾‾‾‾ │ ‾‾‾‾‾
└─ Article └─ Article
Maybe you wanted to write "Case1"
#return code 123# #return code 123#
``` ```