# Nested multi-line string literals

**URL:** <https://forums.swift.org/t/nested-multi-line-string-literals/6032>\
**Category:** Compiler\
**Created:** [May 31, 2017, 8:59pm UTC](https://forums.swift.org/t/nested-multi-line-string-literals/6032 "2017-05-31T20:59:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_Hawes](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nathan_hawes/32/274_2.png) [@Nathan\_Hawes](https://forums.swift.org/u/Nathan_Hawes)\
**Post date:** [May 31, 2017, 8:59pm UTC](https://forums.swift.org/t/nested-multi-line-string-literals/6032/1 "2017-05-31T20:59:39Z")

</div>

Hey all,

I’ve been playing with multi-line string literals recently and was wondering, are the below cases supported intentionally or did they just fall out of the implementation?

1) Nested multi-line strings:

let x = """  
outer multi-line  
\(  
&nbsp;&nbsp;"""  
&nbsp;&nbsp;&nbsp;&nbsp;inner multiline  
&nbsp;&nbsp;"""  
)  
outer multi-line  
"""  
print(x)

2) Multi-line strings nested in "single-line" strings:

let x = "outer string \( """  
&nbsp;&nbsp;&nbsp;&nbsp;inner multiline  
&nbsp;&nbsp;""") outer string"  
print(x)

I’ve been looking at still syntax highlighting strings in the invalid states they pass through while writing or editing them, and trying to keep any changes to the file’s highlighted ranges as localized as possible. At the moment when you open an interpolation in an otherwise terminated multi-line string literal you get one giant unknown token – an unterminated string – from the opening triple quotes, past the ‘closing’ triple quotes (that we treat as nested opening quotes), to the end of the file.

It’d be great to be able to bound the unknown token to what were more likely intended to be closing quotes, so we still produce tokens for (and syntax highlight) the rest of the file. Of course with nesting you can’t be sure they’re closing quotes though, so I wanted to check if the nesting support was intentional.

Thanks!  
Nathan

---

<div class="post-metadata">

**Author:** ![jpsim](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jpsim/32/3390_2.png) [@jpsim](https://forums.swift.org/u/jpsim)\
**Post date:** [May 31, 2017, 9:08pm UTC](https://forums.swift.org/t/nested-multi-line-string-literals/6032/2 "2017-05-31T21:08:07Z")

</div>

Looks like both those cases are supported based on similar tests here:

> <https://github.com/apple/swift/blob/main/test/Parse/multiline_string.swift>

Should be available in snapshots  
`swift-4.0-DEVELOPMENT-SNAPSHOT-2017-05-09-a` onwards.

> **···**
>
> On Wed, 31 May 2017 at 13:59 Nathan Hawes via swift-dev \<swift-dev@swift.org\> wrote:
> 
> > Hey all,
> > 
> > I’ve been playing with multi-line string literals recently and was  
> > wondering, are the below cases supported intentionally or did they just  
> > fall out of the implementation?
> > 
> > 1) Nested multi-line strings:
> > 
> > let x = """  
> > outer multi-line  
> > \(  
> > &nbsp;&nbsp;"""  
> > &nbsp;&nbsp;&nbsp;&nbsp;inner multiline  
> > &nbsp;&nbsp;"""  
> > )  
> > outer multi-line  
> > """  
> > print(x)
> > 
> > 2) Multi-line strings nested in "single-line" strings:
> > 
> > let x = "outer string \( """  
> > &nbsp;&nbsp;&nbsp;&nbsp;inner multiline  
> > &nbsp;&nbsp;""") outer string"  
> > print(x)
> > 
> > I’ve been looking at still syntax highlighting strings in the invalid  
> > states they pass through while writing or editing them, and trying to keep  
> > any changes to the file’s highlighted ranges as localized as possible. At  
> > the moment when you open an interpolation in an otherwise terminated  
> > multi-line string literal you get one giant unknown token – an  
> > unterminated string – from the opening triple quotes, past the ‘closing’  
> > triple quotes (that we treat as nested opening quotes), to the end of the  
> > file.
> > 
> > It’d be great to be able to bound the unknown token to what were more  
> > likely intended to be closing quotes, so we still produce tokens for (and  
> > syntax highlight) the rest of the file. Of course with nesting you can’t be  
> > sure they’re closing quotes though, so I wanted to check if the nesting  
> > support was intentional.
> > 
> > Thanks!  
> > Nathan  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-dev mailing list  
> > swift-dev@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-dev](https://lists.swift.org/mailman/listinfo/swift-dev)
