ocaml-ppx / ppx_tools

Tools for authors of ppx rewriters
MIT License
134 stars 39 forks source link

ppx antiquotations? #34

Closed smolkaj closed 8 years ago

smolkaj commented 8 years ago

I realize this is not the right place to ask this question, but unfortunately I don't know what the right place is. We're considering moving from camlp4 to ppx for the frenetic project. Currently we use camlp4 to embed our DSL (namely NetKAT) into OCaml, so we can write things like

let p1 = <:netkat< port:=2 >>
let p2 = <:netkat< ip4Dst:=10.0.0.1 >>
let p = <:netkat< switch=1; $p1$ + switch=2; $p2$  >>

Camlp4 will parse the DSL at compile time and replace the code snippets with their less pretty AST versions:

let p1 = Filter(Port, 2)
let p2 = Filter(Ip4Dst, 167772161L)
let p = Union (Seq (Filter(Switch,1), p1), Seq (Filter(Switch,2), p2))

Is ppx powerful enough to support this kind of use case? In particular, can ppx support antiquotations, as in the definition of p above? I have seen that with ppx we could have something like

let%netkat p = ip4Dst=10.0.0.1 

but I couldn't find anything on antiquotations.

Sorry for asking this question here, and thanks in advance for any help or pointers you might be able to provide.

alainfrisch commented 8 years ago

If your concrete syntax compatible with OCaml's, you can indeed just use extension nodes. But an IP address is not valid OCaml syntax, so either you change the syntax e.g. to something like:

  let%netkat p = ip4Dst := IP("10.0.0.1")

or you need to create you own parser:

  let%netkat p = "ip4Dst := 10.0.0.1"' 

(and then you decide on the syntax for "antiquotations")