Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 177b745

Browse files
authored
Merge pull request #11065 from lrytz/xmlVector
Use `toVector` for XML literal sequences
2 parents bd9f766 + 71dc0c3 commit 177b745

File tree

6 files changed

+37
-19
lines changed

6 files changed

+37
-19
lines changed

src/compiler/scala/tools/nsc/ast/parser/MarkupParsers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ trait MarkupParsers {
391391
nextch()
392392
content_LT(ts)
393393
} while (charComingAfter(xSpaceOpt()) == '<')
394-
handle.makeXMLseq(r2p(start, start, curOffset), ts)
394+
handle.makeXMLseq(r2p(start, start, curOffset), ts, toVector = false)
395395
}
396396
else {
397397
assert(ts.length == 1, "Require one tree")

src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,15 @@ abstract class SymbolicXMLBuilder(@unused p: Parsers#Parser, @unused preserveWS:
6262
val _scope: NameType = nameType("$scope")
6363
val _tmpscope: NameType = nameType("$tmpscope")
6464
val _xml: NameType = nameType("xml")
65+
val _toVector = nameType("toVector")
6566
}
6667

6768
import xmltypes.{
6869
_Comment, _Elem, _EntityRef, _Group, _MetaData, _NamespaceBinding, _NodeBuffer,
6970
_PCData, _PrefixedAttribute, _ProcInstr, _Text, _Unparsed, _UnprefixedAttribute
7071
}
7172

72-
import xmlterms.{ _Null, __Elem, __Text, _buf, _md, _plus, _scope, _tmpscope, _xml }
73+
import xmlterms.{ _Null, __Elem, __Text, _buf, _md, _plus, _scope, _tmpscope, _xml, _toVector }
7374

7475
/** Attachment for trees deriving from text nodes (Text, CData, entities). Used for coalescing. */
7576
case class TextAttache(pos: Position, text: String)
@@ -111,7 +112,7 @@ abstract class SymbolicXMLBuilder(@unused p: Parsers#Parser, @unused preserveWS:
111112
{
112113
def starArgs =
113114
if (children.isEmpty) Nil
114-
else List(Typed(makeXMLseq(pos, children), wildStar))
115+
else List(Typed(makeXMLseq(pos, children, toVector = true), wildStar))
115116

116117
def pat = Apply(_scala_xml__Elem, List(pre, label, wild, wild) ::: convertToTextPat(children))
117118
def nonpat = New(_scala_xml_Elem, List(List(pre, label, attrs, scope, if (empty) Literal(Constant(true)) else Literal(Constant(false))) ::: starArgs))
@@ -166,7 +167,7 @@ abstract class SymbolicXMLBuilder(@unused p: Parsers#Parser, @unused preserveWS:
166167
parseAttributeValue(s, text(pos, _), entityRef(pos, _)) match {
167168
case Nil => gen.mkNil
168169
case t :: Nil => t
169-
case ts => makeXMLseq(pos, ts.toList)
170+
case ts => makeXMLseq(pos, ts, toVector = true)
170171
}
171172
}
172173

@@ -176,11 +177,12 @@ abstract class SymbolicXMLBuilder(@unused p: Parsers#Parser, @unused preserveWS:
176177
}
177178

178179
/** could optimize if args.length == 0, args.length == 1 AND args(0) is <: Node. */
179-
def makeXMLseq(pos: Position, args: scala.collection.Seq[Tree]) = {
180+
def makeXMLseq(pos: Position, args: scala.collection.Seq[Tree], toVector: Boolean) = {
180181
val buffer = atPos(pos)(ValDef(NoMods, _buf, TypeTree(), New(_scala_xml_NodeBuffer, ListOfNil)))
181182
val applies = args.filterNot(isEmptyText).map(t => atPos(t.pos)(Apply(Select(Ident(_buf), _plus), List(t))))
182183

183-
atPos(pos)( gen.mkBlock(buffer :: applies.toList ::: List(Ident(_buf))) )
184+
val res = if (toVector) Select(Ident(_buf), _toVector) else Ident(_buf)
185+
atPos(pos)( gen.mkBlock(buffer :: applies.toList ::: List(res)) )
184186
}
185187

186188
/** Returns (Some(prefix) | None, rest) based on position of ':' */
@@ -191,7 +193,7 @@ abstract class SymbolicXMLBuilder(@unused p: Parsers#Parser, @unused preserveWS:
191193

192194
/** Various node constructions. */
193195
def group(pos: Position, args: scala.collection.Seq[Tree]): Tree =
194-
atPos(pos)( New(_scala_xml_Group, LL(makeXMLseq(pos, args))) )
196+
atPos(pos)( New(_scala_xml_Group, LL(makeXMLseq(pos, args, toVector = true))) )
195197

196198
def unparsed(pos: Position, str: String): Tree =
197199
atPos(pos)( New(_scala_xml_Unparsed, LL(const(str))) )

test/files/run/t3368-b.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ package <empty> {
2525
$buf.$amp$plus(new _root_.scala.xml.Elem(null, "d", _root_.scala.xml.Null, $scope, true));
2626
$buf.$amp$plus(new _root_.scala.xml.Text("stuff"));
2727
$buf.$amp$plus(new _root_.scala.xml.PCData("red & black"));
28-
$buf
28+
$buf.toVector
2929
}: _*))
3030
};
3131
abstract trait Z extends scala.AnyRef {
@@ -43,18 +43,18 @@ package <empty> {
4343
val $buf = new _root_.scala.xml.NodeBuffer();
4444
$buf.$amp$plus(new _root_.scala.xml.Text("x"));
4545
$buf.$amp$plus(new _root_.scala.xml.PCData("hello, world"));
46-
$buf
46+
$buf.toVector
4747
}: _*));
4848
def g = new _root_.scala.xml.Elem(null, "foo", _root_.scala.xml.Null, $scope, false, ({
4949
val $buf = new _root_.scala.xml.NodeBuffer();
5050
$buf.$amp$plus(new _root_.scala.xml.PCData("hello, world"));
51-
$buf
51+
$buf.toVector
5252
}: _*));
5353
def h = new _root_.scala.xml.Elem(null, "foo", _root_.scala.xml.Null, $scope, false, ({
5454
val $buf = new _root_.scala.xml.NodeBuffer();
5555
$buf.$amp$plus(new _root_.scala.xml.PCData("hello, world"));
5656
$buf.$amp$plus(new _root_.scala.xml.PCData("hello, world"));
57-
$buf
57+
$buf.toVector
5858
}: _*))
5959
}
6060
}

