Skip to content

Commit e07abb7

Browse files
authored
SPICE-0028: Add support for multi-line string line continuations (#1507)
SPICE: apple/pkl-evolution#31
1 parent d85f06b commit e07abb7

24 files changed

Lines changed: 185 additions & 10 deletions

File tree

docs/modules/language-reference/pages/index.adoc

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,7 @@ String literals are enclosed in double quotes:
255255
"Hello, World!"
256256
----
257257

258-
TIP: Except for a few minor differences footnote:[Pkl's string literals have fewer character escape sequences,
259-
have stricter rules for line indentation in multiline strings, and do not have a line continuation character.],
258+
TIP: Except for a few minor differences footnote:[Pkl's string literals have fewer character escape sequences and stricter rules for line indentation in multiline strings.],
260259
String literals have the same syntax and semantics as in Swift 5. Learn one of them, know both of them!
261260

262261
Inside a string literal, the following character escape sequences have special meaning:
@@ -362,6 +361,23 @@ str = """
362361
"""
363362
----
364363

364+
To prevent line breaks from becoming part of the string's value, use a backslash (`\`) to end those lines.
365+
366+
[source%tested,{pkl}]
367+
----
368+
str = """
369+
Although the Dodo is extinct, \
370+
the species will be remembered.
371+
"""
372+
----
373+
374+
This multiline string is equivalent to the following single-line string:
375+
376+
[source%parsed,{pkl-expr}]
377+
----
378+
"Although the Dodo is extinct, the species will be remembered."
379+
----
380+
365381
[[custom-string-delimiters]]
366382
=== Custom String Delimiters
367383

pkl-core/src/main/java/org/pkl/core/util/SyntaxHighlighter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.pkl.parser.Token.FALSE;
1919
import static org.pkl.parser.Token.NULL;
2020
import static org.pkl.parser.Token.STRING_ESCAPE_BACKSLASH;
21+
import static org.pkl.parser.Token.STRING_ESCAPE_CONTINUATION;
2122
import static org.pkl.parser.Token.STRING_ESCAPE_NEWLINE;
2223
import static org.pkl.parser.Token.STRING_ESCAPE_QUOTE;
2324
import static org.pkl.parser.Token.STRING_ESCAPE_RETURN;
@@ -41,7 +42,8 @@ private SyntaxHighlighter() {}
4142
STRING_ESCAPE_RETURN,
4243
STRING_ESCAPE_QUOTE,
4344
STRING_ESCAPE_BACKSLASH,
44-
STRING_ESCAPE_UNICODE);
45+
STRING_ESCAPE_UNICODE,
46+
STRING_ESCAPE_CONTINUATION);
4547

4648
private static final EnumSet<Token> constant = EnumSet.of(TRUE, FALSE, NULL);
4749

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
amends "../snippetTest.pkl"
2+
3+
examples {
4+
["string continuation"] {
5+
"""
6+
hello \
7+
world
8+
"""
9+
10+
#"""
11+
hello \#
12+
world
13+
"""#
14+
15+
"""
16+
hello \
17+
\
18+
world
19+
"""
20+
}
21+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
res1 = "xxx\ xxx"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
foo = """
2+
hello \
3+
\
4+
world
5+
"""
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
foo = "hello \
2+
world"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
foo =
2+
"""
3+
hello \
4+
world
5+
"""
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
examples {
2+
["string continuation"] {
3+
"hello world"
4+
"hello world"
5+
"hello world"
6+
}
7+
}

pkl-core/src/test/files/LanguageSnippetTests/output/errors/invalidCharacterEscape.err

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
–– Pkl Error ––
22
Invalid character escape sequence `\a`.
33

4-
Valid character escape sequences are: \n \r \t \" \\
4+
Valid character escape sequences are: \n \r \t \" \\ \<newline>
55

66
x | res1 = "xxx\axxx"
77
^^
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
–– Pkl Error ––
2+
Invalid character escape sequence `\ `.
3+
4+
Valid character escape sequences are: \n \r \t \" \\ \<newline>
5+
6+
x | res1 = "xxx\ xxx"
7+
^^
8+
at invalidCharacterEscape2 (file:///$snippetsDir/input/errors/invalidCharacterEscape2.pkl)

0 commit comments

Comments
 (0)