| 1 | ppx_sexp_message - Easy construction of s-expressions |
| 2 | ===================================================== |
| 3 | |
| 4 | Overview |
| 5 | -------- |
| 6 | |
| 7 | The aim of ppx\_sexp\_message is to ease the creation of s-expressions |
| 8 | in OCaml. This is mainly motivated by writing error and debugging |
| 9 | messages, where one needs to construct a s-expression based on various |
| 10 | element of the context such as function arguments. For instance: |
| 11 | |
| 12 | ```ocaml |
| 13 | open Core |
| 14 | |
| 15 | let rename ~src ~dst = |
| 16 | try Unix.rename ~src:tmpfile ~dst |
| 17 | with Unix.Unix_Error (error, _, _) -> |
| 18 | raise_s |
| 19 | [%message |
| 20 | "Error while renaming file" |
| 21 | ~source:(tmpfile : string) |
| 22 | ~dest: (dst : string) |
| 23 | (error : Unix.Error.t) |
| 24 | ] |
| 25 | ``` |
| 26 | |
| 27 | would produce the following s-expression: |
| 28 | |
| 29 | ```scheme |
| 30 | ("Error while renaming file" |
| 31 | (source tmp/XYZ) |
| 32 | (dest blah) |
| 33 | (error ENOENT)) |
| 34 | ``` |
| 35 | |
| 36 | Syntax |
| 37 | ------ |
| 38 | |
| 39 | Ppx\_sexp\_message expands the `[%message ...]` extension point into |
| 40 | an expression that evaluates to an s-expression, i.e. an expression of |
| 41 | type `Sexplib.Sexp.t`. The grammar of the payload is a small DSL that |
| 42 | specifies what the generated s-expression looks like. |
| 43 | |
| 44 | The user can then format and display such an s-expression as usual, using |
| 45 | [sexplib](https://github.com/janestreet/sexplib), or as |
| 46 | [explained here](https://dev.realworldocaml.org/data-serialization.html#basic-usage). |
| 47 | |
| 48 | ### Basic syntax |
| 49 | |
| 50 | Ppx\_sexp\_message recognizes the form `[%message expr1 expr2 ...]`, |
| 51 | and maps it pointwise to an s-expression that looks like |
| 52 | `((<optional_tag_1> <value_1>) ... (<optional_tag_n> <value_n>))`. |
| 53 | A single expression also works (but it can't syntactically be an application). |
| 54 | |
| 55 | Every `expr` is mapped to an an optional tag and value as follows. |
| 56 | |
| 57 | The tag is determined by the following rules: |
| 58 | |
| 59 | - no tag if the labelled expression has the label `_` |
| 60 | - the tag is the label when the label is not `_` |
| 61 | - when the expression is not labelled |
| 62 | - the tag is `expr` if the expression has the form `(expr : typ)` |
| 63 | - otherwise there is no tag |
| 64 | |
| 65 | Here are examples of each of these rules: |
| 66 | |
| 67 | - `[%message "error" ~_:(msg : string)]` becomes `(error "value of msg")` |
| 68 | - `[%message "error" ~tag:(msg : string)]` becomes `(error (tag "value of msg"))` |
| 69 | - `[%message "error" (msg : string)]` becomes `(error (msg "value of msg"))` |
| 70 | - `[%message "error" "value of msg"]` becomes `(error "value of msg")` |
| 71 | |
| 72 | Having the tag derived from the expression is by far the most common |
| 73 | case, since it is convenient to not have to come up with a descriptive |
| 74 | name. This is especially valuable for debugging messages. |
| 75 | |
| 76 | The rest of this section describe how each expression is converted |
| 77 | to a value in the s-expression. |
| 78 | |
| 79 | #### Conversion of expressions |
| 80 | |
| 81 | Literals of base types (constant strings, integers, floats, ...) are |
| 82 | converted to their natural sexp representation. |
| 83 | |
| 84 | When an expression is annotated with a type, the type is used to |
| 85 | convert the value exactly like `ppx_sexp_conv` does. |
| 86 | |
| 87 | Otherwise, expressions are assumed to be valid ocaml expressions of |
| 88 | type string and the resulting string ends up as an atom in the |
| 89 | s-expression. |
| 90 | |
| 91 | For instance: |
| 92 | |
| 93 | - `"foo"` becomes `foo` |
| 94 | - `(Map.keys m : string list)` becomes `("Map.keys m" (.bashrc .emacs))` |
| 95 | - `(sprintf "expected %s of type" ast_type)` becomes `"expected a pattern of type"` |
| 96 | |
| 97 | #### Optionally displayed expression |
| 98 | |
| 99 | When an expression is annotated with a type, if the type has the |
| 100 | syntax `a option [@sexp.option]` for some `a`, then: |
| 101 | |
| 102 | - when the value is `None`, the tag and expression are omitted |
| 103 | - when the value is `Some x`, then the tag and `x` are displayed |
| 104 | |
| 105 | If the type annotation has the attribute `[@sexp.omit_nil]`, then when |
| 106 | the expression is converted into an sexp, if that sexp is `()`, then |
| 107 | both the tag and the expression are omitted. |
| 108 | |
| 109 | One can also use `[%message.omit_nil exprs]`, which is a variation of |
| 110 | `[%message exprs]` which behaves as if all `option` types were annotated |
| 111 | with `[@sexp.option]` and all other expressions were annotated with |
| 112 | `[@sexp.omit_nil]`. |
| 113 | |
| 114 | #### Special case of the empty string |
| 115 | |
| 116 | An exception to the previous rules is the treatment of the empty |
| 117 | string. An empty string will be treated as if it did not appear in the |
| 118 | source code. It it useful to work around syntactic limitations: |
| 119 | |
| 120 | - the inability to put a label on the first expression |
| 121 | - the inability to tell the difference between an application and a |
| 122 | single expression which is an application |
| 123 | |
| 124 | For instance: |
| 125 | |
| 126 | ```ocaml |
| 127 | `[%message "" ~problem:(error : Error.t)] |
| 128 | ``` |
| 129 | |
| 130 | ```ocaml |
| 131 | `[%message "" (sprintf "invalid %s" name)] |
| 132 | ``` |
| 133 | |
| 134 | #### Eagerness |
| 135 | |
| 136 | The extension points `[%lazy_message]` and `[%lazy_message.omit_nil]` |
| 137 | are additionally provided, which just wrap the generated code in |
| 138 | `[lazy]` to delay computing and allocating of possibly large sexps. |
| 139 | This is intended to be used with, for example, `[Error.of_lazy_sexp]`, |
| 140 | where the error messages may be large, but are typically not used: |
| 141 | |
| 142 | ```ocaml |
| 143 | let execute_query_exn ~database ~query = |
| 144 | Database.execute_query ~database ~query |
| 145 | |> Option.value_exn |
| 146 | ~error: |
| 147 | (Error.of_lazy_sexp |
| 148 | [%lazy_message "Query failed" (database : Database.t) (query : Query.t)]) |
| 149 | ``` |
| 150 | |
| 151 | #### Misc |
| 152 | |
| 153 | For convenience and continuity of the syntax `[%message]` becomes |
| 154 | `()`. |
| 155 | |
| 156 | Difference with ppx\_sexp\_value |
| 157 | -------------------------------- |
| 158 | |
| 159 | Ppx\_sexp\_message is similar to |
| 160 | [ppx_sexp_value](https://github.com/janestreet/ppx_sexp_value) in the |
| 161 | sense that it makes the creation of s-expression nicer. The main |
| 162 | difference is that ppx\_sexp\_value is a more general rewriter that |
| 163 | build a s-expression based closely on what the user wrote. On the |
| 164 | other hand ppx\_sexp\_message tries to focus on having a DSL that is |
| 165 | as light as possible for building error messages. |