Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit71dc0c3

Browse files
committed
UsetoVector for XML literal sequences
In `<a><b/><c/><a>`, a `NodeBuffer` (which extends `ArrayBuffer`) isused to accumulate the children. The buffer is passed to`new Elem($buf: _*)`, which only works thanks to the implicit`collection.Seq[Node] => NodeSeq` declared in scala-xml.With `-Vprint:typer`:```scalascala> <a><b/></a>[[syntax trees at end of typer]] // <console> private[this] val res0: scala.xml.Elem = new scala.xml.Elem(null, "a", scala.xml.Null, scala.xml.TopScope, false, (xml.this.NodeSeq.seqToNodeSeq({ val $buf: scala.xml.NodeBuffer = new scala.xml.NodeBuffer(); $buf.&+(new scala.xml.Elem(null, "b", scala.xml.Null, scala.xml.TopScope, true)); $buf }): _*));```The implicit was not inserted in 2.12 because the varargs parameter ofElem accepted a `collection.Seq`.
1 parent7e411f1 commit71dc0c3

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
importxmltypes.{
6869
_Comment,_Elem,_EntityRef,_Group,_MetaData,_NamespaceBinding,_NodeBuffer,
6970
_PCData,_PrefixedAttribute,_ProcInstr,_Text,_Unparsed,_UnprefixedAttribute
7071
}
7172

72-
importxmlterms.{_Null,__Elem,__Text,_buf,_md,_plus,_scope,_tmpscope,_xml }
73+
importxmlterms.{_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
caseclassTextAttache(pos:Position,text:String)
@@ -111,7 +112,7 @@ abstract class SymbolicXMLBuilder(@unused p: Parsers#Parser, @unused preserveWS:
111112
{
112113
defstarArgs=
113114
if (children.isEmpty)Nil
114-
elseList(Typed(makeXMLseq(pos, children), wildStar))
115+
elseList(Typed(makeXMLseq(pos, children, toVector=true), wildStar))
115116

116117
defpat=Apply(_scala_xml__Elem,List(pre, label, wild, wild)::: convertToTextPat(children))
117118
defnonpat=New(_scala_xml_Elem,List(List(pre, label, attrs, scope,if (empty)Literal(Constant(true))elseLiteral(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
caseNil=> 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-
defmakeXMLseq(pos:Position,args: scala.collection.Seq[Tree])= {
180+
defmakeXMLseq(pos:Position,args: scala.collection.Seq[Tree],toVector:Boolean)= {
180181
valbuffer= atPos(pos)(ValDef(NoMods, _buf,TypeTree(),New(_scala_xml_NodeBuffer,ListOfNil)))
181182
valapplies= 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+
valres=if (toVector)Select(Ident(_buf), _toVector)elseIdent(_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
defgroup(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
defunparsed(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
objectTest {
3-
importscala.xml.NodeBuffer
3+
importscala.xml._
44

55
defmain(args:Array[String]):Unit= {
66
valxml= <hello>world</hello>
77
assert(xml.toString=="helloworld")
8-
valnodeBuffer:NodeBuffer= <hello/><world/>
9-
assert(nodeBuffer.mkString=="helloworld")
8+
valnodeSeq:NodeBuffer= <hello/><world/>
9+
assert(nodeSeq.mkString=="helloworld")
10+
valsubSeq: 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+
valattrSeq:Elem= <afoo="txt&entityref;txt"/>
15+
assert(attrSeq.attributes.asInstanceOf[UnprefixedAttribute].value.toString=="Vector(txt, &entityref;, txt)")
16+
17+
valg: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
defchild:Seq[Node]
1010
overridedeftoString= label+ child.mkString
1111
}
12-
classElem(prefix:String,vallabel:String,attributes1:MetaData,scope:NamespaceBinding,minimizeEmpty:Boolean,valchild:Node*)extendsNode
12+
classElem(prefix:String,vallabel:String,valattributes:MetaData,scope:NamespaceBinding,minimizeEmpty:Boolean,valchild:Node*)extendsNode
1313
classNodeBufferextendsSeq[Node] {
1414
valnodes= scala.collection.mutable.ArrayBuffer.empty[Node]
1515
def&+(o:Any):NodeBuffer=
@@ -28,4 +28,11 @@ package scala.xml {
2828
deflabel= t.text
2929
defchild=Nil
3030
}
31+
caseclassUnprefixedAttribute(key:String,value:Seq[Node],next:MetaData)extendsMetaData
32+
caseclassEntityRef(entityName:String)extendsNode {
33+
deflabel=s"&$entityName;"
34+
defchild=Nil
35+
}
36+
37+
caseclassGroup(nodes:Seq[Node])
3138
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp