Add repitition as list in case class (scala parser) -
i have following parser:
import scala.util.parsing.combinator.regexparsers class simpleparser extends regexparsers { override val skipwhitespace = false private val eol = sys.props("line.separator") def word: parser[string] = "[a-z]+".r ^^ { _.tostring } def freq: parser[list[string]] = repsep(word, eol) ^^ { case word => word } } object testsimpleparser extends simpleparser { def main(args: array[string]) = { parse(freq, """mike |john |sara """.stripmargin) match { case success(matched,_) => println(matched) case failure(msg,_) => println(msg) case error(msg,_) => println(msg) } } }
the result of execution list(mike, john, sara)
isn't want.
i'd have following case class:
case class someentity(list: list[string])
and result following:
someentity(list(mike), list(john), list(sara)).
i want add lists there words new lines. please, don't put big attention on fact there 1 word in list. simplified version of issue. so, should change in parser or in case class?
the case class definition not match expected output. you're looking this:
case class someentity(list: list[string]*) { override def tostring = this.getclass.getsimplename+"("+list.mkstring(", ")+")" }
now if, want someentity
instance, type of parser parser[someentity]
.
as repsep
give list[string]
, need map each element in list singleton list (so list[list[string]]
) , forces see varargs type :_*
def freq: parser[someentity] = repsep(word, eol) ^^ (list => new someentity(list.map(list(_)) : _*))
given input, outputs:
someentity(list(mike), list(john), list(sara))
your case class take list[list[string]]
parameter. although list[string]
correct type in opinion, have reasons require this.
note mapping word
parser redundant, remove it.
Comments
Post a Comment