thekevinscott commited on
Commit
529e22e
·
verified ·
1 Parent(s): ba69853

Upload experiments v1, v2-models-effort, v2-tests-configuration as on disk 2026-11-11 (part 62)

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/rules_builder/types.py +28 -0
  2. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/__init__.py +0 -0
  3. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/__pycache__/__init__.cpython-314.pyc +0 -0
  4. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/__pycache__/is_point_in_range.cpython-314.pyc +0 -0
  5. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/errors/__init__.py +4 -0
  6. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/errors/__pycache__/__init__.cpython-314.pyc +0 -0
  7. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/errors/__pycache__/build_error_position.cpython-314.pyc +0 -0
  8. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/errors/__pycache__/get_input_as_string.cpython-314.pyc +0 -0
  9. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/errors/__pycache__/grammar_parse_error.cpython-314.pyc +0 -0
  10. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/errors/__pycache__/input_parse_error.cpython-314.pyc +0 -0
  11. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/errors/build_error_position.py +20 -0
  12. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/errors/get_input_as_string.py +13 -0
  13. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/errors/grammar_parse_error.py +34 -0
  14. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/errors/input_parse_error.py +50 -0
  15. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/is_point_in_range.py +6 -0
  16. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/pyproject.toml +50 -0
  17. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/tests/generated/iteration/__pycache__/iteration_test.cpython-314-pytest-9.1.1.pyc +0 -0
  18. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/tests/generated/iteration/__pycache__/iteration_with_additional_strings_test.cpython-314-pytest-9.1.1.pyc +0 -0
  19. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/tests/generated/iteration/__pycache__/iteration_with_an_initial_string_test.cpython-314-pytest-9.1.1.pyc +0 -0
  20. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/tests/generated/iteration/iteration_test.py +313 -0
  21. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/tests/generated/iteration/iteration_with_additional_strings_test.py +1005 -0
  22. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/tests/generated/iteration/iteration_with_an_initial_string_test.py +1228 -0
  23. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/tests/generated/validation/__pycache__/validate_grammar_test.cpython-314-pytest-9.1.1.pyc +0 -0
  24. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/tests/generated/validation/__pycache__/validate_input_test.cpython-314-pytest-9.1.1.pyc +0 -0
  25. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/tests/generated/validation/validate_grammar_test.py +82 -0
  26. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/tests/generated/validation/validate_input_test.py +320 -0
  27. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/proxy.log +19 -0
  28. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/result.json +1 -0
  29. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/run.json +11 -0
  30. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/stderr.txt +0 -0
  31. v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/transcript/.claude/projects/-target/b0e5b3d5-7ce4-4ad3-ba39-bc03b3df8e79.jsonl +0 -0
  32. v2-tests-configuration/runs/20261012T023847Z_df0e91f6/manifest.json +98 -0
  33. v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/README.md +19 -0
  34. v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/__init__.py +16 -0
  35. v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/gbnf.py +29 -0
  36. v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/__init__.py +3 -0
  37. v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/colorize.py +18 -0
  38. v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/generic_set.py +36 -0
  39. v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/get_input_as_code_points.py +4 -0
  40. v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/get_parent_stack_id.py +15 -0
  41. v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/get_serialized_rule_key.py +20 -0
  42. v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/graph.py +167 -0
  43. v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/graph_node.py +28 -0
  44. v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/graph_pointer.py +70 -0
  45. v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/parse_state.py +40 -0
  46. v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/print.py +69 -0
  47. v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/rule_ref.py +24 -0
  48. v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/serialize.py +13 -0
  49. v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/type_guards.py +58 -0
  50. v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/types.py +20 -0
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/rules_builder/types.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import dataclass, field
4
+ from enum import StrEnum
5
+
6
+
7
+ class InternalRuleType(StrEnum):
8
+ CHAR = "CHAR"
9
+ CHAR_RNG_UPPER = "CHAR_RNG_UPPER"
10
+ RULE_REF = "RULE_REF"
11
+ ALT = "ALT"
12
+ END = "END"
13
+ CHAR_NOT = "CHAR_NOT"
14
+ CHAR_ALT = "CHAR_ALT"
15
+
16
+
17
+ @dataclass
18
+ class InternalRuleDef:
19
+ """
20
+ A linear rule definition.
21
+
22
+ For CHAR and CHAR_NOT, `value` is a list of code points.
23
+ For RULE_REF, CHAR_ALT and CHAR_RNG_UPPER, `value` is a single number.
24
+ For ALT and END, `value` is None.
25
+ """
26
+
27
+ type: InternalRuleType
28
+ value: list[int] | int | None = field(default=None)
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/__init__.py ADDED
File without changes
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/__pycache__/__init__.cpython-314.pyc ADDED
Binary file (129 Bytes). View file
 
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/__pycache__/is_point_in_range.cpython-314.pyc ADDED
Binary file (644 Bytes). View file
 
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/errors/__init__.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ from .grammar_parse_error import GrammarParseError
2
+ from .input_parse_error import InputParseError
3
+
4
+ __all__ = ["GrammarParseError", "InputParseError"]
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/errors/__pycache__/__init__.cpython-314.pyc ADDED
Binary file (296 Bytes). View file
 
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/errors/__pycache__/build_error_position.cpython-314.pyc ADDED
Binary file (1.35 kB). View file
 
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/errors/__pycache__/get_input_as_string.cpython-314.pyc ADDED
Binary file (1.1 kB). View file
 
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/errors/__pycache__/grammar_parse_error.cpython-314.pyc ADDED
Binary file (2.64 kB). View file
 
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/errors/__pycache__/input_parse_error.cpython-314.pyc ADDED
Binary file (3.53 kB). View file
 
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/errors/build_error_position.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ MAXIMUM_NUMBER_OF_ERROR_LINES_TO_SHOW = 3
4
+
5
+
6
+ def build_error_position(src: str, pos: int) -> list[str]:
7
+ if src == "":
8
+ return ["No input provided"]
9
+
10
+ lines = src.split("\n")
11
+
12
+ line_idx = 0
13
+ while line_idx < len(lines) and lines[line_idx] and pos > len(lines[line_idx]) - 1:
14
+ pos -= len(lines[line_idx])
15
+ line_idx += 1
16
+
17
+ lines_to_show: list[str] = []
18
+ for i in range(max(0, line_idx - (MAXIMUM_NUMBER_OF_ERROR_LINES_TO_SHOW - 1)), line_idx + 1):
19
+ lines_to_show.append(lines[i] if i < len(lines) else "")
20
+ return [*lines_to_show, " " * pos + "^"]
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/errors/get_input_as_string.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ if TYPE_CHECKING:
6
+ from gbnf.grammar_graph.types import ValidInput
7
+
8
+
9
+ def get_input_as_string(src: ValidInput) -> str:
10
+ if isinstance(src, str):
11
+ return src
12
+ code_points = src if isinstance(src, list | tuple) else [src]
13
+ return "".join(chr(cp) for cp in code_points)
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/errors/grammar_parse_error.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from .build_error_position import build_error_position
4
+
5
+
6
+ def GRAMMAR_PARSER_ERROR_HEADER_MESSAGE(reason: str) -> str: # noqa: N802
7
+ return f"Failed to parse grammar: {reason}"
8
+
9
+
10
+ class GrammarParseError(Exception):
11
+ def __init__(self, grammar: str, pos: int, reason: str):
12
+ super().__init__(
13
+ "\n".join([
14
+ GRAMMAR_PARSER_ERROR_HEADER_MESSAGE(reason),
15
+ "",
16
+ *build_error_position(grammar, pos),
17
+ ]),
18
+ )
19
+ self.name = "GrammarParseError"
20
+ self.grammar = grammar
21
+ self.reason = reason
22
+ self.pos = pos
23
+
24
+ def __eq__(self, other: object) -> bool:
25
+ if not isinstance(other, GrammarParseError):
26
+ return NotImplemented
27
+ return (
28
+ self.grammar == other.grammar
29
+ and self.pos == other.pos
30
+ and self.reason == other.reason
31
+ )
32
+
33
+ def __hash__(self) -> int:
34
+ return hash((self.grammar, self.pos, self.reason))
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/errors/input_parse_error.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ from .build_error_position import build_error_position
6
+ from .get_input_as_string import get_input_as_string
7
+
8
+ if TYPE_CHECKING:
9
+ from gbnf.grammar_graph.types import ValidInput
10
+
11
+ INPUT_PARSER_ERROR_HEADER_MESSAGE = "Failed to parse input string:"
12
+
13
+
14
+ class InputParseError(Exception):
15
+ def __init__(self, most_recent_input: ValidInput, pos: int, previous_input: ValidInput = ""):
16
+ self.name = "InputParseError"
17
+ self.most_recent_input = most_recent_input
18
+ self.pos = pos
19
+ self.previous_input = previous_input
20
+ previous = get_input_as_string(previous_input)
21
+ super().__init__(
22
+ "\n".join([
23
+ INPUT_PARSER_ERROR_HEADER_MESSAGE,
24
+ "",
25
+ *build_error_position(
26
+ f"{previous}{get_input_as_string(most_recent_input)}",
27
+ pos + len(previous),
28
+ ),
29
+ ]),
30
+ )
31
+
32
+ @property
33
+ def src(self) -> str:
34
+ return f"{get_input_as_string(self.previous_input)}{get_input_as_string(self.most_recent_input)}"
35
+
36
+ @property
37
+ def error_for_most_recent_input(self) -> str:
38
+ return "\n".join([
39
+ INPUT_PARSER_ERROR_HEADER_MESSAGE,
40
+ "",
41
+ *build_error_position(get_input_as_string(self.most_recent_input), self.pos),
42
+ ])
43
+
44
+ def __eq__(self, other: object) -> bool:
45
+ if not isinstance(other, InputParseError):
46
+ return NotImplemented
47
+ return str(self) == str(other) and self.pos == other.pos
48
+
49
+ def __hash__(self) -> int:
50
+ return hash((str(self), self.pos))
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/gbnf/utils/is_point_in_range.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+
4
+ def is_point_in_range(point: int, rng: list[int]) -> bool:
5
+ start, end = rng[0], rng[1]
6
+ return start <= point <= end
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/pyproject.toml ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [project]
2
+ name = "gbnf"
3
+ version = "0.0.30"
4
+ readme = "README.md"
5
+ requires-python = ">=3.11"
6
+ dependencies = []
7
+ description = "A library for parsing GBNF grammars"
8
+ classifiers = [ "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent",]
9
+ [[project.authors]]
10
+ name = "Ambient Labs"
11
+ email = "maintainers@gbnf.dev"
12
+
13
+ [build-system]
14
+ requires = [ "setuptools",]
15
+ build-backend = "setuptools.build_meta"
16
+
17
+ [project.urls]
18
+ Homepage = "https://github.com/ambient-labs/GBNF/packages/gbnf"
19
+ "Bug Tracker" = "https://github.com/ambient-labs/GBNF/packages/gbnf/issues"
20
+
21
+ [tool.ruff]
22
+ line-length = 120
23
+ preview = true
24
+
25
+ [tool.uv]
26
+ dev-dependencies = [ "pytest-describe>=2.2.0", "pytest-watcher>=0.4.3", "pytest>=8.3.3",]
27
+
28
+ [tool.ruff.lint]
29
+ extend-select = [ "A", "ARG", "B", "BLE", "COM", "E", "F", "FA", "FIX", "FLY", "FURB", "G", "I", "ICN", "ISC", "INP", "PERF", "PGH", "PIE", "PT", "PTH", "RET", "RSE", "RUF", "SLOT", "T20", "TD", "TID", "UP", "W", "YTT",]
30
+ ignore = [ "TD002", "TD003", "TID252", "FIX002", "BLE001", "FLY002",]
31
+ fixable = [ "ALL",]
32
+ unfixable = []
33
+
34
+ [tool.ruff.format]
35
+ quote-style = "double"
36
+ indent-style = "space"
37
+ skip-magic-trailing-comma = false
38
+ line-ending = "auto"
39
+
40
+ [tool.pytest.ini_options]
41
+ minversion = "6.0"
42
+ pythonpath = [ ".",]
43
+ testpaths = [ "gbnf/**/*_test.py",]
44
+
45
+ [tool.ruff.lint.per-file-ignores]
46
+ "**/*_test.py" = [ "T201", "RUF012", "PERF203", "FLY002", "ARG001", "B008", "ARG002", "A002", "A001",]
47
+
48
+ [tool.setuptools.packages.find]
49
+ where = [ ".",]
50
+ include = [ "gbnf*",]
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/tests/generated/iteration/__pycache__/iteration_test.cpython-314-pytest-9.1.1.pyc ADDED
Binary file (5.96 kB). View file
 
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/tests/generated/iteration/__pycache__/iteration_with_additional_strings_test.cpython-314-pytest-9.1.1.pyc ADDED
Binary file (16.1 kB). View file
 
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/tests/generated/iteration/__pycache__/iteration_with_an_initial_string_test.cpython-314-pytest-9.1.1.pyc ADDED
Binary file (16.2 kB). View file
 
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/tests/generated/iteration/iteration_test.py ADDED
@@ -0,0 +1,313 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ from gbnf import GBNF, RuleChar, RuleCharExclude, RuleEnd
3
+
4
+
5
+ def describe_iteration():
6
+ @pytest.mark.parametrize(
7
+ ("grammar", "expected"),
8
+ [
9
+ # single char rule
10
+ (
11
+ 'root ::= "foo"',
12
+ [
13
+ {
14
+ "type": "char",
15
+ "value": [ord("f")],
16
+ },
17
+ ],
18
+ ),
19
+ # two char rules
20
+ (
21
+ 'root ::= "foo" | "bar" ',
22
+ [
23
+ {
24
+ "type": "char",
25
+ "value": [ord("f")],
26
+ },
27
+ {
28
+ "type": "char",
29
+ "value": [ord("b")],
30
+ },
31
+ ],
32
+ ),
33
+ # three char rules
34
+ (
35
+ 'root ::= "foo" | "bar" | "gaz"',
36
+ [
37
+ {
38
+ "type": "char",
39
+ "value": [ord("f")],
40
+ },
41
+ {
42
+ "type": "char",
43
+ "value": [ord("b")],
44
+ },
45
+ {
46
+ "type": "char",
47
+ "value": [ord("g")],
48
+ },
49
+ ],
50
+ ),
51
+ (
52
+ 'root ::= "foo" | "bar" | "baz"',
53
+ [
54
+ {
55
+ "type": "char",
56
+ "value": [ord("f")],
57
+ },
58
+ {
59
+ "type": "char",
60
+ "value": [ord("b")],
61
+ },
62
+ ],
63
+ ),
64
+ # char not
65
+ (
66
+ "root ::= [^x]",
67
+ [
68
+ {
69
+ "type": "char_exclude",
70
+ "value": [ord("x")],
71
+ },
72
+ ],
73
+ ),
74
+ (
75
+ 'root ::= [^f] "o"',
76
+ [
77
+ {
78
+ "type": "char_exclude",
79
+ "value": [ord("f")],
80
+ },
81
+ ],
82
+ ),
83
+ (
84
+ "root ::= [^A-Z]",
85
+ [
86
+ {
87
+ "type": "char_exclude",
88
+ "value": [
89
+ [
90
+ ord("A"),
91
+ ord("Z"),
92
+ ],
93
+ ],
94
+ },
95
+ ],
96
+ ),
97
+ (
98
+ "root ::= [^A-Z0-9]",
99
+ [
100
+ {
101
+ "type": "char_exclude",
102
+ "value": [
103
+ [
104
+ ord("A"),
105
+ ord("Z"),
106
+ ],
107
+ [
108
+ ord("0"),
109
+ ord("9"),
110
+ ],
111
+ ],
112
+ },
113
+ ],
114
+ ),
115
+ (
116
+ "root ::= [^A-Z0-9_-]",
117
+ [
118
+ {
119
+ "type": "char_exclude",
120
+ "value": [
121
+ [
122
+ ord("A"),
123
+ ord("Z"),
124
+ ],
125
+ [
126
+ ord("0"),
127
+ ord("9"),
128
+ ],
129
+ ord("_"),
130
+ ord("-"),
131
+ ],
132
+ },
133
+ ],
134
+ ),
135
+ # expressions
136
+ (
137
+ """
138
+ root ::= foo
139
+ foo ::= "foo"
140
+ """,
141
+ [
142
+ {
143
+ "type": "char",
144
+ "value": [ord("f")],
145
+ },
146
+ ],
147
+ ),
148
+ (
149
+ """
150
+ root ::= f
151
+ f ::= foo
152
+ foo ::= "foo"
153
+ """,
154
+ [
155
+ {
156
+ "type": "char",
157
+ "value": [ord("f")],
158
+ },
159
+ ],
160
+ ),
161
+ # expression and a char rule
162
+ (
163
+ '''
164
+ root ::= foo | "bar"
165
+ foo ::= "foo"''',
166
+ [
167
+ {
168
+ "type": "char",
169
+ "value": [ord("f")],
170
+ },
171
+ {
172
+ "type": "char",
173
+ "value": [ord("b")],
174
+ },
175
+ ],
176
+ ),
177
+ # two expressions
178
+ (
179
+ """
180
+ root ::= foo | bar
181
+ foo ::= "foo"
182
+ bar ::= "bar"
183
+ """,
184
+ [
185
+ {
186
+ "type": "char",
187
+ "value": [ord("f")],
188
+ },
189
+ {
190
+ "type": "char",
191
+ "value": [ord("b")],
192
+ },
193
+ ],
194
+ ),
195
+ # nested expressions
196
+ (
197
+ """
198
+ root ::= f | b
199
+ f ::= fo
200
+ b ::= ba
201
+ fo ::= foo
202
+ ba ::= bar | baz
203
+ foo ::= "foo"
204
+ bar ::= "bar"
205
+ baz ::= "baz"
206
+ """,
207
+ [
208
+ {
209
+ "type": "char",
210
+ "value": [ord("f")],
211
+ },
212
+ {
213
+ "type": "char",
214
+ "value": [ord("b")],
215
+ },
216
+ ],
217
+ ),
218
+ # ranges
219
+ (
220
+ "root ::= [a-z]",
221
+ [
222
+ {
223
+ "type": "char",
224
+ "value": [[ord("a"), ord("z")]],
225
+ },
226
+ ],
227
+ ),
228
+ (
229
+ "root ::= [a-zA-Z]",
230
+ [
231
+ {
232
+ "type": "char",
233
+ "value": [[ord("a"), ord("z")], [ord("A"), ord("Z")]],
234
+ },
235
+ ],
236
+ ),
237
+ # range with ? modifier
238
+ (
239
+ "root ::= [a-z]?",
240
+ [
241
+ {
242
+ "type": "char",
243
+ "value": [[ord("a"), ord("z")]],
244
+ },
245
+ {
246
+ "type": "end",
247
+ },
248
+ ],
249
+ ),
250
+ # range with + modifier
251
+ (
252
+ "root ::= [a-z]+",
253
+ [
254
+ {
255
+ "type": "char",
256
+ "value": [[ord("a"), ord("z")]],
257
+ },
258
+ ],
259
+ ),
260
+ # range with * modifier
261
+ (
262
+ "root ::= [a-z]*",
263
+ [
264
+ {
265
+ "type": "char",
266
+ "value": [[ord("a"), ord("z")]],
267
+ },
268
+ {
269
+ "type": "end",
270
+ },
271
+ ],
272
+ ),
273
+ # rules that aren't used are silently ignored
274
+ (
275
+ '''
276
+ root ::= "foo"
277
+ foo ::= "foo"''',
278
+ [
279
+ {
280
+ "type": "char",
281
+ "value": [ord("f")],
282
+ },
283
+ ],
284
+ ),
285
+ # real world use cases
286
+ # arithmetic
287
+ (
288
+ """
289
+ root ::= (expr "=" term "\
290
+ ")+
291
+ expr ::= term ([-+*/] term)*
292
+ term ::= [0-9]+
293
+ """,
294
+ [
295
+ {"type": "char", "value": [[ord("0"), ord("9")]]},
296
+ ],
297
+ ),
298
+ ],
299
+ )
300
+ def test_it_returns_parse_state_for_grammar(grammar, expected):
301
+ def transformed_expected_dict(e: dict):
302
+ if e["type"] == "char":
303
+ return RuleChar(e["value"])
304
+ elif e["type"] == "char_exclude":
305
+ return RuleCharExclude(e["value"])
306
+ elif e["type"] == "end":
307
+ return RuleEnd()
308
+ else:
309
+ raise Exception(f"Unknown type found in expectation array: {type}")
310
+
311
+ state = [*GBNF(grammar)]
312
+ expected = [transformed_expected_dict(e) for e in expected]
313
+ assert state == expected
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/tests/generated/iteration/iteration_with_additional_strings_test.py ADDED
@@ -0,0 +1,1005 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import pytest
3
+ from gbnf import GBNF, InputParseError, RuleChar, RuleCharExclude, RuleEnd
4
+
5
+
6
+ def describe_iteration():
7
+ @pytest.mark.parametrize(
8
+ ("grammar", "starting", "additional"),
9
+ [
10
+ # single char rule
11
+ ['root ::= "foo"', "f", "1"],
12
+ ['root ::= "foo"', "f", "b"],
13
+ ['root ::= "foo"', "f", "o1"],
14
+ ['root ::= "foo"', "f", "ooo"],
15
+ ['root ::= "foo"', "f", "ooooooo"],
16
+ # two char rules
17
+ ['root ::= "foo" | "bar"', "f", "1"],
18
+ ['root ::= "foo" | "bar"', "b", "1"],
19
+ ['root ::= "foo" | "bar"', "f", "o1"],
20
+ ['root ::= "foo" | "bar"', "b", "a1"],
21
+ ['root ::= "foo" | "bar"', "f", "ooo"],
22
+ ['root ::= "foo" | "bar"', "b", "arrr"],
23
+ # three char rules
24
+ ['root ::= "foo" | "bar" | "baz"', "f", "1"],
25
+ ['root ::= "foo" | "bar" | "baz"', "f", "z"],
26
+ ['root ::= "foo" | "bar" | "baz"', "b", "b1"],
27
+ ['root ::= "foo" | "bar" | "baz"', "f", "o1"],
28
+ ['root ::= "foo" | "bar" | "baz"', "b", "al"],
29
+ ['root ::= "foo" | "bar" | "baz"', "f", "ooo"],
30
+ ['root ::= "foo" | "bar" | "baz"', "b", "azrr"],
31
+ # expressions
32
+ [
33
+ '''root ::= foo
34
+ foo ::="foo"''',
35
+ "f",
36
+ "1",
37
+ ],
38
+ [
39
+ '''root ::= foo
40
+ foo ::="foo"''',
41
+ "f",
42
+ "b",
43
+ ],
44
+ [
45
+ '''root ::= foo
46
+ foo ::="foo"''',
47
+ "f",
48
+ "o1",
49
+ ],
50
+ [
51
+ '''root ::= foo
52
+ foo ::="foo"''',
53
+ "f",
54
+ "ooo",
55
+ ],
56
+ # expression and a char rule
57
+ [
58
+ '''root::=foo|"bar"
59
+ foo::="foo"''',
60
+ "f",
61
+ "1",
62
+ ],
63
+ [
64
+ '''root::=foo|"bar"
65
+ foo::="foo"''',
66
+ "f",
67
+ "z",
68
+ ],
69
+ [
70
+ '''root::=foo|"bar"
71
+ foo::="foo"''',
72
+ "b",
73
+ "b1",
74
+ ],
75
+ [
76
+ '''root::=foo|"bar"
77
+ foo::="foo"''',
78
+ "f",
79
+ "o1",
80
+ ],
81
+ [
82
+ '''root::=foo|"bar"
83
+ foo::="foo"''',
84
+ "f",
85
+ "ooo",
86
+ ],
87
+ [
88
+ '''root::=foo|"bar"
89
+ foo::="foo"''',
90
+ "b",
91
+ "arr",
92
+ ],
93
+ # two expressions
94
+ [
95
+ '''root::= foo | bar
96
+ foo::="foo"
97
+ bar::="bar"''',
98
+ "f",
99
+ "1",
100
+ ],
101
+ [
102
+ '''root::= foo | bar
103
+ foo::="foo"
104
+ bar::="bar"''',
105
+ "f",
106
+ "z",
107
+ ],
108
+ [
109
+ '''root::= foo | bar
110
+ foo::="foo"
111
+ bar::="bar"''',
112
+ "b",
113
+ "1",
114
+ ],
115
+ [
116
+ '''root::= foo | bar
117
+ foo::="foo"
118
+ bar::="bar"''',
119
+ "f",
120
+ "o1",
121
+ ],
122
+ [
123
+ '''root::= foo | bar
124
+ foo::="foo"
125
+ bar::="bar"''',
126
+ "f",
127
+ "ooo",
128
+ ],
129
+ [
130
+ '''root::= foo | bar
131
+ foo::="foo"
132
+ bar::="bar"''',
133
+ "b",
134
+ "arr",
135
+ ],
136
+ # nested expressions
137
+ [
138
+ '''root ::= f | b
139
+ f ::= fo
140
+ b ::= ba
141
+ fo ::= foo
142
+ ba ::= bar | baz
143
+ foo ::= "foo"
144
+ bar ::= "bar"
145
+ baz ::= "baz"''',
146
+ "f",
147
+ "1",
148
+ ],
149
+ [
150
+ '''root ::= f | b
151
+ f ::= fo
152
+ b ::= ba
153
+ fo ::= foo
154
+ ba ::= bar | baz
155
+ foo ::= "foo"
156
+ bar ::= "bar"
157
+ baz ::= "baz"''',
158
+ "f",
159
+ "z",
160
+ ],
161
+ [
162
+ '''root ::= f | b
163
+ f ::= fo
164
+ b ::= ba
165
+ fo ::= foo
166
+ ba ::= bar | baz
167
+ foo ::= "foo"
168
+ bar ::= "bar"
169
+ baz ::= "baz"''',
170
+ "b",
171
+ "1",
172
+ ],
173
+ [
174
+ '''root ::= f | b
175
+ f ::= fo
176
+ b ::= ba
177
+ fo ::= foo
178
+ ba ::= bar | baz
179
+ foo ::= "foo"
180
+ bar ::= "bar"
181
+ baz ::= "baz"''',
182
+ "f",
183
+ "o1",
184
+ ],
185
+ [
186
+ '''root ::= f | b
187
+ f ::= fo
188
+ b ::= ba
189
+ fo ::= foo
190
+ ba ::= bar | baz
191
+ foo ::= "foo"
192
+ bar ::= "bar"
193
+ baz ::= "baz"''',
194
+ "f",
195
+ "ooo",
196
+ ],
197
+ [
198
+ '''root ::= f | b
199
+ f ::= fo
200
+ b ::= ba
201
+ fo ::= foo
202
+ ba ::= bar | baz
203
+ foo ::= "foo"
204
+ bar ::= "bar"
205
+ baz ::= "baz"''',
206
+ "b",
207
+ "arr",
208
+ ],
209
+ [
210
+ '''root ::= f | b
211
+ f ::= fo
212
+ b ::= ba
213
+ fo ::= foo
214
+ ba ::= bar | baz
215
+ foo ::= "foo"
216
+ bar ::= "bar"
217
+ baz ::= "baz"''',
218
+ "b",
219
+ "azr",
220
+ ],
221
+ # ranges
222
+ ["root ::= [a-z]", "a", "A"],
223
+ ["root ::= [a-z]", "a", "0"],
224
+ ["root ::= [a-z]", "a", "az"],
225
+ # range with ? modifier
226
+ ["root ::= [a-z]?", "a", "A"],
227
+ ["root ::= [a-z]?", "a", "0"],
228
+ ["root ::= [a-z]?", "a", "az"],
229
+ # range with + modifier
230
+ ["root ::= [a-z]+", "a", "A"],
231
+ ["root ::= [a-z]+", "a", "0"],
232
+ ["root ::= [a-z]+", "a", "z0"],
233
+ # range with * modifier
234
+ ["root ::= [a-z]*", "a", "A"],
235
+ ["root ::= [a-z]*", "a", "0"],
236
+ ["root ::= [a-z]*", "a", "z0"],
237
+ ],
238
+ )
239
+ def test_it_raises_if_encountering_grammar_with_starting_s_and_additional_s(
240
+ grammar, starting, additional
241
+ ):
242
+ state = GBNF(grammar, starting)
243
+ with pytest.raises(InputParseError) as e:
244
+ state + additional
245
+
246
+ @pytest.mark.parametrize(
247
+ ("grammar", "starting", "additional", "expected"), # single char rule
248
+ [
249
+ [
250
+ 'root ::= "foo"',
251
+ "",
252
+ "f",
253
+ [
254
+ {
255
+ "type": "char",
256
+ "value": [ord("o")],
257
+ },
258
+ ],
259
+ ],
260
+ [
261
+ 'root ::= "foo"',
262
+ "f",
263
+ "o",
264
+ [
265
+ {
266
+ "type": "char",
267
+ "value": [ord("o")],
268
+ },
269
+ ],
270
+ ],
271
+ [
272
+ 'root ::= "foo"',
273
+ "fo",
274
+ "o",
275
+ [
276
+ {"type": "end"},
277
+ ],
278
+ ],
279
+ # two char rules
280
+ [
281
+ 'root ::= "foo" | "bar" ',
282
+ "",
283
+ "f",
284
+ [
285
+ {
286
+ "type": "char",
287
+ "value": [ord("o")],
288
+ },
289
+ ],
290
+ ],
291
+ [
292
+ 'root ::= "foo" | "bar" ',
293
+ "f",
294
+ "o",
295
+ [
296
+ {
297
+ "type": "char",
298
+ "value": [ord("o")],
299
+ },
300
+ ],
301
+ ],
302
+ [
303
+ 'root ::= "foo" | "bar" ',
304
+ "fo",
305
+ "o",
306
+ [
307
+ {"type": "end"},
308
+ ],
309
+ ],
310
+ [
311
+ 'root ::= "foo" | "bar" ',
312
+ "",
313
+ "b",
314
+ [
315
+ {
316
+ "type": "char",
317
+ "value": [ord("a")],
318
+ },
319
+ ],
320
+ ],
321
+ [
322
+ 'root ::= "foo" | "bar" ',
323
+ "b",
324
+ "a",
325
+ [
326
+ {
327
+ "type": "char",
328
+ "value": [ord("r")],
329
+ },
330
+ ],
331
+ ],
332
+ [
333
+ 'root ::= "foo" | "bar" ',
334
+ "ba",
335
+ "r",
336
+ [
337
+ {"type": "end"},
338
+ ],
339
+ ],
340
+ # three char rules
341
+ [
342
+ 'root ::= "foo" | "bar" | "baz"',
343
+ "b",
344
+ "a",
345
+ [
346
+ {
347
+ "type": "char",
348
+ "value": [ord("r")],
349
+ },
350
+ {
351
+ "type": "char",
352
+ "value": [ord("z")],
353
+ },
354
+ ],
355
+ ],
356
+ # char not
357
+ [
358
+ 'root ::= [^f] "o"',
359
+ "g",
360
+ "o",
361
+ [
362
+ {"type": "end"},
363
+ ],
364
+ ],
365
+ [
366
+ "root ::= [^A-Z]",
367
+ "",
368
+ "a",
369
+ [
370
+ {"type": "end"},
371
+ ],
372
+ ],
373
+ [
374
+ "root ::= [^A-Z0-9]",
375
+ "",
376
+ "a",
377
+ [
378
+ {"type": "end"},
379
+ ],
380
+ ],
381
+ [
382
+ "root ::= [^A-Z0-9_-]",
383
+ "",
384
+ "a",
385
+ [
386
+ {"type": "end"},
387
+ ],
388
+ ],
389
+ # expressions
390
+ [
391
+ '''root ::= foo
392
+ foo ::= "foo"''',
393
+ "",
394
+ "f",
395
+ [
396
+ {
397
+ "type": "char",
398
+ "value": [ord("o")],
399
+ },
400
+ ],
401
+ ],
402
+ [
403
+ '''root ::= foo
404
+ foo ::= "foo"''',
405
+ "f",
406
+ "o",
407
+ [
408
+ {
409
+ "type": "char",
410
+ "value": [ord("o")],
411
+ },
412
+ ],
413
+ ],
414
+ [
415
+ '''root ::= foo
416
+ foo ::= "foo"''',
417
+ "fo",
418
+ "o",
419
+ [
420
+ {"type": "end"},
421
+ ],
422
+ ],
423
+ # expression and a char rule
424
+ [
425
+ '''root ::= foo | "bar"
426
+ foo ::= "foo"''',
427
+ "",
428
+ "f",
429
+ [
430
+ {
431
+ "type": "char",
432
+ "value": [ord("o")],
433
+ },
434
+ ],
435
+ ],
436
+ [
437
+ '''root ::= foo | "bar"
438
+ foo ::= "foo"''',
439
+ "f",
440
+ "o",
441
+ [
442
+ {
443
+ "type": "char",
444
+ "value": [ord("o")],
445
+ },
446
+ ],
447
+ ],
448
+ [
449
+ '''root ::= foo | "bar"
450
+ foo ::= "foo"''',
451
+ "fo",
452
+ "o",
453
+ [
454
+ {"type": "end"},
455
+ ],
456
+ ],
457
+ [
458
+ '''root ::= foo | "bar"
459
+ foo ::= "foo"''',
460
+ "",
461
+ "b",
462
+ [
463
+ {
464
+ "type": "char",
465
+ "value": [ord("a")],
466
+ },
467
+ ],
468
+ ],
469
+ [
470
+ '''root ::= foo | "bar"
471
+ foo ::= "foo"''',
472
+ "b",
473
+ "a",
474
+ [
475
+ {
476
+ "type": "char",
477
+ "value": [ord("r")],
478
+ },
479
+ ],
480
+ ],
481
+ [
482
+ '''root ::= foo | "bar"
483
+ foo ::= "foo"''',
484
+ "ba",
485
+ "r",
486
+ [
487
+ {"type": "end"},
488
+ ],
489
+ ],
490
+ # two expressions
491
+ [
492
+ '''root ::= foo | bar
493
+ foo ::= "foo"
494
+ bar ::= "bar"''',
495
+ "",
496
+ "f",
497
+ [
498
+ {
499
+ "type": "char",
500
+ "value": [ord("o")],
501
+ },
502
+ ],
503
+ ],
504
+ [
505
+ '''
506
+ root ::= foo | bar
507
+ foo ::= "foo"
508
+ bar ::= "bar"''',
509
+ "f",
510
+ "o",
511
+ [
512
+ {
513
+ "type": "char",
514
+ "value": [ord("o")],
515
+ },
516
+ ],
517
+ ],
518
+ [
519
+ '''
520
+ root ::= foo | bar
521
+ foo ::= "foo"
522
+ bar ::= "bar"''',
523
+ "fo",
524
+ "o",
525
+ [
526
+ {"type": "end"},
527
+ ],
528
+ ],
529
+ [
530
+ '''root ::= foo | bar
531
+ foo ::= "foo"
532
+ bar ::= "bar"''',
533
+ "",
534
+ "b",
535
+ [
536
+ {
537
+ "type": "char",
538
+ "value": [ord("a")],
539
+ },
540
+ ],
541
+ ],
542
+ [
543
+ '''root ::= foo | bar
544
+ foo ::= "foo"
545
+ bar ::= "bar"''',
546
+ "b",
547
+ "a",
548
+ [
549
+ {
550
+ "type": "char",
551
+ "value": [ord("r")],
552
+ },
553
+ ],
554
+ ],
555
+ [
556
+ '''root ::= foo | bar
557
+ foo ::= "foo"
558
+ bar ::= "bar"''',
559
+ "ba",
560
+ "r",
561
+ [
562
+ {"type": "end"},
563
+ ],
564
+ ],
565
+ # nested expressions
566
+ [
567
+ '''
568
+ root ::= f | b
569
+ f ::= fo
570
+ b ::= ba
571
+ fo ::= foo
572
+ ba ::= bar | baz
573
+ foo ::= "foo"
574
+ bar ::= "bar"
575
+ baz ::= "baz"''',
576
+ "",
577
+ "f",
578
+ [
579
+ {
580
+ "type": "char",
581
+ "value": [ord("o")],
582
+ },
583
+ ],
584
+ ],
585
+ [
586
+ '''root ::= f | b
587
+ f ::= fo
588
+ b ::= ba
589
+ fo ::= foo
590
+ ba ::= bar | baz
591
+ foo ::= "foo"
592
+ bar ::= "bar"
593
+ baz ::= "baz"''',
594
+ "f",
595
+ "o",
596
+ [
597
+ {
598
+ "type": "char",
599
+ "value": [ord("o")],
600
+ },
601
+ ],
602
+ ],
603
+ [
604
+ '''root ::= f | b
605
+ f ::= fo
606
+ b ::= ba
607
+ fo ::= foo
608
+ ba ::= bar | baz
609
+ foo ::= "foo"
610
+ bar ::= "bar"
611
+ baz ::= "baz"''',
612
+ "fo",
613
+ "o",
614
+ [
615
+ {"type": "end"},
616
+ ],
617
+ ],
618
+ [
619
+ '''root ::= f | b
620
+ f ::= fo
621
+ b ::= ba
622
+ fo ::= foo
623
+ ba ::= bar | baz
624
+ foo ::= "foo"
625
+ bar ::= "bar"
626
+ baz ::= "baz"''',
627
+ "",
628
+ "b",
629
+ [
630
+ {
631
+ "type": "char",
632
+ "value": [ord("a")],
633
+ },
634
+ ],
635
+ ],
636
+ [
637
+ '''
638
+ root ::= f | b
639
+ f ::= fo
640
+ b ::= ba
641
+ fo ::= foo
642
+ ba ::= bar | baz
643
+ foo ::= "foo"
644
+ bar ::= "bar"
645
+ baz ::= "baz"''',
646
+ "b",
647
+ "a",
648
+ [
649
+ {
650
+ "type": "char",
651
+ "value": [ord("r")],
652
+ },
653
+ {
654
+ "type": "char",
655
+ "value": [ord("z")],
656
+ },
657
+ ],
658
+ ],
659
+ [
660
+ '''
661
+ root ::= f | b
662
+ f ::= fo
663
+ b ::= ba
664
+ fo ::= foo
665
+ ba ::= bar | baz
666
+ foo ::= "foo"
667
+ bar ::= "bar"
668
+ baz ::= "baz"''',
669
+ "ba",
670
+ "r",
671
+ [
672
+ {"type": "end"},
673
+ ],
674
+ ],
675
+ [
676
+ '''
677
+ root ::= f | b
678
+ f ::= fo
679
+ b ::= ba
680
+ fo ::= foo
681
+ ba ::= bar | baz
682
+ foo ::= "foo"
683
+ bar ::= "bar"
684
+ baz ::= "baz"''',
685
+ "ba",
686
+ "z",
687
+ [
688
+ {"type": "end"},
689
+ ],
690
+ ],
691
+ # ranges
692
+ [
693
+ "root ::= [a-z]",
694
+ "",
695
+ "a",
696
+ [
697
+ {"type": "end"},
698
+ ],
699
+ ],
700
+ [
701
+ "root ::= [a-z]",
702
+ "",
703
+ "m",
704
+ [
705
+ {"type": "end"},
706
+ ],
707
+ ],
708
+ [
709
+ "root ::= [a-z]",
710
+ "",
711
+ "z",
712
+ [
713
+ {"type": "end"},
714
+ ],
715
+ ],
716
+ [
717
+ "root ::= [a-zA-Z]",
718
+ "",
719
+ "a",
720
+ [
721
+ {"type": "end"},
722
+ ],
723
+ ],
724
+ [
725
+ "root ::= [a-zA-Z]",
726
+ "",
727
+ "Z",
728
+ [
729
+ {"type": "end"},
730
+ ],
731
+ ],
732
+ [
733
+ "root ::= [a-zA-Z0-9]",
734
+ "",
735
+ "Z",
736
+ [
737
+ {"type": "end"},
738
+ ],
739
+ ],
740
+ [
741
+ "root ::= [a-zA-Z0-9]",
742
+ "",
743
+ "0",
744
+ [
745
+ {"type": "end"},
746
+ ],
747
+ ],
748
+ [
749
+ "root ::= [a-zA-Z0-9]",
750
+ "",
751
+ "9",
752
+ [
753
+ {"type": "end"},
754
+ ],
755
+ ],
756
+ # range with ? modifier
757
+ [
758
+ "root ::= [a-z]?",
759
+ "",
760
+ "a",
761
+ [
762
+ {
763
+ "type": "end",
764
+ },
765
+ ],
766
+ ],
767
+ [
768
+ "root ::= [a-zA-Z]?",
769
+ "",
770
+ "Z",
771
+ [
772
+ {
773
+ "type": "end",
774
+ },
775
+ ],
776
+ ],
777
+ [
778
+ "root ::= [a-zA-Z0-9]?",
779
+ "",
780
+ "0",
781
+ [
782
+ {
783
+ "type": "end",
784
+ },
785
+ ],
786
+ ],
787
+ # range with + modifier
788
+ [
789
+ "root ::= [a-z]+",
790
+ "",
791
+ "a",
792
+ [
793
+ {
794
+ "type": "char",
795
+ "value": [[ord("a"), ord("z")]],
796
+ },
797
+ {
798
+ "type": "end",
799
+ },
800
+ ],
801
+ ],
802
+ [
803
+ "root ::= [a-zA-Z]+",
804
+ "",
805
+ "Z",
806
+ [
807
+ {
808
+ "type": "char",
809
+ "value": [[ord("a"), ord("z")], [ord("A"), ord("Z")]],
810
+ },
811
+ {
812
+ "type": "end",
813
+ },
814
+ ],
815
+ ],
816
+ [
817
+ "root ::= [a-zA-Z]+",
818
+ "azA",
819
+ "Z",
820
+ [
821
+ {
822
+ "type": "char",
823
+ "value": [[ord("a"), ord("z")], [ord("A"), ord("Z")]],
824
+ },
825
+ {
826
+ "type": "end",
827
+ },
828
+ ],
829
+ ],
830
+ # range with * modifier
831
+ [
832
+ "root ::= [a-z]*",
833
+ "",
834
+ "a",
835
+ [
836
+ {
837
+ "type": "char",
838
+ "value": [[ord("a"), ord("z")]],
839
+ },
840
+ {
841
+ "type": "end",
842
+ },
843
+ ],
844
+ ],
845
+ [
846
+ "root ::= [a-zA-Z]+",
847
+ "",
848
+ "Z",
849
+ [
850
+ {
851
+ "type": "char",
852
+ "value": [[ord("a"), ord("z")], [ord("A"), ord("Z")]],
853
+ },
854
+ {
855
+ "type": "end",
856
+ },
857
+ ],
858
+ ],
859
+ [
860
+ "root ::= [a-zA-Z]+",
861
+ "acczABC",
862
+ "Z",
863
+ [
864
+ {
865
+ "type": "char",
866
+ "value": [[ord("a"), ord("z")], [ord("A"), ord("Z")]],
867
+ },
868
+ {
869
+ "type": "end",
870
+ },
871
+ ],
872
+ ],
873
+ [
874
+ 'root ::= "foo" | "bar" | "baz" | "bazaar" | "barrington" ',
875
+ "baza",
876
+ "a",
877
+ [
878
+ {
879
+ "type": "char",
880
+ "value": [ord("r")],
881
+ },
882
+ ],
883
+ ],
884
+ # char not with modifiers
885
+ [
886
+ 'root ::= [^f]+ "o"',
887
+ "aaa",
888
+ "a",
889
+ [
890
+ {
891
+ "type": "char_exclude",
892
+ "value": [ord("f")],
893
+ },
894
+ {
895
+ "type": "char",
896
+ "value": [ord("o")],
897
+ },
898
+ ],
899
+ ],
900
+ [
901
+ "root ::= [^A-Z]+",
902
+ "abc",
903
+ "d",
904
+ [
905
+ {
906
+ "type": "char_exclude",
907
+ "value": [
908
+ [
909
+ ord("A"),
910
+ ord("Z"),
911
+ ],
912
+ ],
913
+ },
914
+ {
915
+ "type": "end",
916
+ },
917
+ ],
918
+ ],
919
+ [
920
+ "root ::= [^A-Z0-9]*",
921
+ "abc",
922
+ "d",
923
+ [
924
+ {
925
+ "type": "char_exclude",
926
+ "value": [
927
+ [
928
+ ord("A"),
929
+ ord("Z"),
930
+ ],
931
+ [
932
+ ord("0"),
933
+ ord("9"),
934
+ ],
935
+ ],
936
+ },
937
+ {
938
+ "type": "end",
939
+ },
940
+ ],
941
+ ],
942
+ [
943
+ "root ::= [^A-Z0-9_-]*",
944
+ "abc",
945
+ "d",
946
+ [
947
+ {
948
+ "type": "char_exclude",
949
+ "value": [
950
+ [
951
+ ord("A"),
952
+ ord("Z"),
953
+ ],
954
+ [
955
+ ord("0"),
956
+ ord("9"),
957
+ ],
958
+ ord("_"),
959
+ ord("-"),
960
+ ],
961
+ },
962
+ {
963
+ "type": "end",
964
+ },
965
+ ],
966
+ ],
967
+ ],
968
+ )
969
+ def test_it_parses_a_grammar_with_starting_and_additional(
970
+ grammar, starting, additional, expected
971
+ ):
972
+ def transformed_expected_dict(e: dict):
973
+ if e["type"] == "char":
974
+ return RuleChar(e["value"])
975
+ elif e["type"] == "char_exclude":
976
+ return RuleCharExclude(e["value"])
977
+ elif e["type"] == "end":
978
+ return RuleEnd()
979
+ else:
980
+ raise Exception(f"Unknown type found in expectation array: {type}")
981
+
982
+ state = GBNF(grammar, starting)
983
+ state = state + additional
984
+ expected = [transformed_expected_dict(e) for e in expected]
985
+ state = sorted(state, key=lambda r: json.dumps(r.__dict__))
986
+ expected = sorted(expected, key=lambda r: json.dumps(r.__dict__))
987
+ assert state == expected
988
+
989
+ @pytest.mark.parametrize(("error_for_most_recent_input"), [False, True])
990
+ def test_it_raises_a_particular_error(error_for_most_recent_input):
991
+ grammar = 'root ::= "bar"'
992
+ state = GBNF(grammar)
993
+ state = state.add("b")
994
+ state = state.add("a")
995
+ with pytest.raises(InputParseError) as e:
996
+ state.add("z")
997
+ err = e.value
998
+ expected_error = InputParseError("z", 0, "ba")
999
+ if error_for_most_recent_input:
1000
+ assert (
1001
+ err.error_for_most_recent_input
1002
+ == expected_error.error_for_most_recent_input
1003
+ )
1004
+ else:
1005
+ assert str(err) == str(expected_error)
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/tests/generated/iteration/iteration_with_an_initial_string_test.py ADDED
@@ -0,0 +1,1228 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import pytest
3
+ from gbnf import GBNF, RuleChar, RuleCharExclude, RuleEnd
4
+
5
+
6
+ def describe_iteration():
7
+ @pytest.mark.parametrize(
8
+ ("grammar", "input_string", "expected"),
9
+ [
10
+ # single char rule
11
+ (
12
+ 'root ::= "foo"',
13
+ "",
14
+ [
15
+ {
16
+ "type": "char",
17
+ "value": [ord("f")],
18
+ },
19
+ ],
20
+ ),
21
+ (
22
+ 'root ::= "foo"',
23
+ "f",
24
+ [
25
+ {
26
+ "type": "char",
27
+ "value": [ord("o")],
28
+ },
29
+ ],
30
+ ),
31
+ (
32
+ 'root ::= "foo"',
33
+ "fo",
34
+ [
35
+ {
36
+ "type": "char",
37
+ "value": [ord("o")],
38
+ },
39
+ ],
40
+ ),
41
+ (
42
+ 'root ::= "foo"',
43
+ "foo",
44
+ [
45
+ {"type": "end"},
46
+ ],
47
+ ),
48
+ # two char rules
49
+ (
50
+ 'root ::= "foo" | "bar" ',
51
+ "",
52
+ [
53
+ {
54
+ "type": "char",
55
+ "value": [ord("f")],
56
+ },
57
+ {
58
+ "type": "char",
59
+ "value": [ord("b")],
60
+ },
61
+ ],
62
+ ),
63
+ (
64
+ 'root ::= "foo" | "bar" ',
65
+ "f",
66
+ [
67
+ {
68
+ "type": "char",
69
+ "value": [ord("o")],
70
+ },
71
+ ],
72
+ ),
73
+ (
74
+ 'root ::= "foo" | "bar" ',
75
+ "fo",
76
+ [
77
+ {
78
+ "type": "char",
79
+ "value": [ord("o")],
80
+ },
81
+ ],
82
+ ),
83
+ (
84
+ 'root ::= "foo" | "bar" ',
85
+ "foo",
86
+ [
87
+ {"type": "end"},
88
+ ],
89
+ ),
90
+ (
91
+ 'root ::= "foo" | "bar" ',
92
+ "b",
93
+ [
94
+ {
95
+ "type": "char",
96
+ "value": [ord("a")],
97
+ },
98
+ ],
99
+ ),
100
+ (
101
+ 'root ::= "foo" | "bar" ',
102
+ "ba",
103
+ [
104
+ {
105
+ "type": "char",
106
+ "value": [ord("r")],
107
+ },
108
+ ],
109
+ ),
110
+ (
111
+ 'root ::= "foo" | "bar" ',
112
+ "bar",
113
+ [
114
+ {"type": "end"},
115
+ ],
116
+ ),
117
+ # three char rules
118
+ (
119
+ 'root ::= "foo" | "bar" | "baz"',
120
+ "ba",
121
+ [
122
+ {
123
+ "type": "char",
124
+ "value": [ord("r")],
125
+ },
126
+ {
127
+ "type": "char",
128
+ "value": [ord("z")],
129
+ },
130
+ ],
131
+ ),
132
+ # chars that shadow built-in characters
133
+ (
134
+ 'root ::= "["',
135
+ "[",
136
+ [
137
+ {"type": "end"},
138
+ ],
139
+ ),
140
+ (
141
+ 'root ::= "]"',
142
+ "]",
143
+ [
144
+ {"type": "end"},
145
+ ],
146
+ ),
147
+ (
148
+ 'root ::= "[]"',
149
+ "[]",
150
+ [
151
+ {"type": "end"},
152
+ ],
153
+ ),
154
+ (
155
+ 'root ::= "{"',
156
+ "{",
157
+ [
158
+ {"type": "end"},
159
+ ],
160
+ ),
161
+ (
162
+ 'root ::= "}"',
163
+ "}",
164
+ [
165
+ {"type": "end"},
166
+ ],
167
+ ),
168
+ (
169
+ 'root ::= "{}"',
170
+ "{}",
171
+ [
172
+ {"type": "end"},
173
+ ],
174
+ ),
175
+ (
176
+ 'root ::= "[{}]"',
177
+ "[{}]",
178
+ [
179
+ {"type": "end"},
180
+ ],
181
+ ),
182
+ # ('root ::= "[{\"}]"', '[{"}]', [
183
+ # { "type": 'end' },
184
+ # ]),
185
+ # char not
186
+ (
187
+ 'root ::= [^f] "o"',
188
+ "",
189
+ [
190
+ {
191
+ "type": "char_exclude",
192
+ "value": [ord("f")],
193
+ },
194
+ ],
195
+ ),
196
+ (
197
+ 'root ::= [^f] "o"',
198
+ "a",
199
+ [
200
+ {
201
+ "type": "char",
202
+ "value": [ord("o")],
203
+ },
204
+ ],
205
+ ),
206
+ (
207
+ "root ::= [^A-Z]",
208
+ "",
209
+ [
210
+ {
211
+ "type": "char_exclude",
212
+ "value": [
213
+ [
214
+ ord("A"),
215
+ ord("Z"),
216
+ ],
217
+ ],
218
+ },
219
+ ],
220
+ ),
221
+ (
222
+ "root ::= [^A-Z0-9]",
223
+ "",
224
+ [
225
+ {
226
+ "type": "char_exclude",
227
+ "value": [
228
+ [
229
+ ord("A"),
230
+ ord("Z"),
231
+ ],
232
+ [
233
+ ord("0"),
234
+ ord("9"),
235
+ ],
236
+ ],
237
+ },
238
+ ],
239
+ ),
240
+ (
241
+ "root ::= [^A-Z0-9_-]",
242
+ "",
243
+ [
244
+ {
245
+ "type": "char_exclude",
246
+ "value": [
247
+ [
248
+ ord("A"),
249
+ ord("Z"),
250
+ ],
251
+ [
252
+ ord("0"),
253
+ ord("9"),
254
+ ],
255
+ ord("_"),
256
+ ord("-"),
257
+ ],
258
+ },
259
+ ],
260
+ ),
261
+ # expressions
262
+ (
263
+ '''
264
+ root ::= foo
265
+ foo ::= "foo"''',
266
+ "",
267
+ [
268
+ {
269
+ "type": "char",
270
+ "value": [ord("f")],
271
+ },
272
+ ],
273
+ ),
274
+ (
275
+ """root ::= foo
276
+ foo ::= "foo"
277
+ """,
278
+ "f",
279
+ [
280
+ {
281
+ "type": "char",
282
+ "value": [ord("o")],
283
+ },
284
+ ],
285
+ ),
286
+ (
287
+ """
288
+ root ::= foo
289
+ foo ::= "foo"
290
+ """,
291
+ "fo",
292
+ [
293
+ {
294
+ "type": "char",
295
+ "value": [ord("o")],
296
+ },
297
+ ],
298
+ ),
299
+ (
300
+ '''root ::= foo
301
+ foo ::= "foo"''',
302
+ "foo",
303
+ [
304
+ {"type": "end"},
305
+ ],
306
+ ),
307
+ (
308
+ '''root ::= f
309
+ f ::= foo
310
+ foo ::= "foo"''',
311
+ "",
312
+ [
313
+ {
314
+ "type": "char",
315
+ "value": [ord("f")],
316
+ },
317
+ ],
318
+ ),
319
+ # expression and a char rule
320
+ (
321
+ """
322
+ root ::= foo | "bar"
323
+ foo ::= "foo"
324
+ """,
325
+ "",
326
+ [
327
+ {
328
+ "type": "char",
329
+ "value": [ord("f")],
330
+ },
331
+ {
332
+ "type": "char",
333
+ "value": [ord("b")],
334
+ },
335
+ ],
336
+ ),
337
+ (
338
+ '''
339
+ root ::= foo | "bar"
340
+ foo ::= "foo"''',
341
+ "f",
342
+ [
343
+ {
344
+ "type": "char",
345
+ "value": [ord("o")],
346
+ },
347
+ ],
348
+ ),
349
+ (
350
+ '''root ::= foo | "bar"
351
+ foo ::= "foo"''',
352
+ "fo",
353
+ [
354
+ {
355
+ "type": "char",
356
+ "value": [ord("o")],
357
+ },
358
+ ],
359
+ ),
360
+ (
361
+ '''root ::= foo | "bar"
362
+ foo ::= "foo"''',
363
+ "foo",
364
+ [
365
+ {"type": "end"},
366
+ ],
367
+ ),
368
+ (
369
+ '''root ::= foo | "bar"
370
+ foo ::= "foo"''',
371
+ "b",
372
+ [
373
+ {
374
+ "type": "char",
375
+ "value": [ord("a")],
376
+ },
377
+ ],
378
+ ),
379
+ (
380
+ '''root ::= foo | "bar"
381
+ foo ::= "foo"''',
382
+ "ba",
383
+ [
384
+ {
385
+ "type": "char",
386
+ "value": [ord("r")],
387
+ },
388
+ ],
389
+ ),
390
+ (
391
+ '''root ::= foo | "bar"
392
+ foo ::= "foo"''',
393
+ "bar",
394
+ [
395
+ {"type": "end"},
396
+ ],
397
+ ),
398
+ # two expressions
399
+ (
400
+ '''
401
+ root ::= foo | bar
402
+ foo ::= "foo"
403
+ bar ::= "bar"''',
404
+ "",
405
+ [
406
+ {
407
+ "type": "char",
408
+ "value": [ord("f")],
409
+ },
410
+ {
411
+ "type": "char",
412
+ "value": [ord("b")],
413
+ },
414
+ ],
415
+ ),
416
+ (
417
+ '''
418
+ root ::= foo | bar
419
+ foo ::= "foo"
420
+ bar ::= "bar"''',
421
+ "f",
422
+ [
423
+ {
424
+ "type": "char",
425
+ "value": [ord("o")],
426
+ },
427
+ ],
428
+ ),
429
+ (
430
+ '''
431
+ root ::= foo | bar
432
+ foo ::= "foo"
433
+ bar ::= "bar"''',
434
+ "fo",
435
+ [
436
+ {
437
+ "type": "char",
438
+ "value": [ord("o")],
439
+ },
440
+ ],
441
+ ),
442
+ (
443
+ '''
444
+ root ::= foo | bar
445
+ foo ::= "foo"
446
+ bar ::= "bar"''',
447
+ "foo",
448
+ [
449
+ {"type": "end"},
450
+ ],
451
+ ),
452
+ (
453
+ '''
454
+ root ::= foo | bar
455
+ foo ::= "foo"
456
+ bar ::= "bar"''',
457
+ "b",
458
+ [
459
+ {
460
+ "type": "char",
461
+ "value": [ord("a")],
462
+ },
463
+ ],
464
+ ),
465
+ (
466
+ '''
467
+ root ::= foo | bar
468
+ foo ::= "foo"
469
+ bar ::= "bar"''',
470
+ "ba",
471
+ [
472
+ {
473
+ "type": "char",
474
+ "value": [ord("r")],
475
+ },
476
+ ],
477
+ ),
478
+ (
479
+ '''
480
+ root ::= foo | bar
481
+ foo ::= "foo"
482
+ bar ::= "bar"''',
483
+ "bar",
484
+ [
485
+ {"type": "end"},
486
+ ],
487
+ ),
488
+ # nested expressions
489
+ (
490
+ '''
491
+ root ::= f | b
492
+ f ::= fo
493
+ b ::= ba
494
+ fo ::= foo
495
+ ba ::= bar | baz
496
+ foo ::= "foo"
497
+ bar ::= "bar"
498
+ baz ::= "baz"''',
499
+ "",
500
+ [
501
+ {
502
+ "type": "char",
503
+ "value": [ord("f")],
504
+ },
505
+ {
506
+ "type": "char",
507
+ "value": [ord("b")],
508
+ },
509
+ ],
510
+ ),
511
+ (
512
+ '''
513
+ root ::= f | b
514
+ f ::= fo
515
+ b ::= ba
516
+ fo ::= foo
517
+ ba ::= bar | baz
518
+ foo ::= "foo"
519
+ bar ::= "bar"
520
+ baz ::= "baz"''',
521
+ "f",
522
+ [
523
+ {
524
+ "type": "char",
525
+ "value": [ord("o")],
526
+ },
527
+ ],
528
+ ),
529
+ (
530
+ '''
531
+ root ::= f | b
532
+ f ::= fo
533
+ b ::= ba
534
+ fo ::= foo
535
+ ba ::= bar | baz
536
+ foo ::= "foo"
537
+ bar ::= "bar"
538
+ baz ::= "baz"''',
539
+ "fo",
540
+ [
541
+ {
542
+ "type": "char",
543
+ "value": [ord("o")],
544
+ },
545
+ ],
546
+ ),
547
+ (
548
+ '''
549
+ root ::= f | b
550
+ f ::= fo
551
+ b ::= ba
552
+ fo ::= foo
553
+ ba ::= bar | baz
554
+ foo ::= "foo"
555
+ bar ::= "bar"
556
+ baz ::= "baz"''',
557
+ "foo",
558
+ [
559
+ {"type": "end"},
560
+ ],
561
+ ),
562
+ (
563
+ '''
564
+ root ::= f | b
565
+ f ::= fo
566
+ b ::= ba
567
+ fo ::= foo
568
+ ba ::= bar | baz
569
+ foo ::= "foo"
570
+ bar ::= "bar"
571
+ baz ::= "baz"''',
572
+ "b",
573
+ [
574
+ {
575
+ "type": "char",
576
+ "value": [ord("a")],
577
+ },
578
+ ],
579
+ ),
580
+ (
581
+ '''
582
+ root ::= f | b
583
+ f ::= fo
584
+ b ::= ba
585
+ fo ::= foo
586
+ ba ::= bar | baz
587
+ foo ::= "foo"
588
+ bar ::= "bar"
589
+ baz ::= "baz"''',
590
+ "ba",
591
+ [
592
+ {
593
+ "type": "char",
594
+ "value": [ord("r")],
595
+ },
596
+ {
597
+ "type": "char",
598
+ "value": [ord("z")],
599
+ },
600
+ ],
601
+ ),
602
+ (
603
+ '''
604
+ root ::= f | b
605
+ f ::= fo
606
+ b ::= ba
607
+ fo ::= foo
608
+ ba ::= bar | baz
609
+ foo ::= "foo"
610
+ bar ::= "bar"
611
+ baz ::= "baz"''',
612
+ "bar",
613
+ [
614
+ {"type": "end"},
615
+ ],
616
+ ),
617
+ (
618
+ '''
619
+ root ::= f | b
620
+ f ::= fo
621
+ b ::= ba
622
+ fo ::= foo
623
+ ba ::= bar | baz
624
+ foo ::= "foo"
625
+ bar ::= "bar"
626
+ baz ::= "baz"''',
627
+ "baz",
628
+ [
629
+ {"type": "end"},
630
+ ],
631
+ ),
632
+ # ranges
633
+ (
634
+ "root ::= [a-z]",
635
+ "",
636
+ [
637
+ {
638
+ "type": "char",
639
+ "value": [[ord("a"), ord("z")]],
640
+ },
641
+ ],
642
+ ),
643
+ (
644
+ "root ::= [a-zA-Z]",
645
+ "",
646
+ [
647
+ {
648
+ "type": "char",
649
+ "value": [[ord("a"), ord("z")], [ord("A"), ord("Z")]],
650
+ },
651
+ ],
652
+ ),
653
+ (
654
+ "root ::= [a-z]",
655
+ "a",
656
+ [
657
+ {"type": "end"},
658
+ ],
659
+ ),
660
+ (
661
+ "root ::= [a-z]",
662
+ "m",
663
+ [
664
+ {"type": "end"},
665
+ ],
666
+ ),
667
+ (
668
+ "root ::= [a-z]",
669
+ "z",
670
+ [
671
+ {"type": "end"},
672
+ ],
673
+ ),
674
+ (
675
+ "root ::= [a-zA-Z]",
676
+ "a",
677
+ [
678
+ {"type": "end"},
679
+ ],
680
+ ),
681
+ (
682
+ "root ::= [a-zA-Z]",
683
+ "Z",
684
+ [
685
+ {"type": "end"},
686
+ ],
687
+ ),
688
+ (
689
+ "root ::= [a-zA-Z0-9]",
690
+ "Z",
691
+ [
692
+ {"type": "end"},
693
+ ],
694
+ ),
695
+ (
696
+ "root ::= [a-zA-Z0-9]",
697
+ "0",
698
+ [
699
+ {"type": "end"},
700
+ ],
701
+ ),
702
+ (
703
+ "root ::= [a-zA-Z0-9]",
704
+ "9",
705
+ [
706
+ {"type": "end"},
707
+ ],
708
+ ),
709
+ # range with ? modifier
710
+ (
711
+ "root ::= [a-z]?",
712
+ "",
713
+ [
714
+ {
715
+ "type": "char",
716
+ "value": [[ord("a"), ord("z")]],
717
+ },
718
+ {
719
+ "type": "end",
720
+ },
721
+ ],
722
+ ),
723
+ (
724
+ "root ::= [a-z]?",
725
+ "a",
726
+ [
727
+ {
728
+ "type": "end",
729
+ },
730
+ ],
731
+ ),
732
+ (
733
+ "root ::= [a-zA-Z]?",
734
+ "Z",
735
+ [
736
+ {
737
+ "type": "end",
738
+ },
739
+ ],
740
+ ),
741
+ (
742
+ "root ::= [a-zA-Z0-9]?",
743
+ "0",
744
+ [
745
+ {
746
+ "type": "end",
747
+ },
748
+ ],
749
+ ),
750
+ # range with + modifier
751
+ (
752
+ "root ::= [a-z]+",
753
+ "",
754
+ [
755
+ {
756
+ "type": "char",
757
+ "value": [[ord("a"), ord("z")]],
758
+ },
759
+ ],
760
+ ),
761
+ (
762
+ "root ::= [a-z]+",
763
+ "l",
764
+ [
765
+ {
766
+ "type": "char",
767
+ "value": [[ord("a"), ord("z")]],
768
+ },
769
+ {
770
+ "type": "end",
771
+ },
772
+ ],
773
+ ),
774
+ (
775
+ "root ::= [a-zA-Z]+",
776
+ "Z",
777
+ [
778
+ {
779
+ "type": "char",
780
+ "value": [[ord("a"), ord("z")], [ord("A"), ord("Z")]],
781
+ },
782
+ {
783
+ "type": "end",
784
+ },
785
+ ],
786
+ ),
787
+ (
788
+ "root ::= [a-zA-Z]+",
789
+ "aZ",
790
+ [
791
+ {
792
+ "type": "char",
793
+ "value": [[ord("a"), ord("z")], [ord("A"), ord("Z")]],
794
+ },
795
+ {
796
+ "type": "end",
797
+ },
798
+ ],
799
+ ),
800
+ (
801
+ "root ::= [a-zA-Z]+",
802
+ "azAZ",
803
+ [
804
+ {
805
+ "type": "char",
806
+ "value": [[ord("a"), ord("z")], [ord("A"), ord("Z")]],
807
+ },
808
+ {
809
+ "type": "end",
810
+ },
811
+ ],
812
+ ),
813
+ # range with * modifier
814
+ (
815
+ "root ::= [a-z]*",
816
+ "",
817
+ [
818
+ {
819
+ "type": "char",
820
+ "value": [[ord("a"), ord("z")]],
821
+ },
822
+ {
823
+ "type": "end",
824
+ },
825
+ ],
826
+ ),
827
+ (
828
+ "root ::= [a-z]*",
829
+ "a",
830
+ [
831
+ {
832
+ "type": "char",
833
+ "value": [[ord("a"), ord("z")]],
834
+ },
835
+ {
836
+ "type": "end",
837
+ },
838
+ ],
839
+ ),
840
+ (
841
+ "root ::= [a-zA-Z]+",
842
+ "Z",
843
+ [
844
+ {
845
+ "type": "char",
846
+ "value": [[ord("a"), ord("z")], [ord("A"), ord("Z")]],
847
+ },
848
+ {
849
+ "type": "end",
850
+ },
851
+ ],
852
+ ),
853
+ (
854
+ "root ::= [a-zA-Z]+",
855
+ "abczABCZ",
856
+ [
857
+ {
858
+ "type": "char",
859
+ "value": [[ord("a"), ord("z")], [ord("A"), ord("Z")]],
860
+ },
861
+ {
862
+ "type": "end",
863
+ },
864
+ ],
865
+ ),
866
+ # char not with modifiers
867
+ (
868
+ 'root ::= [^f]+ "o"',
869
+ "aaa",
870
+ [
871
+ {
872
+ "type": "char_exclude",
873
+ "value": [ord("f")],
874
+ },
875
+ {
876
+ "type": "char",
877
+ "value": [ord("o")],
878
+ },
879
+ ],
880
+ ),
881
+ (
882
+ "root ::= [^A-Z]+",
883
+ "abc",
884
+ [
885
+ {
886
+ "type": "char_exclude",
887
+ "value": [
888
+ [
889
+ ord("A"),
890
+ ord("Z"),
891
+ ],
892
+ ],
893
+ },
894
+ {
895
+ "type": "end",
896
+ },
897
+ ],
898
+ ),
899
+ (
900
+ "root ::= [^A-Z0-9]*",
901
+ "abc",
902
+ [
903
+ {
904
+ "type": "char_exclude",
905
+ "value": [
906
+ [
907
+ ord("A"),
908
+ ord("Z"),
909
+ ],
910
+ [
911
+ ord("0"),
912
+ ord("9"),
913
+ ],
914
+ ],
915
+ },
916
+ {
917
+ "type": "end",
918
+ },
919
+ ],
920
+ ),
921
+ (
922
+ "root ::= [^A-Z0-9_-]*",
923
+ "abc",
924
+ [
925
+ {
926
+ "type": "char_exclude",
927
+ "value": [
928
+ [
929
+ ord("A"),
930
+ ord("Z"),
931
+ ],
932
+ [
933
+ ord("0"),
934
+ ord("9"),
935
+ ],
936
+ ord("_"),
937
+ ord("-"),
938
+ ],
939
+ },
940
+ {
941
+ "type": "end",
942
+ },
943
+ ],
944
+ ),
945
+ # real world bugs
946
+ (
947
+ '''
948
+ root ::= [a-z] | xyx
949
+ xyx ::= "foo"''',
950
+ "f",
951
+ [
952
+ {"type": "end"},
953
+ {
954
+ "type": "char",
955
+ "value": [ord("o")],
956
+ },
957
+ ],
958
+ ),
959
+ # "baz" and "bazaar" are ambiguous, and a string "bazaa" should not result in an 'a' CHAR rule
960
+ (
961
+ 'root ::= "foo" | "bar" | "baz" | "bazaar" | "barrington" ',
962
+ "bazaa",
963
+ [
964
+ {
965
+ "type": "char",
966
+ "value": [ord("r")],
967
+ },
968
+ ],
969
+ ),
970
+ # should be able to step _into_ a rule, and then continue with the previous rule
971
+ (
972
+ 'root ::= ("bar" | "foo") "zyx"',
973
+ "bar",
974
+ [
975
+ {
976
+ "type": "char",
977
+ "value": [ord("z")],
978
+ },
979
+ ],
980
+ ),
981
+ # should be able to process a rule, then step _into_ a rule, with a pointer to previous rule
982
+ (
983
+ 'root ::= "z" ("bar" | "foo") "zzzz"',
984
+ "z",
985
+ [
986
+ {
987
+ "type": "char",
988
+ "value": [ord("b")],
989
+ },
990
+ {
991
+ "type": "char",
992
+ "value": [ord("f")],
993
+ },
994
+ ],
995
+ ),
996
+ # should be able to process a rule, then step _into_ a rule, and then continue with the previous rule
997
+ (
998
+ 'root ::= "z" ("bar" | "foo") "zzz"',
999
+ "zbar",
1000
+ [
1001
+ {
1002
+ "type": "char",
1003
+ "value": [ord("z")],
1004
+ },
1005
+ ],
1006
+ ),
1007
+ (
1008
+ """
1009
+ root ::= termz ([-+*/] termz)*
1010
+ termz ::= [0-9]+""",
1011
+ "1",
1012
+ [
1013
+ {
1014
+ "type": "char",
1015
+ "value": [[ord("0"), ord("9")]],
1016
+ },
1017
+ {
1018
+ "type": "char",
1019
+ "value": [
1020
+ ord("-"),
1021
+ ord("+"),
1022
+ ord("*"),
1023
+ ord("/"),
1024
+ ],
1025
+ },
1026
+ {
1027
+ "type": "end",
1028
+ },
1029
+ ],
1030
+ ),
1031
+ (
1032
+ """
1033
+ root ::= expr "=" termy
1034
+ expr ::= termy ([-+*/] termy)*
1035
+ termy ::= [0-9]+""",
1036
+ "1",
1037
+ [
1038
+ {
1039
+ "type": "char",
1040
+ "value": [[ord("0"), ord("9")]],
1041
+ },
1042
+ {
1043
+ "type": "char",
1044
+ "value": [
1045
+ ord("-"),
1046
+ ord("+"),
1047
+ ord("*"),
1048
+ ord("/"),
1049
+ ],
1050
+ },
1051
+ {
1052
+ "type": "char",
1053
+ "value": [
1054
+ ord("="),
1055
+ ],
1056
+ },
1057
+ ],
1058
+ ),
1059
+ (
1060
+ """
1061
+ root ::= (expr "=" terma "\n")+
1062
+ expr ::= terma ([-+*/] terma)*
1063
+ terma ::= [0-9]+""",
1064
+ "1",
1065
+ [
1066
+ {
1067
+ "type": "char",
1068
+ "value": [[ord("0"), ord("9")]],
1069
+ },
1070
+ {
1071
+ "type": "char",
1072
+ "value": [
1073
+ ord("-"),
1074
+ ord("+"),
1075
+ ord("*"),
1076
+ ord("/"),
1077
+ ],
1078
+ },
1079
+ {
1080
+ "type": "char",
1081
+ "value": [
1082
+ ord("="),
1083
+ ],
1084
+ },
1085
+ ],
1086
+ ),
1087
+ (
1088
+ """root ::= (expr "=" termb "\n")+
1089
+ expr ::= termb ([-+*/] termb)*
1090
+ termb ::= [0-9]+""",
1091
+ "1+",
1092
+ [
1093
+ {
1094
+ "type": "char",
1095
+ "value": [[ord("0"), ord("9")]],
1096
+ },
1097
+ ],
1098
+ ),
1099
+ (
1100
+ """
1101
+ root ::= (expr "=" termc "\n")+
1102
+ expr ::= termc ([-+*/] termc)*
1103
+ termc ::= [0-9]+""",
1104
+ "1=",
1105
+ [
1106
+ {
1107
+ "type": "char",
1108
+ "value": [[ord("0"), ord("9")]],
1109
+ },
1110
+ ],
1111
+ ),
1112
+ (
1113
+ """root ::= (expr "=" termd "\n")+
1114
+ expr ::= termd ([-+*/] termd)*
1115
+ termd ::= [0-9]+""",
1116
+ "1+1",
1117
+ [
1118
+ {
1119
+ "type": "char",
1120
+ "value": [[ord("0"), ord("9")]],
1121
+ },
1122
+ {
1123
+ "type": "char",
1124
+ "value": [ord("-"), ord("+"), ord("*"), ord("/")],
1125
+ },
1126
+ {
1127
+ "type": "char",
1128
+ "value": [ord("=")],
1129
+ },
1130
+ ],
1131
+ ),
1132
+ (
1133
+ """root ::= (expr "=" term "\n")+
1134
+ expr ::= term ([-+*/] term)*
1135
+ term ::= [0-9]+""",
1136
+ "1=1",
1137
+ [
1138
+ {
1139
+ "type": "char",
1140
+ "value": [[ord("0"), ord("9")]],
1141
+ },
1142
+ {
1143
+ "type": "char",
1144
+ "value": [ord("\n")],
1145
+ },
1146
+ ],
1147
+ ),
1148
+ (
1149
+ """root ::= "\\"" ( [^\\"abcdefghA-Z])* """,
1150
+ """"is not only in its lyrism, its vow to sustin it in its poo-sion, its r,v:l'y, it's tory,""",
1151
+ [
1152
+ {
1153
+ "type": "char_exclude",
1154
+ "value": [
1155
+ ord('"'),
1156
+ ord("a"),
1157
+ ord("b"),
1158
+ ord("c"),
1159
+ ord("d"),
1160
+ ord("e"),
1161
+ ord("f"),
1162
+ ord("g"),
1163
+ ord("h"),
1164
+ [ord("A"), ord("Z")],
1165
+ ],
1166
+ },
1167
+ {
1168
+ "type": "end",
1169
+ },
1170
+ ],
1171
+ ),
1172
+ # sticking to llama.cpp's implementation, for a char_not rule _and_ a char rule
1173
+ # side by side, _either_ is valid.
1174
+ # that means that input explicitly forbidden by char_not can be allowed by the char
1175
+ # rule.
1176
+ # it seems weird. but it's the way it is.
1177
+ # so below, anything in b-z is allowed, because of the second rule. Anything _not_ a-z is
1178
+ # also allowed, because of the first rule. So really the only character disallowed is 'a'.
1179
+ (
1180
+ "root ::= ( [^abcdefgh] | [b-z])* ",
1181
+ "bcdefghzyxw0123ABCZ",
1182
+ [
1183
+ {
1184
+ "type": "char_exclude",
1185
+ "value": [
1186
+ ord("a"),
1187
+ ord("b"),
1188
+ ord("c"),
1189
+ ord("d"),
1190
+ ord("e"),
1191
+ ord("f"),
1192
+ ord("g"),
1193
+ ord("h"),
1194
+ ],
1195
+ },
1196
+ {
1197
+ "type": "char",
1198
+ "value": [
1199
+ [ord("b"), ord("z")],
1200
+ ],
1201
+ },
1202
+ {
1203
+ "type": "end",
1204
+ },
1205
+ ],
1206
+ ),
1207
+ ],
1208
+ )
1209
+ def test_it_returns_parse_state_for_grammar_and_initial_string(
1210
+ grammar, input_string, expected
1211
+ ):
1212
+ def transformed_expected_dict(e: dict):
1213
+ if e["type"] == "char":
1214
+ return RuleChar(e["value"])
1215
+ elif e["type"] == "char_exclude":
1216
+ return RuleCharExclude(e["value"])
1217
+ elif e["type"] == "end":
1218
+ return RuleEnd()
1219
+ else:
1220
+ raise Exception(f"Unknown type found in expectation array: {type}")
1221
+
1222
+ state = GBNF(grammar)
1223
+ state = state + input_string
1224
+ state = [*state]
1225
+ expected = [transformed_expected_dict(e) for e in expected]
1226
+ state = sorted(state, key=lambda r: json.dumps(r.__dict__))
1227
+ expected = sorted(expected, key=lambda r: json.dumps(r.__dict__))
1228
+ assert state == expected
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/tests/generated/validation/__pycache__/validate_grammar_test.cpython-314-pytest-9.1.1.pyc ADDED
Binary file (3.25 kB). View file
 
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/tests/generated/validation/__pycache__/validate_input_test.cpython-314-pytest-9.1.1.pyc ADDED
Binary file (6.45 kB). View file
 
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/tests/generated/validation/validate_grammar_test.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ from gbnf import GBNF, GrammarParseError
3
+
4
+
5
+ def describe_validate_grammar():
6
+ @pytest.mark.parametrize(
7
+ ("grammar"),
8
+ [
9
+ '''root ::= "foo"''',
10
+ '''root ::= "foo" | "bar"''',
11
+ """root ::= ("foo" | "bar")""",
12
+ """root ::= ("foo" | "bar")?""",
13
+ """root ::= ("foo" | "bar")*""",
14
+ """root ::= ("foo" | "bar")+""",
15
+ """root ::= [a-z]""",
16
+ """root ::= [a-zA-Z]""",
17
+ """root ::= [a-zA-Z0-9]""",
18
+ """root ::= [a-zA-Z0-9]*""",
19
+ """root ::= [a-zA-Z0-9]?""",
20
+ """root ::= [a-z]+""",
21
+ """root ::= [a-zA-Z0-9]+""",
22
+ """root ::= ([a-zA-Z0-9])*""",
23
+ """root ::= ([a-zA-Z0-9])?""",
24
+ """root ::= ([a-zA-Z0-9])+""",
25
+ '''
26
+ root ::= foo
27
+ foo ::= "foo"''',
28
+ """
29
+ root ::= foo
30
+ foo ::= "foo" | "bar" | ([a-z])?
31
+ """,
32
+ ],
33
+ )
34
+ def test_it_parses_a_grammar(grammar):
35
+ GBNF(grammar)
36
+
37
+ @pytest.mark.parametrize(
38
+ ("grammar", "error_pos", "error_reason"),
39
+ [
40
+ ("", 0, "No rules were found"),
41
+ ('root = "foo"', 5, "Expecting ::= at 5"),
42
+ (
43
+ """root ::= foo
44
+ foo := "foo"
45
+ """,
46
+ 17,
47
+ "Expecting ::= at 17",
48
+ ),
49
+ ("root ::= foo", 9, 'Undefined rule identifier "foo"'),
50
+ (
51
+ """root ::= foo
52
+ bar ::= "bar"
53
+ """,
54
+ 9,
55
+ 'Undefined rule identifier "foo"',
56
+ ),
57
+ (
58
+ """root ::= foo
59
+ foo ::= baz
60
+ bar ::= "bar"
61
+ """,
62
+ 21,
63
+ 'Undefined rule identifier "baz"',
64
+ ),
65
+ ("root ::= foo ::= bar", 13, "Expecting newline or end at 13"),
66
+ (
67
+ """root ::= ([a-z]
68
+ foo ::= "foo"
69
+ """,
70
+ 20,
71
+ "Expecting ')' at 20",
72
+ ),
73
+ ],
74
+ )
75
+ def test_it_reports_an_error_for_an_invalid_grammar(
76
+ grammar, error_pos, error_reason
77
+ ):
78
+ expected = GrammarParseError(grammar, error_pos, error_reason)
79
+ with pytest.raises(GrammarParseError) as e:
80
+ graph = GBNF(grammar)
81
+ e = e.value
82
+ assert e == expected
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/ported_implementation/tests/generated/validation/validate_input_test.py ADDED
@@ -0,0 +1,320 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ from gbnf import GBNF, InputParseError
3
+
4
+
5
+ def describe_validate_input():
6
+ @pytest.mark.parametrize(
7
+ ("grammar", "input"),
8
+ [
9
+ ['root ::= "foo"', "f"],
10
+ ],
11
+ )
12
+ def test_it_parses_a_grammar(grammar, input):
13
+ graph = GBNF(grammar, input)
14
+ assert bool(graph) is True
15
+
16
+ @pytest.mark.parametrize(
17
+ ("grammar", "input_text", "error_pos"),
18
+ [
19
+ ['root ::= "foo"', "2", 0],
20
+ ['root ::= "foo"', "b", 0],
21
+ ['root ::= "foo"', "f1", 1],
22
+ ['root ::= "foo"', "fo1", 2],
23
+ ['root ::= "foo"', "fooo", 3],
24
+ ['root ::= "foo"', "fooooooo", 3],
25
+ ['root ::= "foo" | "bar"', "1", 0],
26
+ ['root ::= "foo" | "bar"', "z", 0],
27
+ ['root ::= "foo" | "bar"', "f1", 1],
28
+ ['root ::= "foo" | "bar"', "b1", 1],
29
+ ['root ::= "foo" | "bar"', "fo1", 2],
30
+ ['root ::= "foo" | "bar"', "ba1", 2],
31
+ ['root ::= "foo" | "bar"', "fooo", 3],
32
+ ['root ::= "foo" | "bar"', "barrr", 3],
33
+ ['root ::= "foo" | "bar" | "baz"', "1", 0],
34
+ ['root ::= "foo" | "bar" | "baz"', "z", 0],
35
+ ['root ::= "foo" | "bar" | "baz"', "f1", 1],
36
+ ['root ::= "foo" | "bar" | "baz"', "b1", 1],
37
+ ['root ::= "foo" | "bar" | "baz"', "fo1", 2],
38
+ ['root ::= "foo" | "bar" | "baz"', "bal", 2],
39
+ ['root ::= "foo" | "bar" | "baz"', "fooo", 3],
40
+ ['root ::= "foo" | "bar" | "baz"', "bazrr", 3],
41
+ ["root ::= [^a]", "a", 0],
42
+ ["root ::= [^abc]", "b", 0],
43
+ ["root ::= [^a-z]", "z", 0],
44
+ ["root ::= [^a-zA-Z]", "X", 0],
45
+ ["root ::= [^a-zA-Z0-9]", "8", 0],
46
+ [
47
+ '''
48
+ root ::= foo
49
+ foo ::= "foo"''',
50
+ "1",
51
+ 0,
52
+ ],
53
+ [
54
+ '''
55
+ root ::= foo
56
+ foo ::= "foo"''',
57
+ "b",
58
+ 0,
59
+ ],
60
+ [
61
+ '''
62
+ root ::= foo
63
+ foo ::= "foo"''',
64
+ "f1",
65
+ 1,
66
+ ],
67
+ [
68
+ '''
69
+ root ::= foo
70
+ foo ::= "foo"''',
71
+ "fo1",
72
+ 2,
73
+ ],
74
+ [
75
+ '''
76
+ root ::= foo
77
+ foo ::= "foo"''',
78
+ "fooo",
79
+ 3,
80
+ ],
81
+ [
82
+ '''
83
+ root::=foo|"bar"
84
+ foo::="foo"''',
85
+ "1",
86
+ 0,
87
+ ],
88
+ [
89
+ '''
90
+ root::=foo|"bar"
91
+ foo::="foo"''',
92
+ "z",
93
+ 0,
94
+ ],
95
+ [
96
+ '''
97
+ root::=foo|"bar"
98
+ foo::="foo"''',
99
+ "f1",
100
+ 1,
101
+ ],
102
+ [
103
+ '''
104
+ root::=foo|"bar"
105
+ foo::="foo"''',
106
+ "b1",
107
+ 1,
108
+ ],
109
+ [
110
+ '''
111
+ root::=foo|"bar"
112
+ foo::="foo"''',
113
+ "fo1",
114
+ 2,
115
+ ],
116
+ [
117
+ '''
118
+ root::=foo|"bar"
119
+ foo::="foo"''',
120
+ "fooo",
121
+ 3,
122
+ ],
123
+ [
124
+ '''
125
+ root::=foo|"bar"
126
+ foo::="foo"''',
127
+ "barr",
128
+ 3,
129
+ ],
130
+ [
131
+ '''
132
+ root::= foo | bar
133
+ foo::="foo"
134
+ bar::="bar"''',
135
+ "1",
136
+ 0,
137
+ ],
138
+ [
139
+ '''
140
+ root::= foo | bar
141
+ foo::="foo"
142
+ bar::="bar"''',
143
+ "z",
144
+ 0,
145
+ ],
146
+ [
147
+ '''
148
+ root::= foo | bar
149
+ foo::="foo"
150
+ bar::="bar"''',
151
+ "f1",
152
+ 1,
153
+ ],
154
+ [
155
+ '''
156
+ root::= foo | bar
157
+ foo::="foo"
158
+ bar::="bar"''',
159
+ "b1",
160
+ 1,
161
+ ],
162
+ [
163
+ '''
164
+ root::= foo | bar
165
+ foo::="foo"
166
+ bar::="bar"''',
167
+ "fo1",
168
+ 2,
169
+ ],
170
+ [
171
+ '''
172
+ root::= foo | bar
173
+ foo::="foo"
174
+ bar::="bar"''',
175
+ "fooo",
176
+ 3,
177
+ ],
178
+ [
179
+ '''
180
+ root::= foo | bar
181
+ foo::="foo"
182
+ bar::="bar"''',
183
+ "barr",
184
+ 3,
185
+ ],
186
+ [
187
+ """
188
+ root ::= f | b
189
+ f ::= fo
190
+ b ::= ba
191
+ fo ::= foo
192
+ ba ::= bar | baz
193
+ foo ::= "foo"
194
+ bar ::= "bar"
195
+ baz ::= "baz"
196
+ """,
197
+ "1",
198
+ 0,
199
+ ],
200
+ [
201
+ '''
202
+ root ::= f | b
203
+ f ::= fo
204
+ b ::= ba
205
+ fo ::= foo
206
+ ba ::= bar | baz
207
+ foo ::= "foo"
208
+ bar ::= "bar"
209
+ baz ::= "baz"''',
210
+ "z",
211
+ 0,
212
+ ],
213
+ [
214
+ '''
215
+ root ::= f | b
216
+ f ::= fo
217
+ b ::= ba
218
+ fo ::= foo
219
+ ba ::= bar | baz
220
+ foo ::= "foo"
221
+ bar ::= "bar"
222
+ baz ::= "baz"''',
223
+ "f1",
224
+ 1,
225
+ ],
226
+ [
227
+ '''
228
+ root ::= f | b
229
+ f ::= fo
230
+ b ::= ba
231
+ fo ::= foo
232
+ ba ::= bar | baz
233
+ foo ::= "foo"
234
+ bar ::= "bar"
235
+ baz ::= "baz"''',
236
+ "b1",
237
+ 1,
238
+ ],
239
+ [
240
+ '''
241
+ root ::= f | b
242
+ f ::= fo
243
+ b ::= ba
244
+ fo ::= foo
245
+ ba ::= bar | baz
246
+ foo ::= "foo"
247
+ bar ::= "bar"
248
+ baz ::= "baz"''',
249
+ "fo1",
250
+ 2,
251
+ ],
252
+ [
253
+ '''
254
+ root ::= f | b
255
+ f ::= fo
256
+ b ::= ba
257
+ fo ::= foo
258
+ ba ::= bar | baz
259
+ foo ::= "foo"
260
+ bar ::= "bar"
261
+ baz ::= "baz"''',
262
+ "fooo",
263
+ 3,
264
+ ],
265
+ [
266
+ '''
267
+ root ::= f | b
268
+ f ::= fo
269
+ b ::= ba
270
+ fo ::= foo
271
+ ba ::= bar | baz
272
+ foo ::= "foo"
273
+ bar ::= "bar"
274
+ baz ::= "baz"''',
275
+ "barr",
276
+ 3,
277
+ ],
278
+ [
279
+ '''
280
+ root ::= f | b
281
+ f ::= fo
282
+ b ::= ba
283
+ fo ::= foo
284
+ ba ::= bar | baz
285
+ foo ::= "foo"
286
+ bar ::= "bar"
287
+ baz ::= "baz"''',
288
+ "bazr",
289
+ 3,
290
+ ],
291
+ ["root ::= [a-z]", "A", 0],
292
+ ["root ::= [a-z]", "0", 0],
293
+ ["root ::= [a-z]", "az", 1],
294
+ ["root ::= [a-z]?", "A", 0],
295
+ ["root ::= [a-z]?", "0", 0],
296
+ ["root ::= [a-z]?", "az", 1],
297
+ ["root ::= [a-z]+", "A", 0],
298
+ ["root ::= [a-z]+", "0", 0],
299
+ ["root ::= [a-z]+", "az0", 2],
300
+ ["root ::= [a-z]*", "A", 0],
301
+ ["root ::= [a-z]*", "0", 0],
302
+ ["root ::= [a-z]*", "az0", 2],
303
+ # sticking to llama.cpp's implementation, for a char_not rule _and_ a char rule
304
+ # side by side, _either_ is valid.
305
+ # that means that input explicitly forbidden by char_not can be allowed by the char
306
+ # rule.
307
+ # it seems weird. but it's the way it is.
308
+ # so below, anything in b-z is allowed, because of the second rule. Anything _not_ a-z is
309
+ # also allowed, because of the first rule. So really the only character disallowed is 'a'.
310
+ ["root ::= ( [^abcdefgh] | [b-z])*", "a", 0],
311
+ ],
312
+ )
313
+ def test_it_reports_an_error_for_an_invalid_input(grammar, input_text, error_pos):
314
+ graph = GBNF(grammar)
315
+ expected = InputParseError(input_text, error_pos)
316
+ with pytest.raises(InputParseError) as e:
317
+ graph.add(input_text)
318
+
319
+ e = e.value
320
+ assert e == expected
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/proxy.log ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ NOTICE Oct 12 02:38:11.088 [1]: Initializing tinyproxy ...
2
+ NOTICE Oct 12 02:38:11.092 [1]: Reloading config file (/etc/tinyproxy/tinyproxy.conf)
3
+ INFO Oct 12 02:38:11.093 [1]: Added address [0.0.0.0] to listen addresses.
4
+ INFO Oct 12 02:38:11.095 [1]: Adding Port [443] to the list allowed by CONNECT
5
+ NOTICE Oct 12 02:38:11.096 [1]: Reloading config file finished
6
+ INFO Oct 12 02:38:11.097 [1]: listen_sock called with addr = '0.0.0.0'
7
+ INFO Oct 12 02:38:11.098 [1]: trying to listen on host[0.0.0.0], family[2], socktype[1], proto[6]
8
+ INFO Oct 12 02:38:11.100 [1]: listening on fd [3]
9
+ INFO Oct 12 02:38:11.101 [1]: Not running as root, so not changing UID/GID.
10
+ INFO Oct 12 02:38:11.102 [1]: Setting the various signals.
11
+ INFO Oct 12 02:38:11.103 [1]: Starting main loop. Accepting connections.
12
+ CONNECT Oct 12 02:38:11.613 [1]: Connect (file descriptor 5): 172.27.0.3
13
+ CONNECT Oct 12 02:38:11.615 [1]: Request (file descriptor 5): CONNECT api.anthropic.com:443 HTTP/1.1
14
+ INFO Oct 12 02:38:11.616 [1]: No upstream proxy for api.anthropic.com
15
+ INFO Oct 12 02:38:11.618 [1]: opensock: opening connection to api.anthropic.com:443
16
+ INFO Oct 12 02:38:11.622 [1]: opensock: getaddrinfo returned for api.anthropic.com:443
17
+ CONNECT Oct 12 02:38:11.627 [1]: Established connection to host "api.anthropic.com" using file descriptor 6.
18
+ INFO Oct 12 02:38:11.629 [1]: Not sending client headers to remote machine
19
+ INFO Oct 12 02:42:25.788 [1]: Closed connection between local client (fd:5) and remote client (fd:6)
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/result.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"duration_api_ms":250967,"stop_reason":"end_turn","session_id":"b0e5b3d5-7ce4-4ad3-ba39-bc03b3df8e79","total_cost_usd":1.1915162,"usage":{"input_tokens":36,"cache_creation_input_tokens":75552,"cache_read_input_tokens":1123161,"output_tokens":29449,"output_tokens_details":{"thinking_tokens":5239},"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0},"service_tier":"standard","cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":75552},"inference_geo":"global","iterations":[],"speed":"standard","fallback_credit":null},"modelUsage":{"claude-opus-5-5":{"inputTokens":36,"outputTokens":29449,"cacheReadInputTokens":1123161,"cacheCreationInputTokens":75552,"webSearchRequests":0,"costUSD":1.1915162,"contextWindow":1000000,"maxOutputTokens":128000,"thinkingTokens":5239,"canonicalModel":"claude-opus-5-5","provider":"firstParty","costBasis":"list"}},"permission_denials":[],"terminal_reason":"completed","fast_mode_state":"off","fast_mode_disabled_reason":"sdk_opt_in_required","subagent_stats":{"spawned":0,"requested":{"background":0,"foreground":0,"unset":0},"started_in_background":0,"max_depth":0,"spawned_by_subagents":0,"completed":0,"failed":0,"killed":{"parent":0,"user":0,"system":0},"refused":{"depth_limit":0,"concurrency_limit":0,"budget":0},"by_type":{}},"safety_stops":0,"is_error":false,"num_turns":21,"subtype":"success","api_error_status":null,"result":"I've ported the JavaScript library to Python as the `gbnf` package in `/target/gbnf/`. All 320 Python integration tests in `tests/generated/` pass. I also ran all 453 grammar cases from the JS-only `grammars.test.ts` through the Python port (real grammars like arithmetic, JSON and C), feeding input one character at a time, and none failed.\n\n**Structure:** The package mirrors the JS source file for file: `rules_builder/` reads grammar text into rules, `grammar_parser/` arranges those rules into alternatives, and `grammar_graph/` holds the graph, pointers and `ParseState`. Error classes are in `utils/errors/`.\n\n**Public API** (what the tests import): `GBNF(grammar, initial_string=\"\")` returns an immutable `ParseState`.\n- You add input with `.add(s)`, `state + s`, or `state(s)`.\n- Iterating a state yields `RuleChar`, `RuleCharExclude` or `RuleEnd`, which compare by value.\n- Ranges are `[start, end]` lists.\n- `GrammarParseError` and `InputParseError` give the same messages as JS. `InputParseError` also has `error_for_most_recent_input`.\n\n**Where it differs from JS:**\n- Input is read as Unicode code points rather than JS's UTF-16 units. The grammar and the input are read the same way, so matching is unchanged.\n- In JS, a grammar with no `root` rule throws a generic lookup error instead of the intended message. Python raises the intended `GrammarParseError`, listing the available symbols.\n- `ParseState` has no `__len__`, so `bool(state)` is always true, as the tests expect. Use `.size` for the rule count.\n\n**Setup changes:**\n- I removed `pytest-watcher` from the runtime dependencies in `pyproject.toml`. It's a dev tool, and with no network `uv sync` couldn't resolve it.\n- I added `pythonpath = [\".\"]` to the pytest config so the tests can import the package.\n- The installed `pytest` already includes `pytest-describe`, so the suite runs with `pytest tests/generated` from `/target`.\n- I added a short `README.md` because `pyproject.toml` refers to one.","ttft_ms":1671,"type":"result","duration_ms":254196,"uuid":"abc7e76f-e431-4013-af8a-25281efadcf0","ttft_stream_ms":1521,"time_to_request_ms":49,"first_content_frame_ms":1521,"queued_turn_count":0,"result_index":0}
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/run.json ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "params": {
3
+ "source-language": "javascript",
4
+ "include-unit-tests": false,
5
+ "include-source-integration-tests": true,
6
+ "include-target-integration-tests": true
7
+ },
8
+ "created": "2026-10-12T02:37:59Z",
9
+ "ended": "2026-10-12T02:42:26Z",
10
+ "exit_code": 0
11
+ }
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/stderr.txt ADDED
File without changes
v2-tests-configuration/runs/20261012T023759Z_9fdcf91d/transcript/.claude/projects/-target/b0e5b3d5-7ce4-4ad3-ba39-bc03b3df8e79.jsonl ADDED
The diff for this file is too large to render. See raw diff
 
