Skip to content

Commit fc983fe

Browse files
committed
Merge pull request #30 from gourlaysama/wip/i29
make RegexParser.err handle whitespace like literal and regex.
2 parents ca4cefe + dc9e9cf commit fc983fe

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

Diff for: build.sbt

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import com.typesafe.tools.mima.plugin.{MimaPlugin, MimaKeys}
2+
import com.typesafe.tools.mima.core.{ProblemFilters, MissingMethodProblem}
23

34
scalaModuleSettings
45

@@ -31,3 +32,5 @@ test in Test := {
3132
MimaKeys.reportBinaryIssues.value
3233
(test in Test).value
3334
}
35+
36+
MimaKeys.binaryIssueFilters += ProblemFilters.exclude[MissingMethodProblem]("scala.util.parsing.combinator.RegexParsers.scala$util$parsing$combinator$RegexParsers$$super$err")

Diff for: src/main/scala/scala/util/parsing/combinator/RegexParsers.scala

+16
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,22 @@ trait RegexParsers extends Parsers {
137137
}
138138
}
139139

140+
// we might want to make it public/protected in a future version
141+
private def ws[T](p: Parser[T]): Parser[T] = new Parser[T] {
142+
def apply(in: Input) = {
143+
val offset = in.offset
144+
val start = handleWhiteSpace(in.source, offset)
145+
p(in.drop (start - offset))
146+
}
147+
}
148+
149+
/**
150+
* @inheritdoc
151+
*
152+
* This parser additionnal skips whitespace if `skipWhitespace` returns true.
153+
*/
154+
override def err(msg: String) = ws(super.err(msg))
155+
140156
override def phrase[T](p: Parser[T]): Parser[T] =
141157
super.phrase(p <~ opt("""\z""".r))
142158

Diff for: src/test/scala/scala/util/parsing/combinator/RegexParsersTest.scala

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package scala.util.parsing.combinator
22

33
import org.junit.Test
4-
import org.junit.Assert.assertEquals
4+
import org.junit.Assert.{assertEquals,assertTrue}
55

66
class RegexParsersTest {
77
@Test
@@ -73,4 +73,19 @@ class RegexParsersTest {
7373
val success = parseAll(twoWords, "first second").asInstanceOf[Success[(String, String)]]
7474
assertEquals(("second", "first"), success.get)
7575
}
76+
77+
@Test
78+
def errorConsumesWhitespace: Unit = {
79+
object parser extends RegexParsers {
80+
def num = "\\d+".r
81+
82+
def twoNums = num ~ (num | err("error!"))
83+
}
84+
import parser._
85+
86+
// this used to return a Failure (for the second num)
87+
val error = parseAll(twoNums, "458 bar")
88+
assertTrue(s"expected an Error but got: ${error.getClass.getName}", error.isInstanceOf[Error])
89+
assertEquals("error!", error.asInstanceOf[Error].msg)
90+
}
7691
}

0 commit comments

Comments
 (0)