All Projects → bloomberg → ppx_string_interpolation

bloomberg / ppx_string_interpolation

Licence: Apache-2.0 license
PPX rewriter that enables string interpolation in OCaml

Programming Languages

ocaml
1615 projects

Projects that are alternatives of or similar to ppx string interpolation

bs-getenv
ReasonML/BuckleScript PPX for embedding env variables
Stars: ✭ 25 (-26.47%)
Mutual labels:  ppx, ppx-rewriter
awesome-ppx-reasonml
curated list of reasonml PPX rewriter
Stars: ✭ 28 (-17.65%)
Mutual labels:  ppx, ppx-rewriter
react-rules-of-hooks-ppx
This ppx validates the rules of React hooks.
Stars: ✭ 40 (+17.65%)
Mutual labels:  ppx
tensority
Strongly typed multidimensional array library for OCaml
Stars: ✭ 44 (+29.41%)
Mutual labels:  ppx
proxifier-rules
Rules for proxifiers programs: Proxifier
Stars: ✭ 51 (+50%)
Mutual labels:  ppx
jsoo-react
js_of_ocaml bindings for ReactJS. Based on ReasonReact.
Stars: ✭ 126 (+270.59%)
Mutual labels:  ppx
rescript-logger
Logging implementation for ReScript
Stars: ✭ 103 (+202.94%)
Mutual labels:  ppx
hello-ppx-esy
A simple example of a ppx with Reason and esy
Stars: ✭ 38 (+11.76%)
Mutual labels:  ppx
ppx regexp
Matching Regular Expressions with OCaml Patterns
Stars: ✭ 51 (+50%)
Mutual labels:  ppx-rewriter

PPX rewriter that enables string interpolation.

Menu

Rationale

This PPX implements string interpolation with embedded expressions, thus enabling simple and powerful templates for code generation and pretty printing. In contrast to printf, string interpolation does not separate embedded variables and context, which is important for medium-size strings (several lines).

Quick Start

Plugin uses bash-style syntax for string interpolation and the type of variables and expressions is assumed to be string by default:

let name = "world" in
    [%string "Hello $name!"]

Embedded expressions should be enclosed in parentheses:

let hello = "Hello" and world = "world" in
    [%string "$(hello ^ \" \" ^ world)!"]

Non-string types

Since this PPX run before type inference and type checking, rewriter should have the information about the types of embedded values via Printf.printf format specifiers:

let a = 1 and b = 1.0 in
    [%string {|We know, that %d$a == %f$b is %b$(a = int_of_float b)!|}]

Advanced usage

The extension is essentially syntax sugar for the Printf.sprintf function. The format is assumed to be anything between % and the next $. The expression string is determined by searching for balanced parentheses.

Both $ and % symbols can be escaped, i.e. double % and double $ result in single characters:

[%string "This is $$ and %%!"] = "This is $ and %!"

If the expression is commented out, the PPX dumps the comment with format, without checking for the type of the format:

[%string "The first pythagorean triple: %d$(3), 4, %d$(*5?*)"] =
    "The first pythagorean triple: 3, 4, %d$(*5?*)"

Building

To build the project use dune build.

Installation

To install the package run dune install.

Contributions

We ❤️ contributions.

Have you had a good experience with this project? Why not share some love and contribute code, or just let us know about any issues you had with it?

We welcome issue reports here; be sure to choose the proper issue template for your issue, so that we can be sure you're providing the necessary information.

Before sending a Pull Request, please make sure you read our Contribution Guidelines.

License

Please read the LICENSE file.

Code of Conduct

This project has adopted a Code of Conduct. If you have any concerns about the Code, or behavior which you have experienced in the project, please contact us at [email protected].

Security Vulnerability Reporting

If you believe you have identified a security vulnerability in this project, please send email to the project team at [email protected], detailing the suspected issue and any methods you've found to reproduce it.

Please do NOT open an issue in the GitHub repository, as we'd prefer to keep vulnerability reports private until we've had an opportunity to review and address them.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].