Looking at the parboiled2 section, Rule Combinators and Modifiers:

I don't understand the a, b, and then a ~ b diagram.
So far I've found the documentation straightforward. But I am a bit lost here.
Can you please explain each block?
Looking at the parboiled2 section, Rule Combinators and Modifiers:

I don't understand the a, b, and then a ~ b diagram.
So far I've found the documentation straightforward. But I am a bit lost here.
Can you please explain each block?
Here's the definition of
Rule:The documentation you linked gives more explanation, but essentially
Iis the input to the Rule andOis the output of the Rule. The colon notation can be a little confusing.parboiled2uses a stack to manage the state. Just remember that the types in the colon lists (HList) are produced/pushed from left to right, and consumed/popped from right to left. In theHListA:B:C,Cis the top of the stack, and has to be consumed first.~runs one rule then the next. So in the first exampleaof typeRule[, A]consumes nothing and 'produces' anA, whilebof typeRule[, B]consumes nothing and 'produces' aB. It makes sense then, that if you ranafollowed byb, you would produce anAfollowed by aB. The resulting type isRule[, A:B].Things become much more complicated when you add inputs. You need to make sure that the types produced by
a(or whatever the first rule is) are the types thatbis going to consume.~is just like function composition. If we want to composegandfto getg . f, we need to be sure that the output offis the same type as the input ofg.Let's look at the third example in the table.
ahas typeRule[A, B:C]bhas typeRule[D:B:C, E:F]When we run
aanAis consumed from the stack, aBis added to the stack, and aCis added to the stack. Thenbis run, first it consumes theC, then theB, then it consumes aDoff the stack. To be in the right place at the right time, thatDwould need to be under theAthataconsumed.bwould then produce anEthen anF.In total, we consumed a
Dand anA. TheBandCdon't count because they were produced and consumed internally to the rule. After consumingDandA, we leaveEandFon the stack. So, the type ofa ~ bisRule[D:A, E:F].The fourth example in the README gives an example where
aproduces the wrong types forbto consume. In that casea ~ bis illegal.