How to disable ppx_deriving.show printer output for some AST nodes?

Hi,

I’m loving learning/using OCaml to build a compiler.

However since OCaml is complex I’m havin some issues with some libraries.

Using ppx_deriving.show pre-processor I was able to print the AST of the code , to help in development and debugging.

However I do not want to print every node of the AST as it clutters our output.

For example, the output of the loc (location) node makes the output verbose and makes it difficult to read our AST.

I’m just showing the nodes of id and loc to help with the question. id contains loc node but I do not want the loc node to be printed.

How can I disable it?

I tried removing [@@deriving show { with_path = false }] from loc node, but then the code does not compile for some reason.

Thanks.

and id = Id of string * loc [@@deriving show { with_path = false }]
and loc = Loc of int * int [@@deriving show { with_path = false }]

I print the AST using this method.

(** Print Ix AST for debugging. *)
let print_ast (ast : Ixast.executable) =
  print_endline ("> " ^ Ixast.show_executable ast ^ "\n")
;;


This is the output where the information of loc nodes clutters it.

> (Executable
   [(StatementFunction (TypeInt, (Id ("main", (Loc (8, 326)))),
       (BlockStatement (
          [(StatementReturn
              (ExpressionTerminal ((TerminalInt (0, (Loc (9, 344)))),
                 (Loc (9, 344)))))
            ],
          (Loc (8, 333)))),
       (Loc (1, 0))))
     ])

[@@deriving show] just generates a show and pp function for you. The simplest option is to remove [@@deriving show] from the type you don’t want to show, and then hand-write the functions instead. Since you don’t want to print anything, this this should be simple.

type loc = Loc of int * int
let show_loc _ = ""
let pp_loc _ _ = ()
1 Like

Thanks.

Your solution helped me solve it.

Initially your solution was not working and I was little bit confused, but later I realised that my AST nodes of executable were recursive, so ppx_deriving was printing every node under it.

To silence the output of loc I had to make it a “non-recursive” type and shift it to the top of the file.

Thanks.

type loc = Loc of int * int

(* Silence annoying Location output when printing AST. *)
let show_loc _ = ""
let pp_loc _ _ = ()

type id = Id of string * loc [@@deriving show { with_path = false }]

(* this was recursive *)
type executable = Executable of statements list
[@@deriving show { with_path = false }]

and statements =
  | StatementModule of statements list * loc
  | StatementFunction of types * id * blocks * loc
  | StatementStruct
  | StatementEnum
  | StatmentValue of types * id * expressions * loc
  | StatementReturn of expressions

(* rest of the code ... *)
1 Like

An alternative way is to manually override using [@printer] attribute, as described in ppx_deriving README. In your case this isn’t necessary, but it is necessary if you want to override for only one of the types in a recursive type declaration.

2 Likes