aboutsummaryrefslogtreecommitdiff
path: root/ppx_hash/ppx_hash.ml
blob: 59584d515f7b15c746f1aa26e22a9c4e6be6b044 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
open Ppxlib

(**

   This is a simple ppx which evaluate hash for string at compilation time.

    [%static_hash "deadbeef"] is equivalent to [Hashtbl.hash "deadbeef"]

    the ppx only evaluate strings.
*)

let name = "static_hash"

let expand ~loc ~path:_ (value : string) =
  let h = Hashtbl.hash value in
  Ast_builder.Default.eint ~loc h

let extension =
  Extension.declare
    name
    Extension.Context.expression
    Ast_pattern.(single_expr_payload (estring __))
    expand



let rule = Ppxlib.Context_free.Rule.extension extension

let () =
  Driver.register_transformation
    ~rules:[rule]
    name