parboiled2 icon indicating copy to clipboard operation
parboiled2 copied to clipboard

Support zeroOrMore for Rule2+

Open kiritsuku opened this issue 10 years ago • 6 comments

The following code

def f: RuleN[Int :: Int :: Int :: HNil] = ???
def g = rule { zeroOrMore(f) }

results in the following error:

- The optional, zeroOrMore, oneOrMore and times modifiers can only be used on rules of type 
     Rule0, Rule1[T] and Rule[I, O <: I]!

This restriction is tedious, why does it exist? I worked around the problem by nesting the return value:

def f: RuleN[(Int :: Int :: Int :: HNil) :: HNil] = ???

But this requires further overhead by constructing the return value and by destructing it afterwards. Are there other workarounds?

kiritsuku avatar Dec 28 '14 22:12 kiritsuku

Currently the typing is like this (one of the three possible cases):

zeroOrMore(RuleN[A :: HNil]): RuleN[Seq[A] :: HNil]

or shorter

zeroOrMore(Rule1[A]): Rule1[Seq[A]]

What would you like the type of

zeroOrMore(RuleN[A :: B :: C :: HNil])

to be?

sirthias avatar Jan 02 '15 13:01 sirthias

Looking in more detail to the types, I can see why it does not work but it is still a very weird limitation and one that makes handling rules that produce multiple values a lot harder.

kiritsuku avatar Jan 02 '15 16:01 kiritsuku

What you want is this:

zeroOrMore(RuleN[T :: HNil]): RuleN[Seq[T] :: HNil]
zeroOrMore(RuleN[T :: T :: HNil]): RuleN[Seq[T] :: HNil]
zeroOrMore(RuleN[T :: T :: T :: HNil]): RuleN[Seq[T] :: HNil]
...

which I think is actually a reasonable request. I'm going to reopen the ticket.

sirthias avatar Jan 05 '15 16:01 sirthias

Actually, what I want is

zeroOrMore(RuleN[A :: B :: C :: ... :: HNil]): RuleN[Seq[A :: B :: C :: ... :: HNil] :: HNil]

i.e. return multiple values (of different type) every time the rule is called.

kiritsuku avatar Jan 05 '15 16:01 kiritsuku

If that's what you want you can simply group your values into a tuple, no?

sirthias avatar Jan 07 '15 10:01 sirthias

Yes indeed I can do that. Another thing I overlooked.

kiritsuku avatar Jan 07 '15 10:01 kiritsuku