From 8eaa53d57c8d3346e03ccde7b7d4c4c8103d9253 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Thu, 16 Jun 2016 00:43:51 -0700 Subject: Avoid triple-quoting triple quotes The boolean test for triples was inadvertently flipped. Adds test for pretty printed multiline strings --- test/junit/scala/reflect/internal/PrintersTest.scala | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'test') diff --git a/test/junit/scala/reflect/internal/PrintersTest.scala b/test/junit/scala/reflect/internal/PrintersTest.scala index 2305e7ea50..916f21adc8 100644 --- a/test/junit/scala/reflect/internal/PrintersTest.scala +++ b/test/junit/scala/reflect/internal/PrintersTest.scala @@ -79,6 +79,14 @@ class BasePrintTest { @Test def testConstantLong = assertTreeCode(Literal(Constant(42l)))("42L") + @Test def testConstantMultiline = assertTreeCode(Literal(Constant("hello\nworld")))("\"\"\"hello\nworld\"\"\"") + + val sq = "\"" + val teq = "\\\"" * 3 + val tq = "\"" * 3 + + @Test def testConstantEmbeddedTriple = assertTreeCode(Literal(Constant(s"${tq}hello${tq}\nworld")))(s"${sq}${teq}hello${teq}\\nworld${sq}") + @Test def testOpExpr = assertPrintedCode("(5).+(4)", checkTypedTree = false) @Test def testName1 = assertPrintedCode("class test") -- cgit v1.2.3 From 14d3b9e7062662ae55cca02ad653a68aa6aef78d Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Thu, 16 Jun 2016 09:52:20 -0700 Subject: Refactor triple quote quoting To quote a triple quote, only quote one quote. Refactors the code for legibility. Adds test for other inline cruft like control chars. --- src/reflect/scala/reflect/internal/Printers.scala | 30 +++++++++++----------- .../scala/reflect/internal/PrintersTest.scala | 12 ++++++--- 2 files changed, 23 insertions(+), 19 deletions(-) (limited to 'test') diff --git a/src/reflect/scala/reflect/internal/Printers.scala b/src/reflect/scala/reflect/internal/Printers.scala index 4ad34ff8c7..9602a2859b 100644 --- a/src/reflect/scala/reflect/internal/Printers.scala +++ b/src/reflect/scala/reflect/internal/Printers.scala @@ -1045,23 +1045,23 @@ trait Printers extends api.Printers { self: SymbolTable => print("") } - case l @ Literal(x) => - import Chars.LF - x match { - case Constant(v: String) if { - val strValue = x.stringValue - strValue.contains(LF) && !strValue.contains("\"\"\"") && strValue.size > 1 - } => - val splitValue = x.stringValue.split(s"$LF").toList - val multilineStringValue = if (x.stringValue.endsWith(s"$LF")) splitValue :+ "" else splitValue - val trQuotes = "\"\"\"" - print(trQuotes); printSeq(multilineStringValue) { print(_) } { print(LF) }; print(trQuotes) - case _ => - // processing Float constants - val printValue = x.escapedStringValue + (if (x.value.isInstanceOf[Float]) "F" else "") - print(printValue) + case Literal(k @ Constant(s: String)) if s.contains(Chars.LF) => + val tq = "\"" * 3 + val lines = s.lines.toList + if (lines.lengthCompare(1) <= 0) print(k.escapedStringValue) + else { + val tqp = """["]{3}""".r + val tqq = """""\\"""" // ""\" is triple-quote quoted + print(tq) + printSeq(lines.map(x => tqp.replaceAllIn(x, tqq)))(print(_))(print(Chars.LF)) + print(tq) } + case Literal(x) => + // processing Float constants + val suffix = x.value match { case _: Float => "F" case _ => "" } + print(s"${x.escapedStringValue}${suffix}") + case an @ Annotated(ap, tree) => val printParentheses = needsParentheses(tree)() parenthesize(printParentheses) { print(tree) }; print(if (tree.isType) " " else ": ") diff --git a/test/junit/scala/reflect/internal/PrintersTest.scala b/test/junit/scala/reflect/internal/PrintersTest.scala index 916f21adc8..38fe205af7 100644 --- a/test/junit/scala/reflect/internal/PrintersTest.scala +++ b/test/junit/scala/reflect/internal/PrintersTest.scala @@ -79,13 +79,17 @@ class BasePrintTest { @Test def testConstantLong = assertTreeCode(Literal(Constant(42l)))("42L") - @Test def testConstantMultiline = assertTreeCode(Literal(Constant("hello\nworld")))("\"\"\"hello\nworld\"\"\"") - val sq = "\"" - val teq = "\\\"" * 3 val tq = "\"" * 3 + val teq = "\"\"\\\"" + + @Test def testConstantMultiline = assertTreeCode(Literal(Constant("hello\nworld")))(s"${tq}hello\nworld${tq}") + + @Test def testConstantFormfeed = assertTreeCode(Literal(Constant("hello\fworld")))(s"${sq}hello\\fworld${sq}") + + @Test def testConstantControl = assertTreeCode(Literal(Constant("hello\u0003world")))(s"${sq}hello\\03world${sq}") - @Test def testConstantEmbeddedTriple = assertTreeCode(Literal(Constant(s"${tq}hello${tq}\nworld")))(s"${sq}${teq}hello${teq}\\nworld${sq}") + @Test def testConstantEmbeddedTriple = assertTreeCode(Literal(Constant(s"${tq}hello${tq}\nworld")))(s"${tq}${teq}hello${teq}\nworld${tq}") @Test def testOpExpr = assertPrintedCode("(5).+(4)", checkTypedTree = false) -- cgit v1.2.3 From 0eac3cb85dd4720b015a0d060691f68b63032b85 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Thu, 16 Jun 2016 10:15:56 -0700 Subject: Constant print control in unicode Since octal escape is deprecated, use unicode escape for string representation of constants. --- src/reflect/scala/reflect/internal/Constants.scala | 3 +-- test/files/run/reflection-java-annotations.check | 5 ++--- test/files/run/reflection-java-annotations/Test_2.scala | 4 ++-- test/junit/scala/reflect/internal/PrintersTest.scala | 6 +++++- 4 files changed, 10 insertions(+), 8 deletions(-) (limited to 'test') diff --git a/src/reflect/scala/reflect/internal/Constants.scala b/src/reflect/scala/reflect/internal/Constants.scala index 85d0efdcba..7b47798ff7 100644 --- a/src/reflect/scala/reflect/internal/Constants.scala +++ b/src/reflect/scala/reflect/internal/Constants.scala @@ -7,7 +7,6 @@ package scala package reflect package internal -import java.lang.Integer.toOctalString import scala.annotation.switch trait Constants extends api.Constants { @@ -212,7 +211,7 @@ trait Constants extends api.Constants { case '"' => "\\\"" case '\'' => "\\\'" case '\\' => "\\\\" - case _ => if (ch.isControl) "\\0" + toOctalString(ch.toInt) else String.valueOf(ch) + case _ => if (ch.isControl) "\\u%04X".format(ch.toInt) else String.valueOf(ch) } def escapedStringValue: String = { diff --git a/test/files/run/reflection-java-annotations.check b/test/files/run/reflection-java-annotations.check index 67317d2072..4c20727ea8 100644 --- a/test/files/run/reflection-java-annotations.check +++ b/test/files/run/reflection-java-annotations.check @@ -1,4 +1,3 @@ -warning: there was one deprecation warning (since 2.11.0); re-run with -deprecation for details -List(JavaComplexAnnotation_1(v1 = 1, v10 = "hello", v101 = [101, 101], v102 = [102, 102], v103 = ['g', 'g'], v104 = [104, 104], v105 = [105L, 105L], v106 = [106.0, 106.0], v107 = [107.0, 107.0], v108 = [false, true], v11 = classOf[JavaAnnottee_1], v110 = ["hello", "world"], v111 = [classOf[JavaSimpleAnnotation_1], classOf[JavaComplexAnnotation_1]], v112 = [FOO, BAR], v113 = [JavaSimpleAnnotation_1(v1 = 21, v10 = "world2", v11 = classOf[JavaComplexAnnotation_1], v12 = BAR, v2 = 22, v3 = '\027', v4 = 24, v5 = 25L, v6 = 26.0, v7 = 27.0, v8 = false)], v12 = FOO, v13 = JavaSimpleAnnotation_1(v1 = 11, v10 = "world1", v11 = classOf[JavaSimpleAnnotation_1], v12 = FOO, v2 = 12, v3 = '\r', v4 = 14, v5 = 15L, v6 = 16.0, v7 = 17.0, v8 = false), v2 = 2, v3 = '\03', v4 = 4, v5 = 5L, v6 = 6.0, v7 = 7.0, v8 = false)) +List(JavaComplexAnnotation_1(v1 = 1, v10 = "hello", v101 = [101, 101], v102 = [102, 102], v103 = ['g', 'g'], v104 = [104, 104], v105 = [105L, 105L], v106 = [106.0, 106.0], v107 = [107.0, 107.0], v108 = [false, true], v11 = classOf[JavaAnnottee_1], v110 = ["hello", "world"], v111 = [classOf[JavaSimpleAnnotation_1], classOf[JavaComplexAnnotation_1]], v112 = [FOO, BAR], v113 = [JavaSimpleAnnotation_1(v1 = 21, v10 = "world2", v11 = classOf[JavaComplexAnnotation_1], v12 = BAR, v2 = 22, v3 = '\u0017', v4 = 24, v5 = 25L, v6 = 26.0, v7 = 27.0, v8 = false)], v12 = FOO, v13 = JavaSimpleAnnotation_1(v1 = 11, v10 = "world1", v11 = classOf[JavaSimpleAnnotation_1], v12 = FOO, v2 = 12, v3 = '\r', v4 = 14, v5 = 15L, v6 = 16.0, v7 = 17.0, v8 = false), v2 = 2, v3 = '\u0003', v4 = 4, v5 = 5L, v6 = 6.0, v7 = 7.0, v8 = false)) ======= -new JavaComplexAnnotation_1(v1 = 1, v10 = "hello", v101 = Array(101, 101), v102 = Array(102, 102), v103 = Array('g', 'g'), v104 = Array(104, 104), v105 = Array(105L, 105L), v106 = Array(106.0, 106.0), v107 = Array(107.0, 107.0), v108 = Array(false, true), v11 = classOf[JavaAnnottee_1], v110 = Array("hello", "world"), v111 = Array(classOf[JavaSimpleAnnotation_1], classOf[JavaComplexAnnotation_1]), v112 = Array(FOO, BAR), v113 = Array(new JavaSimpleAnnotation_1(v1 = 21, v10 = "world2", v11 = classOf[JavaComplexAnnotation_1], v12 = BAR, v2 = 22, v3 = '\027', v4 = 24, v5 = 25L, v6 = 26.0, v7 = 27.0, v8 = false)), v12 = FOO, v13 = new JavaSimpleAnnotation_1(v1 = 11, v10 = "world1", v11 = classOf[JavaSimpleAnnotation_1], v12 = FOO, v2 = 12, v3 = '\r', v4 = 14, v5 = 15L, v6 = 16.0, v7 = 17.0, v8 = false), v2 = 2, v3 = '\03', v4 = 4, v5 = 5L, v6 = 6.0, v7 = 7.0, v8 = false) +new JavaComplexAnnotation_1(v1 = 1, v10 = "hello", v101 = Array(101, 101), v102 = Array(102, 102), v103 = Array('g', 'g'), v104 = Array(104, 104), v105 = Array(105L, 105L), v106 = Array(106.0, 106.0), v107 = Array(107.0, 107.0), v108 = Array(false, true), v11 = classOf[JavaAnnottee_1], v110 = Array("hello", "world"), v111 = Array(classOf[JavaSimpleAnnotation_1], classOf[JavaComplexAnnotation_1]), v112 = Array(FOO, BAR), v113 = Array(new JavaSimpleAnnotation_1(v1 = 21, v10 = "world2", v11 = classOf[JavaComplexAnnotation_1], v12 = BAR, v2 = 22, v3 = '\u0017', v4 = 24, v5 = 25L, v6 = 26.0, v7 = 27.0, v8 = false)), v12 = FOO, v13 = new JavaSimpleAnnotation_1(v1 = 11, v10 = "world1", v11 = classOf[JavaSimpleAnnotation_1], v12 = FOO, v2 = 12, v3 = '\r', v4 = 14, v5 = 15L, v6 = 16.0, v7 = 17.0, v8 = false), v2 = 2, v3 = '\u0003', v4 = 4, v5 = 5L, v6 = 6.0, v7 = 7.0, v8 = false) diff --git a/test/files/run/reflection-java-annotations/Test_2.scala b/test/files/run/reflection-java-annotations/Test_2.scala index dec5b45ca7..6d457ebe64 100644 --- a/test/files/run/reflection-java-annotations/Test_2.scala +++ b/test/files/run/reflection-java-annotations/Test_2.scala @@ -2,8 +2,8 @@ object Test extends App { import scala.reflect.runtime.universe._ val sym = typeOf[JavaAnnottee_1].typeSymbol sym.info - sym.annotations foreach (_.javaArgs) + sym.annotations foreach (_.tree.children.tail) println(sym.annotations) println("=======") sym.annotations.map(_.tree).map(println) -} \ No newline at end of file +} diff --git a/test/junit/scala/reflect/internal/PrintersTest.scala b/test/junit/scala/reflect/internal/PrintersTest.scala index 38fe205af7..d581ca8cf4 100644 --- a/test/junit/scala/reflect/internal/PrintersTest.scala +++ b/test/junit/scala/reflect/internal/PrintersTest.scala @@ -87,7 +87,11 @@ class BasePrintTest { @Test def testConstantFormfeed = assertTreeCode(Literal(Constant("hello\fworld")))(s"${sq}hello\\fworld${sq}") - @Test def testConstantControl = assertTreeCode(Literal(Constant("hello\u0003world")))(s"${sq}hello\\03world${sq}") + @Test def testConstantControl = assertTreeCode(Literal(Constant("hello\u0003world")))(s"${sq}hello\\u0003world${sq}") + + @Test def testConstantFormfeedChar = assertTreeCode(Literal(Constant('\f')))("'\\f'") + + @Test def testConstantControlChar = assertTreeCode(Literal(Constant(3.toChar)))("'\\u0003'") @Test def testConstantEmbeddedTriple = assertTreeCode(Literal(Constant(s"${tq}hello${tq}\nworld")))(s"${tq}${teq}hello${teq}\nworld${tq}") -- cgit v1.2.3