Skip to content
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

Fix(#16459) xml parse regression #19531

Merged
merged 6 commits into from
Feb 1, 2024
Merged
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
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,7 @@ object Parsers {
var braces = 0
while (true) {
val token = lookahead.token
if (query != LARROW && token == XMLSTART) return false
i10416 marked this conversation as resolved.
Show resolved Hide resolved
if (braces == 0) {
if (token == query) return true
if (stopScanTokens.contains(token) || lookahead.isNestedEnd) return false
Expand All @@ -927,6 +928,7 @@ object Parsers {
lookahead.nextToken()
while (parens != 0 && lookahead.token != EOF) {
val token = lookahead.token
if (token == XMLSTART) return true
if (token == LPAREN) parens += 1
else if (token == RPAREN) parens -= 1
lookahead.nextToken()
Expand Down
107 changes: 107 additions & 0 deletions tests/run/i16459.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
object Test {
import scala.xml.*
def main(args: Array[String]): Unit = {

val xml = if(true) {
<script type="text/javascript">
'location.reload()'
'foo bar'
</script>
} else <div>empty</div>

assert(
xml match
case elm: Elem if
elm.label == "script"
&& elm.child.length == 1
&& elm.child(0) == Atom(Text("\n 'location.reload()'\n 'foo bar'\n "))
=> true
case _ => false
,
xml
)
// Scala 3 syntax
val auxiliary0 = if true then {
<script type="text/javascript">
'location.reload()'
'foo bar'
</script>
} else <div>empty</div>

val auxiliary1 = if true then
<script type="text/javascript">
'location.reload()'
'foo bar'
</script>
else <div>empty</div>

val auxiliary2 = if true then <div>A</div>else <div>B</div>

// Note:
// This does not pass in Scala 2.12.18 and 2.13.12
// due to "Sequence argument type annotation `: _*` cannot be used here:"
val auxiliary3 = if(true) <div>A</div>else <div>B</div>

// Note: This passes in Scala 2.12.18 and 2.13.12 too.
val auxiliary4 = if(true) <div attr="...">A</div>else <div attr="...">B</div>

// Pattern match without guard.
// Note: This passes in Scala 2.12.18 and 2.13.12 too.
val auxiliary5 = for (case _ @ <div>empty</div> <- Seq(xml)) yield ()
// Note: These pass in Scala 2.12.18 and 2.13.12.
val auxiliary6 = for (case _ @ <div>empty</div><- Seq(xml)) yield ()
val auxiliary7 = for (case _ @ <div>empty</div><-Seq(xml)) yield ()
Comment on lines +52 to +53
Copy link
Contributor Author

@i10416 i10416 Jan 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The left arrow just after xml closing tag broke the parser.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

// Pattern match with if guard.
// Note: This passes in Scala 2.12.18 and 2.13.12 too.
val auxiliary8 = for (case _ @ <foo>FooBar</foo> <- Seq(xml) if true)
yield ()
// Note: These pass in Scala 2.12.18 and 2.13.12.
val auxiliary9 = for (case _ @ <foo>FooBar</foo><- Seq(xml) if true)
yield ()
val auxiliary10 = for (case _ @ <foo>FooBar</foo><-Seq(xml) if true)
yield ()

}

}

package scala.xml {
type MetaData = AnyRef

class UnprefixedAttribute(
val key: String,
val value: Text,
next1: MetaData
) extends MetaData

trait NamespaceBinding
object TopScope extends NamespaceBinding
object Null
abstract class Node {
def label: String
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
object Elem {
def unapply(e:Elem):Option[(String,String,Any,Text,Any)] = Some(("dummy","dummy",null,null,null))
}
class NodeBuffer extends Seq[Node] {
val nodes = scala.collection.mutable.ArrayBuffer.empty[Node]
def &+(o: Any): NodeBuffer = o match {
case n: Node => nodes.addOne(n) ; this
case t: Text => nodes.addOne(Atom(t)) ; this
}
// Members declared in scala.collection.IterableOnce
def iterator: Iterator[scala.xml.Node] = nodes.iterator
// Members declared in scala.collection.SeqOps
def apply(i: Int): scala.xml.Node = nodes(i)
def length: Int = nodes.length
}
case class Text(text: String)
case class Atom(t: Text) extends Node {
def label = t.text
def child = Nil
}
}
Loading