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

Make RegexParser.err handle whitespace like literal and regex #203

Merged
merged 1 commit into from
Mar 1, 2020
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 @@ -138,6 +138,22 @@ trait RegexParsers extends Parsers {
}
}

// we might want to make it public/protected in a future version
private def ws[T](p: Parser[T]): Parser[T] = new Parser[T] {
def apply(in: Input) = {
val offset = in.offset
val start = handleWhiteSpace(in.source, offset)
p(in.drop (start - offset))
}
}

/**
* @inheritdoc
*
* This parser additionally skips whitespace if `skipWhitespace` returns true.
*/
override def err(msg: String) = ws(super.err(msg))

/**
* A parser generator delimiting whole phrases (i.e. programs).
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package scala.util.parsing.combinator

import org.junit.Test
import org.junit.Assert.assertEquals
import org.junit.Assert.{ assertEquals, assertTrue }

class RegexParsersTest {
@Test
Expand Down Expand Up @@ -100,4 +100,19 @@ class RegexParsersTest {
val success = parseAll(twoWords, "first second").asInstanceOf[Success[(String, String)]]
assertEquals(("second", "first"), success.get)
}

@Test
def errorConsumesWhitespace: Unit = {
object parser extends RegexParsers {
def num = "\\d+".r

def twoNums = num ~ (num | err("error!"))
}
import parser._

// this used to return a Failure (for the second num)
val error = parseAll(twoNums, "458 bar")
assertTrue(s"expected an Error but got: ${error.getClass.getName}", error.isInstanceOf[Error])
assertEquals("error!", error.asInstanceOf[Error].msg)
}
}
36 changes: 36 additions & 0 deletions shared/src/test/scala/scala/util/parsing/combinator/gh29.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package scala.util.parsing.combinator

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

class gh29 {
object Foo extends JavaTokenParsers {
def word(x: String) = s"\\b$x\\b".r

lazy val expr = aSentence | something

lazy val aSentence = noun ~ verb ~ obj

lazy val noun = word("noun")
lazy val verb = word("verb") | err("not a verb!")
lazy val obj = word("object")

lazy val something = word("FOO")
}

val expected =
"""[1.6] error: not a verb!

noun vedsfasdf
^""".stripMargin

@Test
def test(): Unit = {
val f = Foo.parseAll(Foo.expr, "noun verb object")

assertEquals("[1.17] parsed: ((noun~verb)~object)", f.toString)

val g = Foo.parseAll(Foo.expr, "noun vedsfasdf")
assertEquals(expected, g.toString)
}
}