Skip to content

Commit 5e1f546

Browse files
authored
Fix(#16459) xml parse regression (#19531)
close #16459 The parser could not parse `if expr` that contains single-quoted text(s) inside XML literal with newline(s) because `followedByToken`, which is used to detect `do` or `then` token after `if`, unintentionally consumed XMLSTART symbol, which prevented the parser from delegating parse to XML parser.
2 parents 3b83317 + 3d156b6 commit 5e1f546

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,7 @@ object Parsers {
906906
var braces = 0
907907
while (true) {
908908
val token = lookahead.token
909+
if (query != LARROW && token == XMLSTART) return false
909910
if (braces == 0) {
910911
if (token == query) return true
911912
if (stopScanTokens.contains(token) || lookahead.isNestedEnd) return false
@@ -927,6 +928,7 @@ object Parsers {
927928
lookahead.nextToken()
928929
while (parens != 0 && lookahead.token != EOF) {
929930
val token = lookahead.token
931+
if (token == XMLSTART) return true
930932
if (token == LPAREN) parens += 1
931933
else if (token == RPAREN) parens -= 1
932934
lookahead.nextToken()

tests/run/i16459.scala

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
object Test {
2+
import scala.xml.*
3+
def main(args: Array[String]): Unit = {
4+
5+
val xml = if(true) {
6+
<script type="text/javascript">
7+
'location.reload()'
8+
'foo bar'
9+
</script>
10+
} else <div>empty</div>
11+
12+
assert(
13+
xml match
14+
case elm: Elem if
15+
elm.label == "script"
16+
&& elm.child.length == 1
17+
&& elm.child(0) == Atom(Text("\n 'location.reload()'\n 'foo bar'\n "))
18+
=> true
19+
case _ => false
20+
,
21+
xml
22+
)
23+
// Scala 3 syntax
24+
val auxiliary0 = if true then {
25+
<script type="text/javascript">
26+
'location.reload()'
27+
'foo bar'
28+
</script>
29+
} else <div>empty</div>
30+
31+
val auxiliary1 = if true then
32+
<script type="text/javascript">
33+
'location.reload()'
34+
'foo bar'
35+
</script>
36+
else <div>empty</div>
37+
38+
val auxiliary2 = if true then <div>A</div>else <div>B</div>
39+
40+
// Note:
41+
// This does not pass in Scala 2.12.18 and 2.13.12
42+
// due to "Sequence argument type annotation `: _*` cannot be used here:"
43+
val auxiliary3 = if(true) <div>A</div>else <div>B</div>
44+
45+
// Note: This passes in Scala 2.12.18 and 2.13.12 too.
46+
val auxiliary4 = if(true) <div attr="...">A</div>else <div attr="...">B</div>
47+
48+
// Pattern match without guard.
49+
// Note: This passes in Scala 2.12.18 and 2.13.12 too.
50+
val auxiliary5 = for (case _ @ <div>empty</div> <- Seq(xml)) yield ()
51+
// Note: These pass in Scala 2.12.18 and 2.13.12.
52+
val auxiliary6 = for (case _ @ <div>empty</div><- Seq(xml)) yield ()
53+
val auxiliary7 = for (case _ @ <div>empty</div><-Seq(xml)) yield ()
54+
// Pattern match with if guard.
55+
// Note: This passes in Scala 2.12.18 and 2.13.12 too.
56+
val auxiliary8 = for (case _ @ <foo>FooBar</foo> <- Seq(xml) if true)
57+
yield ()
58+
// Note: These pass in Scala 2.12.18 and 2.13.12.
59+
val auxiliary9 = for (case _ @ <foo>FooBar</foo><- Seq(xml) if true)
60+
yield ()
61+
val auxiliary10 = for (case _ @ <foo>FooBar</foo><-Seq(xml) if true)
62+
yield ()
63+
64+
}
65+
66+
}
67+
68+
package scala.xml {
69+
type MetaData = AnyRef
70+
71+
class UnprefixedAttribute(
72+
val key: String,
73+
val value: Text,
74+
next1: MetaData
75+
) extends MetaData
76+
77+
trait NamespaceBinding
78+
object TopScope extends NamespaceBinding
79+
object Null
80+
abstract class Node {
81+
def label: String
82+
def child: Seq[Node]
83+
override def toString = label + child.mkString
84+
}
85+
86+
class Elem(prefix: String, val label: String, attributes1: MetaData, scope: NamespaceBinding, minimizeEmpty: Boolean, val child: Node*) extends Node
87+
object Elem {
88+
def unapply(e:Elem):Option[(String,String,Any,Text,Any)] = Some(("dummy","dummy",null,null,null))
89+
}
90+
class NodeBuffer extends Seq[Node] {
91+
val nodes = scala.collection.mutable.ArrayBuffer.empty[Node]
92+
def &+(o: Any): NodeBuffer = o match {
93+
case n: Node => nodes.addOne(n) ; this
94+
case t: Text => nodes.addOne(Atom(t)) ; this
95+
}
96+
// Members declared in scala.collection.IterableOnce
97+
def iterator: Iterator[scala.xml.Node] = nodes.iterator
98+
// Members declared in scala.collection.SeqOps
99+
def apply(i: Int): scala.xml.Node = nodes(i)
100+
def length: Int = nodes.length
101+
}
102+
case class Text(text: String)
103+
case class Atom(t: Text) extends Node {
104+
def label = t.text
105+
def child = Nil
106+
}
107+
}

0 commit comments

Comments
 (0)