Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3442122
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:37:56+00:00 2026-05-18T08:37:56+00:00

Learning how to use scala DSL:s and quite a few examples work nice. However

  • 0

Learning how to use scala DSL:s and quite a few examples work nice.
However I get stuck on a very simple thing:

I’m parsing a language, which has ‘–‘ as comment until end of line.

A single line works fine using:

    def comment: Parser[Comment] = """--.*$""".r ^^ { case c => Comment(c) }

But when connecting multiple lines I get an error.

I’ve tried several varaints, but the following feel simple:

    def commentblock: Parser[List[Comment]] = opt(rep(comment)) ^^ {
    case Some(x) => { x }
    case None => { List() }
}

When running a test with two consecutive commentlines I get an error.

Testcase:

--Test Comment
--Test Line 2

Error:

java.lang.AssertionError: Parse error: [1.1] failure: string matching regex `--.*$' expected but `-' found

Any ideas on how I should fix this?

Complete code below:

    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 => Comment(c) }

    def commentblock: Parser[List[Comment]] = opt(rep(comment)) ^^ {
        case Some(x) => { x }
        case None => { 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)
    }

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:37:56+00:00Added an answer on May 18, 2026 at 8:37 am

    Not familiar with Scala, but in Java, the regex --.*$ matches:

    • --   two hyphens;
    • .*   followed by zero or more characters other than line breaks;
    • $     followed by the end of the input (not necessarily the end of the line!).

    So you could try:

    def comment: Parser[Comment] = """--.*""".r ^^ { case c => Comment(c) }
    

    or even:

    def comment: Parser[Comment] = """--[^\r\n]*""".r ^^ { case c => Comment(c) }
    

    Note that in both cases, the line break is left in place and not “consumed” by your comment “rule”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.