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
I
is the input to the Rule andO
is the output of the Rule. The colon notation can be a little confusing.parboiled2
uses 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 theHList
A:B:C
,C
is the top of the stack, and has to be consumed first.~
runs one rule then the next. So in the first examplea
of typeRule[, A]
consumes nothing and 'produces' anA
, whileb
of typeRule[, B]
consumes nothing and 'produces' aB
. It makes sense then, that if you rana
followed byb
, you would produce anA
followed 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 thatb
is going to consume.~
is just like function composition. If we want to composeg
andf
to getg . f
, we need to be sure that the output off
is the same type as the input ofg
.Let's look at the third example in the table.
a
has typeRule[A, B:C]
b
has typeRule[D:B:C, E:F]
When we run
a
anA
is consumed from the stack, aB
is added to the stack, and aC
is added to the stack. Thenb
is run, first it consumes theC
, then theB
, then it consumes aD
off the stack. To be in the right place at the right time, thatD
would need to be under theA
thata
consumed.b
would then produce anE
then anF
.In total, we consumed a
D
and anA
. TheB
andC
don't count because they were produced and consumed internally to the rule. After consumingD
andA
, we leaveE
andF
on the stack. So, the type ofa ~ b
isRule[D:A, E:F]
.The fourth example in the README gives an example where
a
produces the wrong types forb
to consume. In that casea ~ b
is illegal.