aboutsummaryrefslogtreecommitdiff
path: root/lib/ppx_hash/ppx_hash.ml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ppx_hash/ppx_hash.ml')
-rwxr-xr-xlib/ppx_hash/ppx_hash.ml32
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/ppx_hash/ppx_hash.ml b/lib/ppx_hash/ppx_hash.ml
new file mode 100755
index 0000000..59584d5
--- /dev/null
+++ b/lib/ppx_hash/ppx_hash.ml
@@ -0,0 +1,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