rustnewlinegrammarpegpest

Using Pest.rs how can I manage a multi-line syntax where a line ends in "\"?


A common idiom for bash is is to use \ to escape the newline at the end of the line,

If a \<newline> pair appears, and the backslash is not itself quoted, the \<newline> is treated as a line continuation (that is, it is removed from the input stream and effectively ignored).

Such that

FOO \
BAR

is the same as,

FOO BAR

How would I write this grammar into pest.rs? Note this means that NEWLINE is significant in my grammar, and I can't merely ignore it.


Solution

  • One method is to set your

    WHITESPACE = { ( " "* ~ "\\" ~ NEWLINE ~ " "* ) }
    

    This keeps regular newlines significant unless they're prefixed by \.