Is it possible to capture unescaped text with ppxlib?

By “unescaped” I mean something like:

a /\ b =>
c

Note that the newline is literally newline not “\n” and the backslash is not escaped. Is it possible to write this in a ppx expansion and capture the string literal from it?

The payload type declared in parsetree.mli does not seem to allow it. I guess one needs to work with the lexer if one wants the raw text quotation instead of working with the parsetree.

  1. I think It’s not easy to define infix operator (/\)
  2. You could try to parse this expression as it is and after that try to reconstruct it back using locations of every identifier in the expression.

If you just want the string itself, can’t you just use the string literal syntax:

let x = {|a /\ b =>
  c
 |}

If you instead are trying to get a raw string to use within a ppx rewriter, you can reuse the string literal syntax no?:

let x = {%my_ppx|a /\ b =>
  c
 |}
1 Like

I need to write a ppx rewriter. I didn’t come across any reference on this. Thanks! I will give it a try:

The quoted string syntax for extension node ({%ext|...|} and {%%ext|...|}) is described in the extension node section of the manual.

2 Likes

I was able to get this to work

let t = [%q{|a /\ b|}];;

in utop, but utop didn’t accept the shorthand version.

Cool! Thanks!

1 Like

The shorthand version was only added in 4.11.0, this is probably the reason it is not working in utop for you.

1 Like

That must be it. I am still on 4.10.2.