Home > News > “heredoc” multi-line strings

“heredoc” multi-line strings

February 24th, 2012

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?

News ,

  1. none
    February 24th, 2012 at 09:52 | #1

    autohotkey has the options LTRIM and JOIN

    http://www.autohotkey.com/docs/Scripts.htm#continuation

  2. February 24th, 2012 at 10:14 | #2

    Aligned heredoc:

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

    From the document you linked to :)

  3. February 24th, 2012 at 10:15 | #3

    @gabr

    Well, they were aligned before I posted the comment :(

    Check the Python section of the Here_document link.

  4. February 24th, 2012 at 10:16 | #4

    @gabr

    Pardon me, not Python but CoffeScript.

  5. February 24th, 2012 at 10:19 | #5

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

  6. February 24th, 2012 at 10:24 | #6

    @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’ ?

  7. February 24th, 2012 at 10:25 | #7

    @Eric
    If I’m guessing correctly what you typed before your whitespace was mangled, then yes :)

  8. February 24th, 2012 at 10:28 | #8

    @gabr
    Bloody HTML ;-) I’ve edited my and your snippet to unmangle things. Looking better?

  9. February 24th, 2012 at 10:47 | #9

    @Eric
    Much better, thanks!

  10. February 24th, 2012 at 11:23 | #10

    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)
    ]]
  11. February 24th, 2012 at 12:19 | #11

    @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.
         ""
  12. February 24th, 2012 at 13:10 | #12

    @gabr
    Of course, formatting was again broken for the last example.

  13. February 24th, 2012 at 13:25 | #13

    @gabr
    Updated, I’ve also installed a plugin which allows placing code in <pre>, formatting should be preserved!

  14. February 24th, 2012 at 15:49 | #14

    @Eric
    Thanks again, Eric!

  15. February 25th, 2012 at 03:51 | #15

    I love gabr’s last examples, I would use them a lot in queries!! what do you think Eric?

    testing “pre”
    tag
    can
    remove
    this

  16. February 25th, 2012 at 03:52 | #16

    ummm, my formatting wasn’t preserved for some reasons, I had a lot of spaces, should I have used “ ” in stead of ” ” <- space?

  17. February 25th, 2012 at 22:57 | #17

    @Dorin Duminica
    You should use inferior & superior signs

    no indent
       < - 3 spaces
          six spaces
Comments are closed.