develooper Front page | perl.perl6.users | Postings from March 2021

Re: "rule" declarator: Different results for 'unadorned' match vsunnamed/named captures? (in re Grammars...).

Thread Previous
From:
Ralph Mellor
Date:
March 12, 2021 15:00
Subject:
Re: "rule" declarator: Different results for 'unadorned' match vsunnamed/named captures? (in re Grammars...).
Message ID:
CAPLR5ScHTF4VfMfNaWMNSi92tkDQh9S2FrPpXqKnNpr43paEJQ@mail.gmail.com
On Thu, Mar 11, 2021 at 8:53 PM William Michels <wjm1@caa.columbia.edu> wrote:
>
> I think there's something going on with the examples below, as I'm
> seeing different results when comparing a basic "rule" match vs either
> unnamed or named "rule" captures.

All your examples are correct according to my current understanding.

The key issue is that the patterns you're sub-capturing exclude the
significant space whereas the non-captured ones don't.

I'll respond only to your TL;DR in this comment. Perhaps give yourself
time to have a couple repl sessions, then time away from the computer
doing something completely different, before responding to go through
any residual confusion, or if it turns out I've gotten something wrong.

----

> say $/ if 'ab'  ~~ rule  {.? .?}
「a」

The first `.? ` (including the token boundary implied by the space)
matches against the `a`. It fails.

The second `.?` then matches the `a`.

The `~~` succeeds and reports the match of `a`.

> say $/ if 'ab'  ~~ rule  {(.?) (.?)}

The first `.?` (excluding the token boundary) matches against the `a`
and succeeds.

`:ratchet` is in effect so the engine will not reconsider this decision.

The space in the pattern then fails to match a token boundary.

Due to `:ratchet` being in effect, the entire rule fails.

Because you're using `~~`, the engine then moves forward one character
in the input, pretending the input begins with `b`.

The first `.?` matches against the `b` and succeeds.

The space in the pattern successfully matches a token boundary (end of
input).

Thus the rule succeeds, and so the `~~` succeeds, and reports a match of `b`.

--
love, raiph

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About