After learning how to pass regexes
as arguments, I've tried to build my first regex using a sub
, and I'm stuck once more. Sorry for the complex rules below, I've made my best to simplify them. I need at least some clues how to approach this problem.
The regex
should consist of alternations, each of them consisting of left
, middle
and right
, where left
and right
should come in pairs and the variant of middle
depends on which right
is chosen.
An array of Pairs
contains pairs of left
and right
:
my Pair @leftright =
A => 'a',
...
Z => 'z',
;
Middle
variants are read from a hash:
my Regex %middle =
z => / foo /,
a => / bar /,
m => / twi /,
r => / bin /,
...
;
%middle<z>
should be chosen if right
is z
, %middle<a>
— if right
is a
, etc.
So, the resulting regex should be
my token word {
| A <%middle[a]> a
| Z <%middle[z]> z
| ...
}
or, more generally
my token word {
| <left=@leftright[0].key>
<middle=%middle{@leftright[0].value}>
<right=@leftright[0].value>
| (the same for index == 1)
| (the same for index == 2)
| (the same for index == 3)
...
}
and it should match Abara
and Zfooz
.
How to build token word
(which can be used e.g. in a grammar
) with a sub
that will take every pair from @leftright
, put the suitable %middle{}
depending on the value of right
and then combine it all into one regex
?
my Regex sub sub_word(Pair @l_r, Regex %m) {
...
}
my token word {
<{sub_word(@leftright, %middle)}>
}
After the match I need to know the values of left
, middle
, and right
:
"Abara" ~~ &word;
say join '|', $<left>, $<middle>, $<right> # A|bar|a