thekevinscott commited on
Commit
f4e114d
·
verified ·
1 Parent(s): c51ffe0

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

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-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_graph/test_generic_set.py +65 -0
  2. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_graph/test_get_input_as_code_points.py +15 -0
  3. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_graph/test_get_parent_stack_id.py +55 -0
  4. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_graph/test_get_serialized_rule_key.py +33 -0
  5. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_graph/test_graph.py +57 -0
  6. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_graph/test_graph_node.py +51 -0
  7. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_graph/test_graph_pointer.py +147 -0
  8. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_graph/test_parse_state.py +71 -0
  9. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_graph/test_print.py +83 -0
  10. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_graph/test_rule_ref.py +25 -0
  11. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_graph/test_type_guards.py +120 -0
  12. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_parser/__pycache__/test_build_rule_stack.cpython-314-pytest-9.1.1.pyc +0 -0
  13. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_parser/test_build_rule_stack.py +331 -0
  14. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/rules_builder/__pycache__/test_is_word_char.cpython-314-pytest-9.1.1.pyc +0 -0
  15. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/rules_builder/__pycache__/test_parse_char.cpython-314-pytest-9.1.1.pyc +0 -0
  16. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/rules_builder/__pycache__/test_parse_name.cpython-314-pytest-9.1.1.pyc +0 -0
  17. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/rules_builder/__pycache__/test_parse_space.cpython-314-pytest-9.1.1.pyc +0 -0
  18. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/rules_builder/__pycache__/test_rules_builder.cpython-314-pytest-9.1.1.pyc +0 -0
  19. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/rules_builder/test_is_word_char.py +22 -0
  20. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/rules_builder/test_parse_char.py +55 -0
  21. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/rules_builder/test_parse_name.py +40 -0
  22. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/rules_builder/test_parse_space.py +61 -0
  23. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/rules_builder/test_rules_builder.py +869 -0
  24. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/test_gbnf.py +89 -0
  25. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/test_public_api.py +113 -0
  26. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/utils/__pycache__/test_is_point_in_range.cpython-314-pytest-9.1.1.pyc +0 -0
  27. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/utils/errors/__pycache__/test_build_error_position.cpython-314-pytest-9.1.1.pyc +0 -0
  28. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/utils/errors/__pycache__/test_get_input_as_string.cpython-314-pytest-9.1.1.pyc +0 -0
  29. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/utils/errors/__pycache__/test_grammar_parse_error.cpython-314-pytest-9.1.1.pyc +0 -0
  30. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/utils/errors/__pycache__/test_input_parse_error.cpython-314-pytest-9.1.1.pyc +0 -0
  31. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/utils/errors/test_build_error_position.py +110 -0
  32. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/utils/errors/test_get_input_as_string.py +16 -0
  33. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/utils/errors/test_grammar_parse_error.py +31 -0
  34. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/utils/errors/test_input_parse_error.py +57 -0
  35. v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/utils/test_is_point_in_range.py +15 -0
  36. v2-models-effort/runs/20261017T020314Z_49fc1c31/proxy.log +45 -0
  37. v2-models-effort/runs/20261017T020314Z_49fc1c31/run.json +10 -0
  38. v2-models-effort/runs/20261017T020314Z_49fc1c31/stderr.txt +0 -0
  39. v2-models-effort/runs/20261017T020314Z_49fc1c31/transcript/.claude/projects/-target/40c7e6d4-ddda-4b53-8838-a4ced124132f.jsonl +0 -0
  40. v2-models-effort/runs/20261017T020314Z_8560337c/manifest.json +105 -0
  41. v2-models-effort/runs/20261017T020314Z_8560337c/ported_implementation/.gitignore +9 -0
  42. v2-models-effort/runs/20261017T020314Z_8560337c/ported_implementation/README.md +107 -0
  43. v2-models-effort/runs/20261017T020314Z_8560337c/ported_implementation/gbnf/__init__.py +32 -0
  44. v2-models-effort/runs/20261017T020314Z_8560337c/ported_implementation/gbnf/gbnf.py +40 -0
  45. v2-models-effort/runs/20261017T020314Z_8560337c/ported_implementation/gbnf/grammar_graph/__init__.py +3 -0
  46. v2-models-effort/runs/20261017T020314Z_8560337c/ported_implementation/gbnf/grammar_graph/colorize.py +21 -0
  47. v2-models-effort/runs/20261017T020314Z_8560337c/ported_implementation/gbnf/grammar_graph/generic_set.py +48 -0
  48. v2-models-effort/runs/20261017T020314Z_8560337c/ported_implementation/gbnf/grammar_graph/get_input_as_code_points.py +12 -0
  49. v2-models-effort/runs/20261017T020314Z_8560337c/ported_implementation/gbnf/grammar_graph/get_parent_stack_id.py +18 -0
  50. v2-models-effort/runs/20261017T020314Z_8560337c/ported_implementation/gbnf/grammar_graph/get_serialized_rule_key.py +23 -0
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_graph/test_generic_set.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ from gbnf.grammar_graph.generic_set import GenericSet
4
+
5
+
6
+ def describe_generic_set():
7
+ def it_instantiates():
8
+ assert isinstance(GenericSet(lambda _el: 'foo'), GenericSet)
9
+
10
+ def it_iterates():
11
+ set_ = GenericSet(lambda el: el)
12
+ set_.add(1)
13
+ set_.add(2)
14
+ set_.add(3)
15
+ assert [*set_] == [1, 2, 3]
16
+
17
+ def it_skips_duplicates():
18
+ set_ = GenericSet(lambda el: el)
19
+ set_.add(1)
20
+ set_.add(1)
21
+ set_.add(1)
22
+ assert [*set_] == [1]
23
+
24
+ def it_skips_duplicates_based_on_a_given_key():
25
+ set_ = GenericSet(lambda el: el['id'])
26
+ set_.add({'id': 1})
27
+ set_.add({'id': 1, 'foo': 'foo'})
28
+ set_.add({'id': 1, 'bar': 'bar'})
29
+ assert [*set_] == [{'id': 1}]
30
+
31
+ def it_can_delete():
32
+ set_ = GenericSet(lambda el: el['id'])
33
+ set_.add({'id': 1})
34
+ set_.delete({'id': 1})
35
+ set_.add({'id': 2, 'foo': 'foo'})
36
+ set_.add({'id': 2, 'bar': 'bar'})
37
+ assert [*set_] == [{'id': 2, 'foo': 'foo'}]
38
+
39
+ def it_throws_when_deleting_a_missing_element():
40
+ set_ = GenericSet(lambda el: el['id'])
41
+ with pytest.raises(Exception, match='Could not get ref'):
42
+ set_.delete({'id': 1})
43
+
44
+ def it_gets_the_stored_element_for_a_key():
45
+ set_ = GenericSet(lambda el: el['id'])
46
+ stored = {'id': 1, 'foo': 'foo'}
47
+ set_.add(stored)
48
+ assert set_.get({'id': 1}) is stored
49
+ assert set_.get({'id': 2}) is None
50
+
51
+ def it_checks_membership_by_identity():
52
+ set_ = GenericSet(lambda el: el['id'])
53
+ stored = {'id': 1}
54
+ set_.add(stored)
55
+ assert set_.has(stored)
56
+ assert stored in set_
57
+ assert not set_.has({'id': 1})
58
+
59
+ def it_reports_its_size():
60
+ set_ = GenericSet(lambda el: el)
61
+ set_.add(1)
62
+ set_.add(2)
63
+ set_.add(2)
64
+ assert set_.size == 2
65
+ assert len(set_) == 2
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_graph/test_get_input_as_code_points.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from gbnf.grammar_graph.get_input_as_code_points import get_input_as_code_points
2
+
3
+
4
+ def describe_get_input_as_code_points():
5
+ def it_returns_code_points_for_string():
6
+ assert get_input_as_code_points('abc') == [97, 98, 99]
7
+
8
+ def it_returns_code_points_for_number():
9
+ assert get_input_as_code_points(99) == [99]
10
+
11
+ def it_returns_code_points_for_array_of_number():
12
+ assert get_input_as_code_points([99, 100, 101]) == [99, 100, 101]
13
+
14
+ def it_returns_a_single_code_point_for_characters_outside_the_bmp():
15
+ assert get_input_as_code_points('a😀') == [97, 0x1F600]
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_graph/test_get_parent_stack_id.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from types import SimpleNamespace
3
+
4
+ from gbnf.grammar_graph.colorize import Color
5
+ from gbnf.grammar_graph.get_parent_stack_id import get_parent_stack_id
6
+ from gbnf.grammar_graph.graph_node import GraphNodeMeta
7
+
8
+
9
+ def s(value):
10
+ return json.dumps(value)
11
+
12
+
13
+ def mock_colorize(text, color):
14
+ return f'[{s(color)}]:{text}'
15
+
16
+
17
+ red = s(Color.RED)
18
+ gray = s(Color.GRAY)
19
+
20
+
21
+ def create_mock_pointer(stack_id, path_id, step_id, parent=None):
22
+ return SimpleNamespace(
23
+ node=SimpleNamespace(meta=GraphNodeMeta(stack_id=stack_id, path_id=path_id, step_id=step_id)),
24
+ parent=parent,
25
+ )
26
+
27
+
28
+ def describe_get_parent_stack_id():
29
+ def it_returns_an_empty_string_if_no_parents():
30
+ pointer = create_mock_pointer(1, 1, 1)
31
+ result = get_parent_stack_id(pointer, mock_colorize)
32
+ assert result == ''
33
+
34
+ def it_returns_a_single_parent_id_colored_correctly():
35
+ parent_pointer = create_mock_pointer(1, 1, 1)
36
+ pointer = create_mock_pointer(2, 2, 2, parent_pointer)
37
+ result = get_parent_stack_id(pointer, mock_colorize)
38
+ assert result == f'[{red}]:1,1,1'
39
+
40
+ def it_returns_multiple_parent_ids_separated_by_colored_arrows():
41
+ grandparent_pointer = create_mock_pointer(0, 0, 0)
42
+ parent_pointer = create_mock_pointer(1, 1, 1, grandparent_pointer)
43
+ pointer = create_mock_pointer(2, 2, 2, parent_pointer)
44
+ result = get_parent_stack_id(pointer, mock_colorize)
45
+ expected_output = f'[{red}]:1,1,1[{gray}]:<-[{red}]:0,0,0'
46
+ assert result == expected_output
47
+
48
+ def it_handles_deep_nesting_of_pointers():
49
+ great_grandparent_pointer = create_mock_pointer(0, 0, 0)
50
+ grandparent_pointer = create_mock_pointer(1, 1, 1, great_grandparent_pointer)
51
+ parent_pointer = create_mock_pointer(2, 2, 2, grandparent_pointer)
52
+ pointer = create_mock_pointer(3, 3, 3, parent_pointer)
53
+ result = get_parent_stack_id(pointer, mock_colorize)
54
+ expected_output = f'[{red}]:2,2,2[{gray}]:<-[{red}]:1,1,1[{gray}]:<-[{red}]:0,0,0'
55
+ assert result == expected_output
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_graph/test_get_serialized_rule_key.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ from gbnf.grammar_graph.get_serialized_rule_key import KEY_TRANSLATION, get_serialized_rule_key
4
+ from gbnf.grammar_graph.rule_ref import RuleRef
5
+ from gbnf.grammar_graph.types import RuleChar, RuleCharExclude, RuleEnd, RuleType
6
+
7
+
8
+ def describe_get_serialized_rule_key():
9
+ def it_returns_type_for_end_rules():
10
+ rule = RuleEnd()
11
+ assert get_serialized_rule_key(rule) == f'{KEY_TRANSLATION[RuleType.END]}'
12
+
13
+ def it_returns_type_and_value_for_character_rules():
14
+ rule = RuleChar([97])
15
+ assert get_serialized_rule_key(rule) == f'{KEY_TRANSLATION[RuleType.CHAR]}-[97]'
16
+
17
+ def it_returns_type_and_value_for_character_exclude_rules():
18
+ rule = RuleCharExclude([97])
19
+ assert get_serialized_rule_key(rule) == f'{KEY_TRANSLATION[RuleType.CHAR_EXCLUDE]}-[97]'
20
+
21
+ def it_serializes_ranges_compactly():
22
+ rule = {'type': RuleType.CHAR, 'value': [97, [98, 122]]}
23
+ assert get_serialized_rule_key(rule) == f'{KEY_TRANSLATION[RuleType.CHAR]}-[97,[98,122]]'
24
+
25
+ def it_returns_ref_type_with_value_for_reference_rules():
26
+ rule = RuleRef(99)
27
+ assert get_serialized_rule_key(rule) == '3-99'
28
+
29
+ def it_throws_an_error_for_unknown_rule_types():
30
+ rule = {'type': 'UNKNOWN', 'value': 'something'}
31
+ with pytest.raises(Exception) as exc_info:
32
+ get_serialized_rule_key(rule)
33
+ assert str(exc_info.value) == 'Unknown rule type: {"type":"UNKNOWN","value":"something"}'
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_graph/test_graph.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from gbnf.grammar_graph.generic_set import GenericSet
2
+ from gbnf.grammar_graph.graph import Graph
3
+ from gbnf.grammar_graph.types import RuleType
4
+
5
+
6
+ def describe_graph():
7
+ grammar = 'example-grammar'
8
+ stacked_rules = [
9
+ [
10
+ [{'type': RuleType.CHAR, 'value': [65, 66, 67]}],
11
+ [{'type': RuleType.CHAR, 'value': [68, 69, 70]}],
12
+ ],
13
+ [
14
+ [{'type': RuleType.CHAR, 'value': [71, 72, 73]}],
15
+ [{'type': RuleType.CHAR, 'value': [74, 75, 76]}],
16
+ ],
17
+ ]
18
+ root_id = 0
19
+
20
+ def it_creates_a_graph_instance():
21
+ graph = Graph(grammar, stacked_rules, root_id)
22
+ assert isinstance(graph, Graph)
23
+ assert graph.grammar == grammar
24
+
25
+ def it_gets_the_root_node():
26
+ graph = Graph(grammar, stacked_rules, root_id)
27
+ root_node = graph._get_root_node(root_id)
28
+ assert isinstance(root_node, dict)
29
+ assert len(root_node) == 2
30
+
31
+ def it_gets_the_initial_pointers():
32
+ graph = Graph(grammar, stacked_rules, root_id)
33
+ pointers = graph._get_initial_pointers()
34
+ assert isinstance(pointers, GenericSet)
35
+ assert pointers.size == 2
36
+
37
+ def it_reuses_identical_rules():
38
+ graph = Graph(grammar, [
39
+ [[{'type': RuleType.CHAR, 'value': [65]}, {'type': RuleType.END}]],
40
+ [[{'type': RuleType.CHAR, 'value': [65]}, {'type': RuleType.END}]],
41
+ ], root_id)
42
+ first, second = graph._get_root_node(0)[0], graph._get_root_node(1)[0]
43
+ assert first.rule is second.rule
44
+ assert first.next.rule is second.next.rule
45
+
46
+ def describe_print():
47
+ def it_prints_the_graph():
48
+ graph = Graph(grammar, stacked_rules, root_id)
49
+ printed_graph = graph.print(colors=True)
50
+ assert isinstance(printed_graph, str)
51
+ assert 'ABC' in printed_graph
52
+ assert 'DEF' in printed_graph
53
+ assert 'GHI' in printed_graph
54
+
55
+ def it_prints_the_graph_without_colors():
56
+ graph = Graph(grammar, stacked_rules, root_id)
57
+ assert graph.print() == '\n{0,0,0}[ABC]\n{0,1,0}[DEF]\n{1,0,0}[GHI]\n{1,1,0}[JKL]'
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_graph/test_graph_node.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from unittest import mock
2
+
3
+ import pytest
4
+
5
+ from gbnf.grammar_graph.colorize import colorize
6
+ from gbnf.grammar_graph.graph_node import GraphNode, GraphNodeMeta
7
+ from gbnf.grammar_graph.rule_ref import RuleRef
8
+
9
+
10
+ @pytest.fixture(autouse=True)
11
+ def print_graph_node():
12
+ with mock.patch(
13
+ 'gbnf.grammar_graph.graph_node.print_graph_node',
14
+ side_effect=lambda node: lambda **_opts: f'{node.rule.value}',
15
+ ) as print_graph_node:
16
+ yield print_graph_node
17
+
18
+
19
+ def describe_graph_node():
20
+ meta = GraphNodeMeta(stack_id=1, path_id=2, step_id=3)
21
+ rule = RuleRef(42)
22
+
23
+ def it_constructs_with_a_rule_and_meta():
24
+ node = GraphNode(rule, meta)
25
+ assert node.rule is rule
26
+ assert node.meta == meta
27
+
28
+ def it_accepts_meta_as_a_mapping():
29
+ node = GraphNode(rule, {'stack_id': 1, 'path_id': 2, 'step_id': 3})
30
+ assert node.meta == meta
31
+
32
+ def it_throws_if_meta_is_undefined():
33
+ with pytest.raises(Exception, match='Meta is undefined'):
34
+ GraphNode(rule, None)
35
+
36
+ def it_correctly_calculates_and_caches_its_id():
37
+ node = GraphNode(rule, meta)
38
+ assert node.id == '1,2,3'
39
+ node.meta = GraphNodeMeta(stack_id=4, path_id=5, step_id=6)
40
+ # Check if it uses cached value on second access
41
+ assert node.id == '1,2,3'
42
+
43
+ def it_delegates_print_to_the_print_graph_node_function(print_graph_node):
44
+ node = GraphNode(rule, meta)
45
+ assert node.print(colorize=colorize, show_position=False) == '42'
46
+ print_graph_node.assert_called_with(node)
47
+
48
+ def it_handles_next_node_linkage():
49
+ next_node = GraphNode(RuleRef(43), meta)
50
+ node = GraphNode(rule, meta, next_node)
51
+ assert node.next is next_node
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_graph/test_graph_pointer.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ from gbnf.grammar_graph.graph_node import GraphNode, GraphNodeMeta
4
+ from gbnf.grammar_graph.graph_pointer import GraphPointer
5
+ from gbnf.grammar_graph.rule_ref import RuleRef
6
+ from gbnf.grammar_graph.types import RuleChar, RuleEnd, RuleType
7
+
8
+
9
+ def meta(stack_id, path_id, step_id):
10
+ return GraphNodeMeta(stack_id=stack_id, path_id=path_id, step_id=step_id)
11
+
12
+
13
+ def describe_graph_pointer():
14
+ def describe_constructor():
15
+ def it_initializes_correctly_with_just_node_parameter():
16
+ node = GraphNode({'type': RuleType.END}, meta(1, 2, 3))
17
+ pointer = GraphPointer(node)
18
+ assert pointer.node is node
19
+ assert pointer.id == '1,2,3'
20
+ assert pointer.parent is None
21
+
22
+ def it_throws_an_error_if_node_is_undefined():
23
+ with pytest.raises(Exception, match='Node is undefined'):
24
+ GraphPointer(None)
25
+
26
+ def it_initializes_correctly_with_node_and_parent():
27
+ parent_node = GraphNode({'type': RuleType.CHAR, 'value': [97]}, meta(1, 2, 3))
28
+ child_node = GraphNode({'type': RuleType.CHAR, 'value': [98]}, meta(4, 5, 6))
29
+ parent_pointer = GraphPointer(parent_node)
30
+ child_pointer = GraphPointer(child_node, parent_pointer)
31
+ assert child_pointer.parent is parent_pointer
32
+ assert child_pointer.id == '1,2,3-4,5,6'
33
+
34
+ def it_builds_ids_for_long_parent_chains():
35
+ pointer = None
36
+ for i in range(5000):
37
+ pointer = GraphPointer(GraphNode(RuleEnd(), meta(i, 0, 0)), pointer)
38
+ assert pointer.id == '-'.join(f'{i},0,0' for i in range(5000))
39
+
40
+ def describe_resolve():
41
+ def it_throws_an_error_on_unknown_rule_types():
42
+ # Create a GraphNode with an unhandled rule type
43
+ unhandled_rule = {'type': 'UNKNOWN_RULE_TYPE'}
44
+ node = GraphNode(unhandled_rule, meta(1, 2, 3))
45
+ pointer = GraphPointer(node)
46
+
47
+ with pytest.raises(Exception, match='Unknown rule'):
48
+ list(pointer.resolve())
49
+
50
+ def it_yields_a_char_rule():
51
+ rule = {'type': RuleType.CHAR, 'value': [97]}
52
+ node = GraphNode(rule, meta(1, 2, 3))
53
+ pointer = GraphPointer(node)
54
+
55
+ assert list(pointer.resolve()) == [pointer]
56
+
57
+ def it_yields_a_char_excluded_rule():
58
+ rule = {'type': RuleType.CHAR_EXCLUDE, 'value': [97]}
59
+ node = GraphNode(rule, meta(1, 2, 3))
60
+ pointer = GraphPointer(node)
61
+
62
+ assert list(pointer.resolve()) == [pointer]
63
+
64
+ def it_yields_an_end_rule_without_a_parent():
65
+ rule = {'type': RuleType.END}
66
+ node = GraphNode(rule, meta(1, 2, 3))
67
+ pointer = GraphPointer(node)
68
+
69
+ assert list(pointer.resolve()) == [pointer]
70
+
71
+ def it_yields_an_end_rule_with_a_parent():
72
+ rule = {'type': RuleType.END}
73
+ parent_pointer = GraphPointer(GraphNode(rule, meta(2, 1, 1)))
74
+ child_pointer = GraphPointer(GraphNode(rule, meta(1, 1, 1)), parent_pointer)
75
+
76
+ assert list(child_pointer.resolve()) == [parent_pointer]
77
+
78
+ def it_yields_an_end_rule_with_a_grandparent():
79
+ rule = {'type': RuleType.END}
80
+ grand_parent_pointer = GraphPointer(GraphNode(rule, meta(2, 1, 1)))
81
+ parent_pointer = GraphPointer(GraphNode(rule, meta(2, 1, 1)), grand_parent_pointer)
82
+ child_pointer = GraphPointer(GraphNode(rule, meta(1, 1, 1)), parent_pointer)
83
+
84
+ assert list(child_pointer.resolve()) == [grand_parent_pointer]
85
+
86
+ def it_yields_an_end_rules_parent_that_is_not_an_end_with_a_grandparent():
87
+ rule = {'type': RuleType.END}
88
+ grand_parent_pointer = GraphPointer(GraphNode(rule, meta(2, 1, 1)))
89
+ parent_pointer = GraphPointer(
90
+ GraphNode({'type': RuleType.CHAR, 'value': [97]}, meta(2, 1, 1)), grand_parent_pointer
91
+ )
92
+ child_pointer = GraphPointer(GraphNode(rule, meta(1, 1, 1)), parent_pointer)
93
+
94
+ assert list(child_pointer.resolve()) == [parent_pointer]
95
+
96
+ def it_yields_the_referenced_nodes_of_a_rule_ref_in_order():
97
+ first = GraphNode(RuleChar([97]), meta(1, 0, 0))
98
+ second = GraphNode(RuleChar([98]), meta(1, 1, 0))
99
+ rule_ref = RuleRef(1)
100
+ rule_ref.nodes = [first, second]
101
+ pointer = GraphPointer(GraphNode(rule_ref, meta(0, 0, 0), GraphNode(RuleEnd(), meta(0, 0, 1))))
102
+
103
+ resolved = list(pointer.resolve())
104
+ assert [p.node for p in resolved] == [first, second]
105
+ assert all(p.parent is pointer for p in resolved)
106
+
107
+ def it_yields_the_next_node_of_a_resolved_rule_ref():
108
+ next_node = GraphNode(RuleChar([99]), meta(0, 0, 1))
109
+ rule_ref = RuleRef(1)
110
+ rule_ref.nodes = []
111
+ pointer = GraphPointer(GraphNode(rule_ref, meta(0, 0, 0), next_node))
112
+
113
+ [resolved] = pointer.resolve(True)
114
+ assert resolved.node is next_node
115
+ assert resolved.parent is None
116
+
117
+ def it_throws_for_a_resolved_rule_ref_without_a_next_node():
118
+ rule_ref = RuleRef(1)
119
+ rule_ref.nodes = []
120
+ pointer = GraphPointer(GraphNode(rule_ref, meta(0, 0, 0)))
121
+ with pytest.raises(Exception, match='No next node'):
122
+ list(pointer.resolve(True))
123
+
124
+ def it_throws_for_left_recursive_rules():
125
+ # a ::= a "x"
126
+ rule_ref = RuleRef(0)
127
+ node = GraphNode(rule_ref, meta(0, 0, 0), GraphNode(RuleChar([120]), meta(0, 0, 1)))
128
+ rule_ref.nodes = [node]
129
+ with pytest.raises(RecursionError):
130
+ list(GraphPointer(node).resolve())
131
+
132
+ def describe_fetch_next():
133
+ def it_resolves_the_next_node():
134
+ next_node = GraphNode(RuleChar([98]), meta(0, 0, 1))
135
+ pointer = GraphPointer(GraphNode(RuleChar([97]), meta(0, 0, 0), next_node))
136
+ [fetched] = pointer.fetch_next()
137
+ assert fetched.node is next_node
138
+
139
+ def it_returns_nothing_for_an_invalid_pointer():
140
+ next_node = GraphNode(RuleChar([98]), meta(0, 0, 1))
141
+ pointer = GraphPointer(GraphNode(RuleChar([97]), meta(0, 0, 0), next_node))
142
+ pointer.valid = False
143
+ assert list(pointer.fetch_next()) == []
144
+
145
+ def it_returns_nothing_for_an_end_node_without_a_parent():
146
+ pointer = GraphPointer(GraphNode(RuleEnd(), meta(0, 0, 0)))
147
+ assert list(pointer.fetch_next()) == []
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_graph/test_parse_state.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from types import SimpleNamespace
2
+ from unittest import mock
3
+
4
+ from gbnf.grammar_graph.parse_state import ParseState
5
+
6
+
7
+ def make_pointers(*rules):
8
+ return [SimpleNamespace(rule=rule) for rule in rules]
9
+
10
+
11
+ # Mock implementations for Graph and Pointers
12
+ mock_pointers = make_pointers({'type': 'char', 'value': [65]}) # ASCII 'A'
13
+
14
+
15
+ def make_graph():
16
+ return mock.Mock(grammar='sample-grammar')
17
+
18
+
19
+ def describe_parse_state():
20
+ def it_constructs_with_given_graph_and_pointers():
21
+ parse_state = ParseState(make_graph(), mock_pointers)
22
+ assert isinstance(parse_state, ParseState)
23
+
24
+ def it_returns_unique_rules_from_pointers():
25
+ parse_state = ParseState(make_graph(), make_pointers(
26
+ {'type': 'char', 'value': [65]},
27
+ {'type': 'char', 'value': [65]},
28
+ {'type': 'end'},
29
+ ))
30
+ rules = list(parse_state.rules())
31
+ assert len(rules) == 2
32
+ assert rules[0]['value'] == [65]
33
+ assert rules[1] == {'type': 'end'}
34
+
35
+ def it_iterates_over_unique_rules():
36
+ parse_state = ParseState(make_graph(), mock_pointers)
37
+ rules = [*parse_state]
38
+ assert len(rules) == 1
39
+ assert rules[0]['type'] == 'char'
40
+
41
+ def it_adds_new_input_and_returns_new_parse_state_with_updated_pointers():
42
+ new_pointers = make_pointers({'type': 'char', 'value': [66]}) # ASCII 'B'
43
+ graph = make_graph()
44
+ graph.add.return_value = new_pointers
45
+
46
+ parse_state = ParseState(graph, mock_pointers)
47
+ new_state = parse_state.add('B')
48
+ assert isinstance(new_state, ParseState)
49
+ graph.add.assert_called_with('B', mock_pointers)
50
+ assert [*new_state] == [{'type': 'char', 'value': [66]}]
51
+
52
+ def it_calculates_the_size_of_unique_rules_correctly():
53
+ parse_state = ParseState(make_graph(), mock_pointers)
54
+ size = parse_state.size
55
+ assert size == 1
56
+ assert len(parse_state) == 1
57
+
58
+ def it_provides_access_to_the_underlying_graph_grammar():
59
+ parse_state = ParseState(make_graph(), mock_pointers)
60
+ assert parse_state.grammar == 'sample-grammar'
61
+
62
+ def it_is_callable_as_a_function():
63
+ graph = make_graph()
64
+ graph.add.return_value = mock_pointers
65
+ parse_state = ParseState(graph, mock_pointers)
66
+ new_state = parse_state('B')
67
+ assert isinstance(new_state, ParseState)
68
+ graph.add.assert_called_with('B', mock_pointers)
69
+
70
+ def it_is_always_truthy():
71
+ assert ParseState(make_graph(), [])
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_graph/test_print.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from types import SimpleNamespace
2
+ from unittest import mock
3
+
4
+ import pytest
5
+
6
+ from gbnf.grammar_graph.print import print_graph_node, print_graph_pointer
7
+ from gbnf.grammar_graph.rule_ref import RuleRef
8
+ from gbnf.grammar_graph.types import RuleChar, RuleEnd, RuleType
9
+
10
+ COLORS = {
11
+ '\x1b[34m': 'BLUE',
12
+ '\x1b[36m': 'CYAN',
13
+ '\x1b[32m': 'GREEN',
14
+ '\x1b[31m': 'RED',
15
+ '\x1b[90m': 'GRAY',
16
+ '\x1b[33m': 'YELLOW',
17
+ }
18
+
19
+
20
+ def mock_colorize(text, color):
21
+ return f'[{COLORS[color]}]:{text}'
22
+
23
+
24
+ def create_mock_node(id, rule, next=None):
25
+ return SimpleNamespace(
26
+ id=id,
27
+ rule=rule,
28
+ next=next,
29
+ print=lambda **_opts: f'Node({id})',
30
+ )
31
+
32
+
33
+ def create_mock_graph_pointer(node):
34
+ return SimpleNamespace(
35
+ node=node,
36
+ parent=None,
37
+ print=lambda **_opts: f'Pointer to {node.id}',
38
+ )
39
+
40
+
41
+ def describe_print():
42
+ def describe_print_graph_pointer():
43
+ def it_prints_graph_pointer_details_correctly():
44
+ mock_node = create_mock_node(1, RuleRef(100))
45
+ mock_pointer = create_mock_graph_pointer(mock_node)
46
+ with mock.patch('gbnf.grammar_graph.print.get_parent_stack_id', return_value='foo') as get_parent_stack_id:
47
+ result = print_graph_pointer(mock_pointer)(colorize=mock_colorize)
48
+ assert result == '[RED]:*foo'
49
+ get_parent_stack_id.assert_called_with(mock_pointer, mock_colorize)
50
+
51
+ def describe_print_graph_node():
52
+ def it_prints_a_graph_node_with_a_character_rule():
53
+ rule_char = {
54
+ 'type': RuleType.CHAR,
55
+ 'value': [65], # ASCII for 'A'
56
+ }
57
+ node = create_mock_node(1, rule_char)
58
+ result = print_graph_node(node)(colorize=mock_colorize, show_position=False)
59
+ assert result == '[GRAY]:[[YELLOW]:A[GRAY]:]'
60
+
61
+ def it_prints_a_graph_node_with_a_rule_reference():
62
+ rule_ref = RuleRef(200)
63
+ node = create_mock_node(1, rule_ref)
64
+ result = print_graph_node(node)(colorize=mock_colorize, show_position=True)
65
+ assert result == '[BLUE]:{[GRAY]:1[BLUE]:}[GRAY]:Ref([GREEN]:200[GRAY]:)'
66
+
67
+ @pytest.mark.parametrize(('value', 'expected'), [
68
+ ([[97, 122]], '[GRAY]:[[YELLOW]:[YELLOW]:a,[YELLOW]:z[GRAY]:]'),
69
+ ([10], '[GRAY]:[[YELLOW]:\\n[GRAY]:]'),
70
+ ])
71
+ def it_prints_ranges_and_newlines(value, expected):
72
+ node = create_mock_node(1, RuleChar(value))
73
+ assert print_graph_node(node)(colorize=mock_colorize) == expected
74
+
75
+ def it_prints_the_type_of_other_rules():
76
+ node = create_mock_node(1, RuleEnd())
77
+ assert print_graph_node(node)(colorize=mock_colorize) == '[YELLOW]:end'
78
+
79
+ def it_prints_the_next_node_and_pointers():
80
+ node = create_mock_node(1, RuleEnd(), next=create_mock_node(2, RuleEnd()))
81
+ pointers = [create_mock_graph_pointer(node), create_mock_graph_pointer(create_mock_node(3, RuleEnd()))]
82
+ result = print_graph_node(node)(colorize=mock_colorize, pointers=pointers)
83
+ assert result == '[YELLOW]:end[GRAY]:[Pointer to 1[GRAY]:][GRAY]:-> Node(2)'
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_graph/test_rule_ref.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ from gbnf.grammar_graph.rule_ref import RuleRef
4
+
5
+
6
+ def describe_rule_ref():
7
+ def it_initializes_with_a_given_value():
8
+ rule_ref = RuleRef(123)
9
+ assert rule_ref.value == 123
10
+
11
+ def it_allows_setting_and_getting_nodes():
12
+ mock_nodes = [object()]
13
+ rule_ref = RuleRef(123)
14
+ rule_ref.nodes = mock_nodes # Set the nodes
15
+ assert rule_ref.nodes is mock_nodes # Get the nodes
16
+
17
+ def it_throws_an_error_if_trying_to_get_nodes_before_setting():
18
+ rule_ref = RuleRef(123)
19
+ with pytest.raises(Exception, match='Nodes are not set'):
20
+ rule_ref.nodes
21
+
22
+ def it_compares_by_value():
23
+ assert RuleRef(1) == RuleRef(1)
24
+ assert RuleRef(1) != RuleRef(2)
25
+ assert RuleRef(1) != {'value': 1}
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_graph/test_type_guards.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from gbnf.grammar_graph.graph_node import GraphNode, GraphNodeMeta
2
+ from gbnf.grammar_graph.graph_pointer import GraphPointer
3
+ from gbnf.grammar_graph.rule_ref import RuleRef
4
+ from gbnf.grammar_graph.type_guards import (
5
+ is_graph_pointer_rule_char,
6
+ is_graph_pointer_rule_char_exclude,
7
+ is_graph_pointer_rule_end,
8
+ is_graph_pointer_rule_ref,
9
+ is_range,
10
+ is_rule,
11
+ is_rule_char,
12
+ is_rule_char_excluded,
13
+ is_rule_end,
14
+ is_rule_ref,
15
+ is_rule_type,
16
+ )
17
+ from gbnf.grammar_graph.types import RuleChar, RuleCharExclude, RuleEnd, RuleType
18
+
19
+ meta = GraphNodeMeta(stack_id=1, path_id=2, step_id=3)
20
+
21
+
22
+ def describe_rule_type_guards():
23
+ def describe_is_graph_pointer_rule_ref():
24
+ def it_returns_true():
25
+ pointer = GraphPointer(GraphNode(RuleRef(1), meta))
26
+ assert is_graph_pointer_rule_ref(pointer) is True
27
+
28
+ def it_returns_false():
29
+ pointer = GraphPointer(GraphNode({'type': RuleType.END}, meta))
30
+ assert is_graph_pointer_rule_ref(pointer) is False
31
+
32
+ def describe_is_graph_pointer_rule_end():
33
+ def it_returns_false():
34
+ pointer = GraphPointer(GraphNode(RuleRef(1), meta))
35
+ assert is_graph_pointer_rule_end(pointer) is False
36
+
37
+ def it_returns_true():
38
+ pointer = GraphPointer(GraphNode({'type': RuleType.END}, meta))
39
+ assert is_graph_pointer_rule_end(pointer) is True
40
+
41
+ def describe_is_graph_pointer_rule_char():
42
+ def it_returns_true():
43
+ pointer = GraphPointer(GraphNode({'type': RuleType.CHAR, 'value': [97]}, meta))
44
+ assert is_graph_pointer_rule_char(pointer) is True
45
+
46
+ def it_returns_false():
47
+ pointer = GraphPointer(GraphNode({'type': RuleType.END}, meta))
48
+ assert is_graph_pointer_rule_char(pointer) is False
49
+
50
+ def describe_is_graph_pointer_rule_char_exclude():
51
+ def it_returns_true():
52
+ pointer = GraphPointer(GraphNode({'type': RuleType.CHAR_EXCLUDE, 'value': [97]}, meta))
53
+ assert is_graph_pointer_rule_char_exclude(pointer) is True
54
+
55
+ def it_returns_false():
56
+ pointer = GraphPointer(GraphNode({'type': RuleType.CHAR, 'value': [97]}, meta))
57
+ assert is_graph_pointer_rule_char_exclude(pointer) is False
58
+
59
+ def describe_is_rule():
60
+ def it_returns_true_for_valid_rule_objects():
61
+ rule_char = {'type': RuleType.CHAR, 'value': [65, [66, 67]]}
62
+ assert is_rule(rule_char) is True
63
+ assert is_rule(RuleChar([65])) is True
64
+
65
+ def it_returns_false_for_invalid_objects():
66
+ assert is_rule({'type': 'invalid', 'value': []}) is False
67
+ assert is_rule(RuleRef(1)) is False
68
+
69
+ def it_returns_false_for_none():
70
+ assert is_rule(None) is False
71
+ assert is_rule() is False
72
+
73
+ def describe_is_rule_type():
74
+ def it_returns_true_for_valid_rule_types():
75
+ assert is_rule_type(RuleType.CHAR) is True
76
+ assert is_rule_type(RuleType.END) is True
77
+ assert is_rule_type('char_exclude') is True
78
+
79
+ def it_returns_false_for_invalid_rule_types():
80
+ assert is_rule_type('not_a_rule_type') is False
81
+ assert is_rule_type(None) is False
82
+
83
+ def describe_is_rule_ref():
84
+ def it_recognizes_rule_ref_instances():
85
+ assert is_rule_ref(RuleRef(1)) is True
86
+
87
+ def it_does_not_misidentify_other_rule_types():
88
+ rule_char = {'type': RuleType.CHAR, 'value': [65]}
89
+ assert is_rule_ref(rule_char) is False
90
+
91
+ def describe_is_rule_end():
92
+ def it_recognizes_an_end_rule():
93
+ assert is_rule_end({'type': RuleType.END}) is True
94
+ assert is_rule_end(RuleEnd()) is True
95
+
96
+ def it_does_not_confuse_with_other_types():
97
+ rule_char = {'type': RuleType.CHAR, 'value': [65]}
98
+ assert is_rule_end(rule_char) is False
99
+ assert is_rule_end(None) is False
100
+
101
+ def describe_is_rule_char_and_is_rule_char_excluded():
102
+ def it_identifies_character_rules():
103
+ rule_char = {'type': RuleType.CHAR, 'value': [65]}
104
+ assert is_rule_char(rule_char) is True
105
+ assert is_rule_char_excluded(rule_char) is False
106
+
107
+ def it_identifies_character_exclusion_rules():
108
+ rule_char_exclude = RuleCharExclude([65])
109
+ assert is_rule_char_excluded(rule_char_exclude) is True
110
+ assert is_rule_char(rule_char_exclude) is False
111
+
112
+ def describe_is_range():
113
+ def it_identifies_valid_ranges():
114
+ assert is_range([1, 10]) is True
115
+ assert is_range((1, 10)) is True
116
+
117
+ def it_rejects_invalid_ranges():
118
+ assert is_range([1, '10']) is False # Mixed types
119
+ assert is_range([1]) is False # Not enough elements
120
+ assert is_range(5) is False # Not an array
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_parser/__pycache__/test_build_rule_stack.cpython-314-pytest-9.1.1.pyc ADDED
Binary file (28.6 kB). View file
 
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/grammar_parser/test_build_rule_stack.py ADDED
@@ -0,0 +1,331 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Ported from the reference implementation's javascript/src/grammar-parser/build-rule-stack.test.ts
2
+ import pytest
3
+
4
+ from gbnf.grammar_graph.rule_ref import RuleRef
5
+ from gbnf.grammar_graph.types import RuleType
6
+ from gbnf.grammar_parser.build_rule_stack import build_rule_stack
7
+ from gbnf.rules_builder.types import InternalRuleType
8
+
9
+ NO_MODIFIER_CASES = [
10
+ ('[a-z]',
11
+ [{'type': 'CHAR', 'value': [97]}, {'type': 'CHAR_RNG_UPPER', 'value': 122}, {'type': 'END'}],
12
+ [[{'type': 'char', 'value': [[97, 122]]}, {'type': 'end'}]]),
13
+ ('[a-zA-Z]',
14
+ [{'type': 'CHAR', 'value': [97]},
15
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
16
+ {'type': 'CHAR_ALT', 'value': 65},
17
+ {'type': 'CHAR_RNG_UPPER', 'value': 90},
18
+ {'type': 'END'}],
19
+ [[{'type': 'char', 'value': [[97, 122], [65, 90]]}, {'type': 'end'}]]),
20
+ ('[a-zA-Z0-9]',
21
+ [{'type': 'CHAR', 'value': [97]},
22
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
23
+ {'type': 'CHAR_ALT', 'value': 65},
24
+ {'type': 'CHAR_RNG_UPPER', 'value': 90},
25
+ {'type': 'CHAR_ALT', 'value': 48},
26
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
27
+ {'type': 'END'}],
28
+ [[{'type': 'char', 'value': [[97, 122], [65, 90], [48, 57]]}, {'type': 'end'}]]),
29
+ ]
30
+
31
+ OPTIONAL_CASES = [
32
+ ('[a-z]?',
33
+ [{'type': 'CHAR', 'value': [97]},
34
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
35
+ {'type': 'ALT'},
36
+ {'type': 'END'}],
37
+ [[{'type': 'char', 'value': [[97, 122]]}, {'type': 'end'}], [{'type': 'end'}]]),
38
+ ('[a-zA-Z]?',
39
+ [{'type': 'CHAR', 'value': [97]},
40
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
41
+ {'type': 'CHAR_ALT', 'value': 65},
42
+ {'type': 'CHAR_RNG_UPPER', 'value': 90},
43
+ {'type': 'ALT'},
44
+ {'type': 'END'}],
45
+ [[{'type': 'char', 'value': [[97, 122], [65, 90]]}, {'type': 'end'}], [{'type': 'end'}]]),
46
+ ('[a-zA-Z0-9]?',
47
+ [{'type': 'CHAR', 'value': [97]},
48
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
49
+ {'type': 'CHAR_ALT', 'value': 65},
50
+ {'type': 'CHAR_RNG_UPPER', 'value': 90},
51
+ {'type': 'CHAR_ALT', 'value': 48},
52
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
53
+ {'type': 'ALT'},
54
+ {'type': 'END'}],
55
+ [[{'type': 'char', 'value': [[97, 122], [65, 90], [48, 57]]}, {'type': 'end'}], [{'type': 'end'}]]),
56
+ ]
57
+
58
+ ONE_OR_MORE_CASES = [
59
+ ('[a-z]+',
60
+ [{'type': 'CHAR', 'value': [97]},
61
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
62
+ {'type': 'RULE_REF', 'value': 1},
63
+ {'type': 'ALT'},
64
+ {'type': 'CHAR', 'value': [97]},
65
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
66
+ {'type': 'END'}],
67
+ [[{'type': 'char', 'value': [[97, 122]]}, RuleRef(1), {'type': 'end'}],
68
+ [{'type': 'char', 'value': [[97, 122]]}, {'type': 'end'}]]),
69
+ ('[a-zA-Z]+',
70
+ [{'type': 'CHAR', 'value': [97]},
71
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
72
+ {'type': 'CHAR_ALT', 'value': 65},
73
+ {'type': 'CHAR_RNG_UPPER', 'value': 90},
74
+ {'type': 'RULE_REF', 'value': 1},
75
+ {'type': 'ALT'},
76
+ {'type': 'CHAR', 'value': [97]},
77
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
78
+ {'type': 'CHAR_ALT', 'value': 65},
79
+ {'type': 'CHAR_RNG_UPPER', 'value': 90},
80
+ {'type': 'END'}],
81
+ [[{'type': 'char', 'value': [[97, 122], [65, 90]]}, RuleRef(1), {'type': 'end'}],
82
+ [{'type': 'char', 'value': [[97, 122], [65, 90]]}, {'type': 'end'}]]),
83
+ ('[a-zA-Z0-9]+',
84
+ [{'type': 'CHAR', 'value': [97]},
85
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
86
+ {'type': 'CHAR_ALT', 'value': 65},
87
+ {'type': 'CHAR_RNG_UPPER', 'value': 90},
88
+ {'type': 'CHAR_ALT', 'value': 48},
89
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
90
+ {'type': 'RULE_REF', 'value': 1},
91
+ {'type': 'ALT'},
92
+ {'type': 'CHAR', 'value': [97]},
93
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
94
+ {'type': 'CHAR_ALT', 'value': 65},
95
+ {'type': 'CHAR_RNG_UPPER', 'value': 90},
96
+ {'type': 'CHAR_ALT', 'value': 48},
97
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
98
+ {'type': 'END'}],
99
+ [[{'type': 'char', 'value': [[97, 122], [65, 90], [48, 57]]}, RuleRef(1), {'type': 'end'}],
100
+ [{'type': 'char', 'value': [[97, 122], [65, 90], [48, 57]]}, {'type': 'end'}]]),
101
+ ]
102
+
103
+ ZERO_OR_MORE_CASES = [
104
+ ('[a-z]*',
105
+ [{'type': 'CHAR', 'value': [97]},
106
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
107
+ {'type': 'RULE_REF', 'value': 1},
108
+ {'type': 'ALT'},
109
+ {'type': 'END'}],
110
+ [[{'type': 'char', 'value': [[97, 122]]}, RuleRef(1), {'type': 'end'}], [{'type': 'end'}]]),
111
+ ('[a-zA-Z]*',
112
+ [{'type': 'CHAR', 'value': [97]},
113
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
114
+ {'type': 'CHAR_ALT', 'value': 65},
115
+ {'type': 'CHAR_RNG_UPPER', 'value': 90},
116
+ {'type': 'RULE_REF', 'value': 1},
117
+ {'type': 'ALT'},
118
+ {'type': 'END'}],
119
+ [[{'type': 'char', 'value': [[97, 122], [65, 90]]}, RuleRef(1), {'type': 'end'}], [{'type': 'end'}]]),
120
+ ('[a-zA-Z0-9]*',
121
+ [{'type': 'CHAR', 'value': [97]},
122
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
123
+ {'type': 'CHAR_ALT', 'value': 65},
124
+ {'type': 'CHAR_RNG_UPPER', 'value': 90},
125
+ {'type': 'CHAR_ALT', 'value': 48},
126
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
127
+ {'type': 'RULE_REF', 'value': 1},
128
+ {'type': 'ALT'},
129
+ {'type': 'END'}],
130
+ [[{'type': 'char', 'value': [[97, 122], [65, 90], [48, 57]]}, RuleRef(1), {'type': 'end'}],
131
+ [{'type': 'end'}]]),
132
+ ]
133
+
134
+
135
+ def describe_build_rule_stack():
136
+ def it_builds_rule_stack_for_a_single_path():
137
+ assert build_rule_stack([
138
+ {'type': InternalRuleType.CHAR, 'value': [120]},
139
+ ]) == [[
140
+ {'type': RuleType.CHAR, 'value': [120]},
141
+ {'type': RuleType.END},
142
+ ]]
143
+
144
+ def it_builds_rule_stack_for_two_alternate_paths():
145
+ assert build_rule_stack([
146
+ {'type': InternalRuleType.CHAR, 'value': [120]},
147
+ {'type': InternalRuleType.ALT},
148
+ {'type': InternalRuleType.CHAR, 'value': [121]},
149
+ ]) == [[
150
+ {'type': RuleType.CHAR, 'value': [120]},
151
+ {'type': RuleType.END},
152
+ ], [
153
+ {'type': RuleType.CHAR, 'value': [121]},
154
+ {'type': RuleType.END},
155
+ ]]
156
+
157
+ def it_builds_rule_stack_for_three_alternate_paths():
158
+ assert build_rule_stack([
159
+ {'type': InternalRuleType.CHAR, 'value': [120]},
160
+ {'type': InternalRuleType.ALT},
161
+ {'type': InternalRuleType.CHAR, 'value': [121]},
162
+ {'type': InternalRuleType.ALT},
163
+ {'type': InternalRuleType.CHAR, 'value': [122]},
164
+ ]) == [[
165
+ {'type': RuleType.CHAR, 'value': [120]},
166
+ {'type': RuleType.END},
167
+ ], [
168
+ {'type': RuleType.CHAR, 'value': [121]},
169
+ {'type': RuleType.END},
170
+ ], [
171
+ {'type': RuleType.CHAR, 'value': [122]},
172
+ {'type': RuleType.END},
173
+ ]]
174
+
175
+ def it_builds_rule_stack_for_char_not():
176
+ assert build_rule_stack([
177
+ {'type': InternalRuleType.CHAR_NOT, 'value': [120]},
178
+ {'type': InternalRuleType.ALT},
179
+ {'type': InternalRuleType.CHAR_NOT, 'value': [121]},
180
+ {'type': InternalRuleType.ALT},
181
+ {'type': InternalRuleType.CHAR_NOT, 'value': [122]},
182
+ ]) == [[
183
+ {'type': RuleType.CHAR_EXCLUDE, 'value': [120]},
184
+ {'type': RuleType.END},
185
+ ], [
186
+ {'type': RuleType.CHAR_EXCLUDE, 'value': [121]},
187
+ {'type': RuleType.END},
188
+ ], [
189
+ {'type': RuleType.CHAR_EXCLUDE, 'value': [122]},
190
+ {'type': RuleType.END},
191
+ ]]
192
+
193
+ def it_builds_rule_stack_for_mixed_char_and_char_not():
194
+ assert build_rule_stack([
195
+ {'type': InternalRuleType.CHAR_NOT, 'value': [120]},
196
+ {'type': InternalRuleType.ALT},
197
+ {'type': InternalRuleType.CHAR, 'value': [121]},
198
+ {'type': InternalRuleType.ALT},
199
+ {'type': InternalRuleType.CHAR_NOT, 'value': [122]},
200
+ ]) == [[
201
+ {'type': RuleType.CHAR_EXCLUDE, 'value': [120]},
202
+ {'type': RuleType.END},
203
+ ], [
204
+ {'type': RuleType.CHAR, 'value': [121]},
205
+ {'type': RuleType.END},
206
+ ], [
207
+ {'type': RuleType.CHAR_EXCLUDE, 'value': [122]},
208
+ {'type': RuleType.END},
209
+ ]]
210
+
211
+ def it_builds_rule_stack_for_a_char_not_with_two_characters_and_a_range():
212
+ assert build_rule_stack([
213
+ {'type': InternalRuleType.CHAR_NOT, 'value': [120]},
214
+ {'type': InternalRuleType.CHAR_ALT, 'value': 121},
215
+ {'type': InternalRuleType.CHAR_ALT, 'value': 122},
216
+ {'type': InternalRuleType.CHAR_RNG_UPPER, 'value': 130},
217
+ ]) == [[
218
+ {'type': RuleType.CHAR_EXCLUDE, 'value': [120, 121, [122, 130]]},
219
+ {'type': RuleType.END},
220
+ ]]
221
+
222
+ def it_does_not_mutate_its_input():
223
+ char_def = {'type': InternalRuleType.CHAR, 'value': [97]}
224
+ build_rule_stack([char_def, {'type': InternalRuleType.CHAR_RNG_UPPER, 'value': 122}])
225
+ assert char_def == {'type': InternalRuleType.CHAR, 'value': [97]}
226
+
227
+ def it_throws_for_an_alt_without_anything_before_it():
228
+ with pytest.raises(Exception, match='Encountered alt without anything before it'):
229
+ build_rule_stack([{'type': InternalRuleType.ALT}])
230
+
231
+ def it_throws_for_a_char_alt_without_a_preceding_char():
232
+ with pytest.raises(Exception, match='Encountered char alt, should be handled by above block: CHAR_ALT'):
233
+ build_rule_stack([{'type': InternalRuleType.CHAR_ALT, 'value': 97}])
234
+
235
+ def it_throws_for_an_unsupported_rule_type():
236
+ with pytest.raises(Exception, match='Unsupported rule type: UNKNOWN'):
237
+ build_rule_stack([{'type': 'UNKNOWN'}])
238
+
239
+ def describe_ranges():
240
+ def describe_no_modifiers():
241
+ @pytest.mark.parametrize(('grammar', 'input', 'expected'), NO_MODIFIER_CASES)
242
+ def it_builds_rule_stack_for_a_char(grammar, input, expected):
243
+ assert build_rule_stack(input) == expected
244
+
245
+ def describe_optional():
246
+ @pytest.mark.parametrize(('grammar', 'input', 'expected'), OPTIONAL_CASES)
247
+ def it_builds_rule_stack_for_a_char(grammar, input, expected):
248
+ assert build_rule_stack(input) == expected
249
+
250
+ def describe_one_or_more():
251
+ @pytest.mark.parametrize(('grammar', 'input', 'expected'), ONE_OR_MORE_CASES)
252
+ def it_builds_rule_stack_for_a_char(grammar, input, expected):
253
+ assert build_rule_stack(input) == expected
254
+
255
+ def describe_zero_or_more():
256
+ @pytest.mark.parametrize(('grammar', 'input', 'expected'), ZERO_OR_MORE_CASES)
257
+ def it_builds_rule_stack_for_an_optionally_repeating_char(grammar, input, expected):
258
+ assert build_rule_stack(input) == expected
259
+
260
+ def it_builds_rule_stack_for_char_with_range():
261
+ # `[a-zA-Z_]`
262
+ input = [
263
+ {'type': InternalRuleType.CHAR, 'value': [ord('a')]},
264
+ {'type': InternalRuleType.CHAR_RNG_UPPER, 'value': ord('z')},
265
+ {'type': InternalRuleType.CHAR_ALT, 'value': ord('A')},
266
+ {'type': InternalRuleType.CHAR_RNG_UPPER, 'value': ord('Z')},
267
+ {'type': InternalRuleType.CHAR_ALT, 'value': ord('_')},
268
+ {'type': InternalRuleType.END},
269
+ ]
270
+ assert build_rule_stack(input) == [
271
+ [
272
+ {
273
+ 'type': RuleType.CHAR, 'value': [
274
+ [ord('a'), ord('z')],
275
+ [ord('A'), ord('Z')],
276
+ ord('_'),
277
+ ],
278
+ },
279
+ {'type': RuleType.END},
280
+ ],
281
+ ]
282
+
283
+ def it_builds_rule_stack_for_a_repeated_whitespace_rule():
284
+ # `root ::= (ws )+\nws ::= [ \\t\\n]*`
285
+ input = [
286
+ {'type': InternalRuleType.CHAR, 'value': [32]},
287
+ {'type': InternalRuleType.CHAR_ALT, 'value': 92},
288
+ {'type': InternalRuleType.CHAR_ALT, 'value': 110},
289
+ {'type': InternalRuleType.RULE_REF, 'value': 4},
290
+ {'type': InternalRuleType.ALT},
291
+ {'type': InternalRuleType.END},
292
+ ]
293
+ expected = [
294
+ [
295
+ {'type': RuleType.CHAR, 'value': [32, 92, 110]},
296
+ RuleRef(4),
297
+ {'type': RuleType.END},
298
+ ],
299
+ [
300
+ {'type': RuleType.END},
301
+ ],
302
+ ]
303
+ assert build_rule_stack(input) == expected
304
+
305
+ def it_builds_rule_stack_for_a_repeated_range():
306
+ # `root ::= [a-zA-Z]+`
307
+ input = [
308
+ {'type': InternalRuleType.CHAR, 'value': [97]},
309
+ {'type': InternalRuleType.CHAR_RNG_UPPER, 'value': 122},
310
+ {'type': InternalRuleType.CHAR_ALT, 'value': 65},
311
+ {'type': InternalRuleType.CHAR_RNG_UPPER, 'value': 90},
312
+ {'type': InternalRuleType.RULE_REF, 'value': 1},
313
+ {'type': InternalRuleType.ALT},
314
+ {'type': InternalRuleType.CHAR, 'value': [97]},
315
+ {'type': InternalRuleType.CHAR_RNG_UPPER, 'value': 122},
316
+ {'type': InternalRuleType.CHAR_ALT, 'value': 65},
317
+ {'type': InternalRuleType.CHAR_RNG_UPPER, 'value': 90},
318
+ {'type': InternalRuleType.END},
319
+ ]
320
+ expected = [
321
+ [
322
+ {'type': RuleType.CHAR, 'value': [[97, 122], [65, 90]]},
323
+ RuleRef(1),
324
+ {'type': RuleType.END},
325
+ ],
326
+ [
327
+ {'type': RuleType.CHAR, 'value': [[97, 122], [65, 90]]},
328
+ {'type': RuleType.END},
329
+ ],
330
+ ]
331
+ assert build_rule_stack(input) == expected
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/rules_builder/__pycache__/test_is_word_char.cpython-314-pytest-9.1.1.pyc ADDED
Binary file (8.63 kB). View file
 
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/rules_builder/__pycache__/test_parse_char.cpython-314-pytest-9.1.1.pyc ADDED
Binary file (10.6 kB). View file
 
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/rules_builder/__pycache__/test_parse_name.cpython-314-pytest-9.1.1.pyc ADDED
Binary file (5.27 kB). View file
 
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/rules_builder/__pycache__/test_parse_space.cpython-314-pytest-9.1.1.pyc ADDED
Binary file (9.19 kB). View file
 
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/rules_builder/__pycache__/test_rules_builder.cpython-314-pytest-9.1.1.pyc ADDED
Binary file (27.4 kB). View file
 
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/rules_builder/test_is_word_char.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from gbnf.rules_builder.is_word_char import is_word_char
2
+
3
+
4
+ def describe_is_word_char():
5
+ def it_returns_true_for_lowercase_letters():
6
+ assert is_word_char('a') is True
7
+ assert is_word_char('z') is True
8
+
9
+ def it_returns_true_for_uppercase_letters():
10
+ assert is_word_char('A') is True
11
+ assert is_word_char('Z') is True
12
+
13
+ def it_returns_false_for_digits():
14
+ assert is_word_char('0') is False
15
+ assert is_word_char('9') is False
16
+
17
+ def it_returns_false_for_non_word_characters():
18
+ assert is_word_char('-') is False
19
+ assert is_word_char('@') is False
20
+ assert is_word_char('_') is False
21
+ assert is_word_char('?') is False
22
+ assert is_word_char(' ') is False
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/rules_builder/test_parse_char.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+
3
+ import pytest
4
+
5
+ from gbnf.rules_builder.parse_char import parse_char
6
+ from gbnf.utils.errors.grammar_parse_error import GrammarParseError
7
+
8
+
9
+ def describe_parse_char():
10
+ @pytest.mark.parametrize(('key', 'char', 'code_point'), [
11
+ ('unescaped letter', 'a', ord('a')),
12
+ ('unescaped digit', '9', ord('9')),
13
+ ])
14
+ def it_parses_char(key, char, code_point):
15
+ grammar = f'root ::= "{char}" "foo"'
16
+ assert parse_char(grammar, len('root ::= "')) == (code_point, 1)
17
+
18
+ @pytest.mark.parametrize(('key', 'escaped_char', 'code_point', 'inc_pos'), [
19
+ ('escaped 8-bit unicode char', '\\x2A', ord('\x2A'), 4),
20
+ ('escaped 16-bit unicode char', '\\u006F', ord('o'), 6),
21
+ ('escaped 32-bit unicode char', '\\U0001F4A9', 128169, 10),
22
+ ('escaped tab char', '\\t', ord('\t'), 2),
23
+ ('escaped new line char', '\\n', ord('\n'), 2),
24
+ ('escaped \r char', '\\r', ord('\r'), 2),
25
+ ('escaped quote char', '\\"', ord('"'), 2),
26
+ ('escaped [ char', '\\[', ord('['), 2),
27
+ ('escaped ] char', '\\]', ord(']'), 2),
28
+ ('escaped \\ char', '\\\\', ord('\\'), 2),
29
+ ])
30
+ def it_parses_escaped_char(key, escaped_char, code_point, inc_pos):
31
+ grammar = f'root ::= "{escaped_char}" "foo"'
32
+ assert parse_char(grammar, len('root ::= "')) == (code_point, inc_pos)
33
+
34
+ def it_parses_hex_escapes_leniently():
35
+ # like `parseInt(..., 16)`, only the leading hex digits are used
36
+ assert parse_char('\\x4"', 0) == (4, 4)
37
+ value, inc_pos = parse_char('\\xzz', 0)
38
+ assert math.isnan(value)
39
+ assert inc_pos == 4
40
+
41
+ def it_parses_non_ascii_characters_as_code_points():
42
+ assert parse_char('é', 0) == (233, 1)
43
+ assert parse_char('😀', 0) == (0x1F600, 1)
44
+
45
+ def it_throws_on_invalid_input():
46
+ with pytest.raises(GrammarParseError):
47
+ parse_char('', 0)
48
+ with pytest.raises(GrammarParseError):
49
+ parse_char('a', 1)
50
+
51
+ def it_throws_on_an_unknown_escape():
52
+ with pytest.raises(GrammarParseError, match='Unknown escape at '):
53
+ parse_char('\\q', 0)
54
+ with pytest.raises(GrammarParseError, match='Unknown escape at '):
55
+ parse_char('\\', 0)
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/rules_builder/test_parse_name.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ from gbnf.rules_builder.parse_name import GET_INVALID_CHAR_ERROR, PARSE_NAME_ERROR, parse_name
4
+ from gbnf.utils.errors.grammar_parse_error import GrammarParseError
5
+
6
+
7
+ def describe_parse_name():
8
+ @pytest.mark.parametrize('src', [
9
+ 'v',
10
+ 'valid',
11
+ 'valid-name',
12
+ 'valid-name-foo',
13
+ 'validName',
14
+ 'validName-foo',
15
+ ])
16
+ def it_returns_a_valid_name(src):
17
+ name = parse_name(src, 0)
18
+ assert name == src
19
+
20
+ @pytest.mark.parametrize(('src', 'position', 'expectation'), [
21
+ ('123valid', 3, 'valid'),
22
+ ('123valid-name', 3, 'valid-name'),
23
+ ('123valid _Name', 3, 'valid'),
24
+ ('123valid\\n_Name', 3, 'valid'),
25
+ ('123valid\\t_Name', 3, 'valid'),
26
+ ('123valid\\r_Name', 3, 'valid'),
27
+ ])
28
+ def it_returns_a_valid_name_starting_at_a_non_zero_position(src, position, expectation):
29
+ name = parse_name('\n'.join(src.split('\\n')), position)
30
+ assert name == expectation
31
+
32
+ @pytest.mark.parametrize(('src', 'error'), [
33
+ ('123', GrammarParseError('123', 0, PARSE_NAME_ERROR)),
34
+ ('valid_name', GrammarParseError('valid_name', 5, GET_INVALID_CHAR_ERROR('_'))),
35
+ ('valid123', GrammarParseError('valid123', 5, GET_INVALID_CHAR_ERROR('1'))),
36
+ ])
37
+ def it_throws_an_error_when_encountering_an_invalid_name(src, error):
38
+ with pytest.raises(GrammarParseError) as exc_info:
39
+ parse_name(src, 0)
40
+ assert exc_info.value.message == error.message
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/rules_builder/test_parse_space.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from gbnf.rules_builder.parse_space import parse_space
2
+
3
+
4
+ def describe_parse_space():
5
+ def it_returns_the_input_string_when_no_whitespace_or_comments_are_present():
6
+ input = 'abcdefghijk'
7
+ pos = parse_space(input, 0, True)
8
+ assert input[pos:] == input
9
+
10
+ def it_skips_leading_spaces_and_tabs():
11
+ input = ' \t abcdefghijk'
12
+ expected = 'abcdefghijk'
13
+ pos = parse_space(input, 0, True)
14
+ assert input[pos:] == expected
15
+
16
+ def it_skips_leading_newline_characters_when_newline_ok_is_true():
17
+ input = '\n\n\r\n\r\nabcdefghijk'
18
+ expected = 'abcdefghijk'
19
+ pos = parse_space(input, 0, True)
20
+ assert input[pos:] == expected
21
+
22
+ def it_does_not_skip_leading_newline_characters_when_newline_ok_is_false():
23
+ input = '\n\n\r\n\r\nabcdefghijk'
24
+ expected = '\n\n\r\n\r\nabcdefghijk'
25
+ pos = parse_space(input, 0, False)
26
+ assert input[pos:] == expected
27
+
28
+ def it_skips_comments_and_leading_spaces():
29
+ input = ' # This is a comment\n\t abcdefghijk'
30
+ expected = 'abcdefghijk'
31
+ pos = parse_space(input, 0, True)
32
+ assert input[pos:] == expected
33
+
34
+ def it_skips_comments_and_leading_newline_characters_when_newline_ok_is_true():
35
+ input = '\n\n # This is a comment\n\r\n\r\nabcdefghijk'
36
+ expected = 'abcdefghijk'
37
+ pos = parse_space(input, 0, True)
38
+ assert input[pos:] == expected
39
+
40
+ def it_does_not_skip_comments_and_leading_newline_characters_when_newline_ok_is_false():
41
+ input = '\n\n # This is a comment\n\r\n\r\nabcdefghijk'
42
+ expected = '\n\n # This is a comment\n\r\n\r\nabcdefghijk'
43
+ pos = parse_space(input, 0, False)
44
+ assert input[pos:] == expected
45
+
46
+ def it_skips_a_comment_without_skipping_the_newline_when_newline_ok_is_false():
47
+ input = ' # comment\nabc'
48
+ pos = parse_space(input, 0, False)
49
+ assert input[pos:] == '\nabc'
50
+
51
+ def it_returns_an_empty_string_if_the_input_is_all_whitespace_and_comments():
52
+ input = ' \t# Comment\n# Another comment\n\n'
53
+ expected = ''
54
+ pos = parse_space(input, 0, True)
55
+ assert input[pos:] == expected
56
+
57
+ def it_returns_an_empty_string_for_an_empty_input_string():
58
+ input = ''
59
+ expected = ''
60
+ pos = parse_space(input, 0, True)
61
+ assert input[pos:] == expected
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/rules_builder/test_rules_builder.py ADDED
@@ -0,0 +1,869 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Ported from the reference implementation's javascript/src/rules-builder/rules-builder.test.ts
2
+ import pytest
3
+
4
+ from gbnf.rules_builder.rules_builder import RulesBuilder
5
+
6
+ # As in the reference test, each grammar has had its lines trimmed and joined with a literal backslash-n,
7
+ # which is split back into newlines before parsing.
8
+ CASES = [
9
+ ('single-string',
10
+ 'root ::= "foo"',
11
+ {'symbol_ids': [('root', 0)],
12
+ 'rules': [[{'type': 'CHAR', 'value': [102]},
13
+ {'type': 'CHAR', 'value': [111]},
14
+ {'type': 'CHAR', 'value': [111]},
15
+ {'type': 'END'}]]}),
16
+ ('quote character',
17
+ 'root::= "\\""',
18
+ {'symbol_ids': [('root', 0)], 'rules': [[{'type': 'CHAR', 'value': [34]}, {'type': 'END'}]]}),
19
+ ('two-lines-referencing-expression',
20
+ 'root ::= foo\\nfoo ::= "bar"\\n',
21
+ {'symbol_ids': [('root', 0), ('foo', 1)],
22
+ 'rules': [[{'type': 'RULE_REF', 'value': 1}, {'type': 'END'}],
23
+ [{'type': 'CHAR', 'value': [98]},
24
+ {'type': 'CHAR', 'value': [97]},
25
+ {'type': 'CHAR', 'value': [114]},
26
+ {'type': 'END'}]]}),
27
+ ('expression with dash',
28
+ 'root ::= foo-bar\\nfoo-bar ::= "bar"\\n',
29
+ {'symbol_ids': [('root', 0), ('foo-bar', 1)],
30
+ 'rules': [[{'type': 'RULE_REF', 'value': 1}, {'type': 'END'}],
31
+ [{'type': 'CHAR', 'value': [98]},
32
+ {'type': 'CHAR', 'value': [97]},
33
+ {'type': 'CHAR', 'value': [114]},
34
+ {'type': 'END'}]]}),
35
+ ('simple-grammar',
36
+ '\\nroot ::= (expr "=" term "\\n")+\\nexpr ::= term ([-+*/] term)*\\nterm ::= [0-9]+\\n\\n',
37
+ {'symbol_ids': [('root', 0),
38
+ ('root_1', 1),
39
+ ('expr', 2),
40
+ ('term', 3),
41
+ ('root_4', 4),
42
+ ('expr_5', 5),
43
+ ('expr_6', 6),
44
+ ('term_7', 7)],
45
+ 'rules': [[{'type': 'RULE_REF', 'value': 4}, {'type': 'END'}],
46
+ [{'type': 'RULE_REF', 'value': 2},
47
+ {'type': 'CHAR', 'value': [61]},
48
+ {'type': 'RULE_REF', 'value': 3},
49
+ {'type': 'CHAR', 'value': [10]},
50
+ {'type': 'END'}],
51
+ [{'type': 'RULE_REF', 'value': 3}, {'type': 'RULE_REF', 'value': 6}, {'type': 'END'}],
52
+ [{'type': 'RULE_REF', 'value': 7}, {'type': 'END'}],
53
+ [{'type': 'RULE_REF', 'value': 1},
54
+ {'type': 'RULE_REF', 'value': 4},
55
+ {'type': 'ALT'},
56
+ {'type': 'RULE_REF', 'value': 1},
57
+ {'type': 'END'}],
58
+ [{'type': 'CHAR', 'value': [45]},
59
+ {'type': 'CHAR_ALT', 'value': 43},
60
+ {'type': 'CHAR_ALT', 'value': 42},
61
+ {'type': 'CHAR_ALT', 'value': 47},
62
+ {'type': 'RULE_REF', 'value': 3},
63
+ {'type': 'END'}],
64
+ [{'type': 'RULE_REF', 'value': 5},
65
+ {'type': 'RULE_REF', 'value': 6},
66
+ {'type': 'ALT'},
67
+ {'type': 'END'}],
68
+ [{'type': 'CHAR', 'value': [48]},
69
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
70
+ {'type': 'RULE_REF', 'value': 7},
71
+ {'type': 'ALT'},
72
+ {'type': 'CHAR', 'value': [48]},
73
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
74
+ {'type': 'END'}]]}),
75
+ ('longer-grammar',
76
+ 'root ::= (expr "=" ws term "\\n")+\\nexpr ::= term ([-+*/] term)*\\nterm ::= ident | num | "(" ws '
77
+ 'expr ")" ws\\nident ::= [a-z] [a-z0-9_]* ws\\nnum ::= [0-9]+ ws\\nws ::= [ \\t\\n]*',
78
+ {'symbol_ids': [('root', 0),
79
+ ('root_1', 1),
80
+ ('expr', 2),
81
+ ('ws', 3),
82
+ ('term', 4),
83
+ ('root_5', 5),
84
+ ('expr_6', 6),
85
+ ('expr_7', 7),
86
+ ('ident', 8),
87
+ ('num', 9),
88
+ ('ident_10', 10),
89
+ ('num_11', 11),
90
+ ('ws_12', 12)],
91
+ 'rules': [[{'value': 5, 'type': 'RULE_REF'}, {'type': 'END'}],
92
+ [{'value': 2, 'type': 'RULE_REF'},
93
+ {'value': [61], 'type': 'CHAR'},
94
+ {'value': 3, 'type': 'RULE_REF'},
95
+ {'value': 4, 'type': 'RULE_REF'},
96
+ {'value': [10], 'type': 'CHAR'},
97
+ {'type': 'END'}],
98
+ [{'value': 4, 'type': 'RULE_REF'}, {'value': 7, 'type': 'RULE_REF'}, {'type': 'END'}],
99
+ [{'value': 12, 'type': 'RULE_REF'}, {'type': 'END'}],
100
+ [{'value': 8, 'type': 'RULE_REF'},
101
+ {'type': 'ALT'},
102
+ {'value': 9, 'type': 'RULE_REF'},
103
+ {'type': 'ALT'},
104
+ {'value': [40], 'type': 'CHAR'},
105
+ {'value': 3, 'type': 'RULE_REF'},
106
+ {'value': 2, 'type': 'RULE_REF'},
107
+ {'value': [41], 'type': 'CHAR'},
108
+ {'value': 3, 'type': 'RULE_REF'},
109
+ {'type': 'END'}],
110
+ [{'value': 1, 'type': 'RULE_REF'},
111
+ {'value': 5, 'type': 'RULE_REF'},
112
+ {'type': 'ALT'},
113
+ {'value': 1, 'type': 'RULE_REF'},
114
+ {'type': 'END'}],
115
+ [{'value': [45], 'type': 'CHAR'},
116
+ {'value': 43, 'type': 'CHAR_ALT'},
117
+ {'value': 42, 'type': 'CHAR_ALT'},
118
+ {'value': 47, 'type': 'CHAR_ALT'},
119
+ {'value': 4, 'type': 'RULE_REF'},
120
+ {'type': 'END'}],
121
+ [{'value': 6, 'type': 'RULE_REF'},
122
+ {'value': 7, 'type': 'RULE_REF'},
123
+ {'type': 'ALT'},
124
+ {'type': 'END'}],
125
+ [{'value': [97], 'type': 'CHAR'},
126
+ {'value': 122, 'type': 'CHAR_RNG_UPPER'},
127
+ {'value': 10, 'type': 'RULE_REF'},
128
+ {'value': 3, 'type': 'RULE_REF'},
129
+ {'type': 'END'}],
130
+ [{'value': 11, 'type': 'RULE_REF'}, {'value': 3, 'type': 'RULE_REF'}, {'type': 'END'}],
131
+ [{'value': [97], 'type': 'CHAR'},
132
+ {'value': 122, 'type': 'CHAR_RNG_UPPER'},
133
+ {'value': 48, 'type': 'CHAR_ALT'},
134
+ {'value': 57, 'type': 'CHAR_RNG_UPPER'},
135
+ {'value': 95, 'type': 'CHAR_ALT'},
136
+ {'value': 10, 'type': 'RULE_REF'},
137
+ {'type': 'ALT'},
138
+ {'type': 'END'}],
139
+ [{'value': [48], 'type': 'CHAR'},
140
+ {'value': 57, 'type': 'CHAR_RNG_UPPER'},
141
+ {'value': 11, 'type': 'RULE_REF'},
142
+ {'type': 'ALT'},
143
+ {'value': [48], 'type': 'CHAR'},
144
+ {'value': 57, 'type': 'CHAR_RNG_UPPER'},
145
+ {'type': 'END'}],
146
+ [{'value': [32], 'type': 'CHAR'},
147
+ {'value': 9, 'type': 'CHAR_ALT'},
148
+ {'value': 10, 'type': 'CHAR_ALT'},
149
+ {'value': 12, 'type': 'RULE_REF'},
150
+ {'type': 'ALT'},
151
+ {'type': 'END'}]]}),
152
+ ('character unicode grammar',
153
+ 'root ::= [ぁ-ゟ]',
154
+ {'symbol_ids': [('root', 0)],
155
+ 'rules': [[{'type': 'CHAR', 'value': [12353]},
156
+ {'type': 'CHAR_RNG_UPPER', 'value': 12447},
157
+ {'type': 'END'}]]}),
158
+ ('character alts grammar',
159
+ 'root ::= [az]',
160
+ {'symbol_ids': [('root', 0)],
161
+ 'rules': [[{'type': 'CHAR', 'value': [97]}, {'type': 'CHAR_ALT', 'value': 122}, {'type': 'END'}]]}),
162
+ ('character range grammar',
163
+ 'root ::= [a-zA-Z0-9]',
164
+ {'symbol_ids': [('root', 0)],
165
+ 'rules': [[{'type': 'CHAR', 'value': [97]},
166
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
167
+ {'type': 'CHAR_ALT', 'value': 65},
168
+ {'type': 'CHAR_RNG_UPPER', 'value': 90},
169
+ {'type': 'CHAR_ALT', 'value': 48},
170
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
171
+ {'type': 'END'}]]}),
172
+ ('character range grammar with dash at end',
173
+ 'root ::= [a-z-]',
174
+ {'symbol_ids': [('root', 0)],
175
+ 'rules': [[{'type': 'CHAR', 'value': [97]},
176
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
177
+ {'type': 'CHAR_ALT', 'value': 45},
178
+ {'type': 'END'}]]}),
179
+ ('grouping',
180
+ 'root ::= "f" ("b" | "a")',
181
+ {'symbol_ids': [('root', 0), ('root_1', 1)],
182
+ 'rules': [[{'type': 'CHAR', 'value': [102]}, {'type': 'RULE_REF', 'value': 1}, {'type': 'END'}],
183
+ [{'type': 'CHAR', 'value': [98]},
184
+ {'type': 'ALT'},
185
+ {'type': 'CHAR', 'value': [97]},
186
+ {'type': 'END'}]]}),
187
+ ('optional',
188
+ 'root ::= "f"?',
189
+ {'symbol_ids': [('root', 0), ('root_1', 1)],
190
+ 'rules': [[{'type': 'RULE_REF', 'value': 1}, {'type': 'END'}],
191
+ [{'type': 'CHAR', 'value': [102]}, {'type': 'ALT'}, {'type': 'END'}]]}),
192
+ ('repeating',
193
+ 'root ::= "f"*',
194
+ {'symbol_ids': [('root', 0), ('root_1', 1)],
195
+ 'rules': [[{'type': 'RULE_REF', 'value': 1}, {'type': 'END'}],
196
+ [{'type': 'CHAR', 'value': [102]},
197
+ {'type': 'RULE_REF', 'value': 1},
198
+ {'type': 'ALT'},
199
+ {'type': 'END'}]]}),
200
+ ('repeating-at-least-once',
201
+ 'root ::= "f"+',
202
+ {'symbol_ids': [('root', 0), ('root_1', 1)],
203
+ 'rules': [[{'type': 'RULE_REF', 'value': 1}, {'type': 'END'}],
204
+ [{'type': 'CHAR', 'value': [102]},
205
+ {'type': 'RULE_REF', 'value': 1},
206
+ {'type': 'ALT'},
207
+ {'type': 'CHAR', 'value': [102]},
208
+ {'type': 'END'}]]}),
209
+ ('character range with optional repeating',
210
+ 'root ::= [a-zA-Z0-9]*',
211
+ {'symbol_ids': [('root', 0), ('root_1', 1)],
212
+ 'rules': [[{'type': 'RULE_REF', 'value': 1}, {'type': 'END'}],
213
+ [{'type': 'CHAR', 'value': [97]},
214
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
215
+ {'type': 'CHAR_ALT', 'value': 65},
216
+ {'type': 'CHAR_RNG_UPPER', 'value': 90},
217
+ {'type': 'CHAR_ALT', 'value': 48},
218
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
219
+ {'type': 'RULE_REF', 'value': 1},
220
+ {'type': 'ALT'},
221
+ {'type': 'END'}]]}),
222
+ ('grouping-repeating',
223
+ 'root ::= "f" ("b" | "a")*',
224
+ {'symbol_ids': [('root', 0), ('root_1', 1), ('root_2', 2)],
225
+ 'rules': [[{'type': 'CHAR', 'value': [102]}, {'type': 'RULE_REF', 'value': 2}, {'type': 'END'}],
226
+ [{'type': 'CHAR', 'value': [98]},
227
+ {'type': 'ALT'},
228
+ {'type': 'CHAR', 'value': [97]},
229
+ {'type': 'END'}],
230
+ [{'type': 'RULE_REF', 'value': 1},
231
+ {'type': 'RULE_REF', 'value': 2},
232
+ {'type': 'ALT'},
233
+ {'type': 'END'}]]}),
234
+ ('negation',
235
+ 'root ::= [^\\n]',
236
+ {'symbol_ids': [('root', 0)], 'rules': [[{'type': 'CHAR_NOT', 'value': [10]}, {'type': 'END'}]]}),
237
+ ('negation of range',
238
+ 'root ::= [^0-9]',
239
+ {'symbol_ids': [('root', 0)],
240
+ 'rules': [[{'type': 'CHAR_NOT', 'value': [48]},
241
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
242
+ {'type': 'END'}]]}),
243
+ ('negation with after',
244
+ 'root ::= [^\\n]+ "\\n"',
245
+ {'symbol_ids': [('root', 0), ('root_1', 1)],
246
+ 'rules': [[{'type': 'RULE_REF', 'value': 1}, {'type': 'CHAR', 'value': [10]}, {'type': 'END'}],
247
+ [{'type': 'CHAR_NOT', 'value': [10]},
248
+ {'type': 'RULE_REF', 'value': 1},
249
+ {'type': 'ALT'},
250
+ {'type': 'CHAR_NOT', 'value': [10]},
251
+ {'type': 'END'}]]}),
252
+ ('longer negation',
253
+ 'root ::= "\\"" ( [^"abcdefgh])*',
254
+ {'symbol_ids': [('root', 0), ('root_1', 1), ('root_2', 2)],
255
+ 'rules': [[{'type': 'CHAR', 'value': [34]}, {'type': 'RULE_REF', 'value': 2}, {'type': 'END'}],
256
+ [{'type': 'CHAR_NOT', 'value': [34]},
257
+ {'type': 'CHAR_ALT', 'value': 97},
258
+ {'type': 'CHAR_ALT', 'value': 98},
259
+ {'type': 'CHAR_ALT', 'value': 99},
260
+ {'type': 'CHAR_ALT', 'value': 100},
261
+ {'type': 'CHAR_ALT', 'value': 101},
262
+ {'type': 'CHAR_ALT', 'value': 102},
263
+ {'type': 'CHAR_ALT', 'value': 103},
264
+ {'type': 'CHAR_ALT', 'value': 104},
265
+ {'type': 'END'}],
266
+ [{'type': 'RULE_REF', 'value': 1},
267
+ {'type': 'RULE_REF', 'value': 2},
268
+ {'type': 'ALT'},
269
+ {'type': 'END'}]]}),
270
+ ('longer negation with a range',
271
+ 'root ::= "\\"" ( [^"abcdefghA-Z])*',
272
+ {'symbol_ids': [('root', 0), ('root_1', 1), ('root_2', 2)],
273
+ 'rules': [[{'type': 'CHAR', 'value': [34]}, {'type': 'RULE_REF', 'value': 2}, {'type': 'END'}],
274
+ [{'type': 'CHAR_NOT', 'value': [34]},
275
+ {'type': 'CHAR_ALT', 'value': 97},
276
+ {'type': 'CHAR_ALT', 'value': 98},
277
+ {'type': 'CHAR_ALT', 'value': 99},
278
+ {'type': 'CHAR_ALT', 'value': 100},
279
+ {'type': 'CHAR_ALT', 'value': 101},
280
+ {'type': 'CHAR_ALT', 'value': 102},
281
+ {'type': 'CHAR_ALT', 'value': 103},
282
+ {'type': 'CHAR_ALT', 'value': 104},
283
+ {'type': 'CHAR_ALT', 'value': 65},
284
+ {'type': 'CHAR_RNG_UPPER', 'value': 90},
285
+ {'type': 'END'}],
286
+ [{'type': 'RULE_REF', 'value': 1},
287
+ {'type': 'RULE_REF', 'value': 2},
288
+ {'type': 'ALT'},
289
+ {'type': 'END'}]]}),
290
+ ('escaped 8-bit unicode char',
291
+ 'root ::= "\\x2A"',
292
+ {'symbol_ids': [('root', 0)], 'rules': [[{'type': 'CHAR', 'value': [42]}, {'type': 'END'}]]}),
293
+ ('escaped 16-bit unicode char',
294
+ 'root ::= "\\u006F"',
295
+ {'symbol_ids': [('root', 0)], 'rules': [[{'type': 'CHAR', 'value': [111]}, {'type': 'END'}]]}),
296
+ ('escaped 32-bit unicode char',
297
+ 'root ::= "\\U0001F4A9"',
298
+ {'symbol_ids': [('root', 0)], 'rules': [[{'type': 'CHAR', 'value': [128169]}, {'type': 'END'}]]}),
299
+ ('escaped tab char',
300
+ 'root ::= "\\t"',
301
+ {'symbol_ids': [('root', 0)], 'rules': [[{'type': 'CHAR', 'value': [9]}, {'type': 'END'}]]}),
302
+ ('escaped new line char',
303
+ 'root ::= "\\n"',
304
+ {'symbol_ids': [('root', 0)], 'rules': [[{'type': 'CHAR', 'value': [10]}, {'type': 'END'}]]}),
305
+ ('escaped \r char',
306
+ 'root ::= "\\r"',
307
+ {'symbol_ids': [('root', 0)], 'rules': [[{'type': 'CHAR', 'value': [13]}, {'type': 'END'}]]}),
308
+ ('escaped quote char',
309
+ 'root ::= "\\""',
310
+ {'symbol_ids': [('root', 0)], 'rules': [[{'type': 'CHAR', 'value': [34]}, {'type': 'END'}]]}),
311
+ ('escaped [ char',
312
+ 'root ::= "\\["',
313
+ {'symbol_ids': [('root', 0)], 'rules': [[{'type': 'CHAR', 'value': [91]}, {'type': 'END'}]]}),
314
+ ('escaped ] char',
315
+ 'root ::= "\\]"',
316
+ {'symbol_ids': [('root', 0)], 'rules': [[{'type': 'CHAR', 'value': [93]}, {'type': 'END'}]]}),
317
+ ('escaped \\ char',
318
+ 'root ::= "\\\\"',
319
+ {'symbol_ids': [('root', 0)], 'rules': [[{'type': 'CHAR', 'value': [92]}, {'type': 'END'}]]}),
320
+ ('simple arithmetic',
321
+ '\\nroot ::= (expr "=" term "\\n")+\\nexpr ::= term ([-+*/] term)*\\nterm ::= [0-9]+\\n',
322
+ {'symbol_ids': [('root', 0),
323
+ ('root_1', 1),
324
+ ('expr', 2),
325
+ ('term', 3),
326
+ ('root_4', 4),
327
+ ('expr_5', 5),
328
+ ('expr_6', 6),
329
+ ('term_7', 7)],
330
+ 'rules': [[{'type': 'RULE_REF', 'value': 4}, {'type': 'END'}],
331
+ [{'type': 'RULE_REF', 'value': 2},
332
+ {'type': 'CHAR', 'value': [61]},
333
+ {'type': 'RULE_REF', 'value': 3},
334
+ {'type': 'CHAR', 'value': [10]},
335
+ {'type': 'END'}],
336
+ [{'type': 'RULE_REF', 'value': 3}, {'type': 'RULE_REF', 'value': 6}, {'type': 'END'}],
337
+ [{'type': 'RULE_REF', 'value': 7}, {'type': 'END'}],
338
+ [{'type': 'RULE_REF', 'value': 1},
339
+ {'type': 'RULE_REF', 'value': 4},
340
+ {'type': 'ALT'},
341
+ {'type': 'RULE_REF', 'value': 1},
342
+ {'type': 'END'}],
343
+ [{'type': 'CHAR', 'value': [45]},
344
+ {'type': 'CHAR_ALT', 'value': 43},
345
+ {'type': 'CHAR_ALT', 'value': 42},
346
+ {'type': 'CHAR_ALT', 'value': 47},
347
+ {'type': 'RULE_REF', 'value': 3},
348
+ {'type': 'END'}],
349
+ [{'type': 'RULE_REF', 'value': 5},
350
+ {'type': 'RULE_REF', 'value': 6},
351
+ {'type': 'ALT'},
352
+ {'type': 'END'}],
353
+ [{'type': 'CHAR', 'value': [48]},
354
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
355
+ {'type': 'RULE_REF', 'value': 7},
356
+ {'type': 'ALT'},
357
+ {'type': 'CHAR', 'value': [48]},
358
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
359
+ {'type': 'END'}]]}),
360
+ ('ranges with chars',
361
+ '\\nroot ::= [a-z0-9_]*\\n',
362
+ {'symbol_ids': [('root', 0), ('root_1', 1)],
363
+ 'rules': [[{'type': 'RULE_REF', 'value': 1}, {'type': 'END'}],
364
+ [{'type': 'CHAR', 'value': [97]},
365
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
366
+ {'type': 'CHAR_ALT', 'value': 48},
367
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
368
+ {'type': 'CHAR_ALT', 'value': 95},
369
+ {'type': 'RULE_REF', 'value': 1},
370
+ {'type': 'ALT'},
371
+ {'type': 'END'}]]}),
372
+ ('nested ranges with chars',
373
+ '\\nroot ::= [a-z] [a-z0-9_]*\\n',
374
+ {'symbol_ids': [('root', 0), ('root_1', 1)],
375
+ 'rules': [[{'type': 'CHAR', 'value': [97]},
376
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
377
+ {'type': 'RULE_REF', 'value': 1},
378
+ {'type': 'END'}],
379
+ [{'type': 'CHAR', 'value': [97]},
380
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
381
+ {'type': 'CHAR_ALT', 'value': 48},
382
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
383
+ {'type': 'CHAR_ALT', 'value': 95},
384
+ {'type': 'RULE_REF', 'value': 1},
385
+ {'type': 'ALT'},
386
+ {'type': 'END'}]]}),
387
+ ('expression with nested range with chars',
388
+ '\\nroot ::= ident\\nident ::= [a-z] [a-z0-9_]* ws\\nws ::= [ \\t\\n]*\\n',
389
+ {'symbol_ids': [('root', 0), ('ident', 1), ('ident_2', 2), ('ws', 3), ('ws_4', 4)],
390
+ 'rules': [[{'type': 'RULE_REF', 'value': 1}, {'type': 'END'}],
391
+ [{'type': 'CHAR', 'value': [97]},
392
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
393
+ {'type': 'RULE_REF', 'value': 2},
394
+ {'type': 'RULE_REF', 'value': 3},
395
+ {'type': 'END'}],
396
+ [{'type': 'CHAR', 'value': [97]},
397
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
398
+ {'type': 'CHAR_ALT', 'value': 48},
399
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
400
+ {'type': 'CHAR_ALT', 'value': 95},
401
+ {'type': 'RULE_REF', 'value': 2},
402
+ {'type': 'ALT'},
403
+ {'type': 'END'}],
404
+ [{'type': 'RULE_REF', 'value': 4}, {'type': 'END'}],
405
+ [{'type': 'CHAR', 'value': [32]},
406
+ {'type': 'CHAR_ALT', 'value': 9},
407
+ {'type': 'CHAR_ALT', 'value': 10},
408
+ {'type': 'RULE_REF', 'value': 4},
409
+ {'type': 'ALT'},
410
+ {'type': 'END'}]]}),
411
+ ('lots of escapes',
412
+ 'root ::= "\\x2A" "\\u006F" "\\U0001F4A9" "\\t" "\\n" "\\r" "\\"" "\\[" "\\]" "\\\\"',
413
+ {'symbol_ids': [('root', 0)],
414
+ 'rules': [[{'type': 'CHAR', 'value': [42]},
415
+ {'type': 'CHAR', 'value': [111]},
416
+ {'type': 'CHAR', 'value': [128169]},
417
+ {'type': 'CHAR', 'value': [9]},
418
+ {'type': 'CHAR', 'value': [10]},
419
+ {'type': 'CHAR', 'value': [13]},
420
+ {'type': 'CHAR', 'value': [34]},
421
+ {'type': 'CHAR', 'value': [91]},
422
+ {'type': 'CHAR', 'value': [93]},
423
+ {'type': 'CHAR', 'value': [92]},
424
+ {'type': 'END'}]]}),
425
+ ('lots of escape and alternate escapes',
426
+ 'root ::= "\\x2A" "\\u006F" "\\U0001F4A9" "\\t" "\\n" "\\r" "\\"" "\\[" "\\]" "\\\\" ("\\x2A" | '
427
+ '"\\u006F" | "\\U0001F4A9" | "\\t" | "\\n" | "\\r" | "\\"" | "\\[" | "\\]" | "\\\\" )',
428
+ {'symbol_ids': [('root', 0), ('root_1', 1)],
429
+ 'rules': [[{'type': 'CHAR', 'value': [42]},
430
+ {'type': 'CHAR', 'value': [111]},
431
+ {'type': 'CHAR', 'value': [128169]},
432
+ {'type': 'CHAR', 'value': [9]},
433
+ {'type': 'CHAR', 'value': [10]},
434
+ {'type': 'CHAR', 'value': [13]},
435
+ {'type': 'CHAR', 'value': [34]},
436
+ {'type': 'CHAR', 'value': [91]},
437
+ {'type': 'CHAR', 'value': [93]},
438
+ {'type': 'CHAR', 'value': [92]},
439
+ {'type': 'RULE_REF', 'value': 1},
440
+ {'type': 'END'}],
441
+ [{'type': 'CHAR', 'value': [42]},
442
+ {'type': 'ALT'},
443
+ {'type': 'CHAR', 'value': [111]},
444
+ {'type': 'ALT'},
445
+ {'type': 'CHAR', 'value': [128169]},
446
+ {'type': 'ALT'},
447
+ {'type': 'CHAR', 'value': [9]},
448
+ {'type': 'ALT'},
449
+ {'type': 'CHAR', 'value': [10]},
450
+ {'type': 'ALT'},
451
+ {'type': 'CHAR', 'value': [13]},
452
+ {'type': 'ALT'},
453
+ {'type': 'CHAR', 'value': [34]},
454
+ {'type': 'ALT'},
455
+ {'type': 'CHAR', 'value': [91]},
456
+ {'type': 'ALT'},
457
+ {'type': 'CHAR', 'value': [93]},
458
+ {'type': 'ALT'},
459
+ {'type': 'CHAR', 'value': [92]},
460
+ {'type': 'END'}]]}),
461
+ ('arithmetic',
462
+ '\\nroot ::= (expr "=" ws term "\\n")+\\nexpr ::= term ([-+*/] term)*\\nterm ::= ident | num | "(" '
463
+ 'ws expr ")" ws\\nident ::= [a-z] [a-z0-9_]* ws\\nnum ::= [0-9]+ ws\\nws ::= [ \\t\\n]*\\n',
464
+ {'symbol_ids': [('root', 0),
465
+ ('root_1', 1),
466
+ ('expr', 2),
467
+ ('ws', 3),
468
+ ('term', 4),
469
+ ('root_5', 5),
470
+ ('expr_6', 6),
471
+ ('expr_7', 7),
472
+ ('ident', 8),
473
+ ('num', 9),
474
+ ('ident_10', 10),
475
+ ('num_11', 11),
476
+ ('ws_12', 12)],
477
+ 'rules': [[{'type': 'RULE_REF', 'value': 5}, {'type': 'END'}],
478
+ [{'type': 'RULE_REF', 'value': 2},
479
+ {'type': 'CHAR', 'value': [61]},
480
+ {'type': 'RULE_REF', 'value': 3},
481
+ {'type': 'RULE_REF', 'value': 4},
482
+ {'type': 'CHAR', 'value': [10]},
483
+ {'type': 'END'}],
484
+ [{'type': 'RULE_REF', 'value': 4}, {'type': 'RULE_REF', 'value': 7}, {'type': 'END'}],
485
+ [{'type': 'RULE_REF', 'value': 12}, {'type': 'END'}],
486
+ [{'type': 'RULE_REF', 'value': 8},
487
+ {'type': 'ALT'},
488
+ {'type': 'RULE_REF', 'value': 9},
489
+ {'type': 'ALT'},
490
+ {'type': 'CHAR', 'value': [40]},
491
+ {'type': 'RULE_REF', 'value': 3},
492
+ {'type': 'RULE_REF', 'value': 2},
493
+ {'type': 'CHAR', 'value': [41]},
494
+ {'type': 'RULE_REF', 'value': 3},
495
+ {'type': 'END'}],
496
+ [{'type': 'RULE_REF', 'value': 1},
497
+ {'type': 'RULE_REF', 'value': 5},
498
+ {'type': 'ALT'},
499
+ {'type': 'RULE_REF', 'value': 1},
500
+ {'type': 'END'}],
501
+ [{'type': 'CHAR', 'value': [45]},
502
+ {'type': 'CHAR_ALT', 'value': 43},
503
+ {'type': 'CHAR_ALT', 'value': 42},
504
+ {'type': 'CHAR_ALT', 'value': 47},
505
+ {'type': 'RULE_REF', 'value': 4},
506
+ {'type': 'END'}],
507
+ [{'type': 'RULE_REF', 'value': 6},
508
+ {'type': 'RULE_REF', 'value': 7},
509
+ {'type': 'ALT'},
510
+ {'type': 'END'}],
511
+ [{'type': 'CHAR', 'value': [97]},
512
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
513
+ {'type': 'RULE_REF', 'value': 10},
514
+ {'type': 'RULE_REF', 'value': 3},
515
+ {'type': 'END'}],
516
+ [{'type': 'RULE_REF', 'value': 11}, {'type': 'RULE_REF', 'value': 3}, {'type': 'END'}],
517
+ [{'type': 'CHAR', 'value': [97]},
518
+ {'type': 'CHAR_RNG_UPPER', 'value': 122},
519
+ {'type': 'CHAR_ALT', 'value': 48},
520
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
521
+ {'type': 'CHAR_ALT', 'value': 95},
522
+ {'type': 'RULE_REF', 'value': 10},
523
+ {'type': 'ALT'},
524
+ {'type': 'END'}],
525
+ [{'type': 'CHAR', 'value': [48]},
526
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
527
+ {'type': 'RULE_REF', 'value': 11},
528
+ {'type': 'ALT'},
529
+ {'type': 'CHAR', 'value': [48]},
530
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
531
+ {'type': 'END'}],
532
+ [{'type': 'CHAR', 'value': [32]},
533
+ {'type': 'CHAR_ALT', 'value': 9},
534
+ {'type': 'CHAR_ALT', 'value': 10},
535
+ {'type': 'RULE_REF', 'value': 12},
536
+ {'type': 'ALT'},
537
+ {'type': 'END'}]]}),
538
+ ('json.gbnf (string)',
539
+ '\\nroot ::=\\n"\\"" (\\n[^"\\\\\x7f\\x00-\\x1F] |\\n"\\\\" (["\\\\/bfnrt] | "u" [0-9a-fA-F] '
540
+ '[0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes\\n)* "\\""\\n',
541
+ {'symbol_ids': [('root', 0), ('root_1', 1), ('root_2', 2), ('root_3', 3)],
542
+ 'rules': [[{'type': 'CHAR', 'value': [34]},
543
+ {'type': 'RULE_REF', 'value': 3},
544
+ {'type': 'CHAR', 'value': [34]},
545
+ {'type': 'END'}],
546
+ [{'type': 'CHAR_NOT', 'value': [34]},
547
+ {'type': 'CHAR_ALT', 'value': 92},
548
+ {'type': 'CHAR_ALT', 'value': 127},
549
+ {'type': 'CHAR_ALT', 'value': 0},
550
+ {'type': 'CHAR_RNG_UPPER', 'value': 31},
551
+ {'type': 'ALT'},
552
+ {'type': 'CHAR', 'value': [92]},
553
+ {'type': 'RULE_REF', 'value': 2},
554
+ {'type': 'END'}],
555
+ [{'type': 'CHAR', 'value': [34]},
556
+ {'type': 'CHAR_ALT', 'value': 92},
557
+ {'type': 'CHAR_ALT', 'value': 47},
558
+ {'type': 'CHAR_ALT', 'value': 98},
559
+ {'type': 'CHAR_ALT', 'value': 102},
560
+ {'type': 'CHAR_ALT', 'value': 110},
561
+ {'type': 'CHAR_ALT', 'value': 114},
562
+ {'type': 'CHAR_ALT', 'value': 116},
563
+ {'type': 'ALT'},
564
+ {'type': 'CHAR', 'value': [117]},
565
+ {'type': 'CHAR', 'value': [48]},
566
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
567
+ {'type': 'CHAR_ALT', 'value': 97},
568
+ {'type': 'CHAR_RNG_UPPER', 'value': 102},
569
+ {'type': 'CHAR_ALT', 'value': 65},
570
+ {'type': 'CHAR_RNG_UPPER', 'value': 70},
571
+ {'type': 'CHAR', 'value': [48]},
572
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
573
+ {'type': 'CHAR_ALT', 'value': 97},
574
+ {'type': 'CHAR_RNG_UPPER', 'value': 102},
575
+ {'type': 'CHAR_ALT', 'value': 65},
576
+ {'type': 'CHAR_RNG_UPPER', 'value': 70},
577
+ {'type': 'CHAR', 'value': [48]},
578
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
579
+ {'type': 'CHAR_ALT', 'value': 97},
580
+ {'type': 'CHAR_RNG_UPPER', 'value': 102},
581
+ {'type': 'CHAR_ALT', 'value': 65},
582
+ {'type': 'CHAR_RNG_UPPER', 'value': 70},
583
+ {'type': 'CHAR', 'value': [48]},
584
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
585
+ {'type': 'CHAR_ALT', 'value': 97},
586
+ {'type': 'CHAR_RNG_UPPER', 'value': 102},
587
+ {'type': 'CHAR_ALT', 'value': 65},
588
+ {'type': 'CHAR_RNG_UPPER', 'value': 70},
589
+ {'type': 'END'}],
590
+ [{'type': 'RULE_REF', 'value': 1},
591
+ {'type': 'RULE_REF', 'value': 3},
592
+ {'type': 'ALT'},
593
+ {'type': 'END'}]]}),
594
+ ('json.gbnf (full)',
595
+ '\\nroot ::= object\\nvalue ::= object | array | string | number | ("true" | "false" | "null") '
596
+ 'ws\\n\\nobject ::=\\n"{" ws (\\nstring ":" ws value\\n("," ws string ":" ws value)*\\n)? "}" '
597
+ 'ws\\n\\narray ::=\\n"[" ws (\\nvalue\\n("," ws value)*\\n)? "]" ws\\n\\nstring ::=\\n"\\"" '
598
+ '(\\n[^"\\\\\x7f\\x00-\\x1F] |\\n"\\\\" (["\\\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] '
599
+ '[0-9a-fA-F]) # escapes\\n)* "\\"" ws\\n\\nnumber ::= ("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] '
600
+ '[-+]? [0-9]+)? ws\\n\\n# Optional space: by convention, applied in this grammar after literal chars '
601
+ 'when allowed\\nws ::= ([ \\t\\n] ws)?\\n\\n',
602
+ {'symbol_ids': [('root', 0),
603
+ ('object', 1),
604
+ ('value', 2),
605
+ ('array', 3),
606
+ ('string', 4),
607
+ ('number', 5),
608
+ ('value_6', 6),
609
+ ('ws', 7),
610
+ ('object_8', 8),
611
+ ('object_9', 9),
612
+ ('object_10', 10),
613
+ ('object_11', 11),
614
+ ('array_12', 12),
615
+ ('array_13', 13),
616
+ ('array_14', 14),
617
+ ('array_15', 15),
618
+ ('string_16', 16),
619
+ ('string_17', 17),
620
+ ('string_18', 18),
621
+ ('number_19', 19),
622
+ ('number_20', 20),
623
+ ('number_21', 21),
624
+ ('number_22', 22),
625
+ ('number_23', 23),
626
+ ('number_24', 24),
627
+ ('number_25', 25),
628
+ ('number_26', 26),
629
+ ('number_27', 27),
630
+ ('number_28', 28),
631
+ ('number_29', 29),
632
+ ('ws_30', 30),
633
+ ('ws_31', 31)],
634
+ 'rules': [[{'type': 'RULE_REF', 'value': 1}, {'type': 'END'}],
635
+ [{'type': 'CHAR', 'value': [123]},
636
+ {'type': 'RULE_REF', 'value': 7},
637
+ {'type': 'RULE_REF', 'value': 11},
638
+ {'type': 'CHAR', 'value': [125]},
639
+ {'type': 'RULE_REF', 'value': 7},
640
+ {'type': 'END'}],
641
+ [{'type': 'RULE_REF', 'value': 1},
642
+ {'type': 'ALT'},
643
+ {'type': 'RULE_REF', 'value': 3},
644
+ {'type': 'ALT'},
645
+ {'type': 'RULE_REF', 'value': 4},
646
+ {'type': 'ALT'},
647
+ {'type': 'RULE_REF', 'value': 5},
648
+ {'type': 'ALT'},
649
+ {'type': 'RULE_REF', 'value': 6},
650
+ {'type': 'RULE_REF', 'value': 7},
651
+ {'type': 'END'}],
652
+ [{'type': 'CHAR', 'value': [91]},
653
+ {'type': 'RULE_REF', 'value': 7},
654
+ {'type': 'RULE_REF', 'value': 15},
655
+ {'type': 'CHAR', 'value': [93]},
656
+ {'type': 'RULE_REF', 'value': 7},
657
+ {'type': 'END'}],
658
+ [{'type': 'CHAR', 'value': [34]},
659
+ {'type': 'RULE_REF', 'value': 18},
660
+ {'type': 'CHAR', 'value': [34]},
661
+ {'type': 'RULE_REF', 'value': 7},
662
+ {'type': 'END'}],
663
+ [{'type': 'RULE_REF', 'value': 19},
664
+ {'type': 'RULE_REF', 'value': 25},
665
+ {'type': 'RULE_REF', 'value': 29},
666
+ {'type': 'RULE_REF', 'value': 7},
667
+ {'type': 'END'}],
668
+ [{'type': 'CHAR', 'value': [116]},
669
+ {'type': 'CHAR', 'value': [114]},
670
+ {'type': 'CHAR', 'value': [117]},
671
+ {'type': 'CHAR', 'value': [101]},
672
+ {'type': 'ALT'},
673
+ {'type': 'CHAR', 'value': [102]},
674
+ {'type': 'CHAR', 'value': [97]},
675
+ {'type': 'CHAR', 'value': [108]},
676
+ {'type': 'CHAR', 'value': [115]},
677
+ {'type': 'CHAR', 'value': [101]},
678
+ {'type': 'ALT'},
679
+ {'type': 'CHAR', 'value': [110]},
680
+ {'type': 'CHAR', 'value': [117]},
681
+ {'type': 'CHAR', 'value': [108]},
682
+ {'type': 'CHAR', 'value': [108]},
683
+ {'type': 'END'}],
684
+ [{'type': 'RULE_REF', 'value': 31}, {'type': 'END'}],
685
+ [{'type': 'RULE_REF', 'value': 4},
686
+ {'type': 'CHAR', 'value': [58]},
687
+ {'type': 'RULE_REF', 'value': 7},
688
+ {'type': 'RULE_REF', 'value': 2},
689
+ {'type': 'RULE_REF', 'value': 10},
690
+ {'type': 'END'}],
691
+ [{'type': 'CHAR', 'value': [44]},
692
+ {'type': 'RULE_REF', 'value': 7},
693
+ {'type': 'RULE_REF', 'value': 4},
694
+ {'type': 'CHAR', 'value': [58]},
695
+ {'type': 'RULE_REF', 'value': 7},
696
+ {'type': 'RULE_REF', 'value': 2},
697
+ {'type': 'END'}],
698
+ [{'type': 'RULE_REF', 'value': 9},
699
+ {'type': 'RULE_REF', 'value': 10},
700
+ {'type': 'ALT'},
701
+ {'type': 'END'}],
702
+ [{'type': 'RULE_REF', 'value': 8}, {'type': 'ALT'}, {'type': 'END'}],
703
+ [{'type': 'RULE_REF', 'value': 2}, {'type': 'RULE_REF', 'value': 14}, {'type': 'END'}],
704
+ [{'type': 'CHAR', 'value': [44]},
705
+ {'type': 'RULE_REF', 'value': 7},
706
+ {'type': 'RULE_REF', 'value': 2},
707
+ {'type': 'END'}],
708
+ [{'type': 'RULE_REF', 'value': 13},
709
+ {'type': 'RULE_REF', 'value': 14},
710
+ {'type': 'ALT'},
711
+ {'type': 'END'}],
712
+ [{'type': 'RULE_REF', 'value': 12}, {'type': 'ALT'}, {'type': 'END'}],
713
+ [{'type': 'CHAR_NOT', 'value': [34]},
714
+ {'type': 'CHAR_ALT', 'value': 92},
715
+ {'type': 'CHAR_ALT', 'value': 127},
716
+ {'type': 'CHAR_ALT', 'value': 0},
717
+ {'type': 'CHAR_RNG_UPPER', 'value': 31},
718
+ {'type': 'ALT'},
719
+ {'type': 'CHAR', 'value': [92]},
720
+ {'type': 'RULE_REF', 'value': 17},
721
+ {'type': 'END'}],
722
+ [{'type': 'CHAR', 'value': [34]},
723
+ {'type': 'CHAR_ALT', 'value': 92},
724
+ {'type': 'CHAR_ALT', 'value': 47},
725
+ {'type': 'CHAR_ALT', 'value': 98},
726
+ {'type': 'CHAR_ALT', 'value': 102},
727
+ {'type': 'CHAR_ALT', 'value': 110},
728
+ {'type': 'CHAR_ALT', 'value': 114},
729
+ {'type': 'CHAR_ALT', 'value': 116},
730
+ {'type': 'ALT'},
731
+ {'type': 'CHAR', 'value': [117]},
732
+ {'type': 'CHAR', 'value': [48]},
733
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
734
+ {'type': 'CHAR_ALT', 'value': 97},
735
+ {'type': 'CHAR_RNG_UPPER', 'value': 102},
736
+ {'type': 'CHAR_ALT', 'value': 65},
737
+ {'type': 'CHAR_RNG_UPPER', 'value': 70},
738
+ {'type': 'CHAR', 'value': [48]},
739
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
740
+ {'type': 'CHAR_ALT', 'value': 97},
741
+ {'type': 'CHAR_RNG_UPPER', 'value': 102},
742
+ {'type': 'CHAR_ALT', 'value': 65},
743
+ {'type': 'CHAR_RNG_UPPER', 'value': 70},
744
+ {'type': 'CHAR', 'value': [48]},
745
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
746
+ {'type': 'CHAR_ALT', 'value': 97},
747
+ {'type': 'CHAR_RNG_UPPER', 'value': 102},
748
+ {'type': 'CHAR_ALT', 'value': 65},
749
+ {'type': 'CHAR_RNG_UPPER', 'value': 70},
750
+ {'type': 'CHAR', 'value': [48]},
751
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
752
+ {'type': 'CHAR_ALT', 'value': 97},
753
+ {'type': 'CHAR_RNG_UPPER', 'value': 102},
754
+ {'type': 'CHAR_ALT', 'value': 65},
755
+ {'type': 'CHAR_RNG_UPPER', 'value': 70},
756
+ {'type': 'END'}],
757
+ [{'type': 'RULE_REF', 'value': 16},
758
+ {'type': 'RULE_REF', 'value': 18},
759
+ {'type': 'ALT'},
760
+ {'type': 'END'}],
761
+ [{'type': 'RULE_REF', 'value': 20}, {'type': 'RULE_REF', 'value': 21}, {'type': 'END'}],
762
+ [{'type': 'CHAR', 'value': [45]}, {'type': 'ALT'}, {'type': 'END'}],
763
+ [{'type': 'CHAR', 'value': [48]},
764
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
765
+ {'type': 'ALT'},
766
+ {'type': 'CHAR', 'value': [49]},
767
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
768
+ {'type': 'RULE_REF', 'value': 22},
769
+ {'type': 'END'}],
770
+ [{'type': 'CHAR', 'value': [48]},
771
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
772
+ {'type': 'RULE_REF', 'value': 22},
773
+ {'type': 'ALT'},
774
+ {'type': 'END'}],
775
+ [{'type': 'CHAR', 'value': [46]}, {'type': 'RULE_REF', 'value': 24}, {'type': 'END'}],
776
+ [{'type': 'CHAR', 'value': [48]},
777
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
778
+ {'type': 'RULE_REF', 'value': 24},
779
+ {'type': 'ALT'},
780
+ {'type': 'CHAR', 'value': [48]},
781
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
782
+ {'type': 'END'}],
783
+ [{'type': 'RULE_REF', 'value': 23}, {'type': 'ALT'}, {'type': 'END'}],
784
+ [{'type': 'CHAR', 'value': [101]},
785
+ {'type': 'CHAR_ALT', 'value': 69},
786
+ {'type': 'RULE_REF', 'value': 27},
787
+ {'type': 'RULE_REF', 'value': 28},
788
+ {'type': 'END'}],
789
+ [{'type': 'CHAR', 'value': [45]},
790
+ {'type': 'CHAR_ALT', 'value': 43},
791
+ {'type': 'ALT'},
792
+ {'type': 'END'}],
793
+ [{'type': 'CHAR', 'value': [48]},
794
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
795
+ {'type': 'RULE_REF', 'value': 28},
796
+ {'type': 'ALT'},
797
+ {'type': 'CHAR', 'value': [48]},
798
+ {'type': 'CHAR_RNG_UPPER', 'value': 57},
799
+ {'type': 'END'}],
800
+ [{'type': 'RULE_REF', 'value': 26}, {'type': 'ALT'}, {'type': 'END'}],
801
+ [{'type': 'CHAR', 'value': [32]},
802
+ {'type': 'CHAR_ALT', 'value': 9},
803
+ {'type': 'CHAR_ALT', 'value': 10},
804
+ {'type': 'RULE_REF', 'value': 7},
805
+ {'type': 'END'}],
806
+ [{'type': 'RULE_REF', 'value': 30}, {'type': 'ALT'}, {'type': 'END'}]]}),
807
+ ('japanese',
808
+ '\\n# A probably incorrect grammar for Japanese\\nroot ::= jp-char+ ([ \\t\\n] '
809
+ 'jp-char+)*\\njp-char ::= hiragana | katakana | punctuation | cjk\\nhiragana ::= '
810
+ '[ぁ-ゟ]\\nkatakana ::= [ァ-ヿ]\\npunctuation ::= [、-〾]\\ncjk ::= [一-鿿]\\n\\n',
811
+ {'symbol_ids': [('root', 0),
812
+ ('jp-char', 1),
813
+ ('root_2', 2),
814
+ ('root_3', 3),
815
+ ('root_4', 4),
816
+ ('root_5', 5),
817
+ ('hiragana', 6),
818
+ ('katakana', 7),
819
+ ('punctuation', 8),
820
+ ('cjk', 9)],
821
+ 'rules': [[{'type': 'RULE_REF', 'value': 2}, {'type': 'RULE_REF', 'value': 5}, {'type': 'END'}],
822
+ [{'type': 'RULE_REF', 'value': 6},
823
+ {'type': 'ALT'},
824
+ {'type': 'RULE_REF', 'value': 7},
825
+ {'type': 'ALT'},
826
+ {'type': 'RULE_REF', 'value': 8},
827
+ {'type': 'ALT'},
828
+ {'type': 'RULE_REF', 'value': 9},
829
+ {'type': 'END'}],
830
+ [{'type': 'RULE_REF', 'value': 1},
831
+ {'type': 'RULE_REF', 'value': 2},
832
+ {'type': 'ALT'},
833
+ {'type': 'RULE_REF', 'value': 1},
834
+ {'type': 'END'}],
835
+ [{'type': 'CHAR', 'value': [32]},
836
+ {'type': 'CHAR_ALT', 'value': 9},
837
+ {'type': 'CHAR_ALT', 'value': 10},
838
+ {'type': 'RULE_REF', 'value': 4},
839
+ {'type': 'END'}],
840
+ [{'type': 'RULE_REF', 'value': 1},
841
+ {'type': 'RULE_REF', 'value': 4},
842
+ {'type': 'ALT'},
843
+ {'type': 'RULE_REF', 'value': 1},
844
+ {'type': 'END'}],
845
+ [{'type': 'RULE_REF', 'value': 3},
846
+ {'type': 'RULE_REF', 'value': 5},
847
+ {'type': 'ALT'},
848
+ {'type': 'END'}],
849
+ [{'type': 'CHAR', 'value': [12353]},
850
+ {'type': 'CHAR_RNG_UPPER', 'value': 12447},
851
+ {'type': 'END'}],
852
+ [{'type': 'CHAR', 'value': [12449]},
853
+ {'type': 'CHAR_RNG_UPPER', 'value': 12543},
854
+ {'type': 'END'}],
855
+ [{'type': 'CHAR', 'value': [12289]},
856
+ {'type': 'CHAR_RNG_UPPER', 'value': 12350},
857
+ {'type': 'END'}],
858
+ [{'type': 'CHAR', 'value': [19968]},
859
+ {'type': 'CHAR_RNG_UPPER', 'value': 40959},
860
+ {'type': 'END'}]]}),
861
+ ]
862
+
863
+
864
+ def describe_grammar_parser_tests():
865
+ @pytest.mark.parametrize(('key', 'grammar', 'expectation'), CASES, ids=[case[0] for case in CASES])
866
+ def it_parses_grammar(key, grammar, expectation):
867
+ parsed_grammar = RulesBuilder('\n'.join(grammar.split('\\n')))
868
+ assert parsed_grammar.rules == expectation['rules']
869
+ assert list(parsed_grammar.symbol_ids) == expectation['symbol_ids']
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/test_gbnf.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from types import SimpleNamespace
3
+ from unittest import mock
4
+
5
+ import pytest
6
+
7
+ from gbnf.gbnf import GBNF
8
+ from gbnf.rules_builder.symbol_ids import SymbolIds
9
+ from gbnf.rules_builder.types import InternalRuleType
10
+ from gbnf.utils.errors.grammar_parse_error import GrammarParseError
11
+
12
+
13
+ def make_symbol_ids(*names_and_ids):
14
+ symbol_ids = SymbolIds()
15
+ for name, id_ in names_and_ids:
16
+ symbol_ids.set(name, id_, 0)
17
+ return symbol_ids
18
+
19
+
20
+ @pytest.fixture
21
+ def mocks():
22
+ with (
23
+ mock.patch('gbnf.gbnf.RulesBuilder', return_value=SimpleNamespace(rules=[], symbol_ids=SymbolIds())) as rules_builder,
24
+ mock.patch('gbnf.gbnf.build_rule_stack') as build_rule_stack,
25
+ mock.patch('gbnf.gbnf.Graph') as graph,
26
+ mock.patch('gbnf.gbnf.ParseState') as parse_state,
27
+ ):
28
+ yield SimpleNamespace(
29
+ RulesBuilder=rules_builder,
30
+ build_rule_stack=build_rule_stack,
31
+ Graph=graph,
32
+ ParseState=parse_state,
33
+ )
34
+
35
+
36
+ def describe_gbnf():
37
+ def it_throws_if_rules_are_empty(mocks):
38
+ with pytest.raises(GrammarParseError) as exc_info:
39
+ GBNF('')
40
+ assert exc_info.value.message == GrammarParseError('', 0, 'No rules were found').message
41
+
42
+ def it_throws_if_symbol_ids_does_not_have_root(mocks):
43
+ symbol_ids = make_symbol_ids(('root1', 1), ('root2', 1))
44
+ mocks.RulesBuilder.return_value = SimpleNamespace(
45
+ rules=[[{'type': InternalRuleType.END}]],
46
+ symbol_ids=symbol_ids,
47
+ )
48
+ with pytest.raises(GrammarParseError) as exc_info:
49
+ GBNF('')
50
+ assert exc_info.value.message == GrammarParseError(
51
+ '', 0,
52
+ f'Grammar does not contain a root symbol. Available symbols are: {json.dumps(list(symbol_ids.keys()))}',
53
+ ).message
54
+
55
+ def it_builds_parse_state(mocks):
56
+ grammar = 'grammar'
57
+ initial_string = 'foo'
58
+ symbol_ids = make_symbol_ids(('root', 1))
59
+ rules = [
60
+ [{'type': InternalRuleType.END}],
61
+ [{'type': InternalRuleType.CHAR, 'value': [97]}],
62
+ ]
63
+ mocks.RulesBuilder.return_value = SimpleNamespace(rules=rules, symbol_ids=symbol_ids)
64
+ mock_graph = mock.Mock()
65
+ mock_graph.add.return_value = 'foo'
66
+ mocks.Graph.return_value = mock_graph
67
+ mock_parse_state = object()
68
+ mocks.ParseState.return_value = mock_parse_state
69
+ mocks.build_rule_stack.side_effect = lambda rule: rule
70
+
71
+ assert GBNF(grammar, initial_string) is mock_parse_state
72
+ mocks.RulesBuilder.assert_called_with(grammar)
73
+ assert mocks.build_rule_stack.call_args_list == [mock.call(rules[0]), mock.call(rules[1])]
74
+ mocks.Graph.assert_called_with(grammar, rules, 1)
75
+ mock_graph.add.assert_called_with(initial_string)
76
+ mocks.ParseState.assert_called_with(mock_graph, 'foo')
77
+
78
+ def it_accepts_objects_that_stringify_to_a_grammar(mocks):
79
+ class Rule:
80
+ def __str__(self):
81
+ return 'root ::= "foo"'
82
+
83
+ mocks.RulesBuilder.return_value = SimpleNamespace(
84
+ rules=[[{'type': InternalRuleType.END}]],
85
+ symbol_ids=make_symbol_ids(('root', 0)),
86
+ )
87
+ GBNF(Rule())
88
+ mocks.RulesBuilder.assert_called_with('root ::= "foo"')
89
+ mocks.Graph.assert_called_with('root ::= "foo"', mock.ANY, 0)
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/test_public_api.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Behaviour of the public API, including the places where the port deliberately differs from the reference."""
2
+ import json
3
+
4
+ import pytest
5
+
6
+ import gbnf
7
+ from gbnf import GBNF, GrammarParseError, InputParseError, ParseState, RuleChar, RuleCharExclude, RuleEnd, RuleType
8
+
9
+
10
+ def describe_public_api():
11
+ def it_exports_the_public_names():
12
+ assert set(gbnf.__all__) == {
13
+ 'GBNF', 'GrammarParseError', 'InputParseError', 'ParseState', 'Range', 'Rule',
14
+ 'RuleChar', 'RuleCharExclude', 'RuleEnd', 'RuleType', 'is_range',
15
+ }
16
+ assert gbnf.is_range([1, 2])
17
+
18
+ def it_returns_a_parse_state():
19
+ assert isinstance(GBNF('root ::= "yes" | "no"'), ParseState)
20
+
21
+ def it_follows_the_readme_example():
22
+ state = GBNF('root ::= "I like green eggs and ham"')
23
+ assert [*state] == [{'type': 'char', 'value': [ord('I')]}]
24
+ state = state.add('I li')
25
+ assert [*state] == [{'type': 'char', 'value': [ord('k')]}]
26
+ state = state.add('ke gree')
27
+ assert [*state] == [{'type': 'char', 'value': [ord('n')]}]
28
+
29
+ def it_leaves_previous_states_untouched():
30
+ state = GBNF('root ::= "ab" | "ac"')
31
+ after_a = state.add('a')
32
+ assert [*after_a.add('b')] == [{'type': 'end'}]
33
+ assert [*after_a.add('c')] == [{'type': 'end'}]
34
+ assert [*state] == [{'type': 'char', 'value': [97]}]
35
+
36
+ def it_accepts_code_points_as_input():
37
+ state = GBNF('root ::= "ab"')
38
+ assert [*state.add(97)] == [{'type': 'char', 'value': [98]}]
39
+ assert [*state.add([97, 98])] == [{'type': 'end'}]
40
+ assert [*GBNF('root ::= "ab"', [97])] == [{'type': 'char', 'value': [98]}]
41
+
42
+ def it_is_callable():
43
+ assert [*GBNF('root ::= "ab"')('a')] == [{'type': 'char', 'value': [98]}]
44
+
45
+ def it_exposes_the_grammar_and_size():
46
+ state = GBNF('root ::= "a" | "b"')
47
+ assert state.grammar == 'root ::= "a" | "b"'
48
+ assert state.size == 2
49
+
50
+ def describe_rules():
51
+ def it_yields_rule_objects_with_attribute_and_key_access():
52
+ [rule] = GBNF('root ::= [^a-z]')
53
+ assert isinstance(rule, RuleCharExclude)
54
+ assert rule.type == RuleType.CHAR_EXCLUDE == 'char_exclude'
55
+ assert rule.value == rule['value'] == [[97, 122]]
56
+ assert json.loads(json.dumps(rule)) == {'type': 'char_exclude', 'value': [[97, 122]]}
57
+
58
+ def it_compares_rules_with_plain_dicts():
59
+ assert RuleChar([97]) == {'type': 'char', 'value': [97]}
60
+ assert RuleEnd() == {'type': 'end'}
61
+ assert RuleChar([97]) != RuleCharExclude([97])
62
+ assert repr(RuleChar([97])) == "{'type': 'char', 'value': [97]}"
63
+
64
+ def describe_errors():
65
+ def it_throws_a_grammar_parse_error_for_an_invalid_grammar():
66
+ with pytest.raises(GrammarParseError) as exc_info:
67
+ GBNF('root ::= "foo')
68
+ assert exc_info.value.reason == 'Unexpected end of grammar input, failed to complete parse'
69
+ assert exc_info.value.message.endswith('root ::= "foo\n\n^')
70
+
71
+ def it_throws_an_input_parse_error_for_invalid_input():
72
+ with pytest.raises(InputParseError) as exc_info:
73
+ GBNF('root ::= "foo"', 'fx')
74
+ assert exc_info.value.pos == 1
75
+ assert exc_info.value.src == 'fx'
76
+
77
+ def it_throws_a_grammar_parse_error_when_there_is_no_root():
78
+ # the reference implementation throws a generic error from its symbol table here
79
+ with pytest.raises(GrammarParseError) as exc_info:
80
+ GBNF('foo ::= "x"\nbar ::= "y"')
81
+ assert exc_info.value.reason == (
82
+ 'Grammar does not contain a root symbol. Available symbols are: ["foo", "bar"]'
83
+ )
84
+
85
+ def it_throws_a_grammar_parse_error_for_an_undefined_rule_that_is_repeated():
86
+ # the reference implementation throws a TypeError here, tripping over the undefined rule's empty slot
87
+ with pytest.raises(GrammarParseError) as exc_info:
88
+ GBNF('root ::= foo*')
89
+ assert exc_info.value.reason == 'Undefined rule identifier "foo"'
90
+ assert exc_info.value.pos == 9
91
+
92
+ @pytest.mark.parametrize(('grammar', 'input'), [
93
+ ('root ::= root "a" | "b"', ''),
94
+ ('root ::= "x" a\na ::= a "y" | "z"', 'x'),
95
+ ('root ::= "x" a\na ::= b a "y" | "z"\nb ::= "q"?', 'x'),
96
+ ])
97
+ def it_throws_a_recursion_error_for_left_recursive_grammars(grammar, input):
98
+ with pytest.raises(RecursionError):
99
+ GBNF(grammar, input)
100
+
101
+ def describe_unicode():
102
+ def it_treats_strings_as_sequences_of_code_points():
103
+ state = GBNF('root ::= "😀" [\\U0001F600-\\U0001F64F]')
104
+ assert [*state] == [{'type': 'char', 'value': [0x1F600]}]
105
+ assert [*state.add('😀')] == [{'type': 'char', 'value': [[0x1F600, 0x1F64F]]}]
106
+ assert [*state.add('😀😁')] == [{'type': 'end'}]
107
+
108
+ def describe_long_inputs():
109
+ def it_handles_long_repetitions():
110
+ state = GBNF('root ::= [0-9]* "."')
111
+ state = state.add('1' * 3000)
112
+ assert [*state] == [{'type': 'char', 'value': [[48, 57]]}, {'type': 'char', 'value': [46]}]
113
+ assert [*state.add('.')] == [{'type': 'end'}]
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/utils/__pycache__/test_is_point_in_range.cpython-314-pytest-9.1.1.pyc ADDED
Binary file (2.38 kB). View file
 
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/utils/errors/__pycache__/test_build_error_position.cpython-314-pytest-9.1.1.pyc ADDED
Binary file (4.44 kB). View file
 
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/utils/errors/__pycache__/test_get_input_as_string.cpython-314-pytest-9.1.1.pyc ADDED
Binary file (1.87 kB). View file
 
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/utils/errors/__pycache__/test_grammar_parse_error.cpython-314-pytest-9.1.1.pyc ADDED
Binary file (8.2 kB). View file
 
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/utils/errors/__pycache__/test_input_parse_error.cpython-314-pytest-9.1.1.pyc ADDED
Binary file (11.2 kB). View file
 
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/utils/errors/test_build_error_position.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ from gbnf.utils.errors.build_error_position import build_error_position
4
+
5
+
6
+ def describe_build_error_position():
7
+ @pytest.mark.parametrize(('grammar', 'pos', 'expected'), [
8
+ (
9
+ 'root ::= "foo"',
10
+ 1,
11
+ [
12
+ 'root ::= "foo"',
13
+ ' ^',
14
+ ],
15
+ ),
16
+ (
17
+ 'root ::= "foo"',
18
+ 5,
19
+ [
20
+ 'root ::= "foo"',
21
+ ' ^',
22
+ ],
23
+ ),
24
+ # multi line grammars with pos on first line
25
+ (
26
+ 'aa\\nbb',
27
+ 1,
28
+ [
29
+ 'aa',
30
+ ' ^',
31
+ ],
32
+ ),
33
+ # multi line grammars with pos on second line, first character
34
+ (
35
+ 'aa\\nbb',
36
+ 2,
37
+ [
38
+ 'aa\\nbb',
39
+ '^',
40
+ ],
41
+ ),
42
+ # multi line grammars with pos on second line, second character
43
+ (
44
+ 'aa\\nbb',
45
+ 2 + 1,
46
+ [
47
+ 'aa\\nbb',
48
+ ' ^',
49
+ ],
50
+ ),
51
+ # multi line grammars beyond error with pos on second line
52
+ (
53
+ 'aa\\nbb\\ncc',
54
+ 2 + 1,
55
+ [
56
+ 'aa\\nbb',
57
+ ' ^',
58
+ ],
59
+ ),
60
+ # multi line grammars beyond error with pos on third line, first char
61
+ (
62
+ 'aa\\nbb\\ncc',
63
+ 2 + 2 + 0,
64
+ [
65
+ 'aa\\nbb\\ncc',
66
+ '^',
67
+ ],
68
+ ),
69
+ # multi line grammars beyond error with pos on third line, second char
70
+ (
71
+ 'aa\\nbb\\ncc',
72
+ 2 + 2 + 1,
73
+ [
74
+ 'aa\\nbb\\ncc',
75
+ ' ^',
76
+ ],
77
+ ),
78
+ # multi line grammars beyond error with pos on fourth line, first char
79
+ (
80
+ 'aa\\nbb\\ncc\\ndd',
81
+ 2 + 2 + 2 + 0,
82
+ [
83
+ 'bb\\ncc\\ndd',
84
+ '^',
85
+ ],
86
+ ),
87
+ # multi line grammars beyond error with pos on fifth line, second char
88
+ (
89
+ 'aa\\nbb\\ncc\\ndd\\nee',
90
+ 2 + 2 + 2 + 2 + 1,
91
+ [
92
+ 'cc\\ndd\\nee',
93
+ ' ^',
94
+ ],
95
+ ),
96
+ ])
97
+ def it_correctly_shows_position(grammar, pos, expected):
98
+ grammar_out, pos_out = expected
99
+ result = build_error_position('\n'.join(grammar.split('\\n')), pos)
100
+ assert result == [
101
+ *grammar_out.split('\\n'),
102
+ pos_out,
103
+ ]
104
+
105
+ def it_renders_a_message_for_empty_input():
106
+ result = build_error_position('', 0)
107
+ assert result == ['No input provided']
108
+
109
+ def it_renders_an_empty_line_for_a_position_past_the_end_of_input():
110
+ assert build_error_position('ab', 2) == ['ab', '', '^']
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/utils/errors/test_get_input_as_string.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ from gbnf.utils.errors.get_input_as_string import get_input_as_string
4
+
5
+
6
+ def describe_get_input_as_string():
7
+ @pytest.mark.parametrize(('input', 'expected'), [
8
+ ('hello', 'hello'),
9
+ ([104, 101, 108, 108, 111], 'hello'),
10
+ (104, 'h'),
11
+ (0x1F600, '😀'),
12
+ ([0x1F600, 0x1F601], '😀😁'),
13
+ ])
14
+ def it_correctly_converts_to_string(input, expected):
15
+ result = get_input_as_string(input)
16
+ assert result == expected
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/utils/errors/test_grammar_parse_error.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pickle
2
+
3
+ from gbnf.utils.errors.grammar_parse_error import GRAMMAR_PARSER_ERROR_HEADER_MESSAGE, GrammarParseError
4
+
5
+
6
+ def describe_grammar_parse_error():
7
+ def it_renders_a_message():
8
+ grammar = 'aa\\nbb\\ncc\\ndd\\nee'
9
+ pos = 5
10
+ reason = 'reason'
11
+ err = GrammarParseError('\n'.join(grammar.split('\\n')), pos, reason)
12
+ assert err.message == '\n'.join([
13
+ GRAMMAR_PARSER_ERROR_HEADER_MESSAGE(reason),
14
+ '',
15
+ 'aa',
16
+ 'bb',
17
+ 'cc',
18
+ ' ^',
19
+ ])
20
+ assert str(err) == err.message
21
+
22
+ def it_exposes_its_details():
23
+ err = GrammarParseError('root', 2, 'reason')
24
+ assert err.name == 'GrammarParseError'
25
+ assert err.grammar == 'root'
26
+ assert err.pos == 2
27
+ assert err.reason == 'reason'
28
+
29
+ def it_can_be_pickled():
30
+ err = pickle.loads(pickle.dumps(GrammarParseError('root', 2, 'reason')))
31
+ assert err.message == GrammarParseError('root', 2, 'reason').message
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/utils/errors/test_input_parse_error.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pickle
2
+
3
+ from gbnf.utils.errors.input_parse_error import INPUT_PARSER_ERROR_HEADER_MESSAGE, InputParseError
4
+
5
+
6
+ def describe_input_parse_error():
7
+ def it_renders_a_message():
8
+ input = 'some input'
9
+ pos = 1
10
+ err = InputParseError(input, pos)
11
+ assert err.message == '\n'.join([
12
+ INPUT_PARSER_ERROR_HEADER_MESSAGE,
13
+ '',
14
+ input,
15
+ ' ^',
16
+ ])
17
+ assert str(err) == err.message
18
+
19
+ def it_renders_a_message_for_code_point():
20
+ pos = 0
21
+ err = InputParseError('a', pos)
22
+ assert err.message == '\n'.join([
23
+ INPUT_PARSER_ERROR_HEADER_MESSAGE,
24
+ '',
25
+ 'a',
26
+ '^',
27
+ ])
28
+
29
+ def it_renders_a_message_for_code_points():
30
+ pos = 2
31
+ err = InputParseError('abcd', pos)
32
+ assert err.message == '\n'.join([
33
+ INPUT_PARSER_ERROR_HEADER_MESSAGE,
34
+ '',
35
+ 'abcd',
36
+ ' ^',
37
+ ])
38
+
39
+ def it_renders_a_message_including_previous_input():
40
+ err = InputParseError([122], 0, [98, 97])
41
+ assert err.src == 'baz'
42
+ assert err.message == '\n'.join([
43
+ INPUT_PARSER_ERROR_HEADER_MESSAGE,
44
+ '',
45
+ 'baz',
46
+ ' ^',
47
+ ])
48
+ assert err.error_for_most_recent_input == '\n'.join([
49
+ INPUT_PARSER_ERROR_HEADER_MESSAGE,
50
+ '',
51
+ 'z',
52
+ '^',
53
+ ])
54
+
55
+ def it_can_be_pickled():
56
+ err = pickle.loads(pickle.dumps(InputParseError('abcd', 2, 'xy')))
57
+ assert err.message == InputParseError('abcd', 2, 'xy').message
v2-models-effort/runs/20261017T020314Z_49fc1c31/ported_implementation/tests/unit/utils/test_is_point_in_range.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ from gbnf.utils.is_point_in_range import is_point_in_range
4
+
5
+
6
+ def describe_is_point_in_range():
7
+ @pytest.mark.parametrize(('point', 'expectation', 'range_'), [
8
+ (96, False, [97, 122]),
9
+ (97, True, [97, 122]),
10
+ (98, True, [97, 122]),
11
+ (122, True, [97, 122]),
12
+ (123, False, [97, 122]),
13
+ ])
14
+ def it_checks_if_point_is_in_range(point, expectation, range_):
15
+ assert is_point_in_range(point, range_) == expectation
v2-models-effort/runs/20261017T020314Z_49fc1c31/proxy.log ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ NOTICE Oct 17 02:03:38.543 [1]: Initializing tinyproxy ...
2
+ NOTICE Oct 17 02:03:38.546 [1]: Reloading config file (/etc/tinyproxy/tinyproxy.conf)
3
+ INFO Oct 17 02:03:38.549 [1]: Added address [0.0.0.0] to listen addresses.
4
+ INFO Oct 17 02:03:38.550 [1]: Adding Port [443] to the list allowed by CONNECT
5
+ NOTICE Oct 17 02:03:38.551 [1]: Reloading config file finished
6
+ INFO Oct 17 02:03:38.553 [1]: listen_sock called with addr = '0.0.0.0'
7
+ INFO Oct 17 02:03:38.555 [1]: trying to listen on host[0.0.0.0], family[2], socktype[1], proto[6]
8
+ INFO Oct 17 02:03:38.557 [1]: listening on fd [3]
9
+ INFO Oct 17 02:03:38.558 [1]: Not running as root, so not changing UID/GID.
10
+ INFO Oct 17 02:03:38.560 [1]: Setting the various signals.
11
+ INFO Oct 17 02:03:38.562 [1]: Starting main loop. Accepting connections.
12
+ CONNECT Oct 17 02:03:38.912 [1]: Connect (file descriptor 5): 172.31.0.3
13
+ CONNECT Oct 17 02:03:38.914 [1]: Request (file descriptor 5): CONNECT api.anthropic.com:443 HTTP/1.1
14
+ INFO Oct 17 02:03:38.917 [1]: No upstream proxy for api.anthropic.com
15
+ INFO Oct 17 02:03:38.923 [1]: opensock: opening connection to api.anthropic.com:443
16
+ INFO Oct 17 02:03:38.936 [1]: opensock: getaddrinfo returned for api.anthropic.com:443
17
+ CONNECT Oct 17 02:03:38.951 [1]: Established connection to host "api.anthropic.com" using file descriptor 6.
18
+ INFO Oct 17 02:03:38.954 [1]: Not sending client headers to remote machine
19
+ CONNECT Oct 17 02:03:39.068 [1]: Connect (file descriptor 7): 172.31.0.3
20
+ CONNECT Oct 17 02:03:39.070 [1]: Request (file descriptor 7): CONNECT api.anthropic.com:443 HTTP/1.1
21
+ INFO Oct 17 02:03:39.071 [1]: No upstream proxy for api.anthropic.com
22
+ INFO Oct 17 02:03:39.075 [1]: opensock: opening connection to api.anthropic.com:443
23
+ INFO Oct 17 02:03:39.080 [1]: opensock: getaddrinfo returned for api.anthropic.com:443
24
+ CONNECT Oct 17 02:03:39.086 [1]: Established connection to host "api.anthropic.com" using file descriptor 8.
25
+ INFO Oct 17 02:03:39.088 [1]: Not sending client headers to remote machine
26
+ INFO Oct 17 02:03:39.102 [1]: Closed connection between local client (fd:5) and remote client (fd:6)
27
+ CONNECT Oct 17 02:03:39.104 [1]: Connect (file descriptor 5): 172.31.0.3
28
+ CONNECT Oct 17 02:03:39.106 [1]: Request (file descriptor 5): CONNECT api.anthropic.com:443 HTTP/1.1
29
+ INFO Oct 17 02:03:39.108 [1]: No upstream proxy for api.anthropic.com
30
+ INFO Oct 17 02:03:39.111 [1]: opensock: opening connection to api.anthropic.com:443
31
+ INFO Oct 17 02:03:39.114 [1]: opensock: getaddrinfo returned for api.anthropic.com:443
32
+ CONNECT Oct 17 02:03:39.120 [1]: Established connection to host "api.anthropic.com" using file descriptor 6.
33
+ INFO Oct 17 02:03:39.121 [1]: Not sending client headers to remote machine
34
+ INFO Oct 17 02:03:39.260 [1]: Closed connection between local client (fd:7) and remote client (fd:8)
35
+ CONNECT Oct 17 02:04:04.197 [1]: Connect (file descriptor 7): 172.31.0.3
36
+ CONNECT Oct 17 02:04:04.202 [1]: Request (file descriptor 7): CONNECT api.anthropic.com:443 HTTP/1.1
37
+ INFO Oct 17 02:04:04.204 [1]: No upstream proxy for api.anthropic.com
38
+ INFO Oct 17 02:04:04.205 [1]: opensock: opening connection to api.anthropic.com:443
39
+ INFO Oct 17 02:04:04.210 [1]: opensock: getaddrinfo returned for api.anthropic.com:443
40
+ CONNECT Oct 17 02:04:04.216 [1]: Established connection to host "api.anthropic.com" using file descriptor 8.
41
+ INFO Oct 17 02:04:04.218 [1]: Not sending client headers to remote machine
42
+ ERROR Oct 17 02:08:39.172 [1]: read_buffer: read() failed on fd 7: Connection reset by peer
43
+ INFO Oct 17 02:08:39.177 [1]: Closed connection between local client (fd:7) and remote client (fd:8)
44
+ ERROR Oct 17 02:36:40.091 [1]: read_buffer: read() failed on fd 5: Connection reset by peer
45
+ INFO Oct 17 02:36:40.093 [1]: Closed connection between local client (fd:5) and remote client (fd:6)
v2-models-effort/runs/20261017T020314Z_49fc1c31/run.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "params": {
3
+ "source-language": "javascript",
4
+ "model": "claude-opus-5-5",
5
+ "effort": "xhigh"
6
+ },
7
+ "created": "2026-10-17T02:03:14Z",
8
+ "ended": "2026-10-17T02:39:33Z",
9
+ "exit_code": 137
10
+ }
v2-models-effort/runs/20261017T020314Z_49fc1c31/stderr.txt ADDED
File without changes
v2-models-effort/runs/20261017T020314Z_49fc1c31/transcript/.claude/projects/-target/40c7e6d4-ddda-4b53-8838-a4ced124132f.jsonl ADDED
The diff for this file is too large to render. See raw diff
 
v2-models-effort/runs/20261017T020314Z_8560337c/manifest.json ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "timestamp": "2026-10-17T02:03:14Z",
3
+ "completed_at": "2026-10-17T02:43:30Z",
4
+ "exit_code": 0,
5
+ "study": "v2-model-effort",
6
+ "condition": {
7
+ "name": "source-javascript_unit-tests_source-integration-tests_effort-max_model-claude-fable-5-1",
8
+ "source_language": "javascript",
9
+ "target_language": "python",
10
+ "include_unit_tests": true,
11
+ "include_source_integration_tests": true,
12
+ "include_target_integration_tests": false,
13
+ "effort": "max",
14
+ "model": "claude-fable-5-1"
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/integration-tests/.gitignore",
26
+ "javascript/integration-tests/generated/iteration/grammars.test.ts",
27
+ "javascript/integration-tests/generated/iteration/iteration-with-additional-strings.test.ts",
28
+ "javascript/integration-tests/generated/iteration/iteration-with-an-initial-string.test.ts",
29
+ "javascript/integration-tests/generated/iteration/iteration.test.ts",
30
+ "javascript/integration-tests/generated/validation/validate-grammar.test.ts",
31
+ "javascript/integration-tests/generated/validation/validate-input.test.ts",
32
+ "javascript/package.json",
33
+ "javascript/src/gbnf.test.ts",
34
+ "javascript/src/gbnf.ts",
35
+ "javascript/src/grammar-graph/colorize.test.ts",
36
+ "javascript/src/grammar-graph/colorize.ts",
37
+ "javascript/src/grammar-graph/generic-set.test.ts",
38
+ "javascript/src/grammar-graph/generic-set.ts",
39
+ "javascript/src/grammar-graph/get-input-as-code-points.test.ts",
40
+ "javascript/src/grammar-graph/get-input-as-code-points.ts",
41
+ "javascript/src/grammar-graph/get-parent-stack-id.test.ts",
42
+ "javascript/src/grammar-graph/get-parent-stack-id.ts",
43
+ "javascript/src/grammar-graph/get-serialized-rule-key.test.ts",
44
+ "javascript/src/grammar-graph/get-serialized-rule-key.ts",
45
+ "javascript/src/grammar-graph/graph-node.test.ts",
46
+ "javascript/src/grammar-graph/graph-node.ts",
47
+ "javascript/src/grammar-graph/graph-pointer.test.ts",
48
+ "javascript/src/grammar-graph/graph-pointer.ts",
49
+ "javascript/src/grammar-graph/graph.test.ts",
50
+ "javascript/src/grammar-graph/graph.ts",
51
+ "javascript/src/grammar-graph/index.ts",
52
+ "javascript/src/grammar-graph/parse-state.test.ts",
53
+ "javascript/src/grammar-graph/parse-state.ts",
54
+ "javascript/src/grammar-graph/print.test.ts",
55
+ "javascript/src/grammar-graph/print.ts",
56
+ "javascript/src/grammar-graph/rule-ref.test.ts",
57
+ "javascript/src/grammar-graph/rule-ref.ts",
58
+ "javascript/src/grammar-graph/type-guards.test.ts",
59
+ "javascript/src/grammar-graph/type-guards.ts",
60
+ "javascript/src/grammar-graph/types.ts",
61
+ "javascript/src/grammar-parser/build-rule-stack.test.ts",
62
+ "javascript/src/grammar-parser/build-rule-stack.ts",
63
+ "javascript/src/index.ts",
64
+ "javascript/src/rules-builder/index.ts",
65
+ "javascript/src/rules-builder/is-word-char.test.ts",
66
+ "javascript/src/rules-builder/is-word-char.ts",
67
+ "javascript/src/rules-builder/parse-char.test.ts",
68
+ "javascript/src/rules-builder/parse-char.ts",
69
+ "javascript/src/rules-builder/parse-name.test.ts",
70
+ "javascript/src/rules-builder/parse-name.ts",
71
+ "javascript/src/rules-builder/parse-space.test.ts",
72
+ "javascript/src/rules-builder/parse-space.ts",
73
+ "javascript/src/rules-builder/rules-builder.test.ts",
74
+ "javascript/src/rules-builder/rules-builder.ts",
75
+ "javascript/src/rules-builder/symbol-ids.ts",
76
+ "javascript/src/rules-builder/type-guards.ts",
77
+ "javascript/src/rules-builder/types.ts",
78
+ "javascript/src/umd.ts",
79
+ "javascript/src/utils/errors/build-error-position.test.ts",
80
+ "javascript/src/utils/errors/build-error-position.ts",
81
+ "javascript/src/utils/errors/get-input-as-string.test.ts",
82
+ "javascript/src/utils/errors/get-input-as-string.ts",
83
+ "javascript/src/utils/errors/grammar-parse-error.test.ts",
84
+ "javascript/src/utils/errors/grammar-parse-error.ts",
85
+ "javascript/src/utils/errors/input-parse-error.test.ts",
86
+ "javascript/src/utils/errors/input-parse-error.ts",
87
+ "javascript/src/utils/is-point-in-range.test.ts",
88
+ "javascript/src/utils/is-point-in-range.ts",
89
+ "javascript/tsconfig.json",
90
+ "javascript/tsconfig.test.json",
91
+ "javascript/vite.config.esm.ts",
92
+ "javascript/vite.config.ts",
93
+ "javascript/vite.config.umd.ts",
94
+ "javascript/vitest.config.integration.ts",
95
+ "javascript/vitest.config.unit.ts"
96
+ ]
97
+ },
98
+ "sandbox": {
99
+ "image_id": "sha256:088291406783b9b68bad65a1f6a50f2c44d048c5f13a4d13c3d02931e1fcf314"
100
+ },
101
+ "harness": {
102
+ "commit": "9e24e21259430f5e40bca73c392757016eda7cda",
103
+ "dirty": true
104
+ }
105
+ }
v2-models-effort/runs/20261017T020314Z_8560337c/ported_implementation/.gitignore ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ __pycache__/
2
+ *.py[cod]
3
+ .pytest_cache/
4
+ .ruff_cache/
5
+ .mypy_cache/
6
+ build/
7
+ dist/
8
+ *.egg-info/
9
+ .venv/
v2-models-effort/runs/20261017T020314Z_8560337c/ported_implementation/README.md ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # GBNF
2
+
3
+ A library for parsing `.gbnf` grammar files in Python.
4
+
5
+ This is a Python port of the [`gbnf`](https://www.npmjs.com/package/gbnf) JavaScript
6
+ package from [ambient-labs/GBNF](https://github.com/ambient-labs/GBNF). It keeps the
7
+ same algorithm, module layout, and test suite.
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ pip install gbnf
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ Pass your grammar to `GBNF`:
18
+
19
+ ```python
20
+ from gbnf import GBNF
21
+
22
+ state = GBNF("""
23
+ root ::= "yes" | "no"
24
+ """)
25
+ ```
26
+
27
+ If the grammar is invalid, `GBNF` raises a `GrammarParseError`.
28
+
29
+ `GBNF` returns a state representing the parsed state:
30
+
31
+ ```python
32
+ from gbnf import GBNF
33
+
34
+ state = GBNF("""
35
+ root ::= "yes" | "no"
36
+ """)
37
+ for rule in state:
38
+ print(rule)
39
+ # {'type': 'char', 'value': [121]} # ord("y")
40
+ # {'type': 'char', 'value': [110]} # ord("n")
41
+ ```
42
+
43
+ `state` can be iterated over. (You can also call the iterator method directly with
44
+ `state.rules()`.) `state` cannot be indexed directly, but can easily be cast to a list
45
+ with `list(state)` and indexed that way.
46
+
47
+ States are _immutable_. To parse a new token, call `state.add()`:
48
+
49
+ ```python
50
+ from gbnf import GBNF
51
+
52
+ state = GBNF("""
53
+ root ::= "I like green eggs and ham"
54
+ """)
55
+ print(list(state)) # [{'type': 'char', 'value': [73]}] # ord("I")
56
+ state = state.add("I li")
57
+ print(list(state)) # [{'type': 'char', 'value': [107]}] # ord("k")
58
+ state = state.add("ke gree")
59
+ print(list(state)) # [{'type': 'char', 'value': [110]}] # ord("n")
60
+ ```
61
+
62
+ If the input does not conform to the grammar, `add` raises an `InputParseError`.
63
+ A state is also callable, so `state("ke gree")` is the same as `state.add("ke gree")`.
64
+
65
+ Input may be a string, a single code point (an `int`), or a sequence of code points.
66
+
67
+ The possible rules returned include:
68
+
69
+ - `char` (`RuleType.CHAR`) - contains a list of either numbers representing code points
70
+ to match, _or_ a two-element list denoting a range within which a code point may appear.
71
+ - `char_exclude` (`RuleType.CHAR_EXCLUDE`) - contains a list of numbers representing
72
+ code points _not_ to match, _or_ a two-element list denoting a range within which a
73
+ code point may _not_ appear.
74
+ - `end` (`RuleType.END`) - denotes a valid end of a string.
75
+
76
+ Rules are plain dictionaries with a `type` key and, for the character rules, a `value`
77
+ key. `RuleType` is a `str` enum, so `rule["type"] == "char"` and
78
+ `rule["type"] == RuleType.CHAR` are both true.
79
+
80
+ ## Differences from the JavaScript package
81
+
82
+ The port is behavior-for-behavior equivalent for every case in the shared test suite.
83
+ A few deliberate differences exist:
84
+
85
+ - Input strings are handled as Unicode code points rather than UTF-16 code units, so a
86
+ character outside the Basic Multilingual Plane is one code point, matching the `\U`
87
+ escape in a grammar.
88
+ - A grammar without a `root` rule raises a `GrammarParseError` listing the available
89
+ symbols. The JavaScript version raises a generic error from its symbol table.
90
+ - A malformed hexadecimal escape such as `"\xZZ"` raises a `GrammarParseError` instead
91
+ of silently producing a `NaN` code point.
92
+ - The graph is walked with an explicit stack instead of recursion, so arbitrarily long
93
+ inputs do not hit Python's recursion limit. A left-recursive grammar, which would
94
+ overflow the stack in JavaScript, raises a `RecursionError` with an explanation.
95
+ - `GrammarParseError` and `InputParseError` subclass `ValueError`.
96
+
97
+ ## Development
98
+
99
+ Run the test suite with:
100
+
101
+ ```bash
102
+ pytest
103
+ ```
104
+
105
+ The tests under `tests/integration/generated` are ported from the generated JavaScript
106
+ integration tests and use the same cases; the unit tests under `tests/unit` mirror the
107
+ colocated `*.test.ts` files of the original source.
v2-models-effort/runs/20261017T020314Z_8560337c/ported_implementation/gbnf/__init__.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """A library for parsing GBNF grammars."""
2
+
3
+ from .gbnf import GBNF
4
+ from .grammar_graph.parse_state import ParseState
5
+ from .grammar_graph.type_guards import is_range
6
+ from .grammar_graph.types import (
7
+ Range,
8
+ ResolvedRule as Rule,
9
+ RuleChar,
10
+ RuleCharExclude,
11
+ RuleEnd,
12
+ RuleType,
13
+ )
14
+ from .utils.errors.grammar_parse_error import GrammarParseError
15
+ from .utils.errors.input_parse_error import InputParseError
16
+
17
+ __version__ = "0.1.41"
18
+
19
+ __all__ = [
20
+ "GBNF",
21
+ "GrammarParseError",
22
+ "InputParseError",
23
+ "ParseState",
24
+ "Range",
25
+ "Rule",
26
+ "RuleChar",
27
+ "RuleCharExclude",
28
+ "RuleEnd",
29
+ "RuleType",
30
+ "__version__",
31
+ "is_range",
32
+ ]
v2-models-effort/runs/20261017T020314Z_8560337c/ported_implementation/gbnf/gbnf.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ from typing import Protocol, Union
5
+
6
+ from .grammar_graph.graph import Graph
7
+ from .grammar_graph.parse_state import ParseState
8
+ from .grammar_graph.types import UnresolvedRule, ValidInput
9
+ from .grammar_parser.build_rule_stack import build_rule_stack
10
+ from .rules_builder import RulesBuilder
11
+ from .utils.errors.grammar_parse_error import GrammarParseError
12
+
13
+
14
+ class SupportsStr(Protocol):
15
+ def __str__(self) -> str: ...
16
+
17
+
18
+ def GBNF(input: Union[str, SupportsStr], initial_string: ValidInput = "") -> ParseState: # noqa: N802
19
+ """Parse a GBNF grammar and return the parse state after consuming ``initial_string``.
20
+
21
+ Raises ``GrammarParseError`` if the grammar is invalid and ``InputParseError``
22
+ if ``initial_string`` does not conform to the grammar.
23
+ """
24
+ grammar = input if isinstance(input, str) else str(input)
25
+ builder = RulesBuilder(grammar)
26
+ rules, symbol_ids = builder.rules, builder.symbol_ids
27
+ if len(rules) == 0:
28
+ raise GrammarParseError(grammar, 0, "No rules were found")
29
+ if not symbol_ids.has("root"):
30
+ raise GrammarParseError(
31
+ grammar,
32
+ 0,
33
+ "Grammar does not contain a root symbol. "
34
+ f"Available symbols are: {json.dumps(list(symbol_ids.keys()))}",
35
+ )
36
+ root_id = symbol_ids.get("root")
37
+
38
+ stacked_rules: list[list[list[UnresolvedRule]]] = [build_rule_stack(rule) for rule in rules]
39
+ graph = Graph(grammar, stacked_rules, root_id)
40
+ return ParseState(graph, graph.add(initial_string))
v2-models-effort/runs/20261017T020314Z_8560337c/ported_implementation/gbnf/grammar_graph/__init__.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ from .graph import Graph
2
+
3
+ __all__ = ["Graph"]
v2-models-effort/runs/20261017T020314Z_8560337c/ported_implementation/gbnf/grammar_graph/colorize.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from collections.abc import Callable
4
+ from enum import StrEnum
5
+ from typing import Union
6
+
7
+
8
+ class Color(StrEnum):
9
+ BLUE = "\x1b[34m"
10
+ CYAN = "\x1b[36m"
11
+ GREEN = "\x1b[32m"
12
+ RED = "\x1b[31m"
13
+ GRAY = "\x1b[90m"
14
+ YELLOW = "\x1b[33m"
15
+
16
+
17
+ def colorize(value: Union[str, int], color: Color) -> str:
18
+ return f"{color}{value}"
19
+
20
+
21
+ Colorize = Callable[[Union[str, int], Color], str]
v2-models-effort/runs/20261017T020314Z_8560337c/ported_implementation/gbnf/grammar_graph/generic_set.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from collections.abc import Callable, Iterator
4
+ from typing import Generic, TypeVar
5
+
6
+ T = TypeVar("T")
7
+ K = TypeVar("K")
8
+
9
+ _MISSING = object()
10
+
11
+
12
+ class GenericSet(Generic[T, K]):
13
+ """An insertion-ordered set that deduplicates elements by a derived key."""
14
+
15
+ def __init__(self, get_key: Callable[[T], K]) -> None:
16
+ self.get_key = get_key
17
+ self._keys: dict[K, T] = {}
18
+
19
+ def add(self, el: T) -> None:
20
+ key = self.get_key(el)
21
+ if key not in self._keys:
22
+ self._keys[key] = el
23
+
24
+ def delete(self, el: T) -> None:
25
+ key = self.get_key(el)
26
+ if key not in self._keys:
27
+ raise RuntimeError("Could not get ref")
28
+ del self._keys[key]
29
+
30
+ def has(self, el: T) -> bool:
31
+ stored = self._keys.get(self.get_key(el), _MISSING)
32
+ return stored is el or (stored is not _MISSING and stored == el)
33
+
34
+ def __contains__(self, el: object) -> bool:
35
+ return self.has(el) # type: ignore[arg-type]
36
+
37
+ def get(self, el: T) -> T | None:
38
+ return self._keys.get(self.get_key(el))
39
+
40
+ def __iter__(self) -> Iterator[T]:
41
+ yield from self._keys.values()
42
+
43
+ @property
44
+ def size(self) -> int:
45
+ return len(self._keys)
46
+
47
+ def __len__(self) -> int:
48
+ return len(self._keys)
v2-models-effort/runs/20261017T020314Z_8560337c/ported_implementation/gbnf/grammar_graph/get_input_as_code_points.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from .types import ValidInput
4
+
5
+
6
+ def get_input_as_code_points(src: ValidInput) -> list[int]:
7
+ """Normalize a string, a code point, or a sequence of code points to a list of code points."""
8
+ if isinstance(src, str):
9
+ return [ord(char) for char in src]
10
+ if isinstance(src, int):
11
+ return [src]
12
+ return list(src)
v2-models-effort/runs/20261017T020314Z_8560337c/ported_implementation/gbnf/grammar_graph/get_parent_stack_id.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ from .colorize import Color, Colorize
6
+
7
+ if TYPE_CHECKING:
8
+ from .graph_pointer import GraphPointer
9
+
10
+
11
+ def get_parent_stack_id(pointer: GraphPointer, col: Colorize) -> str:
12
+ stack_ids: list[str] = []
13
+ parent: GraphPointer | None = pointer.parent
14
+ while parent is not None:
15
+ meta = parent.node.meta
16
+ stack_ids.append(f"{meta.stack_id},{meta.path_id},{meta.step_id}")
17
+ parent = parent.parent
18
+ return col("<-", Color.GRAY).join(col(stack_id, Color.RED) for stack_id in stack_ids)
v2-models-effort/runs/20261017T020314Z_8560337c/ported_implementation/gbnf/grammar_graph/get_serialized_rule_key.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from ..utils.json_stringify import json_stringify
4
+ from .type_guards import is_rule_char, is_rule_char_excluded, is_rule_end, is_rule_ref
5
+ from .types import RuleType, UnresolvedRule
6
+
7
+ KEY_TRANSLATION = {
8
+ RuleType.END: 0,
9
+ RuleType.CHAR: 1,
10
+ RuleType.CHAR_EXCLUDE: 2,
11
+ }
12
+
13
+
14
+ def get_serialized_rule_key(rule: UnresolvedRule) -> str:
15
+ """A string key that is identical for structurally identical rules."""
16
+ if is_rule_end(rule):
17
+ return f"{KEY_TRANSLATION[RuleType.END]}"
18
+
19
+ if is_rule_char(rule) or is_rule_char_excluded(rule):
20
+ return f"{KEY_TRANSLATION[rule['type']]}-{json_stringify(rule['value'])}"
21
+ if is_rule_ref(rule):
22
+ return f"3-{rule.value}"
23
+ raise ValueError(f"Unknown rule type: {json_stringify(rule)}")