1
0
mirror of https://github.com/janet-lang/janet synced 2025-02-21 18:50:02 +00:00

Address #1554 - capture first 0-width match in repeating rules and

optionals.

This fixes some undesirable interplay between lookaheads and looping
(repeating) rules where lookaheads caused the loop to immediately
terminate _and_ discard any captures. This change adds an exception if
there is a 0-width match at the first iteration of the loop, in which
case captures will be kept, although the loop will still be terminated
(any further iteration of the loop would possibly cause an infinite
 loop).

This allows optionals to work as expected when combined with lookahead.
There is also a question of whether or not optional should be
implemented as separate rule rather than as `(between 0 1 subrule)`.
This commit is contained in:
Calvin Rose 2025-02-02 20:34:36 -06:00
parent 1b278fc657
commit d30fd27575
2 changed files with 9 additions and 1 deletions

View File

@ -343,7 +343,7 @@ tail:
CapState cs2 = cap_save(s);
next_text = peg_rule(s, rule_a, text);
if (!next_text || next_text == text) {
cap_load(s, cs2);
if (!next_text || captured > 0) cap_load(s, cs2);
break;
}
captured++;

View File

@ -789,5 +789,13 @@
"abc123"
@["abc123"])
# Issue 1554
(test "issue 1554 case 1" '(any (> '1)) "abc" @["a"])
(test "issue 1554 case 2" '(any (? (> '1))) "abc" @["a"])
(test "issue 1554 case 3" '(any (> (? '1))) "abc" @["a"])
(test "issue 1554 case 4" '(* "a" (> '1)) "abc" @["b"])
(test "issue 1554 case 5" '(* "a" (? (> '1))) "abc" @["b"])
(test "issue 1554 case 6" '(* "a" (> (? '1))) "abc" @["b"])
(end-suite)