package OCanren-ppx

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file ppx_repr.ml

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
33
34
35
36
37
38
39
40
41
42
43
(*
 * OCanren. PPX suntax extensions.
 * Copyright (C) 2015-2020
 * Dmitri Boulytchev, Dmitry Kosarev, Alexey Syomin, Evgeny Moiseenko
 * St.Petersburg State University, JetBrains Research
 *
 * This software is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License version 2, as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU Library General Public License version 2 for more details
 * (enclosed in the file COPYING).
 *)

open Ppxlib

let string_of_expression e =
  Format.set_margin 1000;
  Format.set_max_indent 2000;
  let ans = Format.asprintf "%a" Pprintast.expression e in
  ans

let mapper = object
  inherit Ast_traverse.map as super

  method! expression e =
    match e with
    | { pexp_desc=Pexp_construct ({txt=Lident "REPR";_}, Some e); _} as expr ->

      let text = string_of_expression e in
      { expr with pexp_desc =
                    Pexp_tuple [Ast_helper.Exp.constant (Pconst_string (text, e.pexp_loc, None)); e] }
    | e -> super#expression e
end

let () =
  Ppxlib.Driver.register_transformation
    ~impl:(fun s  -> mapper#structure s)
    "repr"
OCaml

Innovation. Community. Security.