| 1 | ppx_optcomp - Optional compilation for OCaml |
| 2 | ============================================ |
| 3 | |
| 4 | ppx\_optcomp stands for Optional Compilation. It is a tool used to |
| 5 | handle optional compilations of pieces of code depending of the word |
| 6 | size, the version of the compiler, ... |
| 7 | |
| 8 | The syntax is based on OCaml item extension nodes, with keywords similar to cpp. |
| 9 | |
| 10 | ```ocaml |
| 11 | [%%if ocaml_version < (4, 02, 0)] |
| 12 | let x = 1 |
| 13 | [%%else] |
| 14 | let y = 2 |
| 15 | [%%endif] |
| 16 | ``` |
| 17 | |
| 18 | Syntax |
| 19 | ------ |
| 20 | |
| 21 | ppx\_optcomp is implemented using ppx_driver and operates on ocaml AST. |
| 22 | This means that whole file needs to be grammatically correct ocaml. |
| 23 | |
| 24 | The general syntax is: |
| 25 | |
| 26 | ``` |
| 27 | [%%keyword expression] |
| 28 | ``` |
| 29 | |
| 30 | Most of the statements are only supported on the toplevel. See |
| 31 | [grammar description](http://caml.inria.fr/pub/docs/manual-ocaml/extn.html#sec248) |
| 32 | for detailed information where ```[%% ]``` directives may be placed. |
| 33 | |
| 34 | Note in particular that the item extensions cannot be placed inside an |
| 35 | expression, and this would result in syntax error. |
| 36 | ```ocaml |
| 37 | (* SYNTAX ERROR: let x = [%%if defined(abc) ] 1 [%%else] 2 [%%endif] *) |
| 38 | ``` |
| 39 | |
| 40 | Additional syntax is provided for optional type variant declaration, as in |
| 41 | ```ocaml |
| 42 | type t = |
| 43 | | FOO |
| 44 | | BAR [@if ocaml_version < (4, 02, 0)] |
| 45 | ``` |
| 46 | |
| 47 | Directives |
| 48 | ---------- |
| 49 | |
| 50 | ### Defining variables |
| 51 | |
| 52 | - `[%%define` _identifier_ _expression_`]` |
| 53 | - `[%%undef` _identifier_`]` |
| 54 | |
| 55 | We also allow: `[%%define` _identifier_`]`. This will define |
| 56 | _identifier_ to `()`. The undefined identifiers are not valid in |
| 57 | subsequent expressions, but for expression `defined(`_identifier_`)`, |
| 58 | which evaluates to false. |
| 59 | |
| 60 | The scope of identifiers follows the same scoping rules as OCaml |
| 61 | variables. For instance: |
| 62 | |
| 63 | ```ocaml |
| 64 | (* [x] is undefined *) |
| 65 | [%%define x 0] |
| 66 | (* [x] is bound to [0] *) |
| 67 | module A = struct |
| 68 | (* [x] is bound to [0] *) |
| 69 | [%%define x 42] |
| 70 | (* [x] is bound to [42] *) |
| 71 | end |
| 72 | (* [x] is bound to [0] *) |
| 73 | ``` |
| 74 | |
| 75 | ### Conditionals |
| 76 | |
| 77 | The following directives are available for conditional compilations: |
| 78 | |
| 79 | - `[%%if` _expression_`]` |
| 80 | - `[%%elif` _expression_`]` |
| 81 | - `[%%else]` |
| 82 | - `[%%endif]` |
| 83 | - `[@if` _expression_`]` |
| 84 | |
| 85 | In all cases _expression_ must be an expression that evaluates to a |
| 86 | boolean value. Ppx\_optcomp will fail if it is not the case. |
| 87 | |
| 88 | Pseudo-function `defined(`_identifier_`)` may be then used in |
| 89 | expressions to check whether a given identifier has been defined. |
| 90 | Note that identifiers that were not defined or undefined beforehand |
| 91 | are assumed to be a typo, and therefore are rejected, with a notable |
| 92 | exception of |
| 93 | ``` |
| 94 | [%%ifndef FOO] |
| 95 | [%%define FOO] |
| 96 | ``` |
| 97 | which is allowed even if `FOO` was not seen before. |
| 98 | |
| 99 | The last form may be used only in type-variant definitions and pattern |
| 100 | matching, following constructors which are to be optional. If you |
| 101 | need a few constructors under the same condition, you need to copy the |
| 102 | directive multiple times, sorry. |
| 103 | ``` |
| 104 | type t = |
| 105 | | A of int |
| 106 | | B of int * int [@if ocaml >= 4.04] |
| 107 | ... |
| 108 | |
| 109 | match (v: t) with |
| 110 | | A x -> something x |
| 111 | | B (y,z) [@if ocaml >= 4.04] -> something' y z |
| 112 | ``` |
| 113 | |
| 114 | ### Warnings and errors |
| 115 | |
| 116 | `[%%warning _string_]` will cause the pre-processor to print a |
| 117 | message on stderr. |
| 118 | |
| 119 | `[%%error _string_]` will cause the pre-processor to fail with the |
| 120 | following error message. |
| 121 | |
| 122 | Note that in both cases _expression_ can be an arbitrary expression. |
| 123 | |
| 124 | ### Imports |
| 125 | |
| 126 | Ppx\_optcomp allows one to import another file using: |
| 127 | |
| 128 | `[%%import` _filename_`]` |
| 129 | |
| 130 | where _filename_ is a string constant. Filenames to import are |
| 131 | resolved as follow: |
| 132 | |
| 133 | - if _filename_ is relative, i.e. doesn't start with `/`, it is |
| 134 | considered as relative to the directory of the file being parsed |
| 135 | - if _filename_ is absolute, i.e. starts with `/`, it is used as is |
| 136 | |
| 137 | Only optcomp directives are allowed in the imported files. The |
| 138 | intended use is including some configuration variables at the |
| 139 | beginning of a file: |
| 140 | |
| 141 | ```ocaml |
| 142 | [%%import "config.mlh"] |
| 143 | ``` |
| 144 | |
| 145 | If imported file's extension is `.h`, an alternate C-like syntax is |
| 146 | expected in the file. This is to allow importing both from C and |
| 147 | OCaml single configuration file like: |
| 148 | ``` |
| 149 | #ifndef CONFIG_H |
| 150 | #define CONFIG_H |
| 151 | |
| 152 | #define FOO |
| 153 | #undef BAR |
| 154 | #define BAZ 3*3 + 3 |
| 155 | |
| 156 | #endif |
| 157 | ``` |
| 158 | |
| 159 | |
| 160 | Expressions and patterns |
| 161 | ------------------------ |
| 162 | |
| 163 | ppx\_optcomp supports a subset of OCaml expressions and patterns: |
| 164 | |
| 165 | - literals: integers, characters and strings |
| 166 | - tuples |
| 167 | - `true` and `false` |
| 168 | - let-bindings |
| 169 | - pattern matching |
| 170 | |
| 171 | And it provides the following functions: |
| 172 | |
| 173 | - comparison operators: `=`, `<`, ... |
| 174 | - boolean operators: `||`, `&&`, `not`, ... |
| 175 | - arithmetic operators: `+`, `-`, `*`, `/` |
| 176 | - `min` and `max` |
| 177 | - `fst` and `snd` |
| 178 | - conversion functions: `to_int`, `to_string`, `to_char`, `to_bool` |
| 179 | - `defined`, `not_defined`: check whether a variable is defined |
| 180 | - `show`: act as identity, but pretty-print a value to stderr |