# New Ticket Created by  Aleks-Daniel Jakimenko-Aleksejev 
# Please include the string:  [perl #130117]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org/Ticket/Display.html?id=130117 >


*Code:*
say ‘abcz’ ~~ /:r [‘a’ || ‘abc’ ] ‘z’ /

*Result:*
「abcz」

:r (:ratchet) should prevent backtracking (trying different ways to match a
string). However, it seems like it does not work as intended (or even at
all?).

To make the example above as clear as possible: || does not do LTM (longest
token matching), so it will match things from left to right. ‘a’ matches
just fine, so it proceeds. Then ‘z’ will not match, at which point it
should give up because of :r. However, it goes back and tries ‘abc’, which
is exactly what somebody would expect from backtracking, but we are trying
to turn it off.

There are several ways to make it clear that backtracking actually happens
when using || with :r (if example above is not enough):

*Code:*
grammar G { token TOP { [ ‘a’ {say $/.CURSOR.pos} || {say $/.CURSOR.pos}
‘abc’ ] {say ‘here’} ‘z’ } }; say G.parse(‘abcz’)

*Result:*
1
here
0
here
「abcz」

*Code:*
say ‘abcdefghij’ ~~ /:r [.||.||.||.||.||.||.||.] [.||.||.||.||.||.||.||.]
[.||.||.||.||.||.||.||.] [.||.||.||.||.||.||.||.] [.||.||.||.||.||.||.||.]
[.||.||.||.||.||.||.||.] [.||.||.||.||.||.||.||.] [.||.||.||.||.||.||.||.]
[.||.||.||.||.||.||.||.] [.||.||.||.||.||.||.||.] [.||.||.||.||.||.||.||.]
<!> /

*Result:*
(Takes a really long time to finish because it attempts to try all of the
paths, even though “token” has implicit :r)


According to committable, this behavior has been there since the beginning
( https://gist.github.com/Whateverable/814019538d89ec53fca6c09269136ebd ).
Therefore, I am not sure how much code will break because of us fixing this
bug. However, I am hoping to see some performance improvement after we fix
this.

Reply via email to