Closed
Description
reproduction steps
using Scala 2.13.5, scala-parser-combinators 1.1.2,
the exmaple code is modified from scala-parser-combinators project.
Welcome to Scala 2.13.5 (OpenJDK 64-Bit Server VM, Java 11.0.9.1).
Type in expressions for evaluation. Or try :help.
scala> import scala.util.parsing.combinator.RegexParsers
|
| case class WordFreq(word: String, count: Int) {
| override def toString = s"Word <$word> occurs with frequency $count"
| }
|
| class SimpleParser extends RegexParsers {
| def word: Parser[String] = """[a-z]+""".r ^^ { _.toString }
| def number: Parser[Int] = """(0|[1-9]\d*)""".r ^^ { _.toInt }
| def freq: Parser[WordFreq] = word ~ number ^^ { case wd ~ fr => WordFreq(wd,fr) }
| }
|
| object TestSimpleParser extends SimpleParser {
| def main(args: Array[String]) = {
| parse(freq, "johnny 121") match {
| case Success(matched,_) => println(matched)
| case NoSuccess(msg,_) => println(s"NoSuccess: $msg")
| }
| }
| }
parse(freq, "johnny 121") match {
^
On line 15: warning: match may not be exhaustive.
It would fail on the following inputs: Error(_, _), Failure(_, _)
import scala.util.parsing.combinator.RegexParsers
class WordFreq
class SimpleParser
object TestSimpleParser
problem
To simplicity, it seems like something following (not exactly)
sealed abstract class ParseResult
case class Success(result: String) extends ParseResult
sealed abstract class NoSuccess(msg: String) extends ParseResult
case class Failure(msg: String) extends NoSuccess(msg)
case class Error(msg: String) extends NoSuccess(msg)
object NoSuccess {
def unapply[T](x: ParseResult): Option[String] = x match {
case Failure(msg) => Some(msg)
case Error(msg) => Some(msg)
case _ => None
}
}
The NoSuccess
is a sealed abstact class and only has two sub-class Failure
and Error
.
The ParseResult
is a sealed abstact class and only has two sub-class Success
and NoSuccess
The matching here for Success
and NotSuccess
should be exhaustived already.