Skip to content

Commit 3a7a8cc

Browse files
authored
Merge pull request #163 from Philippus/issue-6464
Implement ~> and <~ for OnceParser, fixes scala/bug#6464
2 parents 61c1493 + f4f00bb commit 3a7a8cc

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -948,5 +948,11 @@ trait Parsers {
948948
trait OnceParser[+T] extends Parser[T] {
949949
override def ~ [U](p: => Parser[U]): Parser[~[T, U]]
950950
= OnceParser{ (for(a <- this; b <- commit(p)) yield new ~(a,b)).named("~") }
951+
952+
override def ~> [U](p: => Parser[U]): Parser[U]
953+
= OnceParser{ (for(a <- this; b <- commit(p)) yield b).named("~>") }
954+
955+
override def <~ [U](p: => Parser[U]): Parser[T]
956+
= OnceParser{ (for(a <- this; b <- commit(p)) yield a).named("<~") }
951957
}
952958
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
19+
@Test
20+
def test: Unit = {
21+
assertEquals(
22+
"[1.9] parsed: ((((<%~List( ))~hi)~List( ))~%>)",
23+
SspParser.phrase(SspParser.ok)(new CharSequenceReader("<% hi %>")).toString)
24+
25+
val expected = """[1.7] error: string matching regex '\w+' expected but '%' found
26+
27+
<% %>
28+
^"""
29+
30+
assertEquals(
31+
expected,
32+
SspParser.phrase(SspParser.ok)(new CharSequenceReader("<% %>")).toString)
33+
34+
assertEquals(
35+
"[1.9] parsed: hi",
36+
SspParser.phrase(SspParser.buggy)(new CharSequenceReader("<% hi %>")).toString)
37+
38+
assertEquals(
39+
expected,
40+
SspParser.phrase(SspParser.buggy)(new CharSequenceReader("<% %>")).toString)
41+
}
42+
}

0 commit comments

Comments
 (0)