How to add ppx_deriving to Location without re-creating the module Location/Lexing

I’m working on a project where Ppxlib, menhir and sedlex need to play nicely together. While there’s some AST that needs Location, I would like to have to the AST type.

As far as I know, this leads me to duplicate Location and Lexing as modules, and add ppx_deriving show to those. Ofcourse, I would need to call them differently Located and Lex which plays very badly with ppxlib (specifically metaquote).

After 500 LOC changes and a bunch of casts from Location.t to Located.t, I start to think it’s a bad idea. Do I have any alternative?


1 Like

You can re-export the location type and add a deriving annotation to it:

type position = Lexing.position = {
  pos_fname : string;
  pos_lnum : int;
  pos_bol : int;
  pos_cnum : int;
} [@@deriving show]

type loc = Location.t {
  loc_start: position;
  loc_end: position;
  loc_ghost: bool;
} [@@deriving show]

Didn’t know you could re-export the type, huge.

OMG, that’s a live saver. Thanks!

1 Like

There’s also ppx_import which automates some of the re-exporting (there’s a specific example in the README for using it with [@@deriving show].)

1 Like