mirror of
https://github.com/CatalaLang/catala.git
synced 2024-11-08 07:51:43 +03:00
Better help for the user
- Clearer (more detailed) error messages (see parser.messages, parser_driver.ml ->) --> Parser_driver.ml : we're more precise and show all possible instructions to the user. - Suggestions.ml : unimportant display changes + renaming
This commit is contained in:
parent
6123ebd004
commit
af9c708960
@ -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 "@{<yellow>\"%s\"@}" string)
|
||||
ppf suggestions_list
|
||||
|
@ -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
|
||||
##
|
||||
|
@ -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: @{<yellow>unexpected token@}"
|
||||
Format.fprintf ppf "Message: @{<yellow>unexpected token@}@,%t"
|
||||
| msg ->
|
||||
Format.fprintf ppf "Message: @{<yellow>%s@}"
|
||||
(String.trim (String.uncapitalize_ascii msg))
|
||||
Format.fprintf ppf "Message: @{<yellow>%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 "@{<yellow>\"%s\"@}" string)
|
||||
ppf
|
||||
(List.map (fun (s, _) -> s) acceptable_tokens))
|
||||
in
|
||||
raise_parser_error ?suggestion:similar_acceptable_tokens
|
||||
(Pos.from_lpos (lexing_positions lexbuf))
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user