| 1 |
From Coq Require Import Derive. |
| 2 |
From Coq Require Import List. |
| 3 |
From Coq Require Import String. |
| 4 |
Require Import Crypto.Util.Strings.String. |
| 5 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 6 |
Require Import Crypto.Util.Strings.Show. |
| 7 |
Require Import Crypto.Util.Strings.Show.Enum. |
| 8 |
Require Import Crypto.Util.Strings.Parse.Common. |
| 9 |
Require Import Crypto.Util.Listable. |
| 10 |
Import ListNotations. |
| 11 |
Local Open Scope bool_scope. |
| 12 |
Local Open Scope list_scope. |
| 13 |
Local Open Scope string_scope. |
| 14 |
|
| 15 |
Local Set Boolean Equality Schemes. |
| 16 |
Local Set Decidable Equality Schemes. |
| 17 |
Inductive name_case_kind := lower | upper. |
| 18 |
Record word_case_data := { first_letter_case : name_case_kind ; rest_letters_case : name_case_kind }. |
| 19 |
Record capitalization_data := { separator : string ; first_word_case : word_case_data ; rest_words_case : word_case_data }. |
| 20 |
Record capitalization_convention := { capitalization_convention_data :> capitalization_data ; only_lower_first_letters : bool }. |
| 21 |
|
| 22 |
Derive name_case_kind_Listable SuchThat (@FinitelyListable name_case_kind name_case_kind_Listable) As name_case_kind_FinitelyListable. |
| 23 |
Proof. prove_ListableDerive. Qed. |
| 24 |
Global Existing Instances name_case_kind_Listable name_case_kind_FinitelyListable. |
| 25 |
|
| 26 |
Global Instance show_name_case_kind : Show name_case_kind. |
| 27 |
Proof. prove_Show_enum (). Defined. |
| 28 |
Global Instance show_lvl_name_case_kind : ShowLevel name_case_kind := show_name_case_kind. |
| 29 |
|
| 30 |
Definition parse_name_case_kind_list : list (string * name_case_kind) |
| 31 |
:= Eval vm_compute in |
| 32 |
List.map |
| 33 |
(fun v => (show v, v)) |
| 34 |
(list_all name_case_kind). |
| 35 |
|
| 36 |
Global Instance show_word_case_data : Show word_case_data |
| 37 |
:= fun v |
| 38 |
=> ("{| first_letter_case := " ++ show v.(first_letter_case)) |
| 39 |
++ " ; rest_letters_case := " ++ show v.(rest_letters_case) ++ " |}". |
| 40 |
Global Instance show_lvl_word_case_data : ShowLevel word_case_data := show_word_case_data. |
| 41 |
|
| 42 |
Global Instance show_capitalization_data : Show capitalization_data |
| 43 |
:= fun v |
| 44 |
=> ("{| separator := " ++ show v.(separator)) |
| 45 |
++ (" ; first_word_case := " ++ show v.(first_word_case)) |
| 46 |
++ " ; rest_words_case := " ++ show v.(rest_words_case) ++ " |}". |
| 47 |
Global Instance show_lvl_capitalization_data : ShowLevel capitalization_data := show_capitalization_data. |
| 48 |
|
| 49 |
Global Instance show_capitalization_convention : Show capitalization_convention |
| 50 |
:= fun v |
| 51 |
=> ("{| capitalization_convention_data := " ++ show v.(capitalization_convention_data)) |
| 52 |
++ " ; only_lower_first_letters := " ++ show v.(only_lower_first_letters) ++ " |}". |
| 53 |
Global Instance show_lvl_capitalization_convention : ShowLevel capitalization_convention := show_capitalization_convention. |
| 54 |
|
| 55 |
Definition case_adjust_ascii (data : name_case_kind) : Ascii.ascii -> Ascii.ascii |
| 56 |
:= match data with |
| 57 |
| lower => Ascii.to_lower |
| 58 |
| upper => Ascii.to_upper |
| 59 |
end. |
| 60 |
Definition case_adjust_string (data : name_case_kind) : string -> string |
| 61 |
:= match data with |
| 62 |
| lower => String.to_lower |
| 63 |
| upper => String.to_upper |
| 64 |
end. |
| 65 |
Definition case_adjust_word (only_lower_first_char : bool) (data : word_case_data) (s : string) : string |
| 66 |
:= match s with |
| 67 |
| EmptyString => EmptyString |
| 68 |
| String ch rest => String (case_adjust_ascii data.(first_letter_case) ch) |
| 69 |
(let adjusted := case_adjust_string data.(rest_letters_case) rest in |
| 70 |
match only_lower_first_char, data.(rest_letters_case) with |
| 71 |
| _, upper => adjusted |
| 72 |
| true, lower => rest |
| 73 |
| false, lower => adjusted |
| 74 |
end) |
| 75 |
end. |
| 76 |
Definition case_adjust_word_list (only_lower_first_char : bool) (data : capitalization_data) (words : list string) : list string |
| 77 |
:= match words with |
| 78 |
| [] => [] |
| 79 |
| firstword :: restwords |
| 80 |
=> (case_adjust_word only_lower_first_char data.(first_word_case) firstword) |
| 81 |
:: List.map (case_adjust_word only_lower_first_char data.(rest_words_case)) restwords |
| 82 |
end. |
| 83 |
Definition case_adjust_words (only_lower_first_char : bool) (data : capitalization_data) (words : list string) : string |
| 84 |
:= String.concat data.(separator) (case_adjust_word_list only_lower_first_char data words). |
| 85 |
|
| 86 |
Fixpoint split_before_gen (f : Ascii.ascii -> bool) (s : string) : list string |
| 87 |
:= match s with |
| 88 |
| EmptyString => [] |
| 89 |
| String ch rest |
| 90 |
=> match split_before_gen f rest with |
| 91 |
| [] => [String ch EmptyString] |
| 92 |
| EmptyString :: xs |
| 93 |
=> String ch EmptyString :: xs |
| 94 |
| String ch' _ as x :: xs |
| 95 |
=> if f ch' |
| 96 |
then String ch EmptyString :: x :: xs |
| 97 |
else String ch x :: xs |
| 98 |
end |
| 99 |
end. |
| 100 |
|
| 101 |
Definition split_before_uppercase (s : string) : list string := split_before_gen Ascii.is_upper s. |
| 102 |
Definition split_before_lowercase (s : string) : list string := split_before_gen Ascii.is_lower s. |
| 103 |
|
| 104 |
Lemma concat_split_before_gen f s : concat "" (split_before_gen f s) = s. |
| 105 |
Proof. |
| 106 |
induction s; cbn; [ reflexivity | ]; edestruct split_before_gen; cbn in *; subst; [ reflexivity | ]. |
| 107 |
break_innermost_match; reflexivity. |
| 108 |
Qed. |
| 109 |
|
| 110 |
Lemma concat_split_before_uppercase s : concat "" (split_before_uppercase s) = s. |
| 111 |
Proof. apply concat_split_before_gen. Qed. |
| 112 |
Lemma concat_split_before_lowercase s : concat "" (split_before_lowercase s) = s. |
| 113 |
Proof. apply concat_split_before_gen. Qed. |
| 114 |
|
| 115 |
Definition before_word_splittable (data : word_case_data) : bool |
| 116 |
:= match data.(first_letter_case), data.(rest_letters_case) with |
| 117 |
| upper, lower |
| 118 |
| lower, upper |
| 119 |
=> true |
| 120 |
| upper, upper |
| 121 |
| lower, lower |
| 122 |
=> false |
| 123 |
end. |
| 124 |
Definition splitter_function (data : capitalization_data) : option (string -> list string) |
| 125 |
:= if (data.(separator) =? "")%string |
| 126 |
then match data.(first_word_case).(first_letter_case), data.(first_word_case).(rest_letters_case), |
| 127 |
data.(rest_words_case).(first_letter_case), data.(rest_words_case).(rest_letters_case) |
| 128 |
with |
| 129 |
| _, lower, upper, lower => Some split_before_uppercase |
| 130 |
| _, upper, lower, upper => Some split_before_lowercase |
| 131 |
| _, _, _, _ => None |
| 132 |
end |
| 133 |
else Some (String.split data.(separator)). |
| 134 |
|
| 135 |
Definition split_case_to_words (data : capitalization_data) (s : string) : if splitter_function data then list string else unit |
| 136 |
:= match splitter_function data as f return if f then _ else _ with |
| 137 |
| Some f => f s |
| 138 |
| None => tt |
| 139 |
end. |
| 140 |
|
| 141 |
Definition convert_case (from : capitalization_data) (to : capitalization_convention) (s : string) : if splitter_function from then string else unit |
| 142 |
:= match splitter_function from as f return if f then _ else _ with |
| 143 |
| Some f => case_adjust_words to.(only_lower_first_letters) to (f s) |
| 144 |
| None => tt |
| 145 |
end. |
| 146 |
|
| 147 |
Module Word. |
| 148 |
Notation lowercase := {| first_letter_case := lower ; rest_letters_case := lower |}. |
| 149 |
Notation uppercase := {| first_letter_case := upper ; rest_letters_case := upper |}. |
| 150 |
Notation titlecase := {| first_letter_case := upper ; rest_letters_case := lower |}. |
| 151 |
End Word. |
| 152 |
|
| 153 |
Notation flatcase := {| separator := "" ; first_word_case := Word.lowercase ; rest_words_case := Word.lowercase |}. |
| 154 |
Notation UPPERFLATCASE := {| separator := "" ; first_word_case := Word.uppercase ; rest_words_case := Word.uppercase |}. |
| 155 |
Notation camelCase := {| separator := "" ; first_word_case := Word.lowercase ; rest_words_case := Word.titlecase |}. |
| 156 |
Notation UpperCamelCase := {| separator := "" ; first_word_case := Word.titlecase ; rest_words_case := Word.titlecase |}. |
| 157 |
Notation snake_case := {| separator := "_" ; first_word_case := Word.lowercase ; rest_words_case := Word.lowercase |}. |
| 158 |
Notation SCREAMING_SNAKE_CASE := {| separator := "_" ; first_word_case := Word.uppercase ; rest_words_case := Word.uppercase |}. |
| 159 |
Notation camel_Snake_Case := {| separator := "_" ; first_word_case := Word.lowercase ; rest_words_case := Word.titlecase |}. |
| 160 |
Notation Pascal_Snake_Case := {| separator := "_" ; first_word_case := Word.titlecase ; rest_words_case := Word.titlecase |}. |
| 161 |
Notation dash_case := {| separator := "-" ; first_word_case := Word.lowercase ; rest_words_case := Word.lowercase |}. |
| 162 |
Notation doner_case := {| separator := "|" ; first_word_case := Word.lowercase ; rest_words_case := Word.lowercase |}. |
| 163 |
Notation TRAIN_CASE := {| separator := "-" ; first_word_case := Word.uppercase ; rest_words_case := Word.uppercase |}. |
| 164 |
Notation Train_Case := {| separator := "-" ; first_word_case := Word.titlecase ; rest_words_case := Word.titlecase |}. |
| 165 |
|
| 166 |
Definition parse_capitalization_data_pre_list : list (capitalization_data * list string) |
| 167 |
:= [ |
| 168 |
(flatcase, ["flat case"; "flatcase"]) |
| 169 |
; (UPPERFLATCASE, ["upper flat case"; "UPPER FLAT CASE"; "UPPERFLATCASE"]) |
| 170 |
; (camelCase, ["camelCase"; "lower camelCase"; "lowerCamelCase"; "dromedaryCase"]) |
| 171 |
; (UpperCamelCase, ["PascalCase"; "UpperCamelCase"; "Upper Camel Case"; "StudlyCase"]) |
| 172 |
; (snake_case, ["snake_case"; "pothole_case"]) |
| 173 |
; (SCREAMING_SNAKE_CASE, ["SCREAMING_SNAKE_CASE"; "MACRO_CASE"; "CONSTANT_CASE"]) |
| 174 |
; (camel_Snake_Case, ["camel_Snake_Case"]) |
| 175 |
; (Pascal_Snake_Case, ["Pascal_Snake_Case"]) |
| 176 |
; (dash_case, ["kebab-case"; "dash-case"; "lisp-case"]) |
| 177 |
; (doner_case, ["doner|case"]) |
| 178 |
; (TRAIN_CASE, ["TRAIN-CASE"; "COBOL-CASE"; "SCREAMING-KEBAB-CASE"]) |
| 179 |
; (Train_Case, ["Train-Case"; "HTTP-Header-Case"]) |
| 180 |
]. |
| 181 |
|
| 182 |
Definition parse_capitalization_data_list : list (string * capitalization_data) |
| 183 |
:= Eval compute in |
| 184 |
List.flat_map |
| 185 |
(fun '(v, ls) => List.map (fun s => (s, v)) ls) |
| 186 |
parse_capitalization_data_pre_list. |
| 187 |
Definition parse_capitalization_data : ParserAction capitalization_data |
| 188 |
:= parse_strs parse_capitalization_data_list. |
| 189 |
|
| 190 |
Definition parse_capitalization_data_strict : string -> option capitalization_data |
| 191 |
:= finalize parse_capitalization_data. |