mirror of
https://github.com/apple/pkl.git
synced 2026-05-02 21:24:18 +02:00
move handling of strings to parser (#962)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
–– Pkl Error ––
|
||||
Unexpected end of file.
|
||||
Missing `"""` delimiter.
|
||||
|
||||
x | res2 = 42
|
||||
^
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
–– Pkl Error ––
|
||||
Unexpected end of file.
|
||||
Missing `"""` delimiter.
|
||||
|
||||
x | res1 = """
|
||||
^
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
–– Pkl Error ––
|
||||
Unexpected end of file.
|
||||
Missing `"` delimiter.
|
||||
|
||||
x | res1 = "
|
||||
^
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
–– Pkl Error ––
|
||||
Unexpected end of file.
|
||||
Missing `"""` delimiter.
|
||||
|
||||
x |
|
||||
^
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
–– Pkl Error ––
|
||||
Unexpected end of file.
|
||||
Missing `"""#` delimiter.
|
||||
|
||||
x |
|
||||
^
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
–– Pkl Error ––
|
||||
Invalid Unicode escape sequence `\u{12x}`.
|
||||
|
||||
Valid Unicode escape sequences are \u{0} to \u{10FFFF} (1-6 hexadecimal characters).
|
||||
|
||||
x | res1 = "\u{12x}"
|
||||
^^^^^^^
|
||||
at invalidUnicodeEscape#res1 (file:///$snippetsDir/input/errors/invalidUnicodeEscape.pkl)
|
||||
|
||||
Valid Unicode escape sequences are \u{0} to \u{10FFFF} (1-6 hexadecimal characters).
|
||||
at invalidUnicodeEscape (file:///$snippetsDir/input/errors/invalidUnicodeEscape.pkl)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
–– Pkl Error ––
|
||||
Unexpected end of file.
|
||||
Missing `"""` delimiter.
|
||||
|
||||
x | res1 = """some string
|
||||
^
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
–– Pkl Error ––
|
||||
Unexpected end of file.
|
||||
Missing `"""` delimiter.
|
||||
|
||||
x | res2 = 2
|
||||
^
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
–– Pkl Error ––
|
||||
Unexpected end of file.
|
||||
Missing `"""` delimiter.
|
||||
|
||||
x | res1 = """some string"
|
||||
^
|
||||
|
||||
@@ -3,4 +3,4 @@ Line must match or exceed indentation of the String's last line.
|
||||
|
||||
x | mismatched indent
|
||||
^^^^^^^^^^^^^^^^^
|
||||
at parser8#res1 (file:///$snippetsDir/input/errors/parser8.pkl)
|
||||
at parser8 (file:///$snippetsDir/input/errors/parser8.pkl)
|
||||
|
||||
@@ -3,4 +3,4 @@ Line must match or exceed indentation of the String's last line.
|
||||
|
||||
x | mismatched indent
|
||||
^^^^^^^^^^^^^^^^^
|
||||
at parser9#res1 (file:///$snippetsDir/input/errors/parser9.pkl)
|
||||
at parser9 (file:///$snippetsDir/input/errors/parser9.pkl)
|
||||
|
||||
@@ -791,7 +791,7 @@ class ANTLRSexpRenderer {
|
||||
|
||||
fun renderSingleLineStringExpr(expr: SingleLineStringLiteralContext) {
|
||||
buf.append(tab)
|
||||
buf.append("(interpolatedStringExpr")
|
||||
buf.append("(singleLineStringLiteralExpr")
|
||||
val oldTab = increaseTab()
|
||||
for (part in expr.singleLineStringPart()) {
|
||||
if (part.expr() != null) {
|
||||
@@ -808,15 +808,14 @@ class ANTLRSexpRenderer {
|
||||
|
||||
fun renderMultiLineStringExpr(expr: MultiLineStringLiteralContext) {
|
||||
buf.append(tab)
|
||||
buf.append("(interpolatedMultiStringExpr")
|
||||
buf.append("(multiLineStringLiteralExpr")
|
||||
val oldTab = increaseTab()
|
||||
// render only interpolated expressions because
|
||||
// the new parser parses string differently
|
||||
for (part in expr.multiLineStringPart()) {
|
||||
if (part.expr() != null) {
|
||||
buf.append('\n')
|
||||
renderExpr(part.expr())
|
||||
} else {
|
||||
buf.append('\n').append(tab)
|
||||
buf.append("(stringConstantExpr)")
|
||||
}
|
||||
}
|
||||
buf.append(')')
|
||||
|
||||
@@ -42,10 +42,18 @@ class ParserComparisonTest : ParserComparisonTestInterface {
|
||||
|
||||
compare(
|
||||
"""
|
||||
prop = ${"\"\"\""}\(bar)${"\"\"\""}
|
||||
prop2 = ${"\"\"\""}foo \(bar)${"\"\"\""}
|
||||
prop3 = ${"\"\"\""}\(bar) foo${"\"\"\""}
|
||||
prop4 = ${"\"\"\""}foo \(bar + baz) foo${"\"\"\""}
|
||||
prop = ""${'"'}
|
||||
\(bar)
|
||||
""${'"'}
|
||||
prop2 = ""${'"'}
|
||||
foo \(bar)
|
||||
""${'"'}
|
||||
prop3 = ""${'"'}
|
||||
\(bar) foo
|
||||
""${'"'}
|
||||
prop4 = ""${'"'}
|
||||
foo \(bar + baz) foo
|
||||
""${'"'}
|
||||
"""
|
||||
.trimIndent()
|
||||
)
|
||||
|
||||
@@ -419,7 +419,7 @@ class SexpRenderer {
|
||||
|
||||
fun renderSingleLineStringLiteral(expr: SingleLineStringLiteralExpr) {
|
||||
buf.append(tab)
|
||||
buf.append("(interpolatedStringExpr")
|
||||
buf.append("(singleLineStringLiteralExpr")
|
||||
val oldTab = increaseTab()
|
||||
for (part in expr.parts) {
|
||||
if (part is StringPart.StringInterpolation) {
|
||||
@@ -436,15 +436,14 @@ class SexpRenderer {
|
||||
|
||||
fun renderMultiLineStringLiteral(expr: MultiLineStringLiteralExpr) {
|
||||
buf.append(tab)
|
||||
buf.append("(interpolatedMultiStringExpr")
|
||||
buf.append("(multiLineStringLiteralExpr")
|
||||
val oldTab = increaseTab()
|
||||
// render only interpolated expressions because
|
||||
// the new parser parses string differently
|
||||
for (part in expr.parts) {
|
||||
if (part is StringPart.StringInterpolation) {
|
||||
buf.append('\n')
|
||||
renderExpr(part.expr)
|
||||
} else {
|
||||
buf.append('\n').append(tab)
|
||||
buf.append("(stringConstantExpr)")
|
||||
}
|
||||
}
|
||||
buf.append(')')
|
||||
|
||||
@@ -302,10 +302,11 @@ class SpanComparison(val path: String, private val softly: SoftAssertions) {
|
||||
}
|
||||
}
|
||||
is MultiLineStringLiteralExpr -> {
|
||||
node.parts.zip((ctx as MultiLineStringLiteralContext).multiLineStringPart()).forEach {
|
||||
(s1, s2) ->
|
||||
compareSpan(s1, s2)
|
||||
}
|
||||
// only compare interpolated expressions
|
||||
val exprs = node.parts.filterIsInstance<StringPart.StringInterpolation>()
|
||||
val antlrExprs =
|
||||
(ctx as MultiLineStringLiteralContext).multiLineStringPart().mapNotNull { it.expr() }
|
||||
exprs.zip(antlrExprs).forEach { (s1, s2) -> compareExpr(s1.expr, s2) }
|
||||
}
|
||||
is ThrowExpr -> compareExpr(node.expr, (ctx as ThrowExprContext).expr())
|
||||
is TraceExpr -> compareExpr(node.expr, (ctx as TraceExprContext).expr())
|
||||
|
||||
Reference in New Issue
Block a user