Matching parser success patterns In Scala

I'm new to Scala and trying to use its excellent library of parser-harvesters. I am trying to compile this code:

import scala.util.parsing.combinator._ ... val r:Parsers#ParseResult[Node] = parser.parseAll(parser.suite,reader) r match { case Success(r, n) => println(r) case Failure(msg, n) => println(msg) case Error(msg, n) => println(msg) } ... 

But I keep getting these errors:

 TowelParser.scala:97: error: not found: value Success case Success(r, n) => println(r) ^ TowelParser.scala:98: error: not found: value Failure case Failure(msg, n) => println(msg) TowelParser.scala:99: error: object Error is not a case class constructor, nor does it have an unapply/unapplySeq method case Error(msg, n) => println(msg) 

I have tried many different things, such as:

 case Parsers#Success(r, n) => println(r) 

and

 case Parsers.Success(r, n) => println(r) 

and

 import scala.util.parsing.combinator.Parsers.Success 

But I can not get this to compile. I'm sure there may be something obvious that I am missing, but I have been on it for a while, and google does not seem to have good examples of this.

Thanks!

+6
source share
1 answer

You need to specify the full path for ParseResult , which includes your Parsers instance. For instance:

 import scala.util.parsing.combinator._ object parser extends RegexParsers { def digits = "\\d+".r ^^ (_.toInt) } val res = parser.parseAll(parser.digits, "42") res match { case parser.Success(r, n) => println(r) case parser.Failure(msg, n) => println(msg) case parser.Error(msg, n) => println(msg) } 

Note that you can also import them if you want a little extra syntax convenience:

 import parser.{ Error, Failure, Success } 

Now your original version will work as expected.

+12
source

All Articles