|
| 1 | +open Rule.Let |
| 2 | + |
| 3 | +let rec match_longest (left_key, left_rule) rules = |
| 4 | + match rules with |
| 5 | + | [] -> |
| 6 | + Rule.Match.bind left_rule (fun value -> return_match (left_key, value)) |
| 7 | + | new_left :: rest -> |
| 8 | + Rule.Match.bind_longest (left_rule, match_longest new_left rest) |
| 9 | + (function |
| 10 | + | `Left value -> return_match (left_key, value) |
| 11 | + | `Right value -> return_match value) |
| 12 | + |
| 13 | +let static rules = |
| 14 | + let rec match_everything values = function |
| 15 | + | [] -> return_match (List.rev values) |
| 16 | + | left :: rest -> |
| 17 | + Rule.Match.bind left (fun value -> |
| 18 | + match_everything (value :: values) rest) |
| 19 | + in |
| 20 | + match_everything [] rules |
| 21 | + |
| 22 | +let extract_expected_value error_msg = |
| 23 | + if not (String.contains error_msg 'E') then None |
| 24 | + else |
| 25 | + try |
| 26 | + let start = String.index error_msg '\'' + 1 in |
| 27 | + let end_idx = String.index_from error_msg start '\'' in |
| 28 | + Some (String.sub error_msg start (end_idx - start)) |
| 29 | + with _ -> None |
| 30 | + |
| 31 | +let extract_got_value error_msg = |
| 32 | + if not (String.contains error_msg 'i') then "the provided value" |
| 33 | + else |
| 34 | + try |
| 35 | + let start = String.rindex error_msg '\'' in |
| 36 | + let before_quote = String.sub error_msg 0 start in |
| 37 | + let second_last = String.rindex before_quote '\'' + 1 in |
| 38 | + String.sub error_msg second_last (start - second_last) |
| 39 | + with _ -> "the provided value" |
| 40 | + |
| 41 | +let format_expected_values = function |
| 42 | + | [] -> "" |
| 43 | + | [ single ] -> "'" ^ single ^ "'" |
| 44 | + | values -> |
| 45 | + let rec format_list = function |
| 46 | + | [] -> "" |
| 47 | + | [ x ] -> "or '" ^ x ^ "'" |
| 48 | + | x :: xs -> "'" ^ x ^ "', " ^ format_list xs |
| 49 | + in |
| 50 | + format_list values |
| 51 | + |
| 52 | +let create_error_message got expected_values = |
| 53 | + match expected_values with |
| 54 | + | [] -> [ "Got '" ^ got ^ "'" ] |
| 55 | + | values -> ( |
| 56 | + match Levenshtein.find_closest_match got values with |
| 57 | + | Some suggestion -> |
| 58 | + [ "Got '" ^ got ^ "', did you mean '" ^ suggestion ^ "'?" ] |
| 59 | + | None -> |
| 60 | + let expected_str = format_expected_values values in |
| 61 | + [ "Got '" ^ got ^ "', expected " ^ expected_str ^ "." ]) |
| 62 | + |
| 63 | +let process_error_messages = function |
| 64 | + | [] -> [ "No alternatives matched" ] |
| 65 | + | errors -> |
| 66 | + let expected_values = |
| 67 | + errors |
| 68 | + |> List.filter_map (function |
| 69 | + | msg :: _ -> extract_expected_value msg |
| 70 | + | _ -> None) |
| 71 | + |> List.filter (fun value -> value <> "$") |
| 72 | + |> List.sort_uniq String.compare |
| 73 | + in |
| 74 | + (match expected_values with |
| 75 | + | [] -> List.hd errors |
| 76 | + | values -> |
| 77 | + let got = |
| 78 | + match List.hd errors with |
| 79 | + | msg :: _ -> extract_got_value msg |
| 80 | + | _ -> "the provided value" |
| 81 | + in |
| 82 | + create_error_message got values) |
| 83 | + |
| 84 | +let xor rules = |
| 85 | + match rules with |
| 86 | + | [] -> failwith "xor doesn't make sense without a single value" |
| 87 | + | all_rules -> |
| 88 | + let try_rules_with_best_match = function |
| 89 | + | [] -> failwith "xor doesn't make sense without a single value" |
| 90 | + | left :: rest -> |
| 91 | + let rules_with_unit = List.map (fun rule -> ((), rule)) rest in |
| 92 | + Rule.Match.map |
| 93 | + (match_longest ((), left) rules_with_unit) |
| 94 | + (fun ((), value) -> value) |
| 95 | + in |
| 96 | + let try_all_and_collect_errors rules tokens = |
| 97 | + let rec collect_errors remaining_rules acc_errors = |
| 98 | + match remaining_rules with |
| 99 | + | [] -> |
| 100 | + let combined_error = process_error_messages acc_errors in |
| 101 | + Rule.Data.return (Error combined_error) tokens |
| 102 | + | rule :: rest -> ( |
| 103 | + let data, remaining = rule tokens in |
| 104 | + match data with |
| 105 | + | Ok value -> Rule.Data.return (Ok value) remaining |
| 106 | + | Error err -> collect_errors rest (acc_errors @ [ err ])) |
| 107 | + in |
| 108 | + collect_errors rules [] |
| 109 | + in |
| 110 | + fun tokens -> |
| 111 | + let successful_rules = |
| 112 | + all_rules |
| 113 | + |> List.filter_map (fun rule -> |
| 114 | + let data, remaining = rule tokens in |
| 115 | + match data with |
| 116 | + | Ok _ -> Some (rule, remaining) |
| 117 | + | Error _ -> None) |
| 118 | + in |
| 119 | + match successful_rules with |
| 120 | + | [] -> try_all_and_collect_errors all_rules tokens |
| 121 | + | _ -> try_rules_with_best_match all_rules tokens |
| 122 | + |
| 123 | +let and_ rules = |
| 124 | + let rec match_everything values indexed_rules = |
| 125 | + match indexed_rules with |
| 126 | + | [] -> return_match (List.rev values) |
| 127 | + | left :: new_rules -> |
| 128 | + Rule.Match.bind (match_longest left new_rules) (fun (key, value) -> |
| 129 | + let remaining = List.remove_assoc key indexed_rules in |
| 130 | + match_everything ((key, value) :: values) remaining) |
| 131 | + in |
| 132 | + let indexed_rules = List.mapi (fun i rule -> (i, rule)) rules in |
| 133 | + Rule.Match.map (match_everything [] indexed_rules) (fun values -> |
| 134 | + values |
| 135 | + |> List.sort (fun (a, _) (b, _) -> Int.compare a b) |
| 136 | + |> List.map (fun (_, v) -> v)) |
| 137 | + |
| 138 | +let or_ rules = |
| 139 | + rules |> List.map Modifier.optional |> and_ |> Modifier.at_least_one |
0 commit comments