Skip to content

Commit

Permalink
Add an irrefutable version of the NoSuccess extractor
Browse files Browse the repository at this point in the history
NoSuccess.unapply cannot be changed due to binary compatibility.
  • Loading branch information
lrytz committed Jan 17, 2023
1 parent a86d01c commit 97d5463
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ trait Parsers {
}
}

/** An irrefutable version of the NoSuccess extractor, which cannot be changed due to binary compatibility. */
object NoSuccessE {
def unapply(x: NoSuccess): Some[(String, Input)] = x match {
case Failure(msg, next) => Some((msg, next))
case Error(msg, next) => Some((msg, next))
}
}

/** The failure case of `ParseResult`: contains an error-message and the remaining input.
* Parsing will back-track when a failure occurs.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ class PackratParsersTest {

def extractResult(r : ParseResult[Int]): Int = r match {
case Success(a,_) => a
case NoSuccess(a,_) => sys.error(a)
case Failure(a, _) => sys.error(a)
case Error(a, _) => sys.error(a)
case NoSuccessE(a,_) => sys.error(a)
}
def check(expected: Int, expr: String): Unit = {
val parseResult = head(new lexical.Scanner(expr))
Expand Down Expand Up @@ -84,9 +82,7 @@ class PackratParsersTest {

def extractResult(r : ParseResult[Int]): Int = r match {
case Success(a,_) => a
case NoSuccess(a,_) => sys.error(a)
case Failure(a, _) => sys.error(a)
case Error(a, _) => sys.error(a)
case NoSuccessE(a,_) => sys.error(a)
}
def check(expected: Int, expr: String): Unit = {
val parseResult = head(new lexical.Scanner(expr))
Expand All @@ -109,9 +105,7 @@ class PackratParsersTest {
val head = phrase(AnBnCn)
def extractResult(r: ParseResult[AnBnCnResult]): AnBnCnResult = r match {
case Success(a,_) => a
case NoSuccess(a,_) => sys.error(a)
case Failure(a, _) => sys.error(a)
case Error(a, _) => sys.error(a)
case NoSuccessE(a,_) => sys.error(a)
}
def threeLists(as: List[Symbol], bs: List[Symbol], cs: List[Symbol]): AnBnCnResult = {
val as1 = as.map(_.name)
Expand Down Expand Up @@ -152,9 +146,7 @@ class PackratParsersTest {

def extractResult(r: ParseResult[Res]): Res = r match {
case Success(a,_) => a
case NoSuccess(a,_) => sys.error(a)
case Failure(a, _) => sys.error(a)
case Error(a, _) => sys.error(a)
case NoSuccessE(a,_) => sys.error(a)
}
def check(expected: Term, input: String, ctx: Ctx): Unit = {
val parseResult = phraseTerm(new lexical.Scanner(input))
Expand Down

0 comments on commit 97d5463

Please sign in to comment.