ocaml-community / cppo

C-style preprocessor for OCaml
BSD 3-Clause "New" or "Revised" License
107 stars 26 forks source link

Multiline strings are not accepted in `define` #81

Closed Lysxia closed 2 years ago

Lysxia commented 2 years ago

The docs say they should work. Minimal example:

#define x \
"a
"
Error: File "w.cppo", line 2, characters 2-3
Error: Unterminated string literal
liyishuai commented 2 years ago

First appearance of the documentation was in 2011: https://github.com/ocaml-community/cppo/blob/6ce5f4340baa4031fa9c98772bfdb28d0db0e529/README#L264-L273

However, the test suite for the same version requires backslash: https://github.com/ocaml-community/cppo/blob/6ce5f4340baa4031fa9c98772bfdb28d0db0e529/test.cppo#L46-L51

Actually, backslash was required since the test suite was first written in 2009: https://github.com/ocaml-community/cppo/blob/433bf21f62ac7170b8ed7fbe597b2c2ce3cbe846/test.cppo#L39-L44

I'd read the documentation as outdated. @mjambon Could you confirm my understanding?

mjambon commented 2 years ago

It's likely that I wasn't careful about this issue when I wrote the code and the documentation at the time. My inclination today is indeed to allow and preserve all "lexical elements" as they exist in OCaml (string literals, comments, ...). This means multiline strings should use the same syntax as in OCaml. If a backslash is present at the end of a line within a string literal, it should not be removed by cppo. e.g.:

#define x "hello \
  world"
x

will be turned by cppo into

"hello \
  world"

not

"hello 
  world"

also not

"hello   world"