Skip to content

Commit

Permalink
Merge pull request #392 from hacspec/extend-view-api
Browse files Browse the repository at this point in the history
feat(engine): add transoforms to name policies
  • Loading branch information
W95Psp authored Dec 19, 2023
2 parents 3cd2e34 + b30d940 commit 8650849
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
28 changes: 19 additions & 9 deletions engine/lib/concrete_ident/concrete_ident.ml
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ module MakeViewAPI (NP : NAME_POLICY) : VIEW_API = struct
let is_reserved_word : string -> bool = Hash_set.mem NP.reserved_words

let rename_definition (_path : string list) (name : string) (kind : Kind.t)
_type_name =
type_name =
(* let path, name = *)
(* match kind with *)
(* | Constructor { is_struct = false } -> *)
Expand All @@ -457,14 +457,21 @@ module MakeViewAPI (NP : NAME_POLICY) : VIEW_API = struct
| Value | Impl ->
if start_uppercase name || is_reserved_word name then "v_" ^ name
else escape name
| Constructor _ ->
if start_lowercase name || is_reserved_word name then "C_" ^ name
else escape name
| Field | AssociatedItem _ -> (
match Stdlib.int_of_string_opt name with
| Some _ -> NP.index_field_transform name
(* | _ -> "f_" ^ Option.value_exn type_name ^ "_" ^ name *)
| _ -> "f_" ^ name)
| Constructor { is_struct } ->
let name =
if start_lowercase name || is_reserved_word name then "C_" ^ name
else escape name
in
if is_struct then NP.struct_constructor_name_transform name
else
let enum_name = type_name |> Option.value_exn in
NP.enum_constructor_name_transform ~enum_name name
| Field | AssociatedItem _ ->
let struct_name = type_name |> Option.value_exn in
NP.field_name_transform ~struct_name
(match Stdlib.int_of_string_opt name with
| Some _ -> NP.index_field_transform name
| _ -> "f_" ^ name)
| Lifetime | Macro -> escape name

let rec to_view ({ def_id; kind } : t) : view =
Expand Down Expand Up @@ -527,6 +534,9 @@ let map_path_strings ~(f : string -> string) (cid : t) : t =
module DefaultNamePolicy = struct
let reserved_words = Hash_set.create (module String)
let index_field_transform = Fn.id
let field_name_transform ~struct_name:_ = Fn.id
let enum_constructor_name_transform ~enum_name:_ = Fn.id
let struct_constructor_name_transform = Fn.id
end

let matches_namespace (ns : Types.namespace) (did : t) : bool =
Expand Down
4 changes: 4 additions & 0 deletions engine/lib/concrete_ident/concrete_ident_sig.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ struct

val index_field_transform : string -> string
(** Transformation applied to indexes fields name (i.e. [x.1]) *)

val field_name_transform : struct_name:string -> string -> string
val enum_constructor_name_transform : enum_name:string -> string -> string
val struct_constructor_name_transform : string -> string
end

module type VIEW_API = sig
Expand Down

0 comments on commit 8650849

Please sign in to comment.