I'm using parboiled to write a parser. I 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 they are very similar. I want to combine them into one, but I don't know how to do it. Maybe it should be:
def InlineCharsBefore(seps: String*) = rule { ??? }
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 don't use parboiled so I cannot test it.
精彩评论