If you do make this a grammar, I think there's more than one way to
have " {@a.push($/.pos)}/" fire after every match, and not repeat that
code snippit on each rule... keep that in mind as a goal...

-y

On Tue, Aug 20, 2019 at 7:13 AM William Michels via perl6-users
<perl6-users@perl.org> wrote:
>
> Thanks to Brad Gilbert's code contribution in this thread, I re-wrote
> a small snippet of his code (code that incrementally checks a series
> of regex matches), to have it return the last position of each match.
> Testing with three 'matches' and one 'willnotmatch' returns three
> positional values, as expected:
>
> use v6
>   my $test = "      foo bar";
>
> sub foo($x) {
>   state @a = 0;
>     $x ~~ m /^\s+  {@a.push($/.pos)}/;
>     $x ~~ m :pos(@a[*-1]) /foo\s+  {@a.push($/.pos)}/;
>     $x ~~ m :pos(@a[*-1]) /willnotmatch  {@a.push($/.pos)}/;
>     $x ~~ m :pos(@a[*-1]) /bar   {@a.push($/.pos)}/;
>   return @a[1 .. *];
> }
>
>   #say foo($test); # returns (6 10 13)
>   put foo($test); # returns 6 10 13
>
>
> I'm actually pleasantly surprised that I can add a dozen or so
> 'willnotmatch' lines, and it doesn't screw up the result. The next
> step might be to 1). pull the individual regexes out into an object
> (as suggested in the SO post below) to simplify each smartmatch line,
> and/or 2). store the results in a hash (instead of an array), for
> later substring extraction. But at this point it seems I'm getting
> into 'Grammar' territory, so that might be the better approach.
>
> HTH, Bill.
>
> https://stackoverflow.com/questions/50829126/perl6-interpolate-array-in-match-for-and-or-not-functions/50838441#50838441
>
>
>
>
>
>
>
>
> On Mon, Aug 19, 2019 at 1:08 AM Patrick Spek via perl6-users
> <perl6-users@perl.org> wrote:
> >
> > On Sun, 18 Aug 2019 13:45:27 -0300
> > Aureliano Guedes <guedes.aureli...@gmail.com> wrote:
> >
> > > Even being another language, Perl6 should be inheriting Perl5's
> > > regexes or even improving it not making it uglier and harder.
> > >
> > > Or I'm seeing how to use it in an easy way. Also, dunno if there is
> > > some GOOD documentation to Perl6 regexes.
> >
> > Beauty is in the eye of the beholder. While I'm much more proficient
> > with PCRE than P6CRE, I do find the Perl 6 variants to be much cleaner
> > and easier to understand when reading regexes of others.
> >
> > If you find that there's a lack of documentation explaining things
> > clearly to you, that'd be an issue to solve in the documentation. This
> > takes a lot of effort, and if you would be so kind as to improve it
> > where you think it's needed, it would be a great help to everyone (we
> > can't really see how or where you're looking for what, after all).
> >
> > --
> > With kind regards,
> >
> > Patrick Spek
> >
> >
> > www:  https://www.tyil.nl/
> > mail: p.s...@tyil.nl
> > pgp:  1660 F6A2 DFA7 5347 322A  4DC0 7A6A C285 E2D9 8827
> >
> > social: https://soc.fglt.nl/tyil
> > git:    https://gitlab.com/tyil/

Reply via email to