Skip to content

Implement ~> and <~ for OnceParser, fixes scala/bug#6464 #163

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

Merged
merged 2 commits into from
Apr 10, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -949,5 +949,11 @@ trait Parsers {
trait OnceParser[+T] extends Parser[T] {
override def ~ [U](p: => Parser[U]): Parser[~[T, U]]
= OnceParser{ (for(a <- this; b <- commit(p)) yield new ~(a,b)).named("~") }

override def ~> [U](p: => Parser[U]): Parser[U]
= OnceParser{ (for(a <- this; b <- commit(p)) yield b).named("~>") }

override def <~ [U](p: => Parser[U]): Parser[T]
= OnceParser{ (for(a <- this; b <- commit(p)) yield a).named("<~") }
}
}
42 changes: 42 additions & 0 deletions shared/src/test/scala/scala/util/parsing/combinator/t6464.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import scala.util.parsing.input.CharSequenceReader
import scala.util.parsing.combinator.RegexParsers

import org.junit.Test
import org.junit.Assert.assertEquals

class t6464 {
object SspParser extends RegexParsers {
val ok: Parser[Any] =
("<%" ~! rep(' ') ~ "\\w+".r ~ rep(' ') ~ "%>"
| "<%" ~! err("should not fail here, because of ~!"))

val buggy: Parser[Any] =
("<%" ~! rep(' ') ~> "\\w+".r <~ rep(' ') ~ "%>"
| "<%" ~! err("should not fail here, because of ~!"))

}

@Test
def test: Unit = {
assertEquals(
"[1.9] parsed: ((((<%~List( ))~hi)~List( ))~%>)",
SspParser.phrase(SspParser.ok)(new CharSequenceReader("<% hi %>")).toString)

val expected = """[1.7] error: string matching regex '\w+' expected but '%' found

<% %>
^"""

assertEquals(
expected,
SspParser.phrase(SspParser.ok)(new CharSequenceReader("<% %>")).toString)

assertEquals(
"[1.9] parsed: hi",
SspParser.phrase(SspParser.buggy)(new CharSequenceReader("<% hi %>")).toString)

assertEquals(
expected,
SspParser.phrase(SspParser.buggy)(new CharSequenceReader("<% %>")).toString)
}
}