v2-tests-configuration/runs/20261012T023847Z_df0e91f6/manifest.json ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "timestamp": "2026-10-12T02:38:47Z",
3
+ "completed_at": "2026-10-12T02:45:16Z",
4
+ "exit_code": 0,
5
+ "study": "v2-test-configurations",
6
+ "condition": {
7
+ "name": "source-javascript_unit-tests_effort-medium_model-claude-opus-5-5",
8
+ "source_language": "javascript",
9
+ "target_language": "python",
10
+ "include_unit_tests": true,
11
+ "include_source_integration_tests": false,
12
+ "include_target_integration_tests": false,
13
+ "effort": "medium",
14
+ "model": "claude-opus-5-5"
15
+ },
16
+ "derivation": {
17
+ "gbnf_commit": "13f1aca495d11e160fffd68c4ba299a2415909d8"
18
+ },
19
+ "reference_implementation": {
20
+ "included": [
21
+ "javascript/.eslintrc.cjs",
22
+ "javascript/.gitignore",
23
+ "javascript/.npmignore",
24
+ "javascript/README.md",
25
+ "javascript/package.json",
26
+ "javascript/src/gbnf.test.ts",
27
+ "javascript/src/gbnf.ts",
28
+ "javascript/src/grammar-graph/colorize.test.ts",
29
+ "javascript/src/grammar-graph/colorize.ts",
30
+ "javascript/src/grammar-graph/generic-set.test.ts",
31
+ "javascript/src/grammar-graph/generic-set.ts",
32
+ "javascript/src/grammar-graph/get-input-as-code-points.test.ts",
33
+ "javascript/src/grammar-graph/get-input-as-code-points.ts",
34
+ "javascript/src/grammar-graph/get-parent-stack-id.test.ts",
35
+ "javascript/src/grammar-graph/get-parent-stack-id.ts",
36
+ "javascript/src/grammar-graph/get-serialized-rule-key.test.ts",
37
+ "javascript/src/grammar-graph/get-serialized-rule-key.ts",
38
+ "javascript/src/grammar-graph/graph-node.test.ts",
39
+ "javascript/src/grammar-graph/graph-node.ts",
40
+ "javascript/src/grammar-graph/graph-pointer.test.ts",
41
+ "javascript/src/grammar-graph/graph-pointer.ts",
42
+ "javascript/src/grammar-graph/graph.test.ts",
43
+ "javascript/src/grammar-graph/graph.ts",
44
+ "javascript/src/grammar-graph/index.ts",
45
+ "javascript/src/grammar-graph/parse-state.test.ts",
46
+ "javascript/src/grammar-graph/parse-state.ts",
47
+ "javascript/src/grammar-graph/print.test.ts",
48
+ "javascript/src/grammar-graph/print.ts",
49
+ "javascript/src/grammar-graph/rule-ref.test.ts",
50
+ "javascript/src/grammar-graph/rule-ref.ts",
51
+ "javascript/src/grammar-graph/type-guards.test.ts",
52
+ "javascript/src/grammar-graph/type-guards.ts",
53
+ "javascript/src/grammar-graph/types.ts",
54
+ "javascript/src/grammar-parser/build-rule-stack.test.ts",
55
+ "javascript/src/grammar-parser/build-rule-stack.ts",
56
+ "javascript/src/index.ts",
57
+ "javascript/src/rules-builder/index.ts",
58
+ "javascript/src/rules-builder/is-word-char.test.ts",
59
+ "javascript/src/rules-builder/is-word-char.ts",
60
+ "javascript/src/rules-builder/parse-char.test.ts",
61
+ "javascript/src/rules-builder/parse-char.ts",
62
+ "javascript/src/rules-builder/parse-name.test.ts",
63
+ "javascript/src/rules-builder/parse-name.ts",
64
+ "javascript/src/rules-builder/parse-space.test.ts",
65
+ "javascript/src/rules-builder/parse-space.ts",
66
+ "javascript/src/rules-builder/rules-builder.test.ts",
67
+ "javascript/src/rules-builder/rules-builder.ts",
68
+ "javascript/src/rules-builder/symbol-ids.ts",
69
+ "javascript/src/rules-builder/type-guards.ts",
70
+ "javascript/src/rules-builder/types.ts",
71
+ "javascript/src/umd.ts",
72
+ "javascript/src/utils/errors/build-error-position.test.ts",
73
+ "javascript/src/utils/errors/build-error-position.ts",
74
+ "javascript/src/utils/errors/get-input-as-string.test.ts",
75
+ "javascript/src/utils/errors/get-input-as-string.ts",
76
+ "javascript/src/utils/errors/grammar-parse-error.test.ts",
77
+ "javascript/src/utils/errors/grammar-parse-error.ts",
78
+ "javascript/src/utils/errors/input-parse-error.test.ts",
79
+ "javascript/src/utils/errors/input-parse-error.ts",
80
+ "javascript/src/utils/is-point-in-range.test.ts",
81
+ "javascript/src/utils/is-point-in-range.ts",
82
+ "javascript/tsconfig.json",
83
+ "javascript/tsconfig.test.json",
84
+ "javascript/vite.config.esm.ts",
85
+ "javascript/vite.config.ts",
86
+ "javascript/vite.config.umd.ts",
87
+ "javascript/vitest.config.integration.ts",
88
+ "javascript/vitest.config.unit.ts"
89
+ ]
90
+ },
91
+ "sandbox": {
92
+ "image_id": "sha256:74a55b57a5036260f0b7828c0cfaacb827b03a42759579e23169d9d3b5641567"
93
+ },
94
+ "harness": {
95
+ "commit": "353d5b38f59a4ff542135ffe337cbfdf9199c5c5",
96
+ "dirty": true
97
+ }
98
+ }
v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/README.md ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # GBNF (Python)
2
+
3
+ Python port of the `gbnf` Javascript library for parsing `.gbnf` grammars.
4
+
5
+ ```python
6
+ from gbnf import GBNF
7
+
8
+ state = GBNF('root ::= "I like green eggs and ham"')
9
+ list(state) # [{'type': RuleType.CHAR, 'value': [73]}]
10
+ state = state.add("I li") # states are immutable; `state("I li")` also works
11
+ list(state) # [{'type': RuleType.CHAR, 'value': [107]}]
12
+ ```
13
+
14
+ Rules are dicts with a `type` of `RuleType.CHAR`, `RuleType.CHAR_EXCLUDE` or `RuleType.END`
15
+ (`RuleType` is a `str` enum, so these compare equal to `"char"`, `"char_exclude"`, `"end"`).
16
+ `value` holds code points, or `[start, end]` ranges (see `is_range`).
17
+ Invalid grammars raise `GrammarParseError`; invalid input raises `InputParseError`.
18
+
19
+ Run tests with `pytest`.
v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/__init__.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """A library for parsing GBNF grammars."""
2
+ from .gbnf import GBNF
3
+ from .grammar_graph.parse_state import ParseState
4
+ from .grammar_graph.type_guards import is_range
5
+ from .grammar_graph.types import RuleType
6
+ from .utils.errors.grammar_parse_error import GrammarParseError
7
+ from .utils.errors.input_parse_error import InputParseError
8
+
9
+ __all__ = [
10
+ "GBNF",
11
+ "ParseState",
12
+ "RuleType",
13
+ "is_range",
14
+ "GrammarParseError",
15
+ "InputParseError",
16
+ ]
v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/gbnf.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .grammar_graph.graph import Graph
2
+ from .grammar_graph.parse_state import ParseState
3
+ from .grammar_parser.build_rule_stack import build_rule_stack
4
+ from .grammar_graph.serialize import to_json
5
+ from .rules_builder import RulesBuilder
6
+ from .utils.errors.grammar_parse_error import GrammarParseError
7
+
8
+
9
+ def GBNF(input_, initial_string="") -> ParseState:
10
+ """Parses a GBNF grammar and returns the initial ParseState.
11
+
12
+ `input_` may be a grammar string, or any object whose `str()` is a grammar.
13
+ `initial_string` may be a string, a code point, or a list of code points.
14
+ """
15
+ grammar = input_ if isinstance(input_, str) else str(input_)
16
+ builder = RulesBuilder(grammar)
17
+ rules, symbol_ids = builder.rules, builder.symbol_ids
18
+ if len(rules) == 0:
19
+ raise GrammarParseError(grammar, 0, "No rules were found")
20
+ if "root" not in symbol_ids:
21
+ raise GrammarParseError(
22
+ grammar, 0,
23
+ f"Grammar does not contain a root symbol. Available symbols are: {to_json(list(symbol_ids.keys()))}",
24
+ )
25
+ root_id = symbol_ids.get("root")
26
+
27
+ stacked_rules = [build_rule_stack(rule) for rule in rules]
28
+ graph = Graph(grammar, stacked_rules, root_id)
29
+ return ParseState(graph, graph.add(initial_string))
v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/__init__.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ from .graph import Graph
2
+
3
+ __all__ = ['Graph']
v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/colorize.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from enum import Enum
2
+
3
+
4
+ class Color(str, Enum):
5
+ BLUE = "\x1b[34m"
6
+ CYAN = "\x1b[36m"
7
+ GREEN = "\x1b[32m"
8
+ RED = "\x1b[31m"
9
+ GRAY = "\x1b[90m"
10
+ YELLOW = "\x1b[33m"
11
+
12
+ def __str__(self) -> str:
13
+ return self.value
14
+
15
+
16
+ def colorize(s, color) -> str:
17
+ color = color.value if isinstance(color, Color) else color
18
+ return f"{color}{s}"
v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/generic_set.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class GenericSet:
2
+ """An insertion-ordered set that de-duplicates elements by a derived key."""
3
+
4
+ def __init__(self, get_key):
5
+ self.get_key = get_key
6
+ self._keys: dict = {}
7
+
8
+ def add(self, el) -> None:
9
+ key = self.get_key(el)
10
+ if key not in self._keys:
11
+ self._keys[key] = el
12
+
13
+ def delete(self, el) -> None:
14
+ key = self.get_key(el)
15
+ if key not in self._keys:
16
+ raise KeyError("Could not get ref")
17
+ del self._keys[key]
18
+
19
+ def has(self, el) -> bool:
20
+ return any(e is el for e in self._keys.values())
21
+
22
+ def __contains__(self, el) -> bool:
23
+ return self.has(el)
24
+
25
+ def get(self, el):
26
+ return self._keys.get(self.get_key(el))
27
+
28
+ def __iter__(self):
29
+ return iter(list(self._keys.values()))
30
+
31
+ @property
32
+ def size(self) -> int:
33
+ return len(self._keys)
34
+
35
+ def __len__(self) -> int:
36
+ return len(self._keys)
v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/get_input_as_code_points.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ def get_input_as_code_points(src) -> list[int]:
2
+ if not isinstance(src, str):
3
+ return src if isinstance(src, list) else list(src) if isinstance(src, tuple) else [src]
4
+ return [ord(char) for char in src]
v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/get_parent_stack_id.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .colorize import Color
2
+
3
+
4
+ def _meta_get(meta, key):
5
+ return meta[key] if isinstance(meta, dict) else getattr(meta, key)
6
+
7
+
8
+ def get_parent_stack_id(pointer, col) -> str:
9
+ stack_ids = []
10
+ parent = pointer.parent
11
+ while parent:
12
+ meta = parent.node.meta
13
+ stack_ids.append(f"{_meta_get(meta, 'stack_id')},{_meta_get(meta, 'path_id')},{_meta_get(meta, 'step_id')}")
14
+ parent = parent.parent
15
+ return col("<-", Color.GRAY).join(col(id_, Color.RED) for id_ in stack_ids)
v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/get_serialized_rule_key.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from . import type_guards
2
+ from .serialize import to_json
3
+ from .types import RuleType
4
+
5
+ KEY_TRANSLATION = {
6
+ RuleType.END: 0,
7
+ RuleType.CHAR: 1,
8
+ RuleType.CHAR_EXCLUDE: 2,
9
+ }
10
+
11
+
12
+ def get_serialized_rule_key(rule) -> str:
13
+ if type_guards.is_rule_end(rule):
14
+ return f"{KEY_TRANSLATION[RuleType.END]}"
15
+
16
+ if type_guards.is_rule_char(rule) or type_guards.is_rule_char_excluded(rule):
17
+ return f"{KEY_TRANSLATION[RuleType(rule['type'])]}-{to_json(rule['value'])}"
18
+ if type_guards.is_rule_ref(rule):
19
+ return f"3-{rule.value}"
20
+ raise ValueError(f"Unknown rule type: {to_json(rule)}")
v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/graph.py ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ..utils.errors.input_parse_error import InputParseError
2
+ from ..utils.is_point_in_range import is_point_in_range
3
+ from .colorize import colorize
4
+ from .generic_set import GenericSet
5
+ from .get_input_as_code_points import get_input_as_code_points
6
+ from .get_serialized_rule_key import get_serialized_rule_key
7
+ from .graph_node import GraphNode
8
+ from .graph_pointer import GraphPointer
9
+ from .serialize import to_json
10
+ from .type_guards import (
11
+ is_range,
12
+ is_rule_char,
13
+ is_rule_char_excluded,
14
+ is_rule_end,
15
+ is_rule_ref,
16
+ )
17
+
18
+
19
+ def make_pointers() -> GenericSet:
20
+ return GenericSet(lambda p: p.id)
21
+
22
+
23
+ class Graph:
24
+ def __init__(self, grammar: str, stacked_rules: list, root_id: int):
25
+ self.roots: dict[int, dict[int, GraphNode]] = {}
26
+ self.grammar = grammar
27
+ self.previous_code_points: list[int] = []
28
+ rule_refs = []
29
+ unique_rules = GenericSet(get_serialized_rule_key)
30
+ for stack_id, stack in enumerate(stacked_rules):
31
+ nodes: dict[int, GraphNode] = {}
32
+ for path_id, path in enumerate(stack):
33
+ node = None
34
+ for step_id in range(len(path) - 1, -1, -1):
35
+ next_node = node
36
+ rule = path[step_id]
37
+ unique_rules.add(rule)
38
+ if is_rule_ref(rule):
39
+ rule_refs.append(rule)
40
+ # rules coming in may be identical but have different references.
41
+ # here, we ensure we always use the same reference for an identical rule.
42
+ # this makes future comparisons easier.
43
+ unique_rule = unique_rules.get(rule)
44
+ if unique_rule is None:
45
+ raise ValueError("Could not get unique rule")
46
+ node = GraphNode(unique_rule, {"stack_id": stack_id, "path_id": path_id, "step_id": step_id}, next_node)
47
+ if node is None:
48
+ raise ValueError("Could not get node")
49
+ nodes[path_id] = node
50
+
51
+ self.roots[stack_id] = nodes
52
+ root_node = self.roots.get(root_id)
53
+ if root_node is None:
54
+ raise ValueError(f"Root node not found for value: {root_id}")
55
+ self._root_node = root_node
56
+
57
+ for rule_ref in rule_refs:
58
+ rule_ref.nodes = list(self.get_root_node(rule_ref.value).values())
59
+
60
+ def get_root_node(self, value: int) -> dict:
61
+ root_node = self.roots.get(value)
62
+ if root_node is None:
63
+ raise ValueError(f"Root node not found for value: {value}")
64
+ return root_node
65
+
66
+ def get_initial_pointers(self) -> GenericSet:
67
+ pointers = make_pointers()
68
+
69
+ root_node = self._root_node
70
+ if root_node is None:
71
+ raise ValueError("Root node is not defined")
72
+
73
+ for node, parent in self._fetch_nodes_for_root_node(root_node):
74
+ pointer = GraphPointer(node, parent)
75
+ for resolved_pointer in self._resolve_pointer(pointer):
76
+ pointers.add(resolved_pointer)
77
+ return pointers
78
+
79
+ @staticmethod
80
+ def _set_valid(pointers, valid: bool) -> None:
81
+ for pointer in pointers:
82
+ pointer.valid = valid
83
+
84
+ def _parse(self, current_pointers: GenericSet, code_point: int) -> GenericSet:
85
+ for rule, graph_pointers in self._iterate_over_pointers(current_pointers):
86
+ if is_rule_char(rule):
87
+ valid = any(
88
+ is_point_in_range(code_point, possible) if is_range(possible) else code_point == possible
89
+ for possible in rule["value"]
90
+ )
91
+ self._set_valid(graph_pointers, valid)
92
+ elif is_rule_char_excluded(rule):
93
+ valid = all(
94
+ not is_point_in_range(code_point, possible) if is_range(possible) else code_point != possible
95
+ for possible in rule["value"]
96
+ )
97
+ self._set_valid(graph_pointers, valid)
98
+ elif not is_rule_end(rule):
99
+ raise ValueError(f"Unsupported rule: {to_json(rule)}")
100
+
101
+ # a pointer's id is the sum of its node's id and its parent's id chain.
102
+ # if two pointers share the same id, it means they point to the same node and have identical parent chains.
103
+ # for the purposes of walking the graph, we only need to keep one of them.
104
+ next_pointers = make_pointers()
105
+ for current_pointer in current_pointers:
106
+ for unresolved_next_pointer in current_pointer.fetch_next():
107
+ for resolved_next_pointer in self._resolve_pointer(unresolved_next_pointer):
108
+ next_pointers.add(resolved_next_pointer)
109
+ return next_pointers
110
+
111
+ @staticmethod
112
+ def _resolve_pointer(unresolved_pointer: GraphPointer):
113
+ for resolved_pointer in unresolved_pointer.resolve():
114
+ if is_rule_ref(resolved_pointer.node.rule):
115
+ raise ValueError("Encountered a reference rule when building pointers to the graph")
116
+ if is_rule_end(resolved_pointer.node.rule) and resolved_pointer.parent:
117
+ raise ValueError("Encountered an ending rule with a parent when building pointers to the graph")
118
+ yield resolved_pointer
119
+
120
+ def add(self, src, _pointers=None) -> GenericSet:
121
+ pointers = _pointers if _pointers is not None else self.get_initial_pointers()
122
+ code_points = get_input_as_code_points(src)
123
+ for code_point_pos, code_point in enumerate(code_points):
124
+ pointers = self._parse(pointers, code_point)
125
+ if pointers.size == 0:
126
+ raise InputParseError(code_points, code_point_pos, self.previous_code_points)
127
+ self.previous_code_points.extend(code_points)
128
+ return pointers
129
+
130
+ # generator that yields either the node, or if a reference rule, the referenced node
131
+ # we need these function, as distinct from leveraging the logic in GraphPointer,
132
+ # because that needs a rule ref with already defined nodes; this function is used to _set_ those nodes
133
+ def _fetch_nodes_for_root_node(self, root_nodes: dict, parent=None):
134
+ for node in root_nodes.values():
135
+ if is_rule_ref(node.rule):
136
+ yield from self._fetch_nodes_for_root_node(
137
+ self.get_root_node(node.rule.value), GraphPointer(node, parent)
138
+ )
139
+ else:
140
+ yield node, parent
141
+
142
+ def __repr__(self) -> str:
143
+ return self.print(colors=True)
144
+
145
+ def print(self, pointers=None, colors: bool = False) -> str:
146
+ col = colorize if colors else (lambda s, _color: f"{s}")
147
+ graph_view = [
148
+ node.print(pointers=pointers, show_position=True, colorize=col)
149
+ for nodes in self.roots.values()
150
+ for node in nodes.values()
151
+ ]
152
+ return "\n" + "\n".join(graph_view)
153
+
154
+ @staticmethod
155
+ def _iterate_over_pointers(pointers):
156
+ seen_rules: dict[int, tuple] = {}
157
+ for pointer in pointers:
158
+ rule = pointer.rule
159
+ if is_rule_ref(rule):
160
+ raise ValueError("Encountered a reference rule in the graph, this should not happen")
161
+ seen = seen_rules.get(id(rule))
162
+ if seen is None:
163
+ seen_rules[id(rule)] = (rule, [pointer])
164
+ else:
165
+ seen[1].append(pointer)
166
+
167
+ yield from seen_rules.values()
v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/graph_node.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from . import print as _print
2
+ from .colorize import colorize
3
+
4
+
5
+ class GraphNode:
6
+ """A node in the grammar graph.
7
+
8
+ `meta` is a dict of the shape {"stack_id": int, "path_id": int, "step_id": int}.
9
+ """
10
+
11
+ def __init__(self, rule, meta, next=None):
12
+ self.rule = rule
13
+ if meta is None:
14
+ raise ValueError("Meta is undefined")
15
+ self.meta = meta
16
+ self.next = next
17
+ self._id = None
18
+ self.print = _print.print_graph_node(self)
19
+
20
+ @property
21
+ def id(self) -> str:
22
+ if not self._id:
23
+ meta = self.meta
24
+ self._id = f"{meta['stack_id']},{meta['path_id']},{meta['step_id']}"
25
+ return self._id
26
+
27
+ def __repr__(self) -> str:
28
+ return self.print(colorize=colorize, show_position=False)
v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/graph_pointer.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from . import type_guards
2
+ from .colorize import colorize
3
+ from .print import print_graph_pointer
4
+ from .serialize import to_json
5
+
6
+
7
+ class GraphPointer:
8
+ def __init__(self, node, parent=None):
9
+ if node is None:
10
+ raise ValueError("Node is undefined")
11
+ self.node = node
12
+ self.parent = parent
13
+ self._valid = None
14
+ self.id = f"{parent.id}-{node.id}" if parent else node.id
15
+ self.print = print_graph_pointer(self)
16
+
17
+ def resolve(self, resolved: bool = False):
18
+ """
19
+ 1. If the current node is an end node, and the pointer has a parent, return the parent's `fetch_next`; else return nothing.
20
+ 2. If the current node is a rule ref, yield the referenced nodes, _unless_ resolved is true, in which case it returns next.
21
+ 3. If the current node is a char or range, we go to the next node. If none exists, throw an error.
22
+ """
23
+ if type_guards.is_graph_pointer_rule_ref(self):
24
+ if resolved:
25
+ if not self.node.next:
26
+ raise ValueError(f"No next node: {self.node!r}")
27
+ yield from GraphPointer(self.node.next, self.parent).resolve()
28
+ else:
29
+ for node in self.node.rule.nodes:
30
+ yield from GraphPointer(node, self).resolve()
31
+ elif type_guards.is_graph_pointer_rule_end(self):
32
+ if not self.parent:
33
+ yield self
34
+ else:
35
+ yield from self.parent.resolve(True)
36
+ elif type_guards.is_graph_pointer_rule_char(self) or type_guards.is_graph_pointer_rule_char_exclude(self):
37
+ yield self
38
+ else:
39
+ raise ValueError(f"Unknown rule: {to_json(self.node.rule)}")
40
+
41
+ def fetch_next(self):
42
+ # if this pointer is invalid, then we don't return any new pointers
43
+ if self._valid is False:
44
+ return
45
+
46
+ # if this pointer is an end node, we return the parent's next node. If no parent exists,
47
+ # we return nothing, since it's the end of the line.
48
+ if type_guards.is_rule_end(self.node.rule):
49
+ if self.parent:
50
+ yield from self.parent.fetch_next()
51
+ else:
52
+ if not self.node.next:
53
+ raise ValueError(f"No next node: {self.node!r}")
54
+ pointer = GraphPointer(self.node.next, self.parent)
55
+ yield from pointer.resolve()
56
+
57
+ @property
58
+ def rule(self):
59
+ return self.node.rule
60
+
61
+ @property
62
+ def valid(self):
63
+ return self._valid
64
+
65
+ @valid.setter
66
+ def valid(self, valid: bool) -> None:
67
+ self._valid = valid
68
+
69
+ def __repr__(self) -> str:
70
+ return self.print(colorize=colorize)
v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/parse_state.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .serialize import to_json
2
+
3
+
4
+ def _rule_of(pointer):
5
+ return pointer["rule"] if isinstance(pointer, dict) else pointer.rule
6
+
7
+
8
+ class ParseState:
9
+ """An immutable parse state. Iterate over it to get the rules that may come next."""
10
+
11
+ def __init__(self, graph, pointers):
12
+ self._graph = graph
13
+ self._pointers = pointers
14
+
15
+ def __call__(self, input_) -> "ParseState":
16
+ return self.add(input_)
17
+
18
+ def __iter__(self):
19
+ return self.rules()
20
+
21
+ def rules(self):
22
+ seen = set()
23
+ for pointer in self._pointers:
24
+ rule = _rule_of(pointer)
25
+ key = to_json(rule)
26
+ if key not in seen:
27
+ seen.add(key)
28
+ yield rule
29
+
30
+ def add(self, input_) -> "ParseState":
31
+ pointers = self._graph.add(input_, self._pointers)
32
+ return ParseState(self._graph, pointers)
33
+
34
+ @property
35
+ def size(self) -> int:
36
+ return len(list(self.rules()))
37
+
38
+ @property
39
+ def grammar(self) -> str:
40
+ return self._graph.grammar
v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/print.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from . import get_parent_stack_id as _get_parent_stack_id
2
+ from .colorize import Color
3
+ from .type_guards import is_range, is_rule_char, is_rule_ref
4
+
5
+
6
+ def print_graph_pointer(pointer):
7
+ def _print(colorize, **_kwargs) -> str:
8
+ col = colorize
9
+ return col(f"*{_get_parent_stack_id.get_parent_stack_id(pointer, col)}", Color.RED)
10
+
11
+ return _print
12
+
13
+
14
+ def print_graph_node(node):
15
+ def _print(colorize, pointers=None, show_position: bool = False) -> str:
16
+ col = colorize
17
+ rule = node.rule
18
+
19
+ parts = []
20
+ if show_position:
21
+ parts.extend([
22
+ col("{", Color.BLUE),
23
+ col(node.id, Color.GRAY),
24
+ col("}", Color.BLUE),
25
+ ])
26
+ if is_rule_char(rule):
27
+ chars = []
28
+ for v in rule["value"]:
29
+ if is_range(v):
30
+ # mirrors Javascript's Array#join, which joins nested arrays with commas
31
+ chars.append(",".join(col(chr(val), Color.YELLOW) for val in v))
32
+ else:
33
+ chars.append(_get_char(v))
34
+ parts.extend([
35
+ col("[", Color.GRAY),
36
+ col("".join(chars), Color.YELLOW),
37
+ col("]", Color.GRAY),
38
+ ])
39
+ elif is_rule_ref(rule):
40
+ parts.append(col("Ref(", Color.GRAY) + col(f"{rule.value}", Color.GREEN) + col(")", Color.GRAY))
41
+ else:
42
+ rule_type = rule.get("type") if isinstance(rule, dict) else getattr(rule, "type", None)
43
+ parts.append(col(str(rule_type), Color.YELLOW))
44
+
45
+ if pointers:
46
+ for pointer in pointers:
47
+ pointer_parts = []
48
+ if pointer.node is node:
49
+ pointer_parts.append(pointer.print(colorize=col))
50
+ if pointer_parts:
51
+ parts.append(col("[", Color.GRAY))
52
+ parts.extend(pointer_parts)
53
+ parts.append(col("]", Color.GRAY))
54
+
55
+ next_node = getattr(node, "next", None)
56
+ segments = [
57
+ "".join(str(p) for p in parts),
58
+ next_node.print(pointers=pointers, colorize=col, show_position=show_position) if next_node else None,
59
+ ]
60
+ return col("-> ", Color.GRAY).join(s for s in segments if s)
61
+
62
+ return _print
63
+
64
+
65
+ def _get_char(char_code: int) -> str:
66
+ char = chr(char_code)
67
+ if char == "\n":
68
+ return "\\n"
69
+ return char
v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/rule_ref.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class RuleRef:
2
+ def __init__(self, value: int):
3
+ self.value = value
4
+ self._nodes = None
5
+
6
+ @property
7
+ def nodes(self):
8
+ if self._nodes is None:
9
+ raise ValueError("Nodes are not set")
10
+ return self._nodes
11
+
12
+ @nodes.setter
13
+ def nodes(self, nodes) -> None:
14
+ self._nodes = nodes
15
+
16
+ def __eq__(self, other):
17
+ if not isinstance(other, RuleRef):
18
+ return NotImplemented
19
+ return self.value == other.value
20
+
21
+ __hash__ = object.__hash__
22
+
23
+ def __repr__(self) -> str:
24
+ return f"RuleRef({self.value})"
v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/serialize.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+
3
+
4
+ def to_json(value) -> str:
5
+ """Serializes a value the same way Javascript's JSON.stringify does (compact)."""
6
+ return json.dumps(value, separators=(",", ":"), ensure_ascii=False, default=_default)
7
+
8
+
9
+ def _default(o):
10
+ value = getattr(o, "value", None)
11
+ if value is not None:
12
+ return {"value": value}
13
+ return repr(o)
v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/type_guards.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .rule_ref import RuleRef
2
+ from .types import RuleType
3
+
4
+ _RULE_TYPES = {t.value for t in RuleType}
5
+
6
+
7
+ def _type_of(rule):
8
+ if isinstance(rule, dict):
9
+ return rule.get("type")
10
+ return getattr(rule, "type", None)
11
+
12
+
13
+ def _is_number(n) -> bool:
14
+ return isinstance(n, (int, float)) and not isinstance(n, bool)
15
+
16
+
17
+ def is_rule_type(type_=None) -> bool:
18
+ return bool(type_) and isinstance(type_, str) and type_ in _RULE_TYPES
19
+
20
+
21
+ def is_rule(rule=None) -> bool:
22
+ return isinstance(rule, dict) and "type" in rule and is_rule_type(rule["type"])
23
+
24
+
25
+ def is_rule_ref(rule=None) -> bool:
26
+ return isinstance(rule, RuleRef)
27
+
28
+
29
+ def is_rule_end(rule=None) -> bool:
30
+ return rule is not None and not is_rule_ref(rule) and _type_of(rule) == RuleType.END
31
+
32
+
33
+ def is_rule_char(rule=None) -> bool:
34
+ return rule is not None and not is_rule_ref(rule) and _type_of(rule) == RuleType.CHAR
35
+
36
+
37
+ def is_rule_char_excluded(rule=None) -> bool:
38
+ return rule is not None and not is_rule_ref(rule) and _type_of(rule) == RuleType.CHAR_EXCLUDE
39
+
40
+
41
+ def is_range(range_=None) -> bool:
42
+ return isinstance(range_, (list, tuple)) and len(range_) == 2 and all(_is_number(n) for n in range_)
43
+
44
+
45
+ def is_graph_pointer_rule_ref(pointer) -> bool:
46
+ return is_rule_ref(pointer.rule)
47
+
48
+
49
+ def is_graph_pointer_rule_end(pointer) -> bool:
50
+ return is_rule_end(pointer.rule)
51
+
52
+
53
+ def is_graph_pointer_rule_char(pointer) -> bool:
54
+ return is_rule_char(pointer.rule)
55
+
56
+
57
+ def is_graph_pointer_rule_char_exclude(pointer) -> bool:
58
+ return is_rule_char_excluded(pointer.rule)
v2-tests-configuration/runs/20261012T023847Z_df0e91f6/ported_implementation/gbnf/grammar_graph/types.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from enum import Enum
2
+
3
+
4
+ class RuleType(str, Enum):
5
+ CHAR = "char"
6
+ CHAR_EXCLUDE = "char_exclude"
7
+ END = "end"
8
+
9
+ def __str__(self) -> str:
10
+ return self.value
11
+
12
+
13
+ # Rules are plain dicts:
14
+ # {"type": RuleType.CHAR, "value": [int | [int, int], ...]}
15
+ # {"type": RuleType.CHAR_EXCLUDE, "value": [int | [int, int], ...]}
16
+ # {"type": RuleType.END}
17
+ # A Range is a two element list [start, end] (inclusive).
18
+ # RuleRef instances (see rule_ref.py) are never exposed to the end user.
19
+ #
20
+ # ValidInput can either be a string, a number indicating a code point, or a list of code points.