MCPcopy Create free account
hub / github.com/abetlen/llama-cpp-python / _visit_pattern

Method _visit_pattern

examples/server/server.py:364–512  ·  view source on GitHub ↗
(self, pattern: str, name: str)

Source from the content-addressed store, hash-verified

362 )
363
364 def _visit_pattern(self, pattern: str, name: str) -> str:
365 assert pattern.startswith("^") and pattern.endswith("$"), (
366 'Pattern must start with "^" and end with "$"'
367 )
368 pattern = pattern[1:-1]
369 sub_rule_ids: Dict[str, str] = {}
370 index = 0
371 length = len(pattern)
372
373 def to_rule(item: Tuple[str, bool]) -> str:
374 text, is_literal = item
375 return f'"{text}"' if is_literal else text
376
377 def transform() -> Tuple[str, bool]:
378 nonlocal index
379 start = index
380 sequence: List[Tuple[str, bool]] = []
381
382 def get_dot() -> str:
383 rule = self.DOTALL if self._dotall else self.DOT
384 return self._add_rule("dot", rule)
385
386 def join_sequence() -> Tuple[str, bool]:
387 if len(sequence) == 1:
388 return sequence[0]
389 return (" ".join(to_rule(item) for item in sequence), False)
390
391 while index < length:
392 char = pattern[index]
393 if char == ".":
394 sequence.append((get_dot(), False))
395 index += 1
396 elif char == "(":
397 index += 1
398 if index < length:
399 assert pattern[index] != "?", (
400 f'Unsupported pattern syntax "{pattern[index]}" at index {index} of /{pattern}/'
401 )
402 sequence.append((f"({to_rule(transform())})", False))
403 elif char == ")":
404 index += 1
405 assert start > 0 and pattern[start - 1] == "(", (
406 f"Unbalanced parentheses; start = {start}, index = {index}, pattern = {pattern}"
407 )
408 return join_sequence()
409 elif char == "[":
410 square_brackets = char
411 index += 1
412 while index < length and pattern[index] != "]":
413 if pattern[index] == "\\":
414 square_brackets += pattern[index : index + 2]
415 index += 2
416 else:
417 square_brackets += pattern[index]
418 index += 1
419 assert index < length, (
420 f"Unbalanced square brackets; start = {start}, index = {index}, pattern = {pattern}"
421 )

Callers 1

visitMethod · 0.95

Calls 1

_add_ruleMethod · 0.95

Tested by

no test coverage detected