Using Parboiled to parse different input types with same separator

126 Views Asked by At

I want to use Parboiled to parse a string that should turn a similar source into different types.

Specifically, I am trying to parse an input of words separated by the same separator into the equivalent of (List[String], String) where the last word is the second element of the tuple.

For example, "a.bb.ccc.dd.e" should be parsed to (["a", "bb", "ccc", "dd"], "e").

A simplified version of my code is as follows:

case class Foo(s: String)

case class Bar(fs: List[Foo], f: Foo)

object FooBarParser extends Parser {

  val SEPARATOR = "."

  def letter: Rule0 = rule { "a" - "z" }

  def word: Rule1[String] = rule { oneOrMore(letter) ~> identity }

  def foo = rule { word ~~> Foo }

  def foos = rule { zeroOrMore(foo, separator = SEPARATOR) }

  def bar = foos ~ SEPARATOR ~ foo  ~~> Bar
}

object TestParser extends App {

  val source = "aaa.bbb.ccc"

  val parseResult = ReportingParseRunner(FooBarParser.bar).run(source)

  println(parseResult.result)
}

This prints None so clearly I am doing something wrong. Is Parboiled capable of parsing this?

1

There are 1 best solutions below

0
On

Please wrap all your rules inside 'rule' block. It helps in some cases