summaryrefslogtreecommitdiff
path: root/experimental/frisch/tracer.ml
blob: 657756a323da8fddbc4ea17550470af5e4a5585d (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
33
34
35
36
37
38
39
40
41
42
43
open Ast_mapper
open Location
open Parsetree

(* To define a concrete AST rewriter, we can inherit from the generic
   mapper, and redefine the cases we are interested in.  In the
   example below, we insert in the AST some debug statements around
   each module structure. We also keep track of the current "path" in
   the compilation unit.  *)

let trace s =
  E.(apply (lid "Pervasives.print_endline") ["", strconst s])

let tracer =
  object(this)
    inherit Ast_mapper.create as super
    val path = ""

    method! implementation input_name ast =
      let path = String.capitalize (Filename.chop_extension input_name) in
      (input_name, {< path = path >} # structure ast)

    method! structure_item = function
      | {pstr_desc = Pstr_module (s, _); pstr_loc = _loc} as si ->
          [ M.map_structure_item {< path = path ^ "." ^ s.txt >} si ]
      | si ->
          [ M.map_structure_item this si ]

    method! structure l =
      M.eval (trace (Printf.sprintf "Entering module %s" path)) ::
      (super # structure l) @
      [ M.eval (trace (Printf.sprintf "Leaving module %s" path)) ]

    method! expr e =
      match e.pexp_desc with
      | Pexp_send (_, s) ->
          E.sequence (trace (Printf.sprintf "calling method %s" s)) (super # expr e)
      | _ ->
          super # expr e

  end

let () = tracer # main