如何将这三种方法合二为一

Fre*_*ind 5 methods scala

我正在使用parboiled来编写解析器.我将一些方法定义为:

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) }
Run Code Online (Sandbox Code Playgroud)

你可以看到它们非常相似.我想把它们组合成一个,但我不知道该怎么做.也许它应该是:

def InlineCharsBefore(seps: String*) = rule { ??? }
Run Code Online (Sandbox Code Playgroud)

par*_*tic 6

vararg版本可以实现为:

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

但是,我不使用parboiled所以我无法测试它.