Questions tagged [ppx]

Questions about the use or developpement of PPXes (OCaml syntax extension)

PPX Syntax Extensions

ppx is the syntax extension format supported currently by OCaml. PPX rewriters are preprocessors for OCaml programs that are applied to the code before passing it on to the compiler. They work over the AST resulting from its parsing, and compute a new AST that will be actually compiled.

Extension Nodes

OCaml features syntax extensions that are meant to be used by external tools. The kind of extensions exist which are attributes and extension nodes.

  1. Attributes are “decorations” of the syntax tree which are mostly ignored by the type-checker but can be used by external tools.
  2. Extension nodes are generic placeholders in the syntax tree. They are rejected by the type-checker and are intended to be “expanded” by external tools such as -ppx rewriters.

The -ppx option

ocamlc and ocamlopt both take a -ppx command line option. This option takes as argument a program that is executed during the compilation of a file in order to transform it on the fly. This program is called a ppx rewriter. More precisely, once the OCaml compiler has parsed the source file and constructed the corresponding AST, it runs the ppx with this AST as input. The ppx returns a new transformed AST and the compiler continues the compilation process with this new AST, discarding the original one.

Several -ppx options can be passed to the compiler. In this case, the compiler will apply the various ppx rewriters one by one, each one feeding its output to the next one.

Using dune

It is easy to integerate a PPX preprocessing to a workflow. If you want to pass command line flags that do not start with a -, you can separate library names from flags using --. So for instance from the following preprocess field:

(preprocess (pps ppx1 -foo ppx2 -- -bar 42))

ppxlib

The modern solution for writing PPX extensions. Without this library, writing PPX extensions is fragile and breaks with OCaml version changes. ppxlib merges several older projects together to provide a complete platform for writing efficient, resilient PPX extensions.

OCaml-migrate-parsetree

This library converts OCaml parsetrees between different major versions thus making a PPX written for a given version of OCaml portable with older versions.

30 questions
2
votes
2 answers

Are there any usage examples for the OCaml ppx_xml_conv module

I'm looking for a simple example for the ppx_xml_conv module from janestreet. I'm not terribly familiar with the (relatively) new ppx thing and can't really figure it out from the source code. Ultimately, I'm trying to write a client for an old SOAP…
Juan Chanco
  • 157
  • 6
1
vote
1 answer

How do I output a non-polymorphic variant using ppxlib Ast_builder?

I am trying to make a PPX extension rewriter that dynamically builds (among other things) a variant type, based on some config in a JSON file... I have got reasonably far but I am confused whether it is possible to output a non-polymorphic variant…
Anentropic
  • 32,188
  • 12
  • 99
  • 147
1
vote
0 answers

OCaml variant augmentation boilerplates

Is there a way to efficiently "augment" or attach some more information without much boilerplate? That is, given types type orig = | A | B of orig | C of orig * orig and type dat = int and type aug = | AugA of dat | AugB of dat * aug |…
Jay Lee
  • 1,684
  • 1
  • 15
  • 27
1
vote
1 answer

How to force ppx to use an OCaml version?

I have the below OCaml file which compiles correctly without ppx and fails with this dune file (library (name so_proj) (preprocess (pps ppx_inline_test ppx_deriving.show ppx_deriving.ord ppx_deriving.map ppx_deriving.eq …
nicolas
  • 9,549
  • 3
  • 39
  • 83
1
vote
1 answer

OCaml - Access Derived Functions from Another Module

If a record is defined in a module in lib and it contains a deriving annotation that generates functions, how can those functions be used in another module? For example, the yaml/ppx_deriving_yaml opam module contains a [@@deriving yaml]. If…
Greg
  • 3,086
  • 3
  • 26
  • 39
1
vote
1 answer

Custom json generator for variant of a sum type with ppx_deriving_yojson

I'd like to generate JSON objects representing an AST, and I'm having a look at ppx_deriving_yojson to do that (more specifically the to_yojson part). Now, there are of course corner cases when I'd like to resort to a customized encoding. As…
Virgile
  • 9,724
  • 18
  • 42
1
vote
1 answer

How to find the interface i.e. the set of provided function of an OCaml package?

I need to use parse an OCaml source file into a typed AST and I believe ppx_jane is the right package to do the work. After installing it using opam, I still don't know what functions are available. This is a link to the ppx_jane package on opam. It…
Yixing Liu
  • 2,179
  • 1
  • 20
  • 36
1
vote
3 answers

Lexer/filter for comments

Is there an OCaml tool that allows filtering comments in source files, similar to gcc -E? Ideally, I'm looking for something that will remove everything but comments, but the other way around would also be useful. For instance, if there is a way to…
anol
  • 8,264
  • 3
  • 34
  • 78
1
vote
1 answer

Enable warning about unused/invalid attributes when using ppx

I'm trying to use some ppx extensions, and I just found out that OCaml emits no warning for unused/invalid attributes. E.g., if I write something like: let[@blaa] () = () Even with -w +A, OCaml will not say anything about the fact that @blaa is…
anol
  • 8,264
  • 3
  • 34
  • 78
1
vote
1 answer

How to use ppx.metaquot in OCaml?

The command written in README.md doesn't work (The usage of ocamlfind is shown.) I understand the ppx_metaquot execution file is a kind of rewrite file, so I can translate my code using quasi quotation in this way: ocamlfind ppx_tools/rewriter…
henoc
  • 183
  • 1
  • 9
0
votes
2 answers

How official / stable is the whole 'ppx' mechanic in Ocaml language?

For context (skip to question if you like): I am learning ocaml and started reading the Real World Ocaml. So far, really (really!) liking the language and the book. One thing I'm starting to not like so much is this mysterious 'ppx' stuff that is…
Kris
  • 3,898
  • 1
  • 23
  • 32
0
votes
2 answers

How to apply [@@deriving show] to a type from module parameter of my functor?

I have a functor that takes a Set type like: module type MySet = functor (S : Set.S) -> sig val my_method : S.t -> S.elt -> S.elt list option end module MySet_Make : MySet = functor (S : Set.S) -> struct let my_method set el = Some [el] (*…
Anentropic
  • 32,188
  • 12
  • 99
  • 147
0
votes
2 answers

OCaml serializing a (no args) variant as a "string enum" (via Yojson)

Say I am building a record type: type thing { fruit: string; } But I want the possible values of fruit to be constrained to a fixed set of strings. It seems natural to model this in OCaml as a variant, e.g.: type fruit = APPLE | BANANA |…
Anentropic
  • 32,188
  • 12
  • 99
  • 147
0
votes
2 answers

OCaml specify path to ppx executable

I'm trying to figure out how to pass in the location of an executable to be run as a ppx filter for the OCaml compilers ocamlc/ocamlopt. My questions are, basically What format is a ppx filter expected to take as input? What is it expected to…
Greg Nisbet
  • 6,710
  • 3
  • 25
  • 65
-1
votes
1 answer

Type error when processing graphql result

I just started playing with reasonML and graphql and have built a simple react component that retrieves data from a world cup API. My code is below: [@bs.module] external gql: ReasonApolloTypes.gql = "graphql-tag"; module GetMatches = [%graphql …
jwesonga
  • 4,249
  • 16
  • 56
  • 83
1
2