From 4df81aab315e587d9c7e319c7a2ece0f0f6fbaf3 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Sat, 20 Dec 2014 02:28:44 -0800 Subject: SI-3368 CDATA gets a Node XML Parser uses `scala.xml.PCData`. A compiler flag `-Yxml:coalescing`, analogous to `DocumentBuilderFactory.setCoalescing`, turns `PCData` nodes into `Text` nodes and coalesces sibling text nodes. This change also fixes parse errors such as rejecting a sequence of CDATA sections. A sequence of "top level" nodes are not coalesced. ``` scala> startworldstuff res0: scala.xml.Elem = startworldstuff scala> :replay -Yxml:coalescing Replaying: startworldstuff res0: scala.xml.Elem = starthi & byeworldstuffred & black ``` --- test/files/pos/t3368.flags | 1 + test/files/pos/t3368.scala | 5 +++++ test/files/run/t3368.check | 46 ++++++++++++++++++++++++++++++++++++++++++++++ test/files/run/t3368.scala | 18 ++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 test/files/pos/t3368.flags create mode 100644 test/files/pos/t3368.scala create mode 100644 test/files/run/t3368.check create mode 100644 test/files/run/t3368.scala (limited to 'test') diff --git a/test/files/pos/t3368.flags b/test/files/pos/t3368.flags new file mode 100644 index 0000000000..cb20509902 --- /dev/null +++ b/test/files/pos/t3368.flags @@ -0,0 +1 @@ +-Ystop-after:parser diff --git a/test/files/pos/t3368.scala b/test/files/pos/t3368.scala new file mode 100644 index 0000000000..c8e861a899 --- /dev/null +++ b/test/files/pos/t3368.scala @@ -0,0 +1,5 @@ + +trait X { + // error: in XML literal: name expected, but char '!' cannot start a name + def x = +} diff --git a/test/files/run/t3368.check b/test/files/run/t3368.check new file mode 100644 index 0000000000..1d9dd677f6 --- /dev/null +++ b/test/files/run/t3368.check @@ -0,0 +1,46 @@ +[[syntax trees at end of parser]] // newSource1.scala +package { + abstract trait X extends scala.AnyRef { + def $init$() = { + () + }; + def x = { + val $buf = new _root_.scala.xml.NodeBuffer(); + $buf.$amp$plus(new _root_.scala.xml.PCData("hi & bye")); + $buf.$amp$plus(new _root_.scala.xml.PCData("red & black")); + $buf + } + }; + abstract trait Y extends scala.AnyRef { + def $init$() = { + () + }; + def y = { + { + new _root_.scala.xml.Elem(null, "a", _root_.scala.xml.Null, $scope, false, ({ + val $buf = new _root_.scala.xml.NodeBuffer(); + $buf.$amp$plus({ + { + new _root_.scala.xml.Elem(null, "b", _root_.scala.xml.Null, $scope, true) + } + }); + $buf.$amp$plus(new _root_.scala.xml.Text("starthi & bye")); + $buf.$amp$plus({ + { + new _root_.scala.xml.Elem(null, "c", _root_.scala.xml.Null, $scope, true) + } + }); + $buf.$amp$plus(new _root_.scala.xml.Text("world")); + $buf.$amp$plus({ + { + new _root_.scala.xml.Elem(null, "d", _root_.scala.xml.Null, $scope, true) + } + }); + $buf.$amp$plus(new _root_.scala.xml.Text("stuffred & black")); + $buf + }: _*)) + } + } + } +} + diff --git a/test/files/run/t3368.scala b/test/files/run/t3368.scala new file mode 100644 index 0000000000..aaf34b43fb --- /dev/null +++ b/test/files/run/t3368.scala @@ -0,0 +1,18 @@ + +import scala.tools.partest.ParserTest + + +object Test extends ParserTest { + + override def code = """ + trait X { + // error: in XML literal: name expected, but char '!' cannot start a name + def x = + } + trait Y { + def y = startworldstuff + } + """ + + override def extraSettings = s"${super.extraSettings} -Yxml:coalescing" +} -- cgit v1.2.3 From b60de05a2e1257d16b7876856e26feec7bb81870 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Sun, 21 Dec 2014 12:50:51 -0800 Subject: SI-5699 Use ParserTest Update the test slightly to use the rig it inspired. --- test/files/run/t5699.scala | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) (limited to 'test') diff --git a/test/files/run/t5699.scala b/test/files/run/t5699.scala index ec3b1d26b4..ede6fefe48 100755 --- a/test/files/run/t5699.scala +++ b/test/files/run/t5699.scala @@ -1,7 +1,7 @@ -import scala.tools.partest.DirectTest +import scala.tools.partest.ParserTest import scala.reflect.internal.util.BatchSourceFile -object Test extends DirectTest { +object Test extends ParserTest { // Java code override def code = """ |public @interface MyAnnotation { String value(); } @@ -9,14 +9,6 @@ object Test extends DirectTest { override def extraSettings: String = "-usejavacp -Ystop-after:typer -Xprint:parser" - override def show(): Unit = { - // redirect err to out, for logging - val prevErr = System.err - System.setErr(System.out) - compile() - System.setErr(prevErr) - } - override def newSources(sourceCodes: String*) = { assert(sourceCodes.size == 1) List(new BatchSourceFile("annodef.java", sourceCodes(0))) -- cgit v1.2.3 From 996d066bb3ef0fc91acab18a5502bb649929881a Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Sun, 8 Feb 2015 15:35:43 -0800 Subject: SI-3368 Promote xml option to -Xxml As long as Scala does XML literals, there is probably parsing behavior worth configuring. Therefore, the umbrella option is promoted to `-Xxml`. It was tempting to make it `-XML`, but we resisted. --- .../scala/tools/nsc/ast/parser/MarkupParsers.scala | 2 +- .../tools/nsc/ast/parser/SymbolicXMLBuilder.scala | 2 +- .../scala/tools/nsc/settings/ScalaSettings.scala | 24 +++++++++++----------- test/files/run/t3368.scala | 2 +- 4 files changed, 15 insertions(+), 15 deletions(-) (limited to 'test') diff --git a/src/compiler/scala/tools/nsc/ast/parser/MarkupParsers.scala b/src/compiler/scala/tools/nsc/ast/parser/MarkupParsers.scala index edee1e296d..52b8a51a79 100755 --- a/src/compiler/scala/tools/nsc/ast/parser/MarkupParsers.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/MarkupParsers.scala @@ -234,7 +234,7 @@ trait MarkupParsers { def content: Buffer[Tree] = { val ts = new ArrayBuffer[Tree] - val coalescing = settings.YxmlSettings.isCoalescing + val coalescing = settings.XxmlSettings.isCoalescing @tailrec def loopContent(): Unit = if (xEmbeddedBlock) { ts append xEmbeddedExpr diff --git a/src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala b/src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala index 90610ab2e6..99399e363f 100755 --- a/src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala @@ -115,7 +115,7 @@ abstract class SymbolicXMLBuilder(p: Parsers#Parser, preserveWS: Boolean) { final def entityRef(pos: Position, n: String) = atPos(pos)( New(_scala_xml_EntityRef, LL(const(n))) ) - private def coalescing = settings.YxmlSettings.isCoalescing + private def coalescing = settings.XxmlSettings.isCoalescing // create scala.xml.Text here <: scala.xml.Node final def text(pos: Position, txt: String): Tree = atPos(pos) { diff --git a/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala b/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala index e10b3bc259..b783e80db9 100644 --- a/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala +++ b/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala @@ -139,6 +139,18 @@ trait ScalaSettings extends AbsScalaSettings val XnoPatmatAnalysis = BooleanSetting ("-Xno-patmat-analysis", "Don't perform exhaustivity/unreachability analysis. Also, ignore @switch annotation.") val XfullLubs = BooleanSetting ("-Xfull-lubs", "Retains pre 2.10 behavior of less aggressive truncation of least upper bounds.") + // XML parsing options + object XxmlSettings extends MultiChoiceEnumeration { + val coalescing = Value + def isCoalescing = Xxml contains coalescing + } + val Xxml = MultiChoiceSetting( + name = "-Xxml", + helpArg = "property", + descr = "Configure XML parsing", + domain = XxmlSettings + ) + /** Compatibility stubs for options whose value name did * not previously match the option name. */ @@ -305,18 +317,6 @@ trait ScalaSettings extends AbsScalaSettings ) withPostSetHook { _ => scala.reflect.internal.util.Statistics.enabled = true } } - // XML parsing options (transitional in 2.11) - object YxmlSettings extends MultiChoiceEnumeration { - val coalescing = Value - def isCoalescing = Yxml contains coalescing - } - val Yxml = MultiChoiceSetting( - name = "-Yxml", - helpArg = "property", - descr = "Configure XML parsing", - domain = YxmlSettings - ) - def YstatisticsEnabled = Ystatistics.value.nonEmpty /** Area-specific debug output. diff --git a/test/files/run/t3368.scala b/test/files/run/t3368.scala index aaf34b43fb..15acba5099 100644 --- a/test/files/run/t3368.scala +++ b/test/files/run/t3368.scala @@ -14,5 +14,5 @@ object Test extends ParserTest { } """ - override def extraSettings = s"${super.extraSettings} -Yxml:coalescing" + override def extraSettings = s"${super.extraSettings} -Xxml:coalescing" } -- cgit v1.2.3 From ef7b5524a7d6a2da82c32f16632ed2304130f32a Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Wed, 8 Apr 2015 09:49:31 -0700 Subject: SI-3368 Review Verbose option help and test tweak. --- src/compiler/scala/tools/nsc/settings/ScalaSettings.scala | 2 +- test/files/run/t5699.scala | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala b/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala index b783e80db9..f217d21c35 100644 --- a/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala +++ b/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala @@ -141,7 +141,7 @@ trait ScalaSettings extends AbsScalaSettings // XML parsing options object XxmlSettings extends MultiChoiceEnumeration { - val coalescing = Value + val coalescing = Choice("coalescing", "Convert PCData to Text and coalesce sibling nodes") def isCoalescing = Xxml contains coalescing } val Xxml = MultiChoiceSetting( diff --git a/test/files/run/t5699.scala b/test/files/run/t5699.scala index ede6fefe48..409bcd250c 100755 --- a/test/files/run/t5699.scala +++ b/test/files/run/t5699.scala @@ -7,7 +7,7 @@ object Test extends ParserTest { |public @interface MyAnnotation { String value(); } """.stripMargin - override def extraSettings: String = "-usejavacp -Ystop-after:typer -Xprint:parser" + override def extraSettings: String = "-usejavacp -Ystop-after:namer -Xprint:parser" override def newSources(sourceCodes: String*) = { assert(sourceCodes.size == 1) -- cgit v1.2.3