Skip to content

Commit 6c7804b

Browse files
committed
Implement ~> and <~ for OnceParser, fixes scala/bug#6464
1 parent 4bcdc4d commit 6c7804b

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

shared/src/main/scala/scala/util/parsing/combinator/Parsers.scala

+7-1
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,12 @@ trait Parsers {
948948
*/
949949
trait OnceParser[+T] extends Parser[T] {
950950
override def ~ [U](p: => Parser[U]): Parser[~[T, U]]
951-
= OnceParser{ (for(a <- this; b <- commit(p)) yield new ~(a,b)).named("~") }
951+
= OnceParser{ (for(a <- this; b <- commit(p)) yield new ~(a,b)).named("~") }
952+
953+
override def ~> [U](p: => Parser[U]): Parser[U]
954+
= OnceParser{ (for(a <- this; b <- commit(p)) yield b).named("~>") }
955+
956+
override def <~ [U](p: => Parser[U]): Parser[T]
957+
= OnceParser{ (for(a <- this; b <- commit(p)) yield a).named("<~") }
952958
}
953959
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import scala.util.parsing.input.CharSequenceReader
2+
import scala.util.parsing.combinator.RegexParsers
3+
4+
import org.junit.Test
5+
import org.junit.Assert.assertEquals
6+
7+
class t6464 {
8+
object SspParser extends RegexParsers {
9+
val ok: Parser[Any] =
10+
("<%" ~! rep(' ') ~ "\\w+".r ~ rep(' ') ~ "%>"
11+
| "<%" ~! err("should not fail here, because of ~!"))
12+
13+
val buggy: Parser[Any] =
14+
("<%" ~! rep(' ') ~> "\\w+".r <~ rep(' ') ~ "%>"
15+
| "<%" ~! err("should not fail here, because of ~!"))
16+
17+
}
18+
@Test
19+
def test: Unit = {
20+
assertEquals(
21+
"[1.9] parsed: ((((<%~List( ))~hi)~List( ))~%>)",
22+
SspParser.phrase(SspParser.ok)(new CharSequenceReader("<% hi %>")).toString)
23+
val expected = """[1.7] error: string matching regex '\w+' expected but '%' found
24+
25+
<% %>
26+
^"""
27+
assertEquals(
28+
expected,
29+
SspParser.phrase(SspParser.ok)(new CharSequenceReader("<% %>")).toString)
30+
31+
assertEquals(
32+
"[1.9] parsed: hi",
33+
SspParser.phrase(SspParser.buggy)(new CharSequenceReader("<% hi %>")).toString)
34+
35+
assertEquals(
36+
expected,
37+
SspParser.phrase(SspParser.buggy)(new CharSequenceReader("<% %>")).toString)
38+
}
39+
}

0 commit comments

Comments
 (0)