0

This is a very similar question to Scala: How to combine parser combinators from different objects, but it is different in that the solution proposed there does not work for my use case.

I have been unable to find a solution given the somewhat sparse documentation for Scala Parser Combinators, but I am also new to Scala so it may be that I am missing something.

Problem statement: I am trying to combine two regex parser combinators that handle whitespace very differently. As per the solution for the question above, I have put the parser I want to combine into a trait for my class to use. The issue is that one parser combinator overrides the skipWhitespace member of RegexParsers, and the other does not:

// This parser handles whitespace in a very particular way
trait WhiteSpaceParser extends RegexParsers {
    override def skipWhitespace = false
    def myWhiteSpaceParser: Parser[Any] = // <special whitespace CFG implementation>
}

// This parser does not care about whitespace, except where myWhiteSpaceParser is used
class DocumentParser extends RegexParsers with WhiteSpaceParser
{
    // This parser must skip whitespace, i.e., skipWhitespace = true

    def myDocumentParser: Parser[Any] = // <implementation that uses myWhiteSpaceParser>
}

Question: How can I utilize myWhiteSpaceParser, where whitespace is handled in a very particular way, from within DocumentParser, where whitespace should be ignored (aside from this exception)? One solution is to handle whitespace everywhere, but requires a lot of changes throughout DocumentParser and is more error prone. I am hoping that there is another way.

Thanks!

2
  • this might help: stackoverflow.com/questions/10810172/… Commented May 30, 2017 at 1:27
  • There are some other Scala parsers that you might want to consider. Parboiled2 is very fast (much faster than Scala Parser Combinator), and IMHO handles whitespace in a more intuitive manner. I know it can handle the case you're looking to address. I'm less familiar with FastParse, but that might work for you too... Commented May 30, 2017 at 2:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.