Debian Codegraph @

README.md

raw symbols SCIP
1 ppx_custom_printf
2 =================
3
4 Extensions to printf-style format-strings for user-defined string conversion.
5
6 `ppx_custom_printf` is a ppx rewriter that allows the use of
7 user-defined string conversion functions in format strings (that is,
8 strings passed to printf, sprintf, etc.).
9
10 No new syntax is introduced. Instead a previously ill-typed use of
11 the `!` operator is re-purposed.
12
13 Basic Usage
14 -----------
15
16 The basic usage is as follows:
17
18 ```ocaml
19 printf !"The time is %{Time} and the timezone is %{Time.Zone}."
20 time zone
21 ```
22
23 The ppx rewriter will turn the `!`-string into a format of type
24 `(Time.t -> Time.Zone.t -> unit, unit, string) format`. This is done
25 by embedding the `Time.to_string` and `Time.Zone.to_string` functions
26 into the format, using the low-level format mechanism of the stdlib.
27
28 In general, specifiers like `%{<Module-path>}` produce a call to
29 `Module-path.to_string`. The module path can even be empty, in which
30 case the generated code calls `to_string`.
31
32 Note that you have to prepend the format string with a `!`, so that
33 the ppx rewriter knows to operate on it.
34
35 Sexps
36 -----
37
38 The syntax `%{sexp:<type>}` is also supported. For example:
39
40 ```ocaml
41 printf !"The time is %{sexp:Time.t}." time
42 ```
43
44 The `time` argument will be turned into a string using:
45
46 ```ocaml
47 fun x -> Sexplib.Sexp.to_string_hum ([%sexp_of: Time.t] x)
48 ```
49
50 This supports arbitrary type expressions.
51
52 You can use `Sexplib.Sexp.to_string_mach` instead of
53 `Sexplib.Sexp.to_string_hum` by using `%{sexp#mach:<type>}`
54
55 Using functions other than `M.to_string`
56 ----------------------------------------
57
58 The format specifier `%{<Module-path>.<lowercase_identifier>}`
59 corresponds to that function. So, for example:
60
61 ```ocaml
62 printf !"The date is %{Core.Date.to_string_iso8601_basic}" date
63 ```
64
65 will turn `date` to a string using the following code:
66
67 ```ocaml
68 fun x -> Core.Date.to_string_iso8601_basic x
69 ```
70
71 Further, the format specifier
72 `%{<Module-path>#<lowercase_identifier>}` corresponds to the function
73 `<Module_path>.to_string_<lowercase_identifier>`. So, for example:
74
75 ```ocaml
76 printf !"The date is %{Core.Date#american}" date
77 ```
78
79 will turn `date` to a string using:
80
81 ```ocaml
82 fun x -> Core.Date.to_string_american x
83 ```
84
85 Subformats disallowed
86 ---------------------
87
88 In a regular format string, you can use format specifiers of the form
89 `%{<spec>%}` and `%(<spec>%)` where `<spec>` is another format
90 specifier.
91
92 Using these specifiers is disallowed in format strings that are
93 processed with custom-printf.