Search code examples
fastparse

P[Node].rep produces P[String] rather than array of nodes


I would expect the result for plusses to be some kind of array

case class Plus()
val plus: P[Plus] = P ("+") map {_ => Plus()}
val plusses: P[List[Plus]] = P ( plus.rep.! )  // type mismatch; found: Parser[String]  required: Parser[List[Plus]]

but compiler says

type mismatch; found : Parser[String] required: Parser[List[Plus]]


Solution

  • Answer

    First, you don't need to capture something with .! as you already have a result in the plus parser. The .rep then "creates" a parser that repeats the plus parser 0 to n times and concatenates the result into a Seq[Plus].

    case class Plus()
    val plus: P[Plus] = P ("+") map {_ ⇒ Plus()}
    val plusses: P[Seq[Plus]] = P ( plus.rep )
    

    Second, if using the repeat operation rep, Parsers return Seq and not List If you really need a list, use map to convert Seq to List.

    val plussesAsList: P[List[Plus]] = P( plus.rep ).map( seq ⇒ seq.toList)