Skip to content

Commit

Permalink
Merge pull request #203 from Philippus/issue/29
Browse files Browse the repository at this point in the history
Make RegexParser.err handle whitespace like literal and regex
  • Loading branch information
Philippus authored Mar 1, 2020
2 parents 8a84b17 + 0a3a641 commit 5147f67
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
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)
}
}

0 comments on commit 5147f67

Please sign in to comment.