Raku / old-issue-tracker

Tickets from RT
https://github.com/Raku/old-issue-tracker/issues
2 stars 1 forks source link

non-ratchet right-hand branch of `||` causes backtracking into the alternation #6570

Open p6rt opened 6 years ago

p6rt commented 6 years ago

Migrated from rt.perl.org#132219 (status was 'new')

Searchable as RT132219$

p6rt commented 6 years ago

From @smls

When backtracking is disabled for an alternation (via `​:` modifier or `​:ratchet` mode), this obviously means that once the alternation has matched once it may no longer reconsider which branch it wants to select​:

  say "ab" ~~ / [ab || a ]​: b /; # Nil

But does it also mean that once the alternation has matched, the selected branch *itself* may no longer do any backtracking?

I would have thought so, but Rakudo's current behavior is inconsistent​:

  say "ab" ~~ / [ .+ | x ]​: b /; # Nil   say "ab" ~~ / [ .+ || x ]​: b /; # Nil

  say "ab" ~~ / [ x | .+ ]​: b /; # Nil   say "ab" ~~ / [ x || .+ ]​: b /; # 「ab」

I.e. in most cases, it commits to what the alternation matched the first time. But when

1) it is an `||` alternation, 2) and its *last* branch matched, 3) and said branch is a backtrackable atom which (unlike the alternation itself) is not affected by `​:ratchet`,

then it looks like that backtrackable atom can reconsider what it wants to match even after the regex execution has moved past the alternation.

I suppose another way to phrase the @​LARRY design question is this​: Does an alternation select a particular match value (like a "function"), or does it select a subregex (like a "macro")?

(PS​: I should note that the backtracking behaviors of || and | were even less consistent before my NQP commit 263257a9c¹ [see RT #​130117²], which was supposed to bring `||` in line. But I didn't discover this edge case until now, and indeed am not sure how to fix it no matter which way it *should* be fixed.)


[1] https://github.com/perl6/nqp/commit/263257a9c [2] https://rt-archive.perl.org/perl6/Ticket/Display.html?id=130117


This is Rakudo version 2017.09-203-g98fae3d84 built on MoarVM version 2017.09.1-553-ga4fef0bd implementing Perl 6.c.