title: Scala parser combinators and newline-delimited text
body: <p>I am writing a Scala parser combinator grammar that reads newline-delimited word lists, where lists are separated by one or more blank lines. Given the following string:</p>
  
  <pre><code>cat
  mouse
  horse
  
  apple
  orange
  pear
  </code></pre>
  
  <p>I would like to have it return <code>List(List(cat, mouse, horse), List(apple, orange, pear))</code>.</p>
  
  <p>I wrote this basic grammar which treats word lists as newline-delimited words. Note that I had to override the default definition of <code>whitespace</code>.</p>
  
  <pre><code>import util.parsing.combinator.RegexParsers
  
  object WordList extends RegexParsers {
  
      private val eol = sys.props("line.separator")
  
      override val whiteSpace = """[ \t]+""".r
  
      val list: Parser[List[String]] = repsep( """\w+""".r, eol)
  
      val lists: Parser[List[List[String]]] = repsep(list, eol)
  
      def main(args: Array[String]) {
          val s =
            """cat
              |mouse
              |horse
              |
              |apple
              |orange
              |pear""".stripMargin
  
          println(parseAll(lists, s))
      }
  }
  </code></pre>
  
  <p>This incorrectly treats blank lines as empty word lists, i.e. it returns</p>
  
  <pre><code>[8.1] parsed: List(List(cat, mouse, horse), List(), List(apple, orange, pear))
  </code></pre>
  
  <p>(Note the empty list in the middle.)</p>
  
  <p>I can put an optional end of line at the end of each list.</p>
  
  <pre><code>val list: Parser[List[String]] = repsep( """\w+""".r, eol) &lt;~ opt(eol)
  </code></pre>
  
  <p>This handles the case where there is a single blank line between lists, but has the same problem with multiple blank lines.</p>
  
  <p>I tried changing the <code>lists</code> definition to allow multiple end-of-line delimiters:</p>
  
  <pre><code>val lists:Parser[List[List[String]]] = repsep(list, rep(eol))
  </code></pre>
  
  <p>but this hangs on the above input.</p>
  
  <p>What is the correct grammar that will handle multiple blank lines as delimiters?</p>