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!