diff --git a/compiler/catala_utils/suggestions.ml b/compiler/catala_utils/suggestions.ml index ae3b9e0b..a042a04e 100644 --- a/compiler/catala_utils/suggestions.ml +++ b/compiler/catala_utils/suggestions.ml @@ -50,20 +50,20 @@ let levenshtein_distance (s : string) (t : string) : int = (*We create a list composed by strings that satisfy the following rule : they have the same levenshtein distance, which is the minimum distance between the - reference word "keyword" and all the strings in "string_list" (with the + reference word "keyword" and all the strings in "candidates" (with the condition that this minimum is equal to or less than one third of the length of keyword + 1, in order to get suggestions close to "keyword")*) let suggestion_minimum_levenshtein_distance_association - (string_list : string list) + (candidates : string list) (keyword : string) : string list option = let rec strings_minimum_levenshtein_distance (minimum : int) (result : string list) - (string_list' : string list) : string list = - (*As we iterate through the "string_list'" list, we create a list "result" + (candidates' : string list) : string list = + (*As we iterate through the "candidates'" list, we create a list "result" with all strings that have the last minimum levenshtein distance found ("minimum").*) - match string_list' with + match candidates' with (*When a new minimum levenshtein distance is found, the new result list is our new element "current_string" followed by strings that have the same minimum distance. It will be the "result" list if there is no levenshtein @@ -85,7 +85,7 @@ let suggestion_minimum_levenshtein_distance_association (*If a levenshtein distance greater than the minimum is found, "result" doesn't change*) else strings_minimum_levenshtein_distance minimum result tail - (*The "result" list is returned at the end of the "string_list'" list.*) + (*The "result" list is returned at the end of the "candidates'" list.*) | [] -> result in let suggestions = @@ -93,7 +93,7 @@ let suggestion_minimum_levenshtein_distance_association (1 + (String.length keyword / 3)) (*In order to select suggestions that are not too far away from the keyword*) - [] string_list + [] candidates in match suggestions with [] -> None | _ :: _ -> Some suggestions @@ -103,6 +103,6 @@ let display (suggestions_list : string list) (ppf : Format.formatter) = | _ :: _ -> Format.pp_print_string ppf "Maybe you wanted to write : "; Format.pp_print_list - ~pp_sep:(fun ppf () -> Format.fprintf ppf ",@,or ") + ~pp_sep:(fun ppf () -> Format.fprintf ppf ",@ or ") (fun ppf string -> Format.fprintf ppf "@{\"%s\"@}" string) ppf suggestions_list diff --git a/compiler/surface/parser.messages b/compiler/surface/parser.messages index ab63ce22..f5a12ad9 100644 --- a/compiler/surface/parser.messages +++ b/compiler/surface/parser.messages @@ -590,7 +590,7 @@ source_file: BEGIN_CODE SCOPE UIDENT COLON RULE LIDENT YEAR ## LIDENT ## -expected a condition or a consequence for this rule, or the rest of the variable qualified name +expected 'under condition' followed by a condition, 'equals' followed by the definition body, or the rest of the variable qualified name source_file: BEGIN_CODE SCOPE UIDENT COLON RULE YEAR ## diff --git a/compiler/surface/parser_driver.ml b/compiler/surface/parser_driver.ml index 15c65856..737cfd0a 100644 --- a/compiler/surface/parser_driver.ml +++ b/compiler/surface/parser_driver.ml @@ -131,12 +131,19 @@ module ParserAux (LocalisedLexer : Lexer_common.LocalisedLexer) = struct in (* The parser has suspended itself because of a syntax error. Stop. *) let custom_menhir_message ppf = - match Parser_errors.message (state env) with + (match Parser_errors.message (state env) with | exception Not_found -> - Format.fprintf ppf "Message: @{unexpected token@}" + Format.fprintf ppf "Message: @{unexpected token@}@,%t" | msg -> - Format.fprintf ppf "Message: @{%s@}" - (String.trim (String.uncapitalize_ascii msg)) + Format.fprintf ppf "Message: @{%s@}@,%t" + (String.trim (String.uncapitalize_ascii msg))) + (fun (ppf : Format.formatter) -> + Format.fprintf ppf "You can only write : "; + Format.pp_print_list + ~pp_sep:(fun ppf () -> Format.fprintf ppf ",@ or ") + (fun ppf string -> Format.fprintf ppf "@{\"%s\"@}" string) + ppf + (List.map (fun (s, _) -> s) acceptable_tokens)) in raise_parser_error ?suggestion:similar_acceptable_tokens (Pos.from_lpos (lexing_positions lexbuf)) diff --git a/examples/NSW_community_gaming/tests/test_nsw_social_housie.catala_en b/examples/NSW_community_gaming/tests/test_nsw_social_housie.catala_en index 19636f16..cbc72deb 100644 --- a/examples/NSW_community_gaming/tests/test_nsw_social_housie.catala_en +++ b/examples/NSW_community_gaming/tests/test_nsw_social_housie.catala_en @@ -31,6 +31,8 @@ $ catala Interpret -s Test1 [ERROR] Syntax error at token "scope" Message: expected either 'condition', or 'content' followed by the expected variable type +You can only write : "condition", +or "content" Error token: ┌─⯈ examples/NSW_community_gaming/tests/test_nsw_social_housie.catala_en:11.21-11.26: @@ -73,6 +75,8 @@ $ catala Interpret -s Test2 [ERROR] Syntax error at token "scope" Message: expected either 'condition', or 'content' followed by the expected variable type +You can only write : "condition", +or "content" Error token: ┌─⯈ examples/NSW_community_gaming/tests/test_nsw_social_housie.catala_en:11.21-11.26: @@ -115,6 +119,8 @@ $ catala Interpret -s Test3 [ERROR] Syntax error at token "scope" Message: expected either 'condition', or 'content' followed by the expected variable type +You can only write : "condition", +or "content" Error token: ┌─⯈ examples/NSW_community_gaming/tests/test_nsw_social_housie.catala_en:11.21-11.26: @@ -159,6 +165,8 @@ $ catala Interpret -s Test4 [ERROR] Syntax error at token "scope" Message: expected either 'condition', or 'content' followed by the expected variable type +You can only write : "condition", +or "content" Error token: ┌─⯈ examples/NSW_community_gaming/tests/test_nsw_social_housie.catala_en:11.21-11.26: