Skip to content

Commit

Permalink
Tweak java getlitch not to skip zero (scala#18491)
Browse files Browse the repository at this point in the history
The "skip zero" behavior was to accommodate escaped EOL in a text block,
but also skips zero-valued escaped char `\0`.

This fix is less precious and uses a flag called `skip`.

Scala 2 previously received the complementary fix, because it wasn't
preserving the char before the escaped EOL (after backporting the dotty
code):
scala/scala#10024
  • Loading branch information
jchyb authored Sep 11, 2023
2 parents 64c3138 + 5f29c21 commit 1be790c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/parsing/JavaScanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ object JavaScanners {
}
oct.asInstanceOf[Char]
end octal
var skip = false
def greatEscape: Char =
nextChar()
if '0' <= ch && ch <= '7' then octal
Expand All @@ -455,11 +456,12 @@ object JavaScanners {
case '\\' => '\\'
case CR | LF if inTextBlock =>
if !scanOnly then nextChar()
skip = true
0
case _ =>
if !scanOnly then error("invalid escape character", charOffset - 1)
ch
if x != 0 then nextChar()
if !skip then nextChar()
x
end greatEscape

Expand All @@ -470,7 +472,7 @@ object JavaScanners {
val res = ch
nextChar()
res
if c != 0 && !scanOnly then putChar(c)
if !skip && !scanOnly then putChar(c)
end getlitch

/** Read a triple-quote delimited text block, starting after the first three double quotes.
Expand Down
8 changes: 8 additions & 0 deletions tests/run/t12290.check
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,11 @@ XY
====
X Y
====
582059
====
00
====
2a
====
c3bf
====
12 changes: 12 additions & 0 deletions tests/run/t12290/Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,16 @@ object Test extends App {
println("====")
println(valueOf[TextBlocks.Octal.type])
println("====")
println(hexdump(valueOf[TextBlocks.Octal.type]))
println("====")
println(hexdump(valueOf[TextBlocks.Zero.type].toString))
println("====")
println(hexdump(valueOf[TextBlocks.Magic.type].toString))
println("====")
println(hexdump(valueOf[TextBlocks.Maxie.type].toString))
println("====")
}

def hexdump(s: String) = s.getBytes(io.Codec.UTF8.charSet) // java.nio.charset.StandardCharsets.UTF_8
.map(b => f"${b & 0xff}%02x")
.mkString
3 changes: 3 additions & 0 deletions tests/run/t12290/TextBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,7 @@ class TextBlocks {
""";

final static String Octal = "X\040Y";
final static char Zero = '\0';
final static char Magic = '\52';
final static char Maxie = '\377';
}

0 comments on commit 1be790c

Please sign in to comment.