title: scala dsl parser: rep, opt and regexps
body: <p>Learning how to use scala DSL:s and quite a few examples work nice.
  However I get stuck on a very simple thing:</p>
  
  <p>I'm parsing a language, which has '--' as comment until end of line.</p>
  
  <p>A single line works fine using:</p>
  
  <pre><code>    def comment: Parser[Comment] = """--.*$""".r ^^ { case c =&gt; Comment(c) }
  </code></pre>
  
  <p>But when connecting multiple lines I get an error.</p>
  
  <p>I've tried several varaints, but the following feel simple:</p>
  
  <pre><code>    def commentblock: Parser[List[Comment]] = opt(rep(comment)) ^^ {
      case Some(x) =&gt; { x }
      case None =&gt; { List() }
  }
  </code></pre>
  
  <p>When running a test with two consecutive commentlines I get an error.</p>
  
  <p>Testcase:</p>
  
  <pre><code>--Test Comment
  --Test Line 2
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>java.lang.AssertionError: Parse error: [1.1] failure: string matching regex `--.*$' expected but `-' found
  </code></pre>
  
  <p><em><strong>Any ideas on how I should fix this?</em></strong></p>
  
  <p>Complete code below:</p>
  
  <pre><code>    import scala.util.parsing.combinator._
  
  abstract class A
  case class Comment(comment:String) extends A
  
  object TstParser extends JavaTokenParsers {
      override def skipWhitespace = true;
  
      def comment: Parser[Comment] = """--.*$""".r ^^ { case c =&gt; Comment(c) }
  
      def commentblock: Parser[List[Comment]] = opt(rep(comment)) ^^ {
          case Some(x) =&gt; { x }
          case None =&gt; { List() }
      }
  
      def parse(text : String) = {
          parseAll(commentblock, text)
      }
  }
  
  class TestParser {
      import org.junit._, Assert._
  
      @Test def testComment() = {
          val y = Asn1Parser.parseAll(Asn1Parser.comment, "--Test Comment")
          assertTrue("Parse error: " + y, y.successful)
          val y2 = Asn1Parser.parseAll(Asn1Parser.commentblock, 
  """--Test Comment
  --Test Line 2
  """)
          assertTrue("Parse error: " + y2, y2.successful)
      }
  </code></pre>
  
  <p>}</p>