[swift-evolution] multi-line string literals.

Ted F.A. van Gaalen tedvgiosdev at gmail.com
Mon Apr 25 13:47:13 CDT 2016


This could be a simple solution: 

Starting each line with a special token.

In the example here it is the \\  double-backslash .  
when the \\ appears in the first two columns of a source line, 
this tells the compiler that it is a data line and that more might follow.
the last line starting with \\ completes the data entry.

Here is an example of a string declaration with some XML
(no escape sequences needed for “) 
Of course it could be anything other kind of textual data as well.

let  str =  
\\<!DOCTYPE html>
\\<html>
\\<body>
\\
\\<h1>W3Schools Internal Note</h1>\n
\\<div>
\\<b>To:</b> <span id="to"></span><br>\n
\\<b>From:</b> <span id="from"></span><br>\n
\\<b>Message:</b> <span id="message"></span>
\\</div>
\\\n
\\<script>
\\var txt, parser, xmlDoc;
\\txt = "<note>" +
\\"<to>Tove</to>" +
\\etc. this is the last data line.


Conditions: 

- Every line starting with \\ in first and second column of the line 
  is  treated as a data line.
- All characters behind the \\ are regarded as data, thus note that:
     - the “ is not regarded as a string delimiter
     - the // chars and whatever follows it are interpreted as data on such a line, not as comment.
     -  \\  within the data itself are treated as data e.g. this line is valid:
\\There \\ are three backslashes (as data) in this line \\\\ today.
\\
the above data line is empty but is allowed. 

- Leading and embedded spaces are respected.
- Tabs, Linefeeds etc. can be inserted the usual way using \t \n etc. 
- trailing spaces and line terminators cr lf are ignored, filtered out.

let dutchNumbers =            
\\ een twee drie vier vijf

\\ zes zeven acht negen tien   
\\these two data lines are orphans,

Blank lines or other Swift statement lines in-between
breaks a set of \\ data lines 


All \\ lines together are treated as one single string literal
and may occur everywhere where “normal” string literals are allowed.

    // E.g. this if statement would be correct:
    // Yes, this would be legal but doesn’t look so great: Indentation not possible here
       if cars == 
\\Ford
\\ Delorean
\\ Chevrolet
      {
          doSomething()
      }

An array with 2 string elements: 

var ar =
[ 
\\sdkdslkdslkdsldkshfkjdljfsdljkfdshjklfd dioioioioio \n\nsljkf sdflkf dsl;dfsk sdlfk dfsfkds 
\\ sdkdfkdfldkfd fdfldk   fdlkfd jkfds  hjklfd dsljkf sdflkf dsl;dfsk sdlfk dfsfkds 
,
\\There are many ships in the ha
\\rbour that are soon sailing away.
]


I also thought about using “”  as token, but this would be interpreted as an empty string.
or “”” but this is an empty string followed by an unclosed string literal.

Should be relatively easy to implement? What y’all think?


Kind Regards
TedvG
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160425/637e908f/attachment.html>


More information about the swift-evolution mailing list