A reader extension for ‘raw strings’

lloda aa1cf783f2 Update CI to use guile-2.2 10 months ago
.travis.yml aa1cf783f2 Update CI to use guile-2.2 10 months ago
README.md 6a74ba6b63 Add .travis.yml 2 years ago
raw-strings.scm 6a74ba6b63 Add .travis.yml 2 years ago
test.scm a1c3ed0ab0 Allow [] or "" as open-close pairs in addition to () 2 years ago


(travis build status)

The file raw-strings.scm contains a reader extension that lets you write verbatim strings such as

#R-(quotes " and escapes \ and newlines
           can " freely be used " here)-

where you'd normally need escapes:

"quotes \" and escapes \\ and newlines\n           can \" freely be used \" here"

This comes in handy for docstrings, regexps, etc.

The string between the #R and the ( is the 'delimiter'. The string ends with a ) followed by the delimiter. In the example above, the delimiter is -. The delimiter can be empty, as in #R(put your '\"\\) for "put your '\\\"\\\\".

You can also use [] or "" instead of () to ‘delimit the delimiter’1. This means that you cannot use the characters [(" as part of the delimiter. Whitespace in the delimiter is forbidden.

  • #R(hello)hello
  • #R"hello"hello
  • #R[hello]hello
  • #Rdo-not-repeat(hello)do-not-repeathello

The open-close pair must be matched, but the delimiter must be repeated verbatim. It's probably better to stick to one style throughout.

  • #R("hello")"hello" —empty delimiter, open-close-pair is ().
  • #R"(hello)"(hello) —in an earlier version of this, "" wasn't an open-close pair, so this gave hello.
  • #R]"hello"]hello —here the delimiter is ] and the open-close pair is "".
  • #R["hello"]"hello" —here the delimiter is empty and the open-close pair is [].

To enable the extension, install raw-strings.scm in your module path and then (import (raw-strings)), or simply paste the code in raw-strings.scm in your ~/.guile.

Run the test with

guile -L . -s test.scm

I hope you find this useful.

¹ Check the comments in raw-strings.scm if you want to customize this list.