Skip to content

Commit 97d5463

Browse files
committed
Add an irrefutable version of the NoSuccess extractor
NoSuccess.unapply cannot be changed due to binary compatibility.
1 parent a86d01c commit 97d5463

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

Diff for: shared/src/main/scala/scala/util/parsing/combinator/Parsers.scala

+8
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,14 @@ trait Parsers {
185185
}
186186
}
187187

188+
/** An irrefutable version of the NoSuccess extractor, which cannot be changed due to binary compatibility. */
189+
object NoSuccessE {
190+
def unapply(x: NoSuccess): Some[(String, Input)] = x match {
191+
case Failure(msg, next) => Some((msg, next))
192+
case Error(msg, next) => Some((msg, next))
193+
}
194+
}
195+
188196
/** The failure case of `ParseResult`: contains an error-message and the remaining input.
189197
* Parsing will back-track when a failure occurs.
190198
*

Diff for: shared/src/test/scala/scala/util/parsing/combinator/PackratParsersTest.scala

+4-12
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ class PackratParsersTest {
2828

2929
def extractResult(r : ParseResult[Int]): Int = r match {
3030
case Success(a,_) => a
31-
case NoSuccess(a,_) => sys.error(a)
32-
case Failure(a, _) => sys.error(a)
33-
case Error(a, _) => sys.error(a)
31+
case NoSuccessE(a,_) => sys.error(a)
3432
}
3533
def check(expected: Int, expr: String): Unit = {
3634
val parseResult = head(new lexical.Scanner(expr))
@@ -84,9 +82,7 @@ class PackratParsersTest {
8482

8583
def extractResult(r : ParseResult[Int]): Int = r match {
8684
case Success(a,_) => a
87-
case NoSuccess(a,_) => sys.error(a)
88-
case Failure(a, _) => sys.error(a)
89-
case Error(a, _) => sys.error(a)
85+
case NoSuccessE(a,_) => sys.error(a)
9086
}
9187
def check(expected: Int, expr: String): Unit = {
9288
val parseResult = head(new lexical.Scanner(expr))
@@ -109,9 +105,7 @@ class PackratParsersTest {
109105
val head = phrase(AnBnCn)
110106
def extractResult(r: ParseResult[AnBnCnResult]): AnBnCnResult = r match {
111107
case Success(a,_) => a
112-
case NoSuccess(a,_) => sys.error(a)
113-
case Failure(a, _) => sys.error(a)
114-
case Error(a, _) => sys.error(a)
108+
case NoSuccessE(a,_) => sys.error(a)
115109
}
116110
def threeLists(as: List[Symbol], bs: List[Symbol], cs: List[Symbol]): AnBnCnResult = {
117111
val as1 = as.map(_.name)
@@ -152,9 +146,7 @@ class PackratParsersTest {
152146

153147
def extractResult(r: ParseResult[Res]): Res = r match {
154148
case Success(a,_) => a
155-
case NoSuccess(a,_) => sys.error(a)
156-
case Failure(a, _) => sys.error(a)
157-
case Error(a, _) => sys.error(a)
149+
case NoSuccessE(a,_) => sys.error(a)
158150
}
159151
def check(expected: Term, input: String, ctx: Ctx): Unit = {
160152
val parseResult = phraseTerm(new lexical.Scanner(input))

0 commit comments

Comments
 (0)