“heredoc” multi-line strings

DWScript now supports multi-line strings, aka “heredoc”, using the same syntax as Prism/Oxygene, which involves double-quoting them, for instance:

s := "Lorem ipsum 'dolor' sit amet, consectetur adipiscing elit.
Duis l'ipsum odio, pretium ""hendrerit"" varius sed,
aliquet vitae elit.
Sed eu libero nec nisl ""malesuada"" dignissim.";

is equivalent to

s := 'Lorem ipsum ''dolor'' sit amet, consectetur adipiscing elit.'#13#10
    +'Duis l''ipsum odio, pretium "hendrerit" varius sed,'#13#10
    +'aliquet vitae elit.'#13#10
    +'Sed eu libero nec nisl "malesuada" dignissim.';

Which can be quite useful when you have multi-line string content, such as an SQL query, a long format string, a snippet of CSS/HTML/XML, etc.

Nate that if you’re using The SynEdit, the DWS highlighter has been updated and properly highlights those, you just need to get it from the SynEdit SVN.

It a slight cosmetic issue, shared AFAICT by other languages that implement “heredoc”, which is that if you don’t want to have spurious whitespace/tabs in the multi-line string, you basically have to break indentation.

Does any one knows a language which has a convenient “heredoc” syntax that would alleviate this issue?

17 thoughts on ““heredoc” multi-line strings

  1. Aligned heredoc:

    s := """
         However, double-quoted heredocs *do* allow these.
         See it in action:
             Content: "#{myDoc}"
         """

    From the document you linked to 🙂

  2. Strictly speaking, this is not a heredoc (because it doesn’t have a user-settable end-of-doc token), but a multiline string.

  3. @gabr
    dang! had missed CoffeeScript snippet! Looks like they always strip the left whitespace up the the indentation, and require the new line.

    So single double-quote wouldn’t strip indentation (as in my snippet), but “double double-quote + newline” could preserve it. Looks quite good.

    s:="my
       string";

    would be ‘my’#13#10′   string’, while

    s:=""
       my
       string
       "";

    would be ‘my’#13#10’string’ ?

  4. Lua has nice feature to skip first CR/LF if multi line string starts with it:

    s = [[
    And then you can indent text as you do 
    in any text editor, from line start.
    
    (This string starts at And)
    ]]
  5. @dmajkic
    I believe that would be very nice extension. The only change in compiler logic is:
    – If a quote is followed only by whitespace and newline, ignore this and start collecting the text with the next line.

    Similar extension could be implemented for the terminating quote.

    Those five would be then equivalent:

    s := "First line.
    Second line."
    
    s := "
    First line.
    Second line."
    
    s := "First line.
    Second line.
    "
    
    s := "
    First line.
    Second line.
    "
    
    s := ""
         First line.
         Second line.
         ""

Comments are closed.