test/files/run/t3368.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ package <empty> {
2323
$buf.$amp$plus(new _root_.scala.xml.Text("world"));
2424
$buf.$amp$plus(new _root_.scala.xml.Elem(null, "d", _root_.scala.xml.Null, $scope, true));
2525
$buf.$amp$plus(new _root_.scala.xml.Text("stuffred & black"));
26-
$buf
26+
$buf.toVector
2727
}: _*))
2828
};
2929
abstract trait Z extends scala.AnyRef {
@@ -40,17 +40,17 @@ package <empty> {
4040
def f = new _root_.scala.xml.Elem(null, "foo", _root_.scala.xml.Null, $scope, false, ({
4141
val $buf = new _root_.scala.xml.NodeBuffer();
4242
$buf.$amp$plus(new _root_.scala.xml.Text("xhello, world"));
43-
$buf
43+
$buf.toVector
4444
}: _*));
4545
def g = new _root_.scala.xml.Elem(null, "foo", _root_.scala.xml.Null, $scope, false, ({
4646
val $buf = new _root_.scala.xml.NodeBuffer();
4747
$buf.$amp$plus(new _root_.scala.xml.Text("hello, world"));
48-
$buf
48+
$buf.toVector
4949
}: _*));
5050
def h = new _root_.scala.xml.Elem(null, "foo", _root_.scala.xml.Null, $scope, false, ({
5151
val $buf = new _root_.scala.xml.NodeBuffer();
5252
$buf.$amp$plus(new _root_.scala.xml.Text("hello, worldhello, world"));
53-
$buf
53+
$buf.toVector
5454
}: _*))
5555
}
5656
}

test/files/run/t9027/test_2.scala

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11

22
object Test {
3-
import scala.xml.NodeBuffer
3+
import scala.xml._
44

55
def main(args: Array[String]): Unit = {
66
val xml = <hello>world</hello>
77
assert(xml.toString == "helloworld")
8-
val nodeBuffer: NodeBuffer = <hello/><world/>
9-
assert(nodeBuffer.mkString == "helloworld")
8+
val nodeSeq: NodeBuffer = <hello/><world/>
9+
assert(nodeSeq.mkString == "helloworld")
10+
val subSeq: scala.xml.Elem = <a><b/><c/></a>
11+
assert(subSeq.child.mkString == "bc")
12+
assert(subSeq.child.toString == "Vector(b, c)") // implementation detail
13+
14+
val attrSeq: Elem = <a foo="txt&entityref;txt"/>
15+
assert(attrSeq.attributes.asInstanceOf[UnprefixedAttribute].value.toString == "Vector(txt, &entityref;, txt)")
16+
17+
val g: Group = <xml:group><a/><b/><c/></xml:group>
18+
assert(g.nodes.toString == "Vector(a, b, c)")
1019
}
1120
}

test/files/run/t9027/xml_1.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package scala.xml {
99
def child: Seq[Node]
1010
override def toString = label + child.mkString
1111
}
12-
class Elem(prefix: String, val label: String, attributes1: MetaData, scope: NamespaceBinding, minimizeEmpty: Boolean, val child: Node*) extends Node
12+
class Elem(prefix: String, val label: String, val attributes: MetaData, scope: NamespaceBinding, minimizeEmpty: Boolean, val child: Node*) extends Node
1313
class NodeBuffer extends Seq[Node] {
1414
val nodes = scala.collection.mutable.ArrayBuffer.empty[Node]
1515
def &+(o: Any): NodeBuffer =
@@ -28,4 +28,11 @@ package scala.xml {
2828
def label = t.text
2929
def child = Nil
3030
}
31+
case class UnprefixedAttribute(key: String, value: Seq[Node], next: MetaData) extends MetaData
32+
case class EntityRef(entityName: String) extends Node {
33+
def label = s"&$entityName;"
34+
def child = Nil
35+
}
36+
37+
case class Group(nodes: Seq[Node])
3138
}

0 commit comments

Comments
 (0)