How to combine these three methods into one

I use parboiled to write a parser. I have defined some methods as:

def InlineCharsBefore(sep: String) 
    = rule { zeroOrMore(!str(sep) ~ InlineChar) }
def InlineCharsBefore(sep1: String, sep2: String) 
    = rule { zeroOrMore((!str(sep1) | !str(sep2)) ~ InlineChar) }
def InlineCharsBefore(sep1: String, sep2: String, sep3: String) 
    = rule { zeroOrMore((!str(sep1) | !str(sep2) | !str(sep3)) ~ InlineChar) }

You can see that they are very similar. I want to combine them into one, but I do not know how to do it. Maybe it should be:

def InlineCharsBefore(seps: String*) = rule { ??? }
+5
source share
1 answer

The vararg version can be implemented as:

 def InlineCharsBefore( seps: String* ) = {
   val sepMatch = seps.map( s => ! str(s) ).reduceLeft( _ | _ )
   rule { zeroOrMore( sepMatch ~ InlineChar) }
 }

However, I do not use steamed, so I can not test it.

+6
source

All Articles