Giter Club home page Giter Club logo

ppx_string_interpolation's Introduction

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).

We tried our best to be compatible with future Compiler versions, so please do not be surprised if the package is not updated for a couple years — it works. Please feel free to file an ISSUE if the Compiler update breaks this PPX.

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.

ppx_string_interpolation's People

Contributors

jobjo avatar kit-ty-kate avatar kpfleming avatar ksromanov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ppx_string_interpolation's Issues

option to trim leading spaces

Is your feature request related to a problem? Please describe.

When leading spaces are significant, the text must be fully justified to the left of the file to avoid including them:

   let ssdp_message =
      [%string
        {|M-SEARCH * HTTP/1.1
HOST:$group
MAN:ssdp:discover
MX:10
ST:urn:smartspeaker-audio:service:SpeakerGroup:1
|}]

Describe the solution you'd like

A separate extension or a special character (as the pipe character in multiline strings in scala) could be used to indicate the leading space should be removed

   let ssdp_message =
      [%string.trimmed
        {|M-SEARCH * HTTP/1.1
          HOST:$group
          MAN:ssdp:discover
          MX:10
          ST:urn:smartspeaker-audio:service:SpeakerGroup:1
        |}]

or

   let ssdp_message =
      [%string
        {|M-SEARCH * HTTP/1.1
          |HOST:$group
          |MAN:ssdp:discover
          |MX:10
          |ST:urn:smartspeaker-audio:service:SpeakerGroup:1
        |}]

ppx_string_interpolation fails to compile with OCaml 4.12.0~alpha3

Describe the bug
The ppx_string_interpolation fails to compile with OCaml 4.12.0~alpha3 with the following error message:

# File "src/interpolation_emitter.ml", line 106, characters 35-65:
# 106 |   pexp_constant ~loc:Location.none (Pconst_string (joined, None))
#                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Error: The constructor Pconst_string expects 3 argument(s),
#        but is applied here to 2 argument(s)

To Reproduce

opam switch create 4.12.0~alpha3 --repositories=default,beta=git+https://github.com/ocaml/ocaml-beta-repository.git
opam repo add alpha git://github.com/kit-ty-kate/opam-alpha-repository.git
opam install ppx_string_interpolation

Expected behavior
opam installs the ppx_string_interpolation package.

Environment:

  • Operating System and Version: macOS 11.0.1 Big Sur

Additional context
Here is the full output:

$ opam install ppx_string_interpolation
The following actions will be performed:
  ∗ install ppx_string_interpolation 1.0.0

<><> Gathering sources ><><><><><><><><><><><><><><><><><><><><><><><><><><>  🐫
[ppx_string_interpolation.1.0.0] found in cache

<><> Processing actions <><><><><><><><><><><><><><><><><><><><><><><><><><>  🐫
[ERROR] The compilation of ppx_string_interpolation failed at "/Users/anton/.opam/opam-init/hooks/sandbox.sh build dune build -p ppx_string_interpolation -j 3".

#=== ERROR while compiling ppx_string_interpolation.1.0.0 =====================#
# context     2.0.7 | macos/x86_64 | ocaml-base-compiler.4.12.0~alpha3 | https://opam.ocaml.org#5e8d6622
# path        ~/.opam/4.12.0~alpha3/.opam-switch/build/ppx_string_interpolation.1.0.0
# command     ~/.opam/opam-init/hooks/sandbox.sh build dune build -p ppx_string_interpolation -j 3
# exit-code   1
# env-file    ~/.opam/log/ppx_string_interpolation-96416-1872ec.env
# output-file ~/.opam/log/ppx_string_interpolation-96416-1872ec.out
### output ###
# 2 | (using fmt 1.1)
# [...]
# Warning: Version 1.1 of integration with automatic formatters is not
# supported until version 1.7 of the dune language.
# Supported versions of this extension in version 1.5 of the dune language:
# - 1.0
#       ocamlc src/.ppx_string_interpolation.objs/byte/ppx_string_interpolation__Interpolation_emitter.{cmi,cmo,cmt} (exit 2)
# (cd _build/default && /Users/anton/.opam/4.12.0~alpha3/bin/ocamlc.opt -w -40 -g -bin-annot -I src/.ppx_string_interpolation.objs/byte -I /Users/anton/.opam/4.12.0~alpha3/lib/bytes -I /Users/anton/.opam/4.12.0~alpha3/lib/gen -I /Users/anton/.opam/4.12.0~alpha3/lib/ocaml-compiler-libs/common -I /Users/anton/.opam/4.12.0~alpha3/lib/ocaml-compiler-libs/shadow -I /Users/anton/.opam/4.12.0~alpha3/l[...]
# File "src/interpolation_emitter.ml", line 106, characters 35-65:
# 106 |   pexp_constant ~loc:Location.none (Pconst_string (joined, None))
#                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Error: The constructor Pconst_string expects 3 argument(s),
#        but is applied here to 2 argument(s)



<><> Error report <><><><><><><><><><><><><><><><><><><><><><><><><><><><><>  🐫
┌─ The following actions failed
│ λ build ppx_string_interpolation 1.0.0
└─
╶─ No changes have been performed

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.