I'm looking at using Scala's Parser Combinators to parse a string (no newlines, contrived example).
The string is made up of many different parts which I want to extract separately and populate a case class.
case class MyRecord(foo: String, bar: String, baz: String, bam: String, bat: String)
object MyParser extends scala.util.parsing.combinator.RegexParsers {
val foo: Parser[String] = "foo"
val bar: Parser[String] = "bar"
val baz: Parser[String] = "baz"
val bam: Parser[String] = "bam"
val bat: Parser[String] = "bat"
val expression: Parser[MyRecord] =
foo ~ bar ~ baz ~ bam ~ bat ^^ {
case foo ~ bar ~ baz ~ bam ~ bat => MyRecord(foo, bar, baz, bam, bat)
}
}
This works perfectly well, but is there a way to apply the parts of the matched results directly to the case class without deconstructing?
val expression: Parser[MyRecord] =
foo ~ bar ~ baz ~ bam ~ bat ^^ MyRecord
Further information: The string I'm parsing is quite long and complex (in reality, it's a whole file full of long complex strings) so changing to regexp is out of the question.