Question

Using the scala.util.parsing.combinator._ package, how can I create a simple Parser that will match on abc?

Example:

val parser = new Parser("abc")
val stream: Stream[Character] = Stream('a', 'b', 'c', 'd')   
println("parser(stream) : " + parser(stream))` 

prints out:

Success(('a', 'b', 'c'), Stream('d'))

Was it helpful?

Solution

Trivial:

object MyParsers extends scala.util.parsing.combinator.RegexParsers {
  val parser: Parser[String] = "abc"
  // or more explicit: val parser = literal("abc")
}

However, you may need a Stream[Char] instead of a Stream[Character].

You can also use acceptSeq("abc"), since String is implicitly convertable to Iterable[Char], but it will be significantly less efficient.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top