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


*Description:*
Longest alternation seems to only evaluate the first nested alternation of
its right side arguments.
That is, if the first regex of an alternation <||> following longest
alternation <|>  fails, and the regex preceding the longest alternation
succeeds, the longest alternation uses the preceding/ left side match
rather than evaluating the rest of the right side alternation which could
have a potentially longer match.

*Example 1:*
'succeed'.match(/ suc | [ fail || succeed ]/).say;

Returns:
「suc」

Expected:
「succeed」

*Example 2:*
grammar TEST {
    regex NestedAlternation { fail || succeed }
    regex LongestAlternation { suc | <NestedAlternation> }
    regex TOP { <LongestAlternation> .* }
}
TEST.parse('succeed').say;

Returns:
「succeed」
 LongestAlternation => 「suc」

Expected:
「succeed」
 LongestAlternation => 「succeed」
  NestedAlternation => 「succeed」

Note if we remove the trailing '.*' in TOP, backtracking forces it to
evaluate the NestedAlternation and return the expected result. However,
backtracking back into the NestedAlternation should not be necessary.

*Example 3:*
grammar TEST {
    token NestedAlternation { fail || succeed }
    token LongestAlternation { suc | <NestedAlternation> }
    token TOP { <LongestAlternation> }
}
TEST.parse('succeed').say;


Returns:
Nil

Expected:
「succeed」
 LongestAlternation => 「succeed」
  NestedAlternation => 「succeed」

Reply via email to