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 { ??? }
What you should do is parameterize the methods, by making parameter(s) of all difference between two equal functions. E.g. Use an if statement depending on the bool add. Btw, it would be better to use an enum instead of a boolean.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With