Fix non-identchars appearing in backend idents

This commit is contained in:
Louis Gesbert 2024-10-30 14:15:56 +01:00
parent b67758ec6a
commit 57f85ffb9c
5 changed files with 18 additions and 7 deletions

View File

@ -17,6 +17,13 @@
include Stdlib.String
let to_ascii : string -> string = Ubase.from_utf8
let to_id s =
to_ascii s
|> map (function
| ('a' .. 'z' | 'A' .. 'Z' | '0' .. '9') as c -> c
| _ -> '_')
let is_uppercase_ascii = function 'A' .. 'Z' -> true | _ -> false
let begins_with_uppercase (s : string) : bool =
@ -29,7 +36,7 @@ let begins_with_uppercase (s : string) : bool =
let to_snake_case (s : string) : string =
let out = Buffer.create (2 * length s) in
s
|> to_ascii
|> to_id
|> iteri (fun i c ->
if is_uppercase_ascii c && 0 <> i && get s (i - 1) <> '_' then
Buffer.add_char out '_';
@ -40,7 +47,7 @@ let to_camel_case (s : string) : string =
let last_was_underscore = ref true in
let out = Buffer.create (length s) in
s
|> to_ascii
|> to_id
|> iter (function
| '_' -> last_was_underscore := true
| c ->

View File

@ -29,6 +29,10 @@ val to_ascii : string -> string
(** Removes all non-ASCII diacritics from a string by converting them to their
base letter in the Latin alphabet. *)
val to_id : string -> string
(** Like [to_ascii], but in addition replaces any non-alphanumeric character by
[_] *)
val is_uppercase_ascii : char -> bool
(** [is_uppercase c] returns if [c] is in the set ['A'...'Z']. *)

View File

@ -37,7 +37,7 @@ module To_jsoo = struct
other modules: here everything is flattened in the current namespace *)
let format_struct_name ppf name =
StructName.to_string name
|> String.to_ascii
|> String.to_id
|> String.uncapitalize_ascii
|> String.map (function '.' -> '_' | c -> c)
|> Format.pp_print_string ppf

View File

@ -81,8 +81,8 @@ let renaming =
let id = f id |> Re.replace_string module_sep_re ~by:"_" in
String.concat "__" (pfx @ [id])
in
let cap s = String.to_ascii s |> String.capitalize_ascii in
let uncap s = String.to_ascii s |> String.uncapitalize_ascii in
let cap s = String.to_id s |> String.capitalize_ascii in
let uncap s = String.to_id s |> String.uncapitalize_ascii in
let upper s = String.to_snake_case s |> String.uppercase_ascii in
Renaming.program ()
~reserved:c_keywords

View File

@ -471,8 +471,8 @@ let process_type_ident
ctx_enums = EnumName.Map.add new_name ctx_constrs tctx.ctx_enums;
}
let cap s = String.to_ascii s |> String.capitalize_ascii
let uncap s = String.to_ascii s |> String.uncapitalize_ascii
let cap s = String.to_id s |> String.capitalize_ascii
let uncap s = String.to_id s |> String.uncapitalize_ascii
(* Todo? - handle separate namespaces ? (e.g. allow a field and var to have the
same name for backends that support it) - register module names as reserved