Skip to content

Commit e84c7a5

Browse files
committed
clarify the case where multiple captures with the same name exists
1 parent be5a1fc commit e84c7a5

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed
9.69 MB
Binary file not shown.

python/llguidance/_lib.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,16 @@ class LLMatcher:
430430
def get_capture(self, name: str) -> Optional[bytes]:
431431
"""
432432
Get the captured bytes for a named capture group.
433-
Returns None if the capture group does not exist.
433+
If there are multiple captures with the same name (e.g., due to repetition of rules),
434+
returns the last matching capture.
435+
Returns None if no capture with the given name exists.
434436
"""
435437

436438
def get_captures(self) -> List[Tuple[str, bytes]]:
437439
"""
438440
Get all captured groups as a list of (name, bytes) tuples.
441+
Includes all captures, even when there are multiple captures
442+
with the same name (e.g., due to repetition of rules).
439443
"""
440444

441445

python/torch_tests/test_matcher.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -668,19 +668,20 @@ def test_grammar_warnings() -> None:
668668

669669

670670
def test_get_capture() -> None:
671-
grm = r"""start: "hello " group1 group2
671+
grm = r"""start: "hello " group1 group2+ group3 " end"
672672
group1[capture,lazy]: /[a-z]+/
673-
group2[capture="body"]: /[a-z]{4}/"""
673+
group2[capture="body"]: /[a-z]{4}/
674+
group3[capture="body"]: /[0-9]{4}/"""
674675
m = matcher(grm)
675676
check_err(m)
676677

677678
assert m.get_capture("group1") is None
678679
assert m.get_capture("body") is None
679680
assert m.get_capture("non-existent-group") is None
680681
assert m.get_captures() == []
681-
m.consume_tokens(tokenizer().tokenize_str("hello world"))
682+
m.consume_tokens(tokenizer().tokenize_str("hello worldabcd1234 end"))
682683
assert m.is_stopped()
683684
assert m.get_capture("group1") == b"w"
684-
assert m.get_capture("body") == b"orld"
685+
assert m.get_capture("body") == b"1234"
685686
assert m.get_capture("non-existent-group") is None
686-
assert m.get_captures() == [("group1", b"w"), ("body", b"orld")]
687+
assert m.get_captures() == [("group1", b"w"), ("body", b"orld"), ("body", b"abcd"), ("body", b"1234")]

0 commit comments

Comments
 (0)