Content-Length: 489102 | pFad | http://github.com/scala/scala/pull/11065/files

0A Use `toVector` for XML literal sequences by lrytz · Pull Request #11065 · scala/scala · GitHub
Skip to content

Use toVector for XML literal sequences #11065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.13.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ trait MarkupParsers {
nextch()
content_LT(ts)
} while (charComingAfter(xSpaceOpt()) == '<')
handle.makeXMLseq(r2p(start, start, curOffset), ts)
handle.makeXMLseq(r2p(start, start, curOffset), ts, toVector = false)
}
else {
assert(ts.length == 1, "Require one tree")
Expand Down
14 changes: 8 additions & 6 deletions src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ abstract class SymbolicXMLBuilder(@unused p: Parsers#Parser, @unused preserveWS:
val _scope: NameType = nameType("$scope")
val _tmpscope: NameType = nameType("$tmpscope")
val _xml: NameType = nameType("xml")
val _toVector = nameType("toVector")
}

import xmltypes.{
_Comment, _Elem, _EntityRef, _Group, _MetaData, _NamespaceBinding, _NodeBuffer,
_PCData, _PrefixedAttribute, _ProcInstr, _Text, _Unparsed, _UnprefixedAttribute
}

import xmlterms.{ _Null, __Elem, __Text, _buf, _md, _plus, _scope, _tmpscope, _xml }
import xmlterms.{ _Null, __Elem, __Text, _buf, _md, _plus, _scope, _tmpscope, _xml, _toVector }

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

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

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

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

atPos(pos)( gen.mkBlock(buffer :: applies.toList ::: List(Ident(_buf))) )
val res = if (toVector) Select(Ident(_buf), _toVector) else Ident(_buf)
atPos(pos)( gen.mkBlock(buffer :: applies.toList ::: List(res)) )
}

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

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

def unparsed(pos: Position, str: String): Tree =
atPos(pos)( New(_scala_xml_Unparsed, LL(const(str))) )
Expand Down
8 changes: 4 additions & 4 deletions test/files/run/t3368-b.check
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ package <empty> {
$buf.$amp$plus(new _root_.scala.xml.Elem(null, "d", _root_.scala.xml.Null, $scope, true));
$buf.$amp$plus(new _root_.scala.xml.Text("stuff"));
$buf.$amp$plus(new _root_.scala.xml.PCData("red & black"));
$buf
$buf.toVector
}: _*))
};
abstract trait Z extends scala.AnyRef {
Expand All @@ -43,18 +43,18 @@ package <empty> {
val $buf = new _root_.scala.xml.NodeBuffer();
$buf.$amp$plus(new _root_.scala.xml.Text("x"));
$buf.$amp$plus(new _root_.scala.xml.PCData("hello, world"));
$buf
$buf.toVector
}: _*));
def g = new _root_.scala.xml.Elem(null, "foo", _root_.scala.xml.Null, $scope, false, ({
val $buf = new _root_.scala.xml.NodeBuffer();
$buf.$amp$plus(new _root_.scala.xml.PCData("hello, world"));
$buf
$buf.toVector
}: _*));
def h = new _root_.scala.xml.Elem(null, "foo", _root_.scala.xml.Null, $scope, false, ({
val $buf = new _root_.scala.xml.NodeBuffer();
$buf.$amp$plus(new _root_.scala.xml.PCData("hello, world"));
$buf.$amp$plus(new _root_.scala.xml.PCData("hello, world"));
$buf
$buf.toVector
}: _*))
}
}
Expand Down
8 changes: 4 additions & 4 deletions test/files/run/t3368.check
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package <empty> {
$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
$buf.toVector
}: _*))
};
abstract trait Z extends scala.AnyRef {
Expand All @@ -40,17 +40,17 @@ package <empty> {
def f = new _root_.scala.xml.Elem(null, "foo", _root_.scala.xml.Null, $scope, false, ({
val $buf = new _root_.scala.xml.NodeBuffer();
$buf.$amp$plus(new _root_.scala.xml.Text("xhello, world"));
$buf
$buf.toVector
}: _*));
def g = new _root_.scala.xml.Elem(null, "foo", _root_.scala.xml.Null, $scope, false, ({
val $buf = new _root_.scala.xml.NodeBuffer();
$buf.$amp$plus(new _root_.scala.xml.Text("hello, world"));
$buf
$buf.toVector
}: _*));
def h = new _root_.scala.xml.Elem(null, "foo", _root_.scala.xml.Null, $scope, false, ({
val $buf = new _root_.scala.xml.NodeBuffer();
$buf.$amp$plus(new _root_.scala.xml.Text("hello, worldhello, world"));
$buf
$buf.toVector
}: _*))
}
}
Expand Down
15 changes: 12 additions & 3 deletions test/files/run/t9027/test_2.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@

object Test {
import scala.xml.NodeBuffer
import scala.xml._

def main(args: Array[String]): Unit = {
val xml = <hello>world</hello>
assert(xml.toString == "helloworld")
val nodeBuffer: NodeBuffer = <hello/><world/>
assert(nodeBuffer.mkString == "helloworld")
val nodeSeq: NodeBuffer = <hello/><world/>
assert(nodeSeq.mkString == "helloworld")
val subSeq: scala.xml.Elem = <a><b/><c/></a>
assert(subSeq.child.mkString == "bc")
assert(subSeq.child.toString == "Vector(b, c)") // implementation detail

val attrSeq: Elem = <a foo="txt&entityref;txt"/>
assert(attrSeq.attributes.asInstanceOf[UnprefixedAttribute].value.toString == "Vector(txt, &entityref;, txt)")

val g: Group = <xml:group><a/><b/><c/></xml:group>
assert(g.nodes.toString == "Vector(a, b, c)")
}
}
9 changes: 8 additions & 1 deletion test/files/run/t9027/xml_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package scala.xml {
def child: Seq[Node]
override def toString = label + child.mkString
}
class Elem(prefix: String, val label: String, attributes1: MetaData, scope: NamespaceBinding, minimizeEmpty: Boolean, val child: Node*) extends Node
class Elem(prefix: String, val label: String, val attributes: MetaData, scope: NamespaceBinding, minimizeEmpty: Boolean, val child: Node*) extends Node
class NodeBuffer extends Seq[Node] {
val nodes = scala.collection.mutable.ArrayBuffer.empty[Node]
def &+(o: Any): NodeBuffer =
Expand All @@ -28,4 +28,11 @@ package scala.xml {
def label = t.text
def child = Nil
}
case class UnprefixedAttribute(key: String, value: Seq[Node], next: MetaData) extends MetaData
case class EntityRef(entityName: String) extends Node {
def label = s"&$entityName;"
def child = Nil
}

case class Group(nodes: Seq[Node])
}








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/scala/scala/pull/11065/files

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy