thekevinscott commited on
Commit
fa33e25
·
verified ·
1 Parent(s): f3b74d7

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

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/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/rules_builder/is_word_char.py +8 -0
  2. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/rules_builder/parse_char.py +41 -0
  3. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/rules_builder/parse_name.py +26 -0
  4. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/rules_builder/parse_space.py +16 -0
  5. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/rules_builder/rules_builder.py +200 -0
  6. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/rules_builder/symbol_ids.py +56 -0
  7. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/rules_builder/type_guards.py +55 -0
  8. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/rules_builder/types.py +56 -0
  9. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/utils/__init__.py +0 -0
  10. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/utils/char_at.py +8 -0
  11. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/utils/errors/__init__.py +0 -0
  12. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/utils/errors/build_error_position.py +25 -0
  13. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/utils/errors/get_input_as_string.py +14 -0
  14. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/utils/errors/grammar_parse_error.py +25 -0
  15. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/utils/errors/input_parse_error.py +54 -0
  16. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/utils/is_point_in_range.py +7 -0
  17. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/utils/stringify.py +11 -0
  18. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/pyproject.toml +35 -0
  19. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/__init__.py +0 -0
  20. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/integration/__init__.py +0 -0
  21. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/integration/iteration/__init__.py +0 -0
  22. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/integration/iteration/test_grammars.py +729 -0
  23. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/integration/iteration/test_iteration.py +53 -0
  24. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/integration/iteration/test_iteration_with_additional_strings.py +245 -0
  25. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/integration/iteration/test_iteration_with_an_initial_string.py +221 -0
  26. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/integration/validation/__init__.py +0 -0
  27. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/integration/validation/test_validate_grammar.py +51 -0
  28. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/integration/validation/test_validate_input.py +126 -0
  29. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/__init__.py +0 -0
  30. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/__init__.py +0 -0
  31. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_colorize.py +19 -0
  32. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_generic_set.py +49 -0
  33. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_get_input_as_code_points.py +14 -0
  34. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_get_parent_stack_id.py +52 -0
  35. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_get_serialized_rule_key.py +32 -0
  36. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_graph.py +102 -0
  37. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_graph_node.py +41 -0
  38. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_graph_pointer.py +120 -0
  39. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_parse_state.py +62 -0
  40. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_print.py +70 -0
  41. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_rule_ref.py +33 -0
  42. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_type_guards.py +126 -0
  43. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_parser/__init__.py +0 -0
  44. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_parser/test_build_rule_stack.py +371 -0
  45. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/rules_builder/__init__.py +0 -0
  46. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/rules_builder/test_is_word_char.py +24 -0
  47. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/rules_builder/test_parse_char.py +49 -0
  48. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/rules_builder/test_parse_name.py +47 -0
  49. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/rules_builder/test_parse_space.py +50 -0
  50. v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/rules_builder/test_rules_builder.py +1380 -0
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/rules_builder/is_word_char.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ import re
2
+
3
+ _WORD_CHAR = re.compile(r"[a-zA-Z]")
4
+
5
+
6
+ def is_word_char(c: str) -> bool:
7
+ """Return whether ``c`` is an ASCII letter (the characters that may start a rule name)."""
8
+ return _WORD_CHAR.search(c) is not None
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/rules_builder/parse_char.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import string
2
+
3
+ from ..utils.char_at import char_at
4
+ from ..utils.errors.grammar_parse_error import GrammarParseError
5
+
6
+
7
+ def _parse_hex(src: str, pos: int, start: int, length: int) -> int:
8
+ """Parse exactly ``length`` hex digits of ``src`` beginning at ``start``."""
9
+ digits = src[start : start + length]
10
+ if len(digits) != length or any(digit not in string.hexdigits for digit in digits):
11
+ raise GrammarParseError(src, pos, f"Expecting {length} hex chars at {start}")
12
+ return int(digits, 16)
13
+
14
+
15
+ def parse_char(src: str, pos: int) -> tuple[int, int]:
16
+ """Parse one (possibly escaped) character of a grammar at ``pos``.
17
+
18
+ Returns the code point and the number of source characters consumed.
19
+ """
20
+ if char_at(src, pos) == "\\":
21
+ match char_at(src, pos + 1):
22
+ case "x":
23
+ return _parse_hex(src, pos, pos + 2, 2), 4
24
+ case "u":
25
+ return _parse_hex(src, pos, pos + 2, 4), 6
26
+ case "U":
27
+ return _parse_hex(src, pos, pos + 2, 8), 10
28
+ case "t":
29
+ return ord("\t"), 2
30
+ case "r":
31
+ return ord("\r"), 2
32
+ case "n":
33
+ return ord("\n"), 2
34
+ case '"' | "[" | "]" | "\\":
35
+ return ord(src[pos + 1]), 2
36
+ case _:
37
+ raise GrammarParseError(src, pos, f"Unknown escape at {src[pos]}")
38
+
39
+ if not char_at(src, pos):
40
+ raise GrammarParseError(src, pos, "Unexpected end of grammar input, failed to complete parse")
41
+ return ord(src[pos]), 1
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/rules_builder/parse_name.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+
3
+ from ..utils.errors.grammar_parse_error import GrammarParseError
4
+
5
+ PARSE_NAME_ERROR = "Failed to find a valid name"
6
+ INVALID_CHAR_ERROR = 'Invalid character "{char}" when parsing name, only lowercase letters and hyphens are allowed.'
7
+
8
+ _VALID_CHAR = re.compile(r"[a-zA-Z-]")
9
+ _INVALID_NEXT_CHAR = re.compile(r"[_0-9]")
10
+
11
+
12
+ def parse_name(grammar: str, pos: int) -> str:
13
+ """Read a rule name (letters and hyphens) starting at ``pos``.
14
+
15
+ Raises ``GrammarParseError`` if no name is found, or if the name is directly
16
+ followed by a digit or underscore.
17
+ """
18
+ name = ""
19
+ while pos < len(grammar) and _VALID_CHAR.match(grammar[pos]):
20
+ name += grammar[pos]
21
+ pos += 1
22
+ if not name:
23
+ raise GrammarParseError(grammar, pos, PARSE_NAME_ERROR)
24
+ if pos < len(grammar) and _INVALID_NEXT_CHAR.match(grammar[pos]):
25
+ raise GrammarParseError(grammar, pos, INVALID_CHAR_ERROR.format(char=grammar[pos]))
26
+ return name
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/rules_builder/parse_space.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ..utils.char_at import char_at
2
+
3
+
4
+ def parse_space(src: str, pos: int, newline_ok: bool) -> int:
5
+ """Advance ``pos`` past spaces, tabs and ``#`` comments.
6
+
7
+ Newlines are only skipped when ``newline_ok`` is true; a comment always runs
8
+ to the end of its line.
9
+ """
10
+ while char_at(src, pos) in (" ", "\t", "#") or (newline_ok and char_at(src, pos) in ("\r", "\n")):
11
+ if src[pos] == "#":
12
+ while char_at(src, pos) and src[pos] not in ("\r", "\n"):
13
+ pos += 1
14
+ else:
15
+ pos += 1
16
+ return pos
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/rules_builder/rules_builder.py ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import time
3
+
4
+ from ..utils.char_at import char_at
5
+ from ..utils.errors.grammar_parse_error import GrammarParseError
6
+ from .is_word_char import is_word_char
7
+ from .parse_char import parse_char
8
+ from .parse_name import parse_name
9
+ from .parse_space import parse_space
10
+ from .symbol_ids import SymbolIds
11
+ from .types import InternalRuleDef, InternalRuleType
12
+
13
+ _WHITESPACE = re.compile(r"\s")
14
+
15
+
16
+ class RulesBuilder:
17
+ """Parse GBNF grammar text into linear rule definitions.
18
+
19
+ After construction, ``rules`` holds one list of ``InternalRuleDef`` elements
20
+ per symbol id, and ``symbol_ids`` maps rule names to those ids. Repetition
21
+ operators (``*``, ``+``, ``?``) and parenthesised groups are expanded into
22
+ generated helper rules named ``<rule>_<id>``.
23
+
24
+ ``limit`` bounds the parse duration in milliseconds.
25
+ """
26
+
27
+ def __init__(self, src: str, limit: float = 1000) -> None:
28
+ self._pos = 0
29
+ self.symbol_ids = SymbolIds()
30
+ self.rules: list[list[InternalRuleDef]] = []
31
+ self.src = src
32
+ self.start = time.perf_counter()
33
+ self._time_limit = limit
34
+ self._parse(src)
35
+
36
+ @property
37
+ def pos(self) -> int:
38
+ """The current cursor position within ``src``."""
39
+ return self._pos
40
+
41
+ def _parse(self, src: str) -> None:
42
+ self._pos = parse_space(src, 0, True) # move cursor forward until we reach non-whitespace content
43
+
44
+ while self._pos < len(src):
45
+ self._parse_rule(src)
46
+
47
+ # Validate the state to ensure that all rules are defined
48
+ for rule in self.rules:
49
+ for elem in rule:
50
+ if elem["type"] == InternalRuleType.RULE_REF:
51
+ # Ensure that the rule at that location exists
52
+ rule_exists = elem["value"] < len(self.rules) and len(self.rules[elem["value"]]) > 0
53
+ if not rule_exists:
54
+ missing_rule_name = self.symbol_ids.reverse_get(elem["value"])
55
+ missing_rule_pos = self.symbol_ids.get_pos(missing_rule_name)
56
+
57
+ # Skip over the ::= and any whitespace
58
+ while missing_rule_pos < len(src) and (
59
+ src[missing_rule_pos] in (":", "=") or _WHITESPACE.match(src[missing_rule_pos])
60
+ ):
61
+ missing_rule_pos += 1
62
+
63
+ raise GrammarParseError(
64
+ src, missing_rule_pos, f'Undefined rule identifier "{missing_rule_name}"'
65
+ )
66
+
67
+ def _parse_rule(self, src: str) -> None:
68
+ name = parse_name(src, self._pos)
69
+ self._pos = parse_space(src, self._pos + len(name), False)
70
+ rule_id = self.get_symbol_id(name)
71
+
72
+ # Skip over whitespace characters and find the ::= sequence
73
+ self._pos = parse_space(src, self._pos, True)
74
+ if not src.startswith("::=", self._pos):
75
+ raise GrammarParseError(src, self._pos, f"Expecting ::= at {self._pos}")
76
+ self._pos = parse_space(src, self._pos + 3, True)
77
+
78
+ self.parse_alternates(name, rule_id)
79
+
80
+ if char_at(src, self._pos) == "\r":
81
+ self._pos += 2 if char_at(src, self._pos + 1) == "\n" else 1
82
+ elif char_at(src, self._pos) == "\n":
83
+ self._pos += 1
84
+ elif char_at(src, self._pos):
85
+ raise GrammarParseError(src, self._pos, f"Expecting newline or end at {self._pos}")
86
+ self._pos = parse_space(src, self._pos, True)
87
+
88
+ def get_symbol_id(self, name: str) -> int:
89
+ """Return the id for ``name``, registering it at the current position if it is new."""
90
+ next_id = self.symbol_ids.size
91
+ if not self.symbol_ids.has(name):
92
+ self.symbol_ids.set(name, next_id, self._pos)
93
+ return self.symbol_ids.get(name)
94
+
95
+ def generate_symbol_id(self, base_name: str) -> int:
96
+ """Register a fresh helper symbol derived from ``base_name`` and return its id."""
97
+ next_id = self.symbol_ids.size
98
+ self.symbol_ids.set(f"{base_name}_{next_id}", next_id, self._pos)
99
+ return next_id
100
+
101
+ def add_rule(self, rule_id: int, rule: list[InternalRuleDef]) -> None:
102
+ """Store ``rule`` under ``rule_id``, growing ``rules`` as needed.
103
+
104
+ Ids are not necessarily assigned in order (a group's helper rule is
105
+ completed before the rule containing it), so intermediate slots are
106
+ filled with empty placeholders until their rules arrive.
107
+ """
108
+ while len(self.rules) <= rule_id:
109
+ self.rules.append([])
110
+ self.rules[rule_id] = rule
111
+
112
+ def check_duration(self) -> None:
113
+ if (time.perf_counter() - self.start) * 1000 > self._time_limit:
114
+ raise GrammarParseError(self.src, self._pos, f"duration of {self._time_limit} exceeded:")
115
+
116
+ def parse_sequence(self, rule_name: str, out_elements: list[InternalRuleDef], depth: int = 0) -> None:
117
+ """Parse one alternative of a rule body, appending its elements to ``out_elements``."""
118
+ is_nested = depth != 0
119
+ src = self.src
120
+ last_sym_start = len(out_elements)
121
+ while char_at(src, self._pos):
122
+ if src[self._pos] == '"':
123
+ self._pos += 1
124
+ last_sym_start = len(out_elements)
125
+ while char_at(src, self._pos) != '"':
126
+ self.check_duration()
127
+ value, inc_pos = parse_char(src, self._pos)
128
+ out_elements.append({"type": InternalRuleType.CHAR, "value": [value]})
129
+ self._pos += inc_pos # Adjusting pos by the length of parsed characters
130
+ self._pos = parse_space(src, self._pos + 1, is_nested)
131
+ elif src[self._pos] == "[":
132
+ self._pos += 1
133
+ start_type = InternalRuleType.CHAR
134
+ if char_at(src, self._pos) == "^":
135
+ self._pos += 1
136
+ start_type = InternalRuleType.CHAR_NOT
137
+ last_sym_start = len(out_elements)
138
+ while char_at(src, self._pos) != "]":
139
+ self.check_duration()
140
+ type_ = InternalRuleType.CHAR_ALT if last_sym_start < len(out_elements) else start_type
141
+ start_char_value, inc_pos = parse_char(src, self._pos)
142
+ self._pos += inc_pos
143
+ if type_ in (InternalRuleType.CHAR, InternalRuleType.CHAR_NOT):
144
+ out_elements.append({"type": type_, "value": [start_char_value]})
145
+ else:
146
+ out_elements.append({"type": type_, "value": start_char_value})
147
+
148
+ if char_at(src, self._pos) == "-" and char_at(src, self._pos + 1) != "]":
149
+ self._pos += 1
150
+ end_char_value, inc_pos = parse_char(src, self._pos)
151
+ out_elements.append({"type": InternalRuleType.CHAR_RNG_UPPER, "value": end_char_value})
152
+ self._pos += inc_pos
153
+ self._pos = parse_space(src, self._pos + 1, is_nested)
154
+ elif is_word_char(src[self._pos]):
155
+ name = parse_name(src, self._pos)
156
+ ref_rule_id = self.get_symbol_id(name)
157
+ self._pos += len(name)
158
+ self._pos = parse_space(src, self._pos, is_nested)
159
+
160
+ last_sym_start = len(out_elements)
161
+ out_elements.append({"type": InternalRuleType.RULE_REF, "value": ref_rule_id})
162
+ elif src[self._pos] == "(":
163
+ self._pos = parse_space(src, self._pos + 1, True)
164
+ sub_rule_id = self.generate_symbol_id(rule_name)
165
+ self.parse_alternates(rule_name, sub_rule_id, depth + 1)
166
+ last_sym_start = len(out_elements)
167
+ out_elements.append({"type": InternalRuleType.RULE_REF, "value": sub_rule_id})
168
+ if char_at(src, self._pos) != ")":
169
+ raise GrammarParseError(src, self._pos, f"Expecting ')' at {self._pos}")
170
+ self._pos = parse_space(src, self._pos + 1, is_nested)
171
+ elif src[self._pos] in ("*", "+", "?"):
172
+ if last_sym_start == len(out_elements):
173
+ raise GrammarParseError(src, self._pos, f"Expecting preceding item to */+/? at {self._pos}")
174
+ sub_rule_id = self.generate_symbol_id(rule_name)
175
+ sub_rule: list[InternalRuleDef] = out_elements[last_sym_start:]
176
+ if src[self._pos] in ("*", "+"):
177
+ sub_rule.append({"type": InternalRuleType.RULE_REF, "value": sub_rule_id})
178
+ sub_rule.append({"type": InternalRuleType.ALT})
179
+ if src[self._pos] == "+":
180
+ sub_rule.extend(out_elements[last_sym_start:])
181
+ sub_rule.append({"type": InternalRuleType.END})
182
+ self.add_rule(sub_rule_id, sub_rule)
183
+ del out_elements[last_sym_start:]
184
+ out_elements.append({"type": InternalRuleType.RULE_REF, "value": sub_rule_id})
185
+ self._pos = parse_space(src, self._pos + 1, is_nested)
186
+ else:
187
+ break
188
+
189
+ def parse_alternates(self, rule_name: str, rule_id: int, depth: int = 0) -> None:
190
+ """Parse a complete rule body (alternatives separated by ``|``) and store it under ``rule_id``."""
191
+ src = self.src
192
+ rule: list[InternalRuleDef] = []
193
+ self.parse_sequence(rule_name, rule, depth)
194
+ while char_at(src, self._pos) == "|":
195
+ self.check_duration()
196
+ rule.append({"type": InternalRuleType.ALT})
197
+ self._pos = parse_space(src, self._pos + 1, True)
198
+ self.parse_sequence(rule_name, rule, depth)
199
+ rule.append({"type": InternalRuleType.END})
200
+ self.add_rule(rule_id, rule)
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/rules_builder/symbol_ids.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from collections.abc import Iterator, KeysView
2
+
3
+
4
+ class SymbolIds:
5
+ """A bidirectional registry of rule names, their numeric ids, and source positions.
6
+
7
+ Entries are never removed, so relationships between names and ids are stable
8
+ for the lifetime of a parse.
9
+ """
10
+
11
+ def __init__(self) -> None:
12
+ self._map: dict[str, int] = {}
13
+ self._pos: dict[str, int] = {}
14
+ self._reverse_map: dict[int, str] = {}
15
+
16
+ @property
17
+ def size(self) -> int:
18
+ return len(self._map)
19
+
20
+ def keys(self) -> KeysView[str]:
21
+ return self._map.keys()
22
+
23
+ def has(self, key: str) -> bool:
24
+ return key in self._map
25
+
26
+ def get(self, key: str) -> int:
27
+ try:
28
+ return self._map[key]
29
+ except KeyError:
30
+ raise KeyError(f"SymbolIds does not contain key: {key}") from None
31
+
32
+ def reverse_get(self, key: int) -> str:
33
+ try:
34
+ return self._reverse_map[key]
35
+ except KeyError:
36
+ raise KeyError(f"SymbolIds does not contain value: {key}") from None
37
+
38
+ def get_pos(self, key: str) -> int:
39
+ try:
40
+ return self._pos[key]
41
+ except KeyError:
42
+ raise KeyError(f"SymbolIds does not contain key: {key}") from None
43
+
44
+ def set(self, key: str, value: int, pos: int) -> None:
45
+ self._map[key] = value
46
+ self._pos[key] = pos
47
+ self._reverse_map[value] = key
48
+
49
+ def __contains__(self, key: object) -> bool:
50
+ return key in self._map
51
+
52
+ def __len__(self) -> int:
53
+ return len(self._map)
54
+
55
+ def __iter__(self) -> Iterator[tuple[str, int]]:
56
+ yield from self._map.items()
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/rules_builder/type_guards.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import TypeGuard
2
+
3
+ from .types import (
4
+ InternalRuleDef,
5
+ InternalRuleDefAlt,
6
+ InternalRuleDefChar,
7
+ InternalRuleDefCharAlt,
8
+ InternalRuleDefCharNot,
9
+ InternalRuleDefCharRngUpper,
10
+ InternalRuleDefEnd,
11
+ InternalRuleDefReference,
12
+ InternalRuleType,
13
+ )
14
+
15
+ _RULE_DEF_TYPES = frozenset(rule_type.value for rule_type in InternalRuleType)
16
+
17
+
18
+ def is_rule_def_type(type_: object = None) -> TypeGuard[InternalRuleType]:
19
+ return isinstance(type_, str) and type_ in _RULE_DEF_TYPES
20
+
21
+
22
+ def is_rule_def(rule: object = None) -> TypeGuard[InternalRuleDef]:
23
+ return isinstance(rule, dict) and "type" in rule and is_rule_def_type(rule["type"])
24
+
25
+
26
+ def _has_type(rule: InternalRuleDef | None, type_: InternalRuleType) -> bool:
27
+ return rule is not None and rule.get("type") == type_
28
+
29
+
30
+ def is_rule_def_alt(rule: InternalRuleDef | None = None) -> TypeGuard[InternalRuleDefAlt]:
31
+ return _has_type(rule, InternalRuleType.ALT)
32
+
33
+
34
+ def is_rule_def_ref(rule: InternalRuleDef | None = None) -> TypeGuard[InternalRuleDefReference]:
35
+ return _has_type(rule, InternalRuleType.RULE_REF)
36
+
37
+
38
+ def is_rule_def_end(rule: InternalRuleDef | None = None) -> TypeGuard[InternalRuleDefEnd]:
39
+ return _has_type(rule, InternalRuleType.END)
40
+
41
+
42
+ def is_rule_def_char(rule: InternalRuleDef | None = None) -> TypeGuard[InternalRuleDefChar]:
43
+ return _has_type(rule, InternalRuleType.CHAR)
44
+
45
+
46
+ def is_rule_def_char_not(rule: InternalRuleDef | None = None) -> TypeGuard[InternalRuleDefCharNot]:
47
+ return _has_type(rule, InternalRuleType.CHAR_NOT)
48
+
49
+
50
+ def is_rule_def_char_alt(rule: InternalRuleDef | None = None) -> TypeGuard[InternalRuleDefCharAlt]:
51
+ return _has_type(rule, InternalRuleType.CHAR_ALT)
52
+
53
+
54
+ def is_rule_def_char_rng_upper(rule: InternalRuleDef | None = None) -> TypeGuard[InternalRuleDefCharRngUpper]:
55
+ return _has_type(rule, InternalRuleType.CHAR_RNG_UPPER)
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/rules_builder/types.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from enum import StrEnum
2
+ from typing import Literal, TypedDict
3
+
4
+
5
+ class InternalRuleType(StrEnum):
6
+ """The kinds of elements produced while parsing a grammar into linear rules."""
7
+
8
+ CHAR = "CHAR"
9
+ CHAR_RNG_UPPER = "CHAR_RNG_UPPER"
10
+ RULE_REF = "RULE_REF"
11
+ ALT = "ALT"
12
+ END = "END"
13
+
14
+ CHAR_NOT = "CHAR_NOT"
15
+ CHAR_ALT = "CHAR_ALT"
16
+
17
+
18
+ class InternalRuleDefChar(TypedDict):
19
+ type: Literal[InternalRuleType.CHAR]
20
+ value: list[int]
21
+
22
+
23
+ class InternalRuleDefCharNot(TypedDict):
24
+ type: Literal[InternalRuleType.CHAR_NOT]
25
+ value: list[int]
26
+
27
+
28
+ class InternalRuleDefCharAlt(TypedDict):
29
+ type: Literal[InternalRuleType.CHAR_ALT]
30
+ value: int
31
+
32
+
33
+ class InternalRuleDefCharRngUpper(TypedDict):
34
+ type: Literal[InternalRuleType.CHAR_RNG_UPPER]
35
+ value: int
36
+
37
+
38
+ class InternalRuleDefReference(TypedDict):
39
+ type: Literal[InternalRuleType.RULE_REF]
40
+ value: int
41
+
42
+
43
+ class InternalRuleDefAlt(TypedDict):
44
+ type: Literal[InternalRuleType.ALT]
45
+
46
+
47
+ class InternalRuleDefEnd(TypedDict):
48
+ type: Literal[InternalRuleType.END]
49
+
50
+
51
+ type InternalRuleDefWithNumericValue = InternalRuleDefReference | InternalRuleDefCharAlt | InternalRuleDefCharRngUpper
52
+ type InternalRuleDefWithoutValue = InternalRuleDefAlt | InternalRuleDefEnd
53
+ type InternalRuleDef = (
54
+ InternalRuleDefChar | InternalRuleDefCharNot | InternalRuleDefWithNumericValue | InternalRuleDefWithoutValue
55
+ )
56
+ type InternalRuleDefCharOrAltChar = InternalRuleDefChar | InternalRuleDefCharAlt
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/utils/__init__.py ADDED
File without changes
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/utils/char_at.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ def char_at(src: str, pos: int) -> str:
2
+ """Return the character of ``src`` at ``pos``, or ``""`` when out of range.
3
+
4
+ Mirrors JavaScript's ``src[pos]``, which yields ``undefined`` (falsy) past
5
+ the end of a string instead of raising, so callers can test the result for
6
+ truthiness or compare it against a character.
7
+ """
8
+ return src[pos] if 0 <= pos < len(src) else ""
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/utils/errors/__init__.py ADDED
File without changes
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/utils/errors/build_error_position.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MAXIMUM_NUMBER_OF_ERROR_LINES_TO_SHOW = 3
2
+
3
+
4
+ def build_error_position(src: str, pos: int) -> list[str]:
5
+ """Render the lines of ``src`` leading up to ``pos`` followed by a caret marker.
6
+
7
+ At most ``MAXIMUM_NUMBER_OF_ERROR_LINES_TO_SHOW`` lines are shown. Positions
8
+ are counted per line without accounting for the newline characters
9
+ themselves, matching the reference implementation.
10
+ """
11
+ if src == "":
12
+ return ["No input provided"]
13
+
14
+ lines = src.split("\n")
15
+
16
+ line_idx = 0
17
+ while line_idx < len(lines) and lines[line_idx] and pos > len(lines[line_idx]) - 1:
18
+ pos -= len(lines[line_idx])
19
+ line_idx += 1
20
+
21
+ lines_to_show = [
22
+ lines[i] if i < len(lines) else ""
23
+ for i in range(max(0, line_idx - (MAXIMUM_NUMBER_OF_ERROR_LINES_TO_SHOW - 1)), line_idx + 1)
24
+ ]
25
+ return [*lines_to_show, " " * pos + "^"]
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/utils/errors/get_input_as_string.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ if TYPE_CHECKING:
6
+ from ...grammar_graph.types import ValidInput
7
+
8
+
9
+ def get_input_as_string(src: ValidInput) -> str:
10
+ """Convert a string, a code point, or a list of code points into a string."""
11
+ if isinstance(src, str):
12
+ return src
13
+ code_points = [src] if isinstance(src, int) else src
14
+ return "".join(chr(code_point) for code_point in code_points)
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/utils/errors/grammar_parse_error.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .build_error_position import build_error_position
2
+
3
+ GRAMMAR_PARSER_ERROR_HEADER_MESSAGE = "Failed to parse grammar: {reason}"
4
+
5
+
6
+ class GrammarParseError(ValueError):
7
+ """Raised when a GBNF grammar cannot be parsed.
8
+
9
+ The message contains the reason followed by the offending grammar lines and
10
+ a caret pointing at the position of the problem.
11
+ """
12
+
13
+ def __init__(self, grammar: str, pos: int, reason: str) -> None:
14
+ super().__init__(
15
+ "\n".join(
16
+ [
17
+ GRAMMAR_PARSER_ERROR_HEADER_MESSAGE.format(reason=reason),
18
+ "",
19
+ *build_error_position(grammar, pos),
20
+ ]
21
+ )
22
+ )
23
+ self.grammar = grammar
24
+ self.pos = pos
25
+ self.reason = reason
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/utils/errors/input_parse_error.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING
4
+
5
+ from .build_error_position import build_error_position
6
+ from .get_input_as_string import get_input_as_string
7
+
8
+ if TYPE_CHECKING:
9
+ from ...grammar_graph.types import ValidInput
10
+
11
+ INPUT_PARSER_ERROR_HEADER_MESSAGE = "Failed to parse input string:"
12
+
13
+
14
+ class InputParseError(ValueError):
15
+ """Raised when input text cannot be matched against a grammar.
16
+
17
+ ``pos`` is the offset of the failing character within ``most_recent_input``;
18
+ ``previous_input`` is any text that had already been accepted, so the full
19
+ message can show the failure in context.
20
+ """
21
+
22
+ def __init__(self, most_recent_input: ValidInput, pos: int, previous_input: ValidInput = "") -> None:
23
+ self._most_recent_input = most_recent_input
24
+ self._pos = pos
25
+ self._previous_input = previous_input
26
+ previous = get_input_as_string(previous_input)
27
+ super().__init__(
28
+ "\n".join(
29
+ [
30
+ INPUT_PARSER_ERROR_HEADER_MESSAGE,
31
+ "",
32
+ *build_error_position(
33
+ f"{previous}{get_input_as_string(most_recent_input)}",
34
+ pos + len(previous),
35
+ ),
36
+ ]
37
+ )
38
+ )
39
+
40
+ @property
41
+ def src(self) -> str:
42
+ """The previously accepted input followed by the most recent input."""
43
+ return f"{get_input_as_string(self._previous_input)}{get_input_as_string(self._most_recent_input)}"
44
+
45
+ @property
46
+ def error_for_most_recent_input(self) -> str:
47
+ """The error message, positioned relative to the most recent input only."""
48
+ return "\n".join(
49
+ [
50
+ INPUT_PARSER_ERROR_HEADER_MESSAGE,
51
+ "",
52
+ *build_error_position(get_input_as_string(self._most_recent_input), self._pos),
53
+ ]
54
+ )
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/utils/is_point_in_range.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ from collections.abc import Sequence
2
+
3
+
4
+ def is_point_in_range(point: int, range_: Sequence[int]) -> bool:
5
+ """Return whether ``point`` lies within the inclusive ``[start, end]`` range."""
6
+ start, end = range_
7
+ return start <= point <= end
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/gbnf/utils/stringify.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+
3
+
4
+ def stringify(value: object) -> str:
5
+ """Serialize ``value`` compactly, the way ``JSON.stringify`` does.
6
+
7
+ Used both for human-readable error messages and for building the keys that
8
+ de-duplicate rules; objects that are not JSON serializable fall back to their
9
+ ``repr``.
10
+ """
11
+ return json.dumps(value, separators=(",", ":"), ensure_ascii=False, default=repr)
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/pyproject.toml ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "gbnf"
7
+ version = "0.1.41"
8
+ description = "A library for parsing GBNF grammars"
9
+ readme = "README.md"
10
+ license = "MIT"
11
+ requires-python = ">=3.12"
12
+ authors = [{ name = "Ambient Labs" }]
13
+ keywords = ["gbnf", "grammar", "grammar-parser"]
14
+ classifiers = [
15
+ "Programming Language :: Python :: 3",
16
+ "Programming Language :: Python :: 3 :: Only",
17
+ "Typing :: Typed",
18
+ ]
19
+ dependencies = []
20
+
21
+ [project.urls]
22
+ Repository = "https://github.com/ambient-labs/GBNF"
23
+
24
+ [dependency-groups]
25
+ test = ["pytest>=8.4", "pytest-describe>=3.0"]
26
+
27
+ [tool.hatch.build.targets.wheel]
28
+ packages = ["gbnf"]
29
+
30
+ [tool.pytest.ini_options]
31
+ testpaths = ["tests"]
32
+ pythonpath = ["."]
33
+
34
+ [tool.ruff]
35
+ line-length = 120
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/__init__.py ADDED
File without changes
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/integration/__init__.py ADDED
File without changes
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/integration/iteration/__init__.py ADDED
File without changes
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/integration/iteration/test_grammars.py ADDED
@@ -0,0 +1,729 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/integration-tests/generated/iteration/grammars.test.ts.
2
+
3
+ The data tables were extracted mechanically from the TypeScript source.
4
+ Each grammar is stored once under its name; every case names the grammar it exercises."""
5
+
6
+ import pytest
7
+
8
+ from gbnf import GBNF
9
+
10
+ GRAMMARS = {
11
+ 'arithmetic': 'root ::= (expr "=" ws term "\\n")+\\nexpr ::= term ([-+*/] term)*\\nterm ::= ident | num | "(" ws expr ")" ws\\nident ::= [a-z] [a-z0-9_]* ws\\nnum ::= [0-9]+ ws\\nws ::= [ \\t\\n]*\\n',
12
+ 'c': 'root ::= (declaration)*\\n\\ndeclaration ::= dataType identifier "(" parameter? ")" "{" statement* "}"\\n\\ndataType ::= "int" ws | "float" ws | "char" ws\\nidentifier ::= [a-zA-Z_] [a-zA-Z_0-9]*\\n\\nparameter ::= dataType identifier\\n\\nstatement ::=\\n ( dataType identifier ws "=" ws expression ";" ) |\\n ( identifier ws "=" ws expression ";" ) |\\n ( identifier ws "(" argList? ")" ";" ) |\\n ( "return" ws expression ";" ) |\\n ( "while" "(" condition ")" "{" statement* "}" ) |\\n ( "for" "(" forInit ";" ws condition ";" ws forUpdate ")" "{" statement* "}" ) |\\n ( "if" "(" condition ")" "{" statement* "}" ("else" "{" statement* "}")? ) |\\n ( singleLineComment ) |\\n ( multiLineComment )\\n\\nforInit ::= dataType identifier ws "=" ws expression | identifier ws "=" ws expression\\nforUpdate ::= identifier ws "=" ws expression\\n\\ncondition ::= expression relationOperator expression\\nrelationOperator ::= ("<=" | "<" | "==" | "!=" | ">=" | ">")\\n\\nexpression ::= term (("+" | "-") term)*\\nterm ::= factor(("*" | "/") factor)*\\n\\nfactor ::= identifier | number | unaryTerm | funcCall | parenExpression\\nunaryTerm ::= "-" factor\\nfuncCall ::= identifier "(" argList? ")"\\nparenExpression ::= "(" ws expression ws ")"\\n\\nargList ::= expression ("," ws expression)*\\n\\nnumber ::= [0-9]+\\n\\nsingleLineComment ::= "//" [^\\n]* "\\n"\\nmultiLineComment ::= "/*" ( [^*] | ("*" [^/]) )* "*/"\\n\\nws ::= ([ \\t\\n]+)\\n',
13
+ 'chess': '# Specifies chess moves as a list in algebraic notation, using PGN conventions\\n\\n# Force first move to "1. ", then any 1-2 digit number after, relying on model to follow the pattern\\nroot ::= "1. " move " " move "\\n" ([1-9] [0-9]? ". " move " " move "\\n")+\\nmove ::= (pawn | nonpawn | castle) [+#]?\\n\\n# piece type, optional file/rank, optional capture, dest file & rank\\nnonpawn ::= [NBKQR] [a-h]? [1-8]? "x"? [a-h] [1-8]\\n\\n# optional file & capture, dest file & rank, optional promotion\\npawn ::= ([a-h] "x")? [a-h] [1-8] ("=" [NBKQR])?\\n\\ncastle ::= "O-O" "-O"?\\n',
14
+ 'japanese': '# A probably incorrect grammar for Japanese\\nroot ::= jp-char+ ([ \\t\\n] jp-char+)*\\njp-char ::= hiragana | katakana | punctuation | cjk\\nhiragana ::= [ぁ-ゟ]\\nkatakana ::= [ァ-ヿ]\\npunctuation ::= [、-〾]\\ncjk ::= [一-鿿]\\n',
15
+ 'json-arr': '# This is the same as json.gbnf but we restrict whitespaces at the end of the root array\\n# Useful for generating JSON arrays\\n\\nroot ::= arr\\nvalue ::= object | array | string | number | ("true" | "false" | "null") ws\\n\\narr ::=\\n "[\\n" ws (\\n value\\n (",\\n" ws value)*\\n )? "]"\\n\\nobject ::=\\n "{" ws (\\n string ":" ws value\\n ("," ws string ":" ws value)*\\n )? "}" ws\\n\\narray ::=\\n "[" ws (\\n value\\n ("," ws value)*\\n )? "]" ws\\n\\nstring ::=\\n "\\"" (\\n [^"\\\\\\x7F\\x00-\\x1F] |\\n "\\\\" (["\\\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes\\n )* "\\"" ws\\n\\nnumber ::= ("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? ws\\n\\n# Optional space: by convention, applied in this grammar after literal chars when allowed\\nws ::= ([ \\t\\n] ws)?\\n',
16
+ 'json': 'root ::= object\\nvalue ::= object | array | string | number | ("true" | "false" | "null") ws\\n\\nobject ::=\\n "{" ws (\\n string ":" ws value\\n ("," ws string ":" ws value)*\\n )? "}" ws\\n\\narray ::=\\n "[" ws (\\n value\\n ("," ws value)*\\n )? "]" ws\\n\\nstring ::=\\n "\\"" (\\n [^"\\\\\\x7F\\x00-\\x1F] |\\n "\\\\" (["\\\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes\\n )* "\\"" ws\\n\\nnumber ::= ("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? ws\\n\\n# Optional space: by convention, applied in this grammar after literal chars when allowed\\nws ::= ([ \\t\\n] ws)?\\n\\n',
17
+ 'list': 'root ::= item+\\n\\n# Excludes various line break characters\\nitem ::= "- " [^\\r\\n\\x0b\\x0c\\x85\\u2028\\u2029]+ "\\n"\\n',
18
+ 'simple': 'root ::= (expr "=" term "\\n")+\\nexpr ::= term ([-+*/] term)*\\nterm ::= [0-9]+\\n',
19
+ }
20
+
21
+ CASES = [
22
+ ("arithmetic", "1"),
23
+ ("arithmetic", "1 +"),
24
+ ("arithmetic", "and"),
25
+ ("arithmetic", "1 +2"),
26
+ ("arithmetic", "1 +2+"),
27
+ ("arithmetic", "1 +2=3"),
28
+ ("arithmetic", "1 +2 +3"),
29
+ ("arithmetic", "1 +2= 3"),
30
+ ("arithmetic", "1 +2 =3"),
31
+ ("arithmetic", "1 +2 = 3"),
32
+ ("arithmetic", "1 +2*3=9"),
33
+ ("arithmetic", "1 +2+3=6"),
34
+ ("arithmetic", "1 +2 *3=9"),
35
+ ("arithmetic", "1 +2+3 = 6"),
36
+ ("arithmetic", "3 +4*5= 23"),
37
+ ("arithmetic", "1 +2*3 = 9"),
38
+ ("arithmetic", "1 +2 /3*4 ="),
39
+ ("arithmetic", "1 +2 *3 = 9"),
40
+ ("arithmetic", "2 +3 *4 =14"),
41
+ ("arithmetic", "2 +3 *4 = 14"),
42
+ ("arithmetic", "2 +5 *4 = 26"),
43
+ ("arithmetic", "(1)\\n -3 +2 *5 =\\n (2)"),
44
+ ("arithmetic", "3\\n\\n *args *nums = list"),
45
+ ("arithmetic", "3\\n \\n *hint*1 +2 *3 = 1"),
46
+ ("arithmetic", "1 +2 *3 = 9\\n \\n \\n \\n\\n2-5"),
47
+ ("arithmetic", "3\\n -2 +1\\n\\n *a *b -c = d"),
48
+ ("arithmetic", "3\\n -3\\n \\n *args -args = 0"),
49
+ ("arithmetic", "3\\n \\n *args -a\\n \\n *args -f"),
50
+ ("arithmetic", "and\\n \\n \\n \\n -2 *(1 -3) +4 = 2"),
51
+ ("arithmetic", "and\\n\\n *args -var_name\\n -1 = args0"),
52
+ ("arithmetic", "3\\n\\n *hint*(1)\\n *looks_like_this\\n *is_valid"),
53
+ ("arithmetic", "(invalid)\\n \\n *valid*(non-answer)\\n \\n *valid*(answer)"),
54
+ ("arithmetic", "(a) -2 *3\\n -4\\n \\n *4\\n -16\\n \\n *6\\n -36\\n \\n /3 \\n -3"),
55
+ ("arithmetic", "2\\n \\n *hint*1-10\\n\\n *hint*11-14\\n\\n *hint*15-17\\n\\n *hint*18-21"),
56
+ ("arithmetic", "3+7/2*5\\n \\n *(numbers) = (number1 +number2 +number3+number4 +number5)"),
57
+ (
58
+ "arithmetic",
59
+ "4\\n \\n *hint*(donot /use/operator)\\n \\n *hint*(try-using-input-function)\\n \\n *hint*(try-using-if-",
60
+ ),
61
+ (
62
+ "arithmetic",
63
+ "3\\n \\n *hint*(you *might*want*to *look*at *the*order-of-operations)\\n\\n *hint*(you *might*want*to *use*a *",
64
+ ),
65
+ (
66
+ "arithmetic",
67
+ "3\\n \\n *hint*(you *can*use *and*or)\\n\\n *hint*(you *can*use *and*or)\\n \\n *hint*(you *can*use *and*or)\\n \\n *",
68
+ ),
69
+ ("c", "i"),
70
+ ("c", "int"),
71
+ ("c", "int thati"),
72
+ ("c", "int theta(f"),
73
+ ("c", "int todo_list("),
74
+ ("c", "int to(int a){"),
75
+ ("c", "int takessinput"),
76
+ ("c", "int thatikssllll"),
77
+ ("c", "int toinflate(char s1"),
78
+ ("c", "int that(int it){iint"),
79
+ ("c", "int that(int that){}int"),
80
+ ("c", "int that(int a){int i ="),
81
+ ("c", "int that(int a){int i = 0"),
82
+ ("c", "int thatehsesgbeorhtuCcie"),
83
+ ("c", "int i3(int i2){return i2*i2;}"),
84
+ ("c", "int todo(int a){//a is the input of"),
85
+ ("c", "int that1s2n3d4n5g6r7h8o9p10a11w12b"),
86
+ ("c", "int theta(char orc){//your code here"),
87
+ ("c", "int thatlllllllllllllllllllllllllllll"),
88
+ ("c", "int thatiSslllwllllllllllllllllllllllll"),
89
+ ("c", "int thatprint(){printf (fputs(array, p));}int"),
90
+ ("c", "int thatiSsolveIt(int x){return x*x;}int main"),
91
+ ("c", "char s1_arr1(int n){return (char*( ( char )-n+"),
92
+ ("c", "int thatprintoutrandomwordsandalphabet(){// i've"),
93
+ ("c", "int thatiksssttststststststst_stststststitststst"),
94
+ ("c", "int thatiksniponkimvssmhllmohlwvnfosrqxqyfqjrthmfkmn"),
95
+ ("c", "float a(int b){return (b+1);}float z(int y){return (( ( float"),
96
+ ("c", "int thatsntttinglytough(char\\n isnttnglytough){}int main(){char"),
97
+ ("c", "char\\n\\nAqeel123456erjfkehkjhkdfjklefkgfgjfgiifjgfhkfekfgdfegfgeff"),
98
+ ("c", "int main(){}int sum(int a){return a;}int product(int a){return a;}"),
99
+ ("c", "int todo(){char c = getchar();}int main(){while(1==1){int i = 0;int j"),
100
+ ("c", "int thatikssllllllllllllllllllllllllllllllllllllllllllllllllllllllllll"),
101
+ ("c", "int todo(int n){}int todo(int n){}int todo(int n){}int todo(int n){}int"),
102
+ ("c", "int thatikssllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll"),
103
+ ("c", "int\\n movzxqyhw0xmm(int I32){xmm2d0xmm_setzero_ps16_pmu_ps1_ps1_ps1_ps1_ps"),
104
+ ("c", "char\\n dvcs_xl_a2hk9y8q4o3dsf1m5n6v7uwxyz87654321jklmnpqrstuvwxyzabcdeghij"),
105
+ ("c", "int thatllprintoutthenumberoftimesthateachletterappearsinaString(char str1){"),
106
+ ("c", "int that(int that){}int that(int that){}int that(int that){}int that(int that)"),
107
+ ("c", "int main(){}char\\n HowdyaDongledoIknowthatcharliehatesprogramming(int a){char b"),
108
+ ("c", "int youroostedsirnstedssirntnsrirtrsdtrrsdsdtrrssdrtrsrdtsrrrtssrtrdsrtsrttrdtsr"),
109
+ ("c", "int main(int argc){//here\\nreturn 0;//and here\\n}int my_program(char c){//here\\nif ("),
110
+ ("c", "int thatasksforanythingbutwhatevahtmlandwhatevsomethingmoreplease(int dont){}char my"),
111
+ ("c", "int i3(int i2){return i2*i2;}int main(){int i1 = 0;for (inti2(0, 1000000000));i1 = i"),
112
+ (
113
+ "c",
114
+ "int thatprint(int a){printf\\n (a);}int main(){int i\\n = 1;do\\n (i--0);}int main(){int i\\n = 1;",
115
+ ),
116
+ (
117
+ "c",
118
+ "int that(int that){}int that(int that){}int that(int that){}int that(int that){}int that(int that){}",
119
+ ),
120
+ (
121
+ "c",
122
+ "int thatwillfindifistevennumberinaverylongintroductionstringofonehundreddigitsormoreandreturntheindexof",
123
+ ),
124
+ (
125
+ "c",
126
+ "int todo(int n){}int main(){int i = 0;int j = 1;while (j*j+i*i);}int main(){int i = 0;int j = 1;while (",
127
+ ),
128
+ (
129
+ "c",
130
+ "int thatiSsasssseeeeeeepyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy",
131
+ ),
132
+ (
133
+ "c",
134
+ "int todo(int n){}int todo(int n){}int todo(int n){}int todo(int n){}int todo(int n){}int todo(int n){}i",
135
+ ),
136
+ (
137
+ "c",
138
+ "int ainted(int n){int i = 0;while (i*i+1);}int main(){printf_s\\n (int);return 0;}int fahrenheit2celsius(",
139
+ ),
140
+ (
141
+ "c",
142
+ "int main(int argc){//argv[0] is name of file to be opened\\nreturn (0);//exit code, no errors\\n}int main(int argc){//argv[",
143
+ ),
144
+ (
145
+ "c",
146
+ "int thatcomparesstringsinmemorywithoutallocatingmorespacethannecessarytoholdbothstringsoranyotherobjectsofthesamelengthandsofwhomit",
147
+ ),
148
+ (
149
+ "c",
150
+ "int thatlllbeoawesome(int a){return (a*2);}int main(){printf\\n (iintantielloawesome(5));}int thatlllbeoawesome(int a){return (a*2);}",
151
+ ),
152
+ (
153
+ "c",
154
+ "char str1(int n){strcpy (n, str);}int n(int s){printf (s);return 0;}int main(){char str = getchar();int i = 1;while(getchar()!=10){if(i",
155
+ ),
156
+ (
157
+ "c",
158
+ "int withing(int a){}char str1(int a){}int main(){}char str2(){return 0;}int i(){return 0;}int j(){return 1;}int k(){return 2;}int l(){return",
159
+ ),
160
+ (
161
+ "c",
162
+ "int that(int that){}char\\n WritemeakillerprogramintheClanguageagaininthatagaininthatagaininthatagaininthatagaininthatagaininthatagaininthatagainint",
163
+ ),
164
+ (
165
+ "c",
166
+ "char buffer1(int length){//length is inputted by user\\n}int main(){while(true!=fscanf(fp, buffer1)){printf_stdout (buffer);}return 0;}float x0(float n){",
167
+ ),
168
+ (
169
+ "c",
170
+ "int tokkenize(char str){}int tokkenize(char str){int i = 0;while (strlen(str)--and);return str;}int tokkenize(char str){int i = 0;while (strlen(str)--and",
171
+ ),
172
+ (
173
+ "c",
174
+ "int main(){}int main(int argc){}int main(){}int main(){}int main(){}int main(){}int main(){}int main(){}int main(){}int main(){}int main(){}int main(){}int",
175
+ ),
176
+ (
177
+ "c",
178
+ "int thatusesregexpandstoragesystemsincompatibilitiesbutalsoworksinotherprogramminglanguageslikepythonorjavascriptorpython3whichhavebeenaroundawhileandaremorecomfortabletousethanCbutstillwanttoc",
179
+ ),
180
+ (
181
+ "c",
182
+ "int thatllprintoutallthewordsinafilethatstartwiththesignaturestringofmychoice(int heresone){}int theordertheyappearinthefileandthenprintoutalltheotherwordsinthefileafterthat(int heresomeotherthing)",
183
+ ),
184
+ (
185
+ "c",
186
+ "int main(){//Program start\\nint num = 0;//variable for number of characters in input string.\\nchar c = 0;//Variable for single character read from input.\\nscanf_s (input, 100);//Scan a line of user-entered text into the array",
187
+ ),
188
+ (
189
+ "c",
190
+ "int thatllmmehmmmmmlllmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm",
191
+ ),
192
+ ("chess", "1"),
193
+ ("chess", "1."),
194
+ ("chess", "1. a"),
195
+ ("chess", "1. N1x"),
196
+ ("chess", "1. a1+ a"),
197
+ ("chess", "1. N1xh1"),
198
+ ("chess", "1. a1+ a2+"),
199
+ ("chess", "1. N1xh1+ N2"),
200
+ ("chess", "1. a1+ a2+\\n2."),
201
+ ("chess", "1. a2+ b3\\n2. a"),
202
+ ("chess", "1. a2+ b3\\n2. a2"),
203
+ ("chess", "1. a1 b2\\n2. a1 b2"),
204
+ ("chess", "1. f1=N f2=N\\n2. f"),
205
+ ("chess", "1. a1 b2\\n2. a1 b2\\n3"),
206
+ ("chess", "1. a2+ b3\\n2. a2+ b4\\n3."),
207
+ ("chess", "1. a2+ b3\\n2. a2+ b3\\n3."),
208
+ ("chess", "1. N2xh3+ h4=N\\n2. N2xh3"),
209
+ ("chess", "1. N2xh3+ h4=N\\n2. N2xh3+"),
210
+ ("chess", "1. N2xh1+ N2xh2+\\n2. N2xh1"),
211
+ ("chess", "1. N2xh1+ N2xh2+\\n2. N2xh1+"),
212
+ ("chess", "1. a4 b5\\n2. a3 b4\\n3. a4 b4"),
213
+ ("chess", "1. Ref1 Ref2\\n2. Ref3 Ref4\\n3"),
214
+ ("chess", "1. a3+ c4+\\n2. a3+ b5+\\n3. d2+"),
215
+ ("chess", "1. a2+ b3\\n2. a2+ b3\\n3. a2+ b3"),
216
+ ("chess", "1. N1d2 N3f4\\n2. f1+ N2e2\\n3. f1+ N"),
217
+ ("chess", "1. N1xg1+ N2xg2+\\n2. N1xg1+ N2xg2+"),
218
+ ("chess", "1. a1+ a2+\\n2. a1+ a2+\\n3. a1+ a2+\\n4"),
219
+ ("chess", "1. N2xb3+ N4xa2\\n2. N5xb1+ N6xb2\\n3."),
220
+ ("chess", "1. a4+ a5\\n2. a4+ a6\\n3. a4+ a7\\n4. a4+"),
221
+ ("chess", "1. a4+ b3\\n2. a5+ b4\\n3. a6+ b5\\n4. a7+ b"),
222
+ ("chess", "1. a4+ a5\\n2. a4+ a6\\n3. a4+ a7\\n4. a4+ a"),
223
+ ("chess", "1. a1+ a2\\n2. b1+ b2\\n3. c1+ c2\\n4. d1+ d2"),
224
+ ("chess", "1. a1+ b2=N\\n2. c1+ d2=N\\n3. e1+ f2=N\\n4. g1+"),
225
+ ("chess", "1. N2xh2+ N3xh3\\n2. N2xh2+ N4xh4\\n3. N2xh2+ N"),
226
+ ("chess", "1. a1 b2\\n2. a1 a2\\n3. a1 b2\\n4. a1 a2\\n5. a1 b2\\n6"),
227
+ ("chess", "1. a4+ b3\\n2. a4+ b3\\n3. a4+ b3\\n4. a4+ b3\\n5. a4+ b"),
228
+ ("chess", "1. a2+ b3\\n2. a2+ b3\\n3. a2+ b3\\n4. a2+ b3\\n5. a2+ b3"),
229
+ ("chess", "1. a4=B b8=B\\n2. b3=B+ Bc4\\n3. c6=B+ Bf5\\n4. c1=B+ B"),
230
+ ("chess", "1. a2+ a3\\n2. a2+ a3+\\n3. a2+ a3+\\n4. a2+ a3+\\n5. a2+"),
231
+ ("chess", "1. a3 b2\\n2. c1 d2\\n3. e3 f4\\n4. g5 h6\\n5. a1 b1\\n6. a3"),
232
+ ("chess", "1. a1 a2\\n2. a1 b1\\n3. a1 c1\\n4. a1 d1\\n5. a1 e1\\n6. a1"),
233
+ ("chess", "1. N2h4 a3\\n2. N2h4 a3\\n3. N2h4 a3\\n4. N2h4 a3\\n5. N2h4"),
234
+ ("chess", "1. b2+ a2\\n2. c2+ d2\\n3. c4+ d4\\n4. a3+ b3\\n5. c7+ d7\\n6. a"),
235
+ ("chess", "1. N2a4+ b4\\n2. N2b3+ c2\\n3. a2 b2\\n4. a2 b3\\n5. a2 b2\\n6. a"),
236
+ ("chess", "1. N2xg3+ N4xg5+\\n2. N2xg3+ N4xg5+\\n3. N2xg3+ N4xg5+\\n4. N2"),
237
+ ("chess", "1. a1+ b2=N\\n2. a1+ b2=N\\n3. a1+ b2=N\\n4. a1+ b2=N\\n5. a1+ b2"),
238
+ ("chess", "1. N1xh1 N2xh2\\n2. h1+ h2+\\n3. h1+ h2+\\n4. h1+ h2+\\n5. h1+ h2+"),
239
+ ("chess", "1. b4 b3\\n2. a5 a4\\n3. a3 a4\\n4. a4 b4\\n5. a2 a1\\n6. a4 a3\\n7. a"),
240
+ ("chess", "1. a1+ b2\\n2. a1+ b2\\n3. a1+ b2\\n4. a1+ b2\\n5. a1+ b2\\n6. a1+ b2"),
241
+ ("chess", "1. a2+ a3\\n2. a1+ a2+\\n3. a1+ a2+\\n4. a1+ a2+\\n5. a1+ a2+\\n6. a1+"),
242
+ ("chess", "1. N2xh2+ N3xh3+\\n2. N2xh2+ N3xh3+\\n3. N2xh2+ N3xh3+\\n4. N2xh2+ N"),
243
+ ("chess", "1. a1+ a2+\\n2. b1+ b2+\\n3. c1+ c2+\\n4. d1+ d2+\\n5. e1+ e2+\\n6. f1+ f"),
244
+ ("chess", "1. a1+ a2+\\n2. c1=B+ Bxc2\\n3. a2+ bxb2=B+\\n4. a2+ bxb2=B+\\n5. a2+ b"),
245
+ ("chess", "1. a2+ b3+\\n2. a2+ b4+\\n3. a2+ b5+\\n4. a2+ b6+\\n5. a2+ b7+\\n6. a2+ b8+\\n7."),
246
+ ("chess", "1. a1+ a2=B\\n2. b1+ b2=B\\n3. c1+ c2=B\\n4. d1+ d2=B\\n5. e1+ e2=B\\n6. f1+ f"),
247
+ ("chess", "1. a2+ b2\\n2. a2+ b2\\n3. a2+ b2\\n4. a2+ b2\\n5. a2+ b2\\n6. a2+ b2\\n7. a2+ b2\\n8"),
248
+ ("chess", "1. N2xg3+ c4\\n2. a5+ b6=N\\n3. b7+ c8=N\\n4. c1+ d2=N\\n5. e3+ f4=N\\n6. g5+ h6=N"),
249
+ ("chess", "1. a1 a2\\n2. a1 a2+\\n3. a1+ a2\\n4. a1 a2=B\\n5. a1+ a2=B\\n6. b1 a1\\n7. b1 a2=B\\n8"),
250
+ ("chess", "1. N1xa1+ N2xa2\\n2. b3=N+ Nb3\\n3. N1xa1+ b3\\n4. N1xa1+ N2xa2+\\n5. N1xa1+ b3+\\n6"),
251
+ ("chess", "1. a2+ b3\\n2. a1+ b2\\n3. a1+ b2\\n4. a1+ b2\\n5. a1+ b2\\n6. a1+ b2\\n7. a1+ b2\\n8. a1"),
252
+ ("chess", "1. a2+ b2\\n2. a3+ b3\\n3. a4+ b4\\n4. a5+ b5\\n5. a6+ b6\\n6. a7+ b7\\n7. a8+ b8\\n8. a8+ b"),
253
+ (
254
+ "chess",
255
+ "1. Rea2 h5\\n2. f2 g3\\n3. h4 h4\\n4. f3 h3\\n5. h6 h8\\n6. g7 g7\\n7. g2 h4\\n8. h7 h6\\n9. h7 h",
256
+ ),
257
+ ("japanese", ""),
258
+ ("japanese", "漫"),
259
+ ("japanese", "横"),
260
+ ("japanese", "書"),
261
+ ("japanese", "江戸の"),
262
+ ("japanese", "比較美"),
263
+ ("japanese", "《玉の恋"),
264
+ ("japanese", "《美女之花"),
265
+ ("japanese", "唱歌。\\n怎么"),
266
+ ("japanese", "《美しき日本"),
267
+ ("japanese", "洋の東方の詩を"),
268
+ ("japanese", "比較美好的品牌"),
269
+ ("japanese", "涙湯したい唄がす"),
270
+ ("japanese", "江戸の詩がいつも"),
271
+ ("japanese", "《花月秋》\\n《花月"),
272
+ ("japanese", "満有のメインが「あな"),
273
+ ("japanese", "美しい日本語詩をつくる"),
274
+ ("japanese", "《花月等》\\n《黄金曲》"),
275
+ ("japanese", "和えているのは、\\n《知ら"),
276
+ ("japanese", "紅花和優惠的漫步\\n《非性"),
277
+ ("japanese", "和美味良的短歌\\n。\\n。\\n。"),
278
+ ("japanese", "嫁ちゃんの花がいただき、\\n《"),
279
+ ("japanese", "毛菜のうたが、それは美しい日本"),
280
+ ("japanese", "《花果道》\\n【笑青春】\\n【笑青春"),
281
+ ("japanese", "《花糖果》\\n《花糖果》\\n《花糖果"),
282
+ ("japanese", "ぬかりのうた しなむらんで、\\n《月"),
283
+ ("japanese", "比較美好的品牌\\n这个词是一个单词。"),
284
+ ("japanese", "。。。\\n」\\n「あなたは、お話を探して"),
285
+ ("japanese", "比較美好的品牌\\n。\\n。\\n。\\n。\\n。\\n。"),
286
+ ("japanese", "哀しい日本語詩を持つま\\n。\\n。\\n。\\n。"),
287
+ ("japanese", "山本美しい日詩\\n」\\n「おもちゃんと、そ"),
288
+ ("japanese", "和おうなへ\\n〈林英雄〉\\n〈美しい日光〉"),
289
+ ("japanese", "果にゃっちょんがま\\n。\\nただく悲しくので"),
290
+ ("japanese", "我想美国十大的曲\\n这样主要让歌手面自己可"),
291
+ ("japanese", "懸念のお風が強いほど谷中の日本詩をまとめて"),
292
+ ("japanese", "严愛華屬\\n十八年百月營用梅子錯誤 修正\\n十"),
293
+ ("japanese", "《美しい日本語詩》\\n《恋の花》\\n《風につきて"),
294
+ ("japanese", "先指一個最好的評論。\\n《研究科學和匯科文章目標"),
295
+ ("japanese", "澤島歌舞\\n。\\n。\\n。\\n「福島」。\\n「福島」。\\n「"),
296
+ ("japanese", "和おうなそれを言います。\\n知らないので、どこにもあ"),
297
+ ("japanese", "《美しい日本語詩》\\n《美しい日本語詩》\\n《美しい日"),
298
+ ("japanese", "美しい日本語詩をみた。\\n」\\n【終了】\\n「あなたは、この"),
299
+ ("japanese", "也为我想了悲症\\n来看我的夏天和春天\\n如此着高的海见这一"),
300
+ ("japanese", "華人語對咲香樂\\n經驗的拜情\\n只有一下容易思考\\n不要快速"),
301
+ ("japanese", "模板内容和古音与文字调整\\n《池沙气》。\\n【阳光流通线】"),
302
+ ("japanese", "小さな日本語の詩をつくても桜らしい。\\n今夕の日本語にある"),
303
+ ("japanese", "別詞を持っていると、それが美しい日本語です。\\n《絵文字の"),
304
+ ("japanese", "詩を大好きです。\\n这道题要求如下。\\n输入一个字符串。返回经"),
305
+ ("japanese", "《花の詩》\\n。\\n。\\n。\\n。\\n。\\n。\\n。\\n。\\n。\\n。\\n。\\n。\\n。"),
306
+ ("japanese", "农村家が重い場所は\\n《夢にしてください》\\nわげてもない空気で、"),
307
+ ("japanese", "和美花の中で、母さんは\\n《華語》を読む。\\n《香港雜誌》書店に表示して"),
308
+ ("japanese", "哉きがわかるために\\n私は、ご果しますので、これに言う屋を\\n彼女となるん"),
309
+ ("japanese", "《華人語》\\n「あなたの夢を抱きしよう」\\n「ご飯は美味に好きです。」\\n「"),
310
+ ("japanese", "。\\n」\\n「そしては、お茶を食べることです。」\\n「どうも、それより美しいなら"),
311
+ ("japanese", "梅の詩がおいしいです。\\n」\\n「美の音は音となることを愛して、\\n唄によっても"),
312
+ ("japanese", "《美しい日本語詩》\\n《麻雨の花》\\n《花を持ってる》\\n《涙がやすくなり》\\n《"),
313
+ ("japanese", "和じゃないそうにして\\n《欲しい花の詩》\\n《美しき日本語の歌》\\n《華人の国》"),
314
+ ("japanese", "満美詩《歐裕のうた》\\n〈歐裕のうた〉\\n〈これらであるつまでがいるとなり〉\\n〈"),
315
+ ("japanese", "喜びないときは、それからも話者にする\\n《細胞の言うため》\\n「憂しくても君を願っ"),
316
+ ("japanese", "桜の詩がおいしい\\n。\\n居住地に私は、\\nあなたともころを見る\\n。\\n居住地に私は、"),
317
+ ("japanese", "美しい日本語の歌をまとめて\\n。\\n。\\n。\\nどうもなるのか\\nどれだけにありますよ。\\n沙に花があるこ"),
318
+ (
319
+ "japanese",
320
+ "比較しておいたい\\n。\\n。\\n。\\n。\\n。\\n。\\n。\\n。\\n。\\n。\\n。\\n。\\n。\\n。\\n。\\n。\\n。\\n。\\n。\\n。\\n。\\n。\\n。",
321
+ ),
322
+ ("json-arr", "["),
323
+ ("json-arr", "[\\n {"),
324
+ ("json-arr", '[\\n {\\n "'),
325
+ ("json-arr", '[\\n {\\n "name'),
326
+ ("json-arr", '[\\n {\\n "key":'),
327
+ ("json-arr", '[\\n {\\n "field1":'),
328
+ ("json-arr", '[\\n {"a":"b"},\\n {}\\n]'),
329
+ ("json-arr", '[\\n {\\n "name": "John'),
330
+ ("json-arr", '[\\n { "key": "value" }\\n ]'),
331
+ ("json-arr", '[\\n { "a": 1, "b": 2 }\\n ]'),
332
+ ("json-arr", '[\\n {\\n "name":"first",'),
333
+ ("json-arr", '[\\n {\\n "name": "John",'),
334
+ ("json-arr", '[\\n {\\n "key1": "value1",'),
335
+ ("json-arr", '[\\n {\\n "name": "A",\\n "'),
336
+ ("json-arr", '[\\n {\\n "name": "John",\\n "age'),
337
+ ("json-arr", '[\\n {\\n "name": "John",\\n "age":'),
338
+ ("json-arr", '[\\n {\\n "a": "b",\\n "c": [1,2,3],'),
339
+ ("json-arr", '[\\n {\\n "name": "John",\\n "age": 30,'),
340
+ ("json-arr", '[\\n {\\n "name": "John",\\n "age": 20,'),
341
+ ("json-arr", '[\\n {\\n "key": "value",\\n "other_stuff'),
342
+ ("json-arr", '[\\n {\\n "name": "John",\\n "age": 25,\\n "'),
343
+ ("json-arr", '[\\n "a",\\n "b",\\n { "c": true },\\n null,\\n "d'),
344
+ ("json-arr", '[\\n {"name": "John", "age": 23},\\n "dog",\\n 123,\\n ["foo"],'),
345
+ ("json-arr", '[\\n {\\n "name": "John",\\n "age": 30,\\n "city":'),
346
+ ("json-arr", '[\\n {\\n "id": "1",\\n "name": "some name"\\n }\\n ]'),
347
+ ("json-arr", '[\\n { "key" : "value", "some_other_key" : "another value"},'),
348
+ ("json-arr", '[\\n {\\n "key1": "value1",\\n "key2": [\\n "value'),
349
+ ("json-arr", '[\\n {"first_name":"A", "last_name":"B"},\\n "abcde12345"\\n ]'),
350
+ ("json-arr", '[\\n {"foo":1, "bar":["a","b"]},\\n {"foo2":[0,1]},\\n "string"]'),
351
+ ("json-arr", '[\\n {\\n "a": 1,\\n "b": 2\\n },\\n {\\n "c": 3\\n }'),
352
+ ("json-arr", '[\\n { "key1": "value1", "key2": "value2" },\\n { "key3": "value3'),
353
+ ("json-arr", '[\\n {\\n "key1": "value1",\\n "key2": "value2",\\n "key3":'),
354
+ ("json-arr", '[\\n { "id": 1, "name": "Bob" },\\n { "id": 2, "name": "Alice" }\\n ]'),
355
+ ("json-arr", '[\\n {\\n "name": "John",\\n "age": 30,\\n "city": "New York"'),
356
+ ("json-arr", '[\\n {\\n "a": "1",\\n "b": "2",\\n "c": "3",\\n "d": "4",\\n "'),
357
+ ("json-arr", '[\\n {\\n "name": "Bob",\\n "age": 40,\\n "email": "bob@example.com"'),
358
+ (
359
+ "json-arr",
360
+ '[\\n {\\n "id": "1234",\\n "name": "John Doe"\\n },\\n {\\n "id": "5678",',
361
+ ),
362
+ (
363
+ "json-arr",
364
+ '[\\n {\\n "a": 1,\\n "b": 2,\\n "c": [3, 4]\\n },\\n {\\n "d": 5\\n }',
365
+ ),
366
+ (
367
+ "json-arr",
368
+ '[\\n {\\n "id": 1,\\n "name": "John Doe",\\n "age": 30\\n },\\n {\\n "id',
369
+ ),
370
+ (
371
+ "json-arr",
372
+ '[\\n {\\n "key1": "value1",\\n "key2": "value2"\\n },\\n {\\n "key3": "value',
373
+ ),
374
+ (
375
+ "json-arr",
376
+ '[\\n {\\n "key": "value",\\n "anotherKey": "anotherValue"\\n },\\n {\\n "key2": "',
377
+ ),
378
+ (
379
+ "json-arr",
380
+ '[\\n {\\n "name": "John",\\n "age": 30,\\n "city": "New York"\\n },\\n {\\n "name": "Jane',
381
+ ),
382
+ (
383
+ "json-arr",
384
+ '[\\n {\\n "name": "John",\\n "age": 30,\\n "city": "New York"\\n },\\n {\\n "name": "Jane",',
385
+ ),
386
+ (
387
+ "json-arr",
388
+ '[\\n {\\n "name": "John",\\n "age": 20,\\n "address": "123 Main Street"\\n },\\n {\\n "name":',
389
+ ),
390
+ (
391
+ "json-arr",
392
+ '[\\n {\\n "key": "value",\\n "nested_key": 2,\\n "more_keys": [\\n 3,\\n 4,\\n 11\\n ]\\n }\\n]',
393
+ ),
394
+ (
395
+ "json-arr",
396
+ '[\\n {\\n "name": "John",\\n "age": 30,\\n "city": "New York"\\n },\\n {\\n "name": "Jane",\\n "age',
397
+ ),
398
+ (
399
+ "json-arr",
400
+ '[\\n {\\n "key1": "value1",\\n "key2": "value2"\\n },\\n {\\n "key3": "value3",\\n "key4": "value4"\\n }',
401
+ ),
402
+ (
403
+ "json-arr",
404
+ '[\\n {\\n "name": "John",\\n "age": 21,\\n "friends": [\\n {\\n "name": "Dave",\\n "age": 23',
405
+ ),
406
+ (
407
+ "json-arr",
408
+ '[\\n {\\n "name": "John",\\n "age": 30,\\n "city": "New York"\\n },\\n {\\n "name": "Jane",\\n "age": 25',
409
+ ),
410
+ (
411
+ "json-arr",
412
+ '[\\n {\\n "name": "John",\\n "age": 20,\\n "city": "New York"\\n },\\n {\\n "name": "Jane",\\n "age": 25,',
413
+ ),
414
+ (
415
+ "json-arr",
416
+ '[\\n {\\n "key1": "value1",\\n "key2": "value2",\\n "key3": "value3"\\n },\\n {\\n "key4": "value4",\\n "',
417
+ ),
418
+ (
419
+ "json-arr",
420
+ '[\\n {\\n "key": "value",\\n "anotherKey": "another value"\\n },\\n {\\n "anotherKey2": "yet another value"\\n }\\n ]',
421
+ ),
422
+ (
423
+ "json-arr",
424
+ '[\\n {\\n "foo": "bar",\\n "other_info": {\\n "field": 1,\\n "array": [1],\\n "boolean": true\\n },\\n "a',
425
+ ),
426
+ (
427
+ "json-arr",
428
+ '[\\n {\\n "name": "John",\\n "age": 30,\\n "city": "New York"\\n },\\n {\\n "name": "Peter",\\n "age": 40,\\n "city": "Berlin"',
429
+ ),
430
+ (
431
+ "json-arr",
432
+ '[\\n {\\n "first_name": "Alice",\\n "last_name": "Jones",\\n "age": 25,\\n "occupation": "Engineer",\\n "address": {\\n "city": "New',
433
+ ),
434
+ (
435
+ "json-arr",
436
+ '[\\n {\\n "name": "John",\\n "age": 30,\\n "city": "New York"\\n },\\n {\\n "name": "Jane",\\n "age": 25,\\n "city": "Los Angeles"\\n }',
437
+ ),
438
+ (
439
+ "json-arr",
440
+ '[\\n {\\n "name": "John",\\n "age": 20,\\n "address": {\\n "street": "123 Main St.",\\n "city": "Anytown"\\n }\\n },\\n {\\n "name": "Jane",',
441
+ ),
442
+ ("json", "{"),
443
+ ("json", '{\\n "'),
444
+ ("json", '{\\n "first'),
445
+ ("json", '{\\n "foo":'),
446
+ ("json", '{\\n "key": "'),
447
+ ("json", '{\\n "key": "value'),
448
+ ("json", '{\\n "foo": "bar",'),
449
+ ("json", '{\\n "name": "John",'),
450
+ ("json", '{\\n "hello": "world",'),
451
+ ("json", '{\\n "key": "value",\\n "'),
452
+ ("json", '{\\n "key1": "value1",\\n "'),
453
+ ("json", '{\\n "key1": "value1",\\n "key'),
454
+ ("json", '{\\n "a": 1, "b": 2, "c": [3] }'),
455
+ ("json", '{\\n "test": "hello world",\\n "'),
456
+ ("json", '{\\n "name": "Joey",\\n "age": 32,'),
457
+ ("json", '{\\n "key1": "value1",\\n "key2": {'),
458
+ ("json", '{\\n "a": 1,\\n "b": 2,\\n "c": 3,'),
459
+ ("json", '{\\n "key": "value",\\n "other_key":'),
460
+ ("json", '{\\n "one": 1,\\n "two": 2,\\n "three'),
461
+ ("json", '{\\n "a": 1, "b": {\\n "c": 2\\n }\\n }'),
462
+ ("json", '{\\n "foo": "bar",\\n "baz": {\\n "qux":'),
463
+ ("json", '{\\n "name": "John",\\n "age": 30,\\n "city'),
464
+ ("json", '{\\n "a": 1,\\n "b": 2,\\n "c": true,\\n "'),
465
+ ("json", '{\\n "a": 1,\\n "b": 2,\\n "c": true,\\n "d'),
466
+ ("json", '{\\n "name": "John",\\n "age": 30,\\n "city":'),
467
+ ("json", '{\\n "key": "value",\\n "other key": [1, 2]\\n }'),
468
+ ("json", '{\\n "foo": "bar",\\n "baz": [1, 2, 3],\\n "boo":'),
469
+ ("json", '{\\n "key": "value",\\n "other_key": "other_value"'),
470
+ ("json", '{\\n "key": "value",\\n "anotherKey": "anotherValue"\\n }'),
471
+ ("json", '{\\n "name": "John",\\n "age": 30,\\n "isStudent": true\\n }'),
472
+ ("json", '{\\n "name": "John",\\n "age": 30,\\n "is_married": false\\n }'),
473
+ ("json", '{\\n "one": "a string",\\n "two": 3,\\n "three": ["list"]\\n }'),
474
+ ("json", '{\\n "one": "two",\\n "three": 4.3,\\n "five": ["six", 7]\\n }'),
475
+ ("json", '{\\n "key1": "value1",\\n "key2": {\\n "key3": "value3"\\n }'),
476
+ ("json", '{\\n "field1":"value1",\\n "field2":true,\\n "field3":["value4"],'),
477
+ ("json", '{\\n "foo": "bar",\\n "baz": true,\\n "some_array": [1, 2, 3]\\n }'),
478
+ ("json", '{\\n "key": "value",\\n "key2": {\\n "key3": "value3"\\n }\\n }'),
479
+ ("json", '{\\n "first": "name",\\n "second": {\\n "third": "value"\\n }\\n }'),
480
+ ("json", '{\\n "name": "John",\\n "age": 30,\\n "city": {\\n "state": "New York'),
481
+ ("json", '{"fizzbuzz": ["fizz", "fizz", "buzz"],\\n "evens": [4, 6],\\n "odds": [1, 3]}'),
482
+ ("json", '{\\n "key1": "value1",\\n "key2": {\\n "subkey1": "subvalue1",\\n "subkey2":'),
483
+ ("json", '{\\n "key1": "value1",\\n "key2": [1, 2, 3],\\n "key3": {\\n "key4": "value4",'),
484
+ ("json", '{\\n "title": "My Title",\\n "content": "This is a sample paragraph.",\\n "tags":['),
485
+ ("json", '{\\n "key1": "value1",\\n "key2": {\\n "subkey1": "subvalue1",\\n "subkey2": ['),
486
+ (
487
+ "json",
488
+ '{\\n "key": "value",\\n "another key": [1, 2, 3],\\n "yet another": {\\n "more keys":',
489
+ ),
490
+ (
491
+ "json",
492
+ '{\\n "key": "value",\\n "another-key": true,\\n "nested key": {\\n "a third": "nested value',
493
+ ),
494
+ (
495
+ "json",
496
+ '{\\n "key1": "value1",\\n "key2": {\\n "subkey1": "subvalue1",\\n "subkey2": [\\n "sub',
497
+ ),
498
+ (
499
+ "json",
500
+ '{\\n "key": "value",\\n "another_key": {\\n "yet_more_key": "whatever I want it to be."\\n }\\n }',
501
+ ),
502
+ (
503
+ "json",
504
+ '{\\n "key1": "value1",\\n "key2": [1, 2],\\n "key3": {\\n "subkey1": "subvalue1",\\n "subkey2":',
505
+ ),
506
+ (
507
+ "json",
508
+ '{\\n "key1": "value1",\\n "key2": {\\n "subkey1": "subvalue1",\\n "subkey2": "subvalue2"\\n }\\n }',
509
+ ),
510
+ (
511
+ "json",
512
+ '{\\n "key": "value",\\n "another_key": "another_value",\\n\\n "object": {\\n "some_property": 1,\\n "',
513
+ ),
514
+ (
515
+ "json",
516
+ '{\\n "name": "John",\\n "age": 21,\\n "isEmployed": true,\\n "children": [\\n {"name": "John Junior"}\\n ]\\n}',
517
+ ),
518
+ (
519
+ "json",
520
+ '{\\n "type":"foo",\\n "name": "bar",\\n \\n "a":[\\n 1,2,3\\n ],\\n "b":[\\n [],\\n 5,6\\n ]\\n }',
521
+ ),
522
+ (
523
+ "json",
524
+ '{\\n "key": "value",\\n "another key": {\\n "more nested keys": [\\n "nested values"\\n ]\\n }\\n }',
525
+ ),
526
+ (
527
+ "json",
528
+ '{\\n "name": "John",\\n "age": 30,\\n "isMarried": false,\\n "cars": [\\n {\\n "make": "Ford",\\n "model',
529
+ ),
530
+ (
531
+ "json",
532
+ '{\\n "hello": "world",\\n "fruits": ["apple", "orange"],\\n \\t"user": {\\n "name": "Bob",\\n "age": 20,\\n \\t"',
533
+ ),
534
+ (
535
+ "json",
536
+ '{\\n "data": "hello",\\n "age": 30,\\n "country": {\\n "state": "California",\\n "city": "San Francisco"\\n }\\n }',
537
+ ),
538
+ (
539
+ "json",
540
+ '{\\n "key1": "value1",\\n "key2": "value2",\\n "key3": {\\n "subkey1": "subvalue1",\\n "subkey2": "subvalue2",\\n "subkey',
541
+ ),
542
+ (
543
+ "json",
544
+ '{\\n "key": "value",\\n "a": [1, 2],\\n "b": true,\\n "c": {\\n "d": 1,\\n "e": {\\n "f": "g"\\n }\\n },\\n "',
545
+ ),
546
+ (
547
+ "json",
548
+ '{\\n "key1": "value1",\\n "key2": {\\n "subkey1": "subvalue1",\\n "subkey2": [\\n "subsubvalue1",\\n "subsubvalue2"',
549
+ ),
550
+ (
551
+ "json",
552
+ '{\\n "name": "John",\\n "age": 23,\\n "address": {\\n "street": "123 Main St.",\\n "city": "Anytown",\\n "state": "CA",\\n "zipcode": "12345"',
553
+ ),
554
+ ("list", "-"),
555
+ ("list", "- a"),
556
+ ("list", "- a list"),
557
+ ("list", "- a list of"),
558
+ ("list", "- a list of cars."),
559
+ ("list", "- a list of fruits"),
560
+ ("list", "- a list of fruits."),
561
+ ("list", "- a list of sports."),
562
+ ("list", "- a list of cities."),
563
+ ("list", "- a list of colors."),
564
+ ("list", "- a list of numbers."),
565
+ ("list", "- a list of colors.\\n-"),
566
+ ("list", "- a list of car models."),
567
+ ("list", "- a list of car brands."),
568
+ ("list", "- a list of colors.\\n- Add"),
569
+ ("list", "- a list of music genres."),
570
+ ("list", "- a list of fruits.\\n- If the"),
571
+ ("list", "- a list of programming languages."),
572
+ ("list", "- a list of fruits.\\n- If the input is not"),
573
+ ("list", "- a list of books.\\n- What is the difference"),
574
+ ("list", '- a list of fruits.\\n- If the user enters "quit" or'),
575
+ ("list", "- a list of fruits.\\n- For example, if the input is:\\n-"),
576
+ ("list", "- a list of colors.\\n- `grocery`: the same as above, but"),
577
+ ("list", "- a list of fruits.\\n- The first item in the returned list"),
578
+ ("list", '- a list of fruits.\\n- If there is no such word, return "I don\'t know'),
579
+ ("list", "- a list of colors.\\n- Each item in the returned list has two properties"),
580
+ ("list", "- a list of fruits.\\n- If you want to get all the items from the grocery"),
581
+ ("list", '- a list of fruits.\\n- If the user enters "exit", return an empty list.\\n- If'),
582
+ ("list", "- a list of sports.\\n- Example:\\n- >>> grocery_list = ['Bread', 'Milk', 'Cheese']"),
583
+ ("list", "- a list of fruits.\\n- If the input is not one of these, return an empty list.\\n- The"),
584
+ (
585
+ "list",
586
+ "- a list of fruits.\\n- If the input is not one of these, return an empty list.\\n- The grocery",
587
+ ),
588
+ ("list", "- a list of fruits.\\n- The function should return the empty list if it fails to find any item"),
589
+ ("list", "- a list of fruits.\\n- Write some code that returns an empty list, so that we can test if the"),
590
+ (
591
+ "list",
592
+ "- a list of cities.\\n- If the input is not in any of these categories, return 'Sorry, I don't know that'.",
593
+ ),
594
+ (
595
+ "list",
596
+ "- a list of movies.\\n- We'll say that the words in this problem are all lowercase and there is no punctuation.",
597
+ ),
598
+ (
599
+ "list",
600
+ '- a list of fruits.\\n- The list should be sorted alphabetically, except that the first item in the list is always "',
601
+ ),
602
+ (
603
+ "list",
604
+ '- a list of fruits.\\n- If the user enters "quit", return an empty list.\\n- If the user enters anything else, return None.',
605
+ ),
606
+ (
607
+ "list",
608
+ '- a list of fruits.\\n- If the user enters "quit", return an empty list.\\n- If the user enters any other word, ask them to',
609
+ ),
610
+ (
611
+ "list",
612
+ "- a list of fruits.\\n- If the input is not one of these, return an empty list.\\n- The input can be any string.\\n- You may assume that",
613
+ ),
614
+ (
615
+ "list",
616
+ "- a list of fruits.\\n- The first argument is the name of the category, and the second argument is a string that contains all items in that",
617
+ ),
618
+ (
619
+ "list",
620
+ "- a list of fruits.\\n- If the user enters 'q', quit the program.\\n- If the user enters anything else, print an error message and ask again.\\n- Use",
621
+ ),
622
+ (
623
+ "list",
624
+ "- a list of programming languages.\\n- Give me one of the following:\\n- the index of the first item in the list that matches my criteria, if there",
625
+ ),
626
+ (
627
+ "list",
628
+ "- a list of planets.\\n- What is the purpose of the `if` statement?\\n- How can we modify the program to take multiple arguments and concatenate them into one",
629
+ ),
630
+ (
631
+ "list",
632
+ '- a list of fruits.\\n- If the user enters "exit", return an empty list.\\n- If the user enters anything else, return None.\\n- The program should ask for input until',
633
+ ),
634
+ (
635
+ "list",
636
+ '- a list of fruits.\\n- If the user enters "quit", return an empty list.\\n- If the user enters anything else, print an error message and ask again.\\n- Use a loop to keep asking until the user enters "quit".\\n- Use a',
637
+ ),
638
+ (
639
+ "list",
640
+ '- a list of fruits.\\n- If the user enters "quit", return an empty list.\\n- If the user enters anything else, return None.\\n- The program should ask for input until the user enters "quit".\\n- Use the `split()` method to split the',
641
+ ),
642
+ (
643
+ "list",
644
+ '- a list of fruits.\\n- If the user enters "exit", return an empty list.\\n- If the user enters anything else, print an error message and ask again.\\n- Use a loop to keep asking until the user enters "exit".\\n- Use a dictionary to store the items',
645
+ ),
646
+ (
647
+ "list",
648
+ '- a list of names.\\n- If the parameter is not a string, return me an empty list.\\n- If the parameter is "", then return me a list with all words in the English language.\\n- If the parameter contains only spaces, then return me a list with all words in the English',
649
+ ),
650
+ ("simple", "1"),
651
+ ("simple", "1+"),
652
+ ("simple", "1+2"),
653
+ ("simple", "1+2="),
654
+ ("simple", "3+5*2"),
655
+ ("simple", "1+2=3"),
656
+ ("simple", "3-2=1"),
657
+ ("simple", "3+4=7"),
658
+ ("simple", "2-5+3*"),
659
+ ("simple", "2-2=0\\n3"),
660
+ ("simple", "3+2=5\\n5/"),
661
+ ("simple", "1+2=3\\n2/4"),
662
+ ("simple", "1+2=3\\n2+3-"),
663
+ ("simple", "1+2=3\\n2/4=0"),
664
+ ("simple", "1+2=3\\n2/4=2"),
665
+ ("simple", "1/0=0\\n2-4+6-8"),
666
+ ("simple", "1+2=3\\n9-1*3/4=0"),
667
+ ("simple", "1/0=0\\n2-4+6-8*10"),
668
+ ("simple", "1/3=0\\n2*7-8+9=10\\n3"),
669
+ ("simple", "3+4*5-6/2=19\\n2-1*2+"),
670
+ ("simple", "1+2=3\\n2/4=2\\n1+2*3=9"),
671
+ ("simple", "1/0=0\\n2-1=1\\n4/3=1\\n5+6"),
672
+ ("simple", "3/2+1/4=5\\n4/2*1/4-7/8=0"),
673
+ ("simple", "1+2=3\\n2/4=2\\n1+2*3=9\\n3/5="),
674
+ ("simple", "1+2=3\\n2/4=2\\n1+2*3=9\\n3-5=2"),
675
+ ("simple", "1+2=3\\n2/4=2\\n1+2*3=9\\n3-6/3+"),
676
+ ("simple", "1+2=3\\n2/4=2\\n1+2*3=9\\n3-5+6=8"),
677
+ ("simple", "2-4=0\\n3-5+6=2\\n4*1/2+8=10\\n5+7-"),
678
+ ("simple", "1+2=3\\n2/4=2\\n1+2*3=9\\n3+5-6=2\\n3"),
679
+ ("simple", "1+2=3\\n2/4=2\\n1+2*3=9\\n3-5+6=4\\n7*"),
680
+ ("simple", "1+2=3\\n2/4=2\\n1+2*3=9\\n3-5+7=5\\n4*6"),
681
+ ("simple", "3+4-5=2\\n10/2*5=25\\n1+2*3-4/2=7\\n5+6+"),
682
+ ("simple", "3+4-5=2\\n10/2*5=25\\n1+2*3-4/2=7\\n5+6+7"),
683
+ ("simple", "1+2=3\\n2/4=2\\n1+2*3=9\\n3-5=2\\n6/3=2\\n7-8"),
684
+ ("simple", "2-3+4*5=21\\n3-1/2-3/4+1/8=0\\n4+12/4-6/"),
685
+ ("simple", "3+4=7\\n2-5=2\\n3*6=18\\n4/8=0\\n5-1=4\\n6*9=54\\n7"),
686
+ ("simple", "1/2+3=4\\n2/5*3=0\\n3-6/2=0\\n9/3+2=5\\n3+4/2=4"),
687
+ ("simple", "2-3=1\\n3-4=1\\n5+6/7=8\\n10*9-11/12=89\\n4+5*6-7/"),
688
+ ("simple", "3+4=7\\n2-1=1\\n3*2=6\\n5/5=1\\n4*3=12\\n3+2*8=23\\n5-"),
689
+ ("simple", "1+2=3\\n2/4=2\\n1+2*3=9\\n3-5/6=0\\n4*8/4=8\\n7-10/3"),
690
+ ("simple", "5/6=0\\n9-9=0\\n3+4*2=14\\n1+8/3=5\\n10-2*5=0\\n7*8/2="),
691
+ ("simple", "3/4+1=0\\n2/5*6=1\\n3/10-8=0\\n4/9+2=5\\n5/7*7=5\\n6/8"),
692
+ ("simple", "2-5=4\\n3-8=5\\n4-6=2\\n5-7=2\\n6-9=3\\n7-10=1\\n8-11=3\\n9-"),
693
+ ("simple", "1+2=3\\n2/4=2\\n1+2*3=9\\n3-5/6=0\\n4+7/8=10\\n5-6/7=4\\n6/7"),
694
+ ("simple", "1+2=3\\n2/4=2\\n1+2*3=9\\n3/3=1\\n5-6=1\\n7+8=15\\n9-10=1\\n11-"),
695
+ ("simple", "2+3*4=14\\n3/5-1/10=0\\n4+15/3=9\\n5*6+7=37\\n8+8*3=40\\n4/2"),
696
+ ("simple", "3-5/8+1/2=1\\n4/3*6+7/9=29\\n5/3+8/6-7/9=24\\n0/3*6*7/9+1/2-5"),
697
+ ("simple", "2-3=1\\n3-4=1\\n5+6/7=8\\n4*5=20\\n6/9=0\\n10/12=0\\n11/13=0\\n17/19=1"),
698
+ ("simple", "1+2=3\\n2/4=2\\n1+2*3=9\\n3/5=0\\n4-6=2\\n7+8=15\\n10-20=10\\n11-12=1\\n13"),
699
+ ("simple", "1-3+5*4/6=14\\n2/1*10/100*0=0\\n3-4+5/6+7=8\\n9*9/99/0=0\\n13-2*3-2/"),
700
+ ("simple", "2-3/4=1\\n3-5+6=8\\n4*6/2-9=3\\n7-6/3*4=1\\n5+10-15*2/3=0\\n8/4+5*6-1+"),
701
+ ("simple", "3+4=7\\n2-5=3\\n3/6=1\\n4*9=36\\n10-5=5\\n3-8=5\\n17-10=7\\n23/3=7\\n4+2*6=16"),
702
+ ("simple", "3/2*4+5=7\\n4/2*3-1=4\\n5-4*2*3=13\\n4*4+4-8*2*3=12\\n3/2*4+5-6*4=9\\n5"),
703
+ ("simple", "5+3/6-2*4=7\\n10*1+8/9-2*8=0\\n3*6/2+2-6=7\\n2*3/6-1*7=1\\n5*4-6*2+7-"),
704
+ ("simple", "1+2=3\\n10-4*5/2=0\\n15+17*11+4/14=194\\n5*6+2/8+9=44\\n16*12/8+7-3*11="),
705
+ ("simple", "2-3+4*5/6=1\\n3-0=3\\n4-3+5*6/7=5\\n8-9+10*11/12=15\\n13-14+15*16/17=43\\n19-"),
706
+ ("simple", "2+3*4-5/6=19\\n3+4*5-6/7=29\\n4+5*6-7/8=32\\n5+6*7-8/9=39\\n6+7*8-9/10=41\\n7"),
707
+ ("simple", "1+2=3\\n2/4=2\\n1+2*3=9\\n3/3=1\\n5-2=3\\n8+6-4=10\\n7+5-3=9\\n15-5+2=12\\n16-4+8=20"),
708
+ ("simple", "1+2=3\\n2/4=2\\n1+2*3=9\\n3/5=0\\n4-6=2\\n7-8=1\\n10-11=1\\n12-13=1\\n14-15=1\\n16-17=1"),
709
+ ("simple", "1+2=3\\n2/4=2\\n1+2*3=9\\n3-2*5+6=7\\n8-16/4+4=0\\n10+20-30/40-50=0\\n12-18+24/36-42/54"),
710
+ ("simple", "1+2=3\\n2/4=2\\n1+2*3=9\\n3-5+7-8+6-9+10-11-12+13-14-15+16-17-18-19+20-21-22-23-24-25-"),
711
+ ("simple", "1+2=3\\n4/4=2\\n1+2*3=9\\n5+6-7+8-9+10-11+12-13+14-15+16-17+18-19+20-21+22-23+24-25-26+"),
712
+ ]
713
+
714
+
715
+ def unescape(value: str) -> str:
716
+ return value.replace("\\n", "\n").replace("\\t", "\t")
717
+
718
+
719
+ def describe_grammars():
720
+ @pytest.mark.parametrize(
721
+ ("grammar", "test_case"),
722
+ [(GRAMMARS[name], test_case) for name, test_case in CASES],
723
+ ids=[f"{name}-{index}" for index, (name, _test_case) in enumerate(CASES)],
724
+ )
725
+ def it_parses_a_known_valid_grammar(grammar, test_case):
726
+ state = GBNF(unescape(grammar))
727
+
728
+ for char in unescape(test_case):
729
+ state = state.add(char)
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/integration/iteration/test_iteration.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/integration-tests/generated/iteration/iteration.test.ts.
2
+
3
+ The data tables were extracted mechanically from the TypeScript source."""
4
+
5
+ import pytest
6
+
7
+ from gbnf import GBNF
8
+
9
+ CASES = [
10
+ ('root ::= "foo"', [{"type": "char", "value": [102]}]),
11
+ ('root ::= "foo" | "bar" ', [{"type": "char", "value": [102]}, {"type": "char", "value": [98]}]),
12
+ (
13
+ 'root ::= "foo" | "bar" | "gaz"',
14
+ [{"type": "char", "value": [102]}, {"type": "char", "value": [98]}, {"type": "char", "value": [103]}],
15
+ ),
16
+ ('root ::= "foo" | "bar" | "baz"', [{"type": "char", "value": [102]}, {"type": "char", "value": [98]}]),
17
+ ("root ::= [^x]", [{"type": "char_exclude", "value": [120]}]),
18
+ ('root ::= [^f] "o"', [{"type": "char_exclude", "value": [102]}]),
19
+ ("root ::= [^A-Z]", [{"type": "char_exclude", "value": [[65, 90]]}]),
20
+ ("root ::= [^A-Z0-9]", [{"type": "char_exclude", "value": [[65, 90], [48, 57]]}]),
21
+ ("root ::= [^A-Z0-9_-]", [{"type": "char_exclude", "value": [[65, 90], [48, 57], 95, 45]}]),
22
+ ('\nroot ::= foo\nfoo ::= "foo"\n ', [{"type": "char", "value": [102]}]),
23
+ ('\nroot ::= f\nf ::= foo\nfoo ::= "foo"\n ', [{"type": "char", "value": [102]}]),
24
+ (
25
+ '\nroot ::= foo | "bar"\nfoo ::= "foo"',
26
+ [{"type": "char", "value": [102]}, {"type": "char", "value": [98]}],
27
+ ),
28
+ (
29
+ '\nroot ::= foo | bar\nfoo ::= "foo"\nbar ::= "bar"\n ',
30
+ [{"type": "char", "value": [102]}, {"type": "char", "value": [98]}],
31
+ ),
32
+ (
33
+ '\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"\n ',
34
+ [{"type": "char", "value": [102]}, {"type": "char", "value": [98]}],
35
+ ),
36
+ ("root ::= [a-z]", [{"type": "char", "value": [[97, 122]]}]),
37
+ ("root ::= [a-zA-Z]", [{"type": "char", "value": [[97, 122], [65, 90]]}]),
38
+ ("root ::= [a-z]?", [{"type": "char", "value": [[97, 122]]}, {"type": "end"}]),
39
+ ("root ::= [a-z]+", [{"type": "char", "value": [[97, 122]]}]),
40
+ ("root ::= [a-z]*", [{"type": "char", "value": [[97, 122]]}, {"type": "end"}]),
41
+ ('\n root ::= "foo"\n foo ::= "foo"', [{"type": "char", "value": [102]}]),
42
+ (
43
+ '\n root ::= (expr "=" term "\\n")+\n expr ::= term ([-+*/] term)*\n term ::= [0-9]+\n ',
44
+ [{"type": "char", "value": [[48, 57]]}],
45
+ ),
46
+ ]
47
+
48
+
49
+ def describe_iteration():
50
+ @pytest.mark.parametrize(("grammar", "expected"), CASES)
51
+ def it_returns_parse_state_for_a_grammar(grammar, expected):
52
+ state = GBNF(grammar)
53
+ assert list(state) == expected
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/integration/iteration/test_iteration_with_additional_strings.py ADDED
@@ -0,0 +1,245 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/integration-tests/generated/iteration/iteration-with-additional-strings.test.ts.
2
+
3
+ The data tables were extracted mechanically from the TypeScript source."""
4
+
5
+ import pytest
6
+
7
+ from gbnf import GBNF, InputParseError
8
+
9
+ THROWING_CASES = [
10
+ ('root ::= "foo"', "f", "1"),
11
+ ('root ::= "foo"', "f", "b"),
12
+ ('root ::= "foo"', "f", "o1"),
13
+ ('root ::= "foo"', "f", "ooo"),
14
+ ('root ::= "foo"', "f", "ooooooo"),
15
+ ('root ::= "foo" | "bar"', "f", "1"),
16
+ ('root ::= "foo" | "bar"', "b", "1"),
17
+ ('root ::= "foo" | "bar"', "f", "o1"),
18
+ ('root ::= "foo" | "bar"', "b", "a1"),
19
+ ('root ::= "foo" | "bar"', "f", "ooo"),
20
+ ('root ::= "foo" | "bar"', "b", "arrr"),
21
+ ('root ::= "foo" | "bar" | "baz"', "f", "1"),
22
+ ('root ::= "foo" | "bar" | "baz"', "f", "z"),
23
+ ('root ::= "foo" | "bar" | "baz"', "b", "b1"),
24
+ ('root ::= "foo" | "bar" | "baz"', "f", "o1"),
25
+ ('root ::= "foo" | "bar" | "baz"', "b", "al"),
26
+ ('root ::= "foo" | "bar" | "baz"', "f", "ooo"),
27
+ ('root ::= "foo" | "bar" | "baz"', "b", "azrr"),
28
+ ('root ::= foo\nfoo ::="foo"', "f", "1"),
29
+ ('root ::= foo\nfoo ::="foo"', "f", "b"),
30
+ ('root ::= foo\nfoo ::="foo"', "f", "o1"),
31
+ ('root ::= foo\nfoo ::="foo"', "f", "ooo"),
32
+ ('root::=foo|"bar"\nfoo::="foo"', "f", "1"),
33
+ ('root::=foo|"bar"\nfoo::="foo"', "f", "z"),
34
+ ('root::=foo|"bar"\nfoo::="foo"', "b", "b1"),
35
+ ('root::=foo|"bar"\nfoo::="foo"', "f", "o1"),
36
+ ('root::=foo|"bar"\nfoo::="foo"', "f", "ooo"),
37
+ ('root::=foo|"bar"\nfoo::="foo"', "b", "arr"),
38
+ ('root::= foo | bar\n foo::="foo"\n bar::="bar"', "f", "1"),
39
+ ('root::= foo | bar\n foo::="foo"\n bar::="bar"', "f", "z"),
40
+ ('root::= foo | bar\n foo::="foo"\n bar::="bar"', "b", "1"),
41
+ ('root::= foo | bar\n foo::="foo"\n bar::="bar"', "f", "o1"),
42
+ ('root::= foo | bar\n foo::="foo"\n bar::="bar"', "f", "ooo"),
43
+ ('root::= foo | bar\n foo::="foo"\n bar::="bar"', "b", "arr"),
44
+ (
45
+ 'root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"',
46
+ "f",
47
+ "1",
48
+ ),
49
+ (
50
+ 'root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"',
51
+ "f",
52
+ "z",
53
+ ),
54
+ (
55
+ 'root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"',
56
+ "b",
57
+ "1",
58
+ ),
59
+ (
60
+ 'root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"',
61
+ "f",
62
+ "o1",
63
+ ),
64
+ (
65
+ 'root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"',
66
+ "f",
67
+ "ooo",
68
+ ),
69
+ (
70
+ 'root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"',
71
+ "b",
72
+ "arr",
73
+ ),
74
+ (
75
+ 'root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"',
76
+ "b",
77
+ "azr",
78
+ ),
79
+ ("root ::= [a-z]", "a", "A"),
80
+ ("root ::= [a-z]", "a", "0"),
81
+ ("root ::= [a-z]", "a", "az"),
82
+ ("root ::= [a-z]?", "a", "A"),
83
+ ("root ::= [a-z]?", "a", "0"),
84
+ ("root ::= [a-z]?", "a", "az"),
85
+ ("root ::= [a-z]+", "a", "A"),
86
+ ("root ::= [a-z]+", "a", "0"),
87
+ ("root ::= [a-z]+", "a", "z0"),
88
+ ("root ::= [a-z]*", "a", "A"),
89
+ ("root ::= [a-z]*", "a", "0"),
90
+ ("root ::= [a-z]*", "a", "z0"),
91
+ ]
92
+
93
+ PARSING_CASES = [
94
+ ('root ::= "foo"', "", "f", [{"type": "char", "value": [111]}]),
95
+ ('root ::= "foo"', "f", "o", [{"type": "char", "value": [111]}]),
96
+ ('root ::= "foo"', "fo", "o", [{"type": "end"}]),
97
+ ('root ::= "foo" | "bar" ', "", "f", [{"type": "char", "value": [111]}]),
98
+ ('root ::= "foo" | "bar" ', "f", "o", [{"type": "char", "value": [111]}]),
99
+ ('root ::= "foo" | "bar" ', "fo", "o", [{"type": "end"}]),
100
+ ('root ::= "foo" | "bar" ', "", "b", [{"type": "char", "value": [97]}]),
101
+ ('root ::= "foo" | "bar" ', "b", "a", [{"type": "char", "value": [114]}]),
102
+ ('root ::= "foo" | "bar" ', "ba", "r", [{"type": "end"}]),
103
+ (
104
+ 'root ::= "foo" | "bar" | "baz"',
105
+ "b",
106
+ "a",
107
+ [{"type": "char", "value": [114]}, {"type": "char", "value": [122]}],
108
+ ),
109
+ ('root ::= [^f] "o"', "g", "o", [{"type": "end"}]),
110
+ ("root ::= [^A-Z]", "", "a", [{"type": "end"}]),
111
+ ("root ::= [^A-Z0-9]", "", "a", [{"type": "end"}]),
112
+ ("root ::= [^A-Z0-9_-]", "", "a", [{"type": "end"}]),
113
+ ('root ::= foo\nfoo ::= "foo"', "", "f", [{"type": "char", "value": [111]}]),
114
+ ('root ::= foo\nfoo ::= "foo"', "f", "o", [{"type": "char", "value": [111]}]),
115
+ ('root ::= foo\nfoo ::= "foo"', "fo", "o", [{"type": "end"}]),
116
+ ('root ::= foo | "bar"\nfoo ::= "foo"', "", "f", [{"type": "char", "value": [111]}]),
117
+ ('root ::= foo | "bar"\nfoo ::= "foo"', "f", "o", [{"type": "char", "value": [111]}]),
118
+ ('root ::= foo | "bar"\nfoo ::= "foo"', "fo", "o", [{"type": "end"}]),
119
+ ('root ::= foo | "bar"\nfoo ::= "foo"', "", "b", [{"type": "char", "value": [97]}]),
120
+ ('root ::= foo | "bar"\nfoo ::= "foo"', "b", "a", [{"type": "char", "value": [114]}]),
121
+ ('root ::= foo | "bar"\nfoo ::= "foo"', "ba", "r", [{"type": "end"}]),
122
+ ('root ::= foo | bar\nfoo ::= "foo"\nbar ::= "bar"', "", "f", [{"type": "char", "value": [111]}]),
123
+ ('\nroot ::= foo | bar\nfoo ::= "foo"\nbar ::= "bar"', "f", "o", [{"type": "char", "value": [111]}]),
124
+ ('\nroot ::= foo | bar\nfoo ::= "foo"\nbar ::= "bar"', "fo", "o", [{"type": "end"}]),
125
+ ('root ::= foo | bar\nfoo ::= "foo"\nbar ::= "bar"', "", "b", [{"type": "char", "value": [97]}]),
126
+ ('root ::= foo | bar\nfoo ::= "foo"\nbar ::= "bar"', "b", "a", [{"type": "char", "value": [114]}]),
127
+ ('root ::= foo | bar\nfoo ::= "foo"\nbar ::= "bar"', "ba", "r", [{"type": "end"}]),
128
+ (
129
+ '\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"',
130
+ "",
131
+ "f",
132
+ [{"type": "char", "value": [111]}],
133
+ ),
134
+ (
135
+ 'root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"',
136
+ "f",
137
+ "o",
138
+ [{"type": "char", "value": [111]}],
139
+ ),
140
+ (
141
+ 'root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"',
142
+ "fo",
143
+ "o",
144
+ [{"type": "end"}],
145
+ ),
146
+ (
147
+ 'root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"',
148
+ "",
149
+ "b",
150
+ [{"type": "char", "value": [97]}],
151
+ ),
152
+ (
153
+ '\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"',
154
+ "b",
155
+ "a",
156
+ [{"type": "char", "value": [114]}, {"type": "char", "value": [122]}],
157
+ ),
158
+ (
159
+ '\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"',
160
+ "ba",
161
+ "r",
162
+ [{"type": "end"}],
163
+ ),
164
+ (
165
+ '\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"',
166
+ "ba",
167
+ "z",
168
+ [{"type": "end"}],
169
+ ),
170
+ ("root ::= [a-z]", "", "a", [{"type": "end"}]),
171
+ ("root ::= [a-z]", "", "m", [{"type": "end"}]),
172
+ ("root ::= [a-z]", "", "z", [{"type": "end"}]),
173
+ ("root ::= [a-zA-Z]", "", "a", [{"type": "end"}]),
174
+ ("root ::= [a-zA-Z]", "", "Z", [{"type": "end"}]),
175
+ ("root ::= [a-zA-Z0-9]", "", "Z", [{"type": "end"}]),
176
+ ("root ::= [a-zA-Z0-9]", "", "0", [{"type": "end"}]),
177
+ ("root ::= [a-zA-Z0-9]", "", "9", [{"type": "end"}]),
178
+ ("root ::= [a-z]?", "", "a", [{"type": "end"}]),
179
+ ("root ::= [a-zA-Z]?", "", "Z", [{"type": "end"}]),
180
+ ("root ::= [a-zA-Z0-9]?", "", "0", [{"type": "end"}]),
181
+ ("root ::= [a-z]+", "", "a", [{"type": "char", "value": [[97, 122]]}, {"type": "end"}]),
182
+ ("root ::= [a-zA-Z]+", "", "Z", [{"type": "char", "value": [[97, 122], [65, 90]]}, {"type": "end"}]),
183
+ ("root ::= [a-zA-Z]+", "azA", "Z", [{"type": "char", "value": [[97, 122], [65, 90]]}, {"type": "end"}]),
184
+ ("root ::= [a-z]*", "", "a", [{"type": "char", "value": [[97, 122]]}, {"type": "end"}]),
185
+ ("root ::= [a-zA-Z]+", "", "Z", [{"type": "char", "value": [[97, 122], [65, 90]]}, {"type": "end"}]),
186
+ (
187
+ "root ::= [a-zA-Z]+",
188
+ "acczABC",
189
+ "Z",
190
+ [{"type": "char", "value": [[97, 122], [65, 90]]}, {"type": "end"}],
191
+ ),
192
+ (
193
+ 'root ::= "foo" | "bar" | "baz" | "bazaar" | "barrington" ',
194
+ "baza",
195
+ "a",
196
+ [{"type": "char", "value": [114]}],
197
+ ),
198
+ (
199
+ 'root ::= [^f]+ "o"',
200
+ "aaa",
201
+ "a",
202
+ [{"type": "char_exclude", "value": [102]}, {"type": "char", "value": [111]}],
203
+ ),
204
+ ("root ::= [^A-Z]+", "abc", "d", [{"type": "char_exclude", "value": [[65, 90]]}, {"type": "end"}]),
205
+ (
206
+ "root ::= [^A-Z0-9]*",
207
+ "abc",
208
+ "d",
209
+ [{"type": "char_exclude", "value": [[65, 90], [48, 57]]}, {"type": "end"}],
210
+ ),
211
+ (
212
+ "root ::= [^A-Z0-9_-]*",
213
+ "abc",
214
+ "d",
215
+ [{"type": "char_exclude", "value": [[65, 90], [48, 57], 95, 45]}, {"type": "end"}],
216
+ ),
217
+ ]
218
+
219
+
220
+ def describe_iteration():
221
+ @pytest.mark.parametrize(("grammar", "starting", "additional"), THROWING_CASES)
222
+ def it_throws_if_encountering_a_grammar_with_starting_and_additional(grammar, starting, additional):
223
+ state = GBNF(grammar, starting)
224
+ with pytest.raises(InputParseError):
225
+ state.add(additional)
226
+
227
+ @pytest.mark.parametrize(("grammar", "starting", "additional", "expected"), PARSING_CASES)
228
+ def it_parses_a_grammar_with_starting_and_additional(grammar, starting, additional, expected):
229
+ state = GBNF(grammar, starting)
230
+ state = state.add(additional)
231
+ assert list(state) == expected
232
+
233
+ @pytest.mark.parametrize("error_for_most_recent_input", [False, True])
234
+ def it_throws_a_particular_error(error_for_most_recent_input):
235
+ grammar = 'root ::= "bar"'
236
+ state = GBNF(grammar)
237
+ state = state.add("b")
238
+ state = state.add("a")
239
+ with pytest.raises(InputParseError) as excinfo:
240
+ state.add("z")
241
+ expected_err = InputParseError("z", 0, "ba")
242
+ if error_for_most_recent_input:
243
+ assert excinfo.value.error_for_most_recent_input == expected_err.error_for_most_recent_input
244
+ else:
245
+ assert str(excinfo.value) == str(expected_err)
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/integration/iteration/test_iteration_with_an_initial_string.py ADDED
@@ -0,0 +1,221 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/integration-tests/generated/iteration/iteration-with-an-initial-string.test.ts.
2
+
3
+ The data tables were extracted mechanically from the TypeScript source."""
4
+
5
+ import pytest
6
+
7
+ from gbnf import GBNF
8
+
9
+ CASES = [
10
+ ('root ::= "foo"', "", [{"type": "char", "value": [102]}]),
11
+ ('root ::= "foo"', "f", [{"type": "char", "value": [111]}]),
12
+ ('root ::= "foo"', "fo", [{"type": "char", "value": [111]}]),
13
+ ('root ::= "foo"', "foo", [{"type": "end"}]),
14
+ ('root ::= "foo" | "bar" ', "", [{"type": "char", "value": [102]}, {"type": "char", "value": [98]}]),
15
+ ('root ::= "foo" | "bar" ', "f", [{"type": "char", "value": [111]}]),
16
+ ('root ::= "foo" | "bar" ', "fo", [{"type": "char", "value": [111]}]),
17
+ ('root ::= "foo" | "bar" ', "foo", [{"type": "end"}]),
18
+ ('root ::= "foo" | "bar" ', "b", [{"type": "char", "value": [97]}]),
19
+ ('root ::= "foo" | "bar" ', "ba", [{"type": "char", "value": [114]}]),
20
+ ('root ::= "foo" | "bar" ', "bar", [{"type": "end"}]),
21
+ (
22
+ 'root ::= "foo" | "bar" | "baz"',
23
+ "ba",
24
+ [{"type": "char", "value": [114]}, {"type": "char", "value": [122]}],
25
+ ),
26
+ ('root ::= "["', "[", [{"type": "end"}]),
27
+ ('root ::= "]"', "]", [{"type": "end"}]),
28
+ ('root ::= "[]"', "[]", [{"type": "end"}]),
29
+ ('root ::= "{"', "{", [{"type": "end"}]),
30
+ ('root ::= "}"', "}", [{"type": "end"}]),
31
+ ('root ::= "{}"', "{}", [{"type": "end"}]),
32
+ ('root ::= "[{}]"', "[{}]", [{"type": "end"}]),
33
+ ('root ::= [^f] "o"', "", [{"type": "char_exclude", "value": [102]}]),
34
+ ('root ::= [^f] "o"', "a", [{"type": "char", "value": [111]}]),
35
+ ("root ::= [^A-Z]", "", [{"type": "char_exclude", "value": [[65, 90]]}]),
36
+ ("root ::= [^A-Z0-9]", "", [{"type": "char_exclude", "value": [[65, 90], [48, 57]]}]),
37
+ ("root ::= [^A-Z0-9_-]", "", [{"type": "char_exclude", "value": [[65, 90], [48, 57], 95, 45]}]),
38
+ ('\nroot ::= foo\nfoo ::= "foo"', "", [{"type": "char", "value": [102]}]),
39
+ ('root ::= foo\nfoo ::= "foo"\n', "f", [{"type": "char", "value": [111]}]),
40
+ ('\nroot ::= foo\nfoo ::= "foo"\n', "fo", [{"type": "char", "value": [111]}]),
41
+ ('root ::= foo\nfoo ::= "foo"', "foo", [{"type": "end"}]),
42
+ ('root ::= f\nf ::= foo\nfoo ::= "foo"', "", [{"type": "char", "value": [102]}]),
43
+ (
44
+ '\nroot ::= foo | "bar"\nfoo ::= "foo"\n',
45
+ "",
46
+ [{"type": "char", "value": [102]}, {"type": "char", "value": [98]}],
47
+ ),
48
+ ('\nroot ::= foo | "bar"\nfoo ::= "foo"', "f", [{"type": "char", "value": [111]}]),
49
+ ('root ::= foo | "bar"\nfoo ::= "foo"', "fo", [{"type": "char", "value": [111]}]),
50
+ ('root ::= foo | "bar"\nfoo ::= "foo"', "foo", [{"type": "end"}]),
51
+ ('root ::= foo | "bar"\nfoo ::= "foo"', "b", [{"type": "char", "value": [97]}]),
52
+ ('root ::= foo | "bar"\nfoo ::= "foo"', "ba", [{"type": "char", "value": [114]}]),
53
+ ('root ::= foo | "bar"\nfoo ::= "foo"', "bar", [{"type": "end"}]),
54
+ (
55
+ '\nroot ::= foo | bar\nfoo ::= "foo"\nbar ::= "bar"',
56
+ "",
57
+ [{"type": "char", "value": [102]}, {"type": "char", "value": [98]}],
58
+ ),
59
+ ('\nroot ::= foo | bar\nfoo ::= "foo"\nbar ::= "bar"', "f", [{"type": "char", "value": [111]}]),
60
+ ('\nroot ::= foo | bar\nfoo ::= "foo"\nbar ::= "bar"', "fo", [{"type": "char", "value": [111]}]),
61
+ ('\nroot ::= foo | bar\nfoo ::= "foo"\nbar ::= "bar"', "foo", [{"type": "end"}]),
62
+ ('\nroot ::= foo | bar\nfoo ::= "foo"\nbar ::= "bar"', "b", [{"type": "char", "value": [97]}]),
63
+ ('\nroot ::= foo | bar\nfoo ::= "foo"\nbar ::= "bar"', "ba", [{"type": "char", "value": [114]}]),
64
+ ('\nroot ::= foo | bar\nfoo ::= "foo"\nbar ::= "bar"', "bar", [{"type": "end"}]),
65
+ (
66
+ '\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"',
67
+ "",
68
+ [{"type": "char", "value": [102]}, {"type": "char", "value": [98]}],
69
+ ),
70
+ (
71
+ '\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"',
72
+ "f",
73
+ [{"type": "char", "value": [111]}],
74
+ ),
75
+ (
76
+ '\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"',
77
+ "fo",
78
+ [{"type": "char", "value": [111]}],
79
+ ),
80
+ (
81
+ '\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"',
82
+ "foo",
83
+ [{"type": "end"}],
84
+ ),
85
+ (
86
+ '\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"',
87
+ "b",
88
+ [{"type": "char", "value": [97]}],
89
+ ),
90
+ (
91
+ '\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"',
92
+ "ba",
93
+ [{"type": "char", "value": [114]}, {"type": "char", "value": [122]}],
94
+ ),
95
+ (
96
+ '\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"',
97
+ "bar",
98
+ [{"type": "end"}],
99
+ ),
100
+ (
101
+ '\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= "foo"\nbar ::= "bar"\nbaz ::= "baz"',
102
+ "baz",
103
+ [{"type": "end"}],
104
+ ),
105
+ ("root ::= [a-z]", "", [{"type": "char", "value": [[97, 122]]}]),
106
+ ("root ::= [a-zA-Z]", "", [{"type": "char", "value": [[97, 122], [65, 90]]}]),
107
+ ("root ::= [a-z]", "a", [{"type": "end"}]),
108
+ ("root ::= [a-z]", "m", [{"type": "end"}]),
109
+ ("root ::= [a-z]", "z", [{"type": "end"}]),
110
+ ("root ::= [a-zA-Z]", "a", [{"type": "end"}]),
111
+ ("root ::= [a-zA-Z]", "Z", [{"type": "end"}]),
112
+ ("root ::= [a-zA-Z0-9]", "Z", [{"type": "end"}]),
113
+ ("root ::= [a-zA-Z0-9]", "0", [{"type": "end"}]),
114
+ ("root ::= [a-zA-Z0-9]", "9", [{"type": "end"}]),
115
+ ("root ::= [a-z]?", "", [{"type": "char", "value": [[97, 122]]}, {"type": "end"}]),
116
+ ("root ::= [a-z]?", "a", [{"type": "end"}]),
117
+ ("root ::= [a-zA-Z]?", "Z", [{"type": "end"}]),
118
+ ("root ::= [a-zA-Z0-9]?", "0", [{"type": "end"}]),
119
+ ("root ::= [a-z]+", "", [{"type": "char", "value": [[97, 122]]}]),
120
+ ("root ::= [a-z]+", "l", [{"type": "char", "value": [[97, 122]]}, {"type": "end"}]),
121
+ ("root ::= [a-zA-Z]+", "Z", [{"type": "char", "value": [[97, 122], [65, 90]]}, {"type": "end"}]),
122
+ ("root ::= [a-zA-Z]+", "aZ", [{"type": "char", "value": [[97, 122], [65, 90]]}, {"type": "end"}]),
123
+ ("root ::= [a-zA-Z]+", "azAZ", [{"type": "char", "value": [[97, 122], [65, 90]]}, {"type": "end"}]),
124
+ ("root ::= [a-z]*", "", [{"type": "char", "value": [[97, 122]]}, {"type": "end"}]),
125
+ ("root ::= [a-z]*", "a", [{"type": "char", "value": [[97, 122]]}, {"type": "end"}]),
126
+ ("root ::= [a-zA-Z]+", "Z", [{"type": "char", "value": [[97, 122], [65, 90]]}, {"type": "end"}]),
127
+ ("root ::= [a-zA-Z]+", "abczABCZ", [{"type": "char", "value": [[97, 122], [65, 90]]}, {"type": "end"}]),
128
+ (
129
+ 'root ::= [^f]+ "o"',
130
+ "aaa",
131
+ [{"type": "char_exclude", "value": [102]}, {"type": "char", "value": [111]}],
132
+ ),
133
+ ("root ::= [^A-Z]+", "abc", [{"type": "char_exclude", "value": [[65, 90]]}, {"type": "end"}]),
134
+ ("root ::= [^A-Z0-9]*", "abc", [{"type": "char_exclude", "value": [[65, 90], [48, 57]]}, {"type": "end"}]),
135
+ (
136
+ "root ::= [^A-Z0-9_-]*",
137
+ "abc",
138
+ [{"type": "char_exclude", "value": [[65, 90], [48, 57], 95, 45]}, {"type": "end"}],
139
+ ),
140
+ ('\nroot ::= [a-z] | xyx\nxyx ::= "foo"', "f", [{"type": "end"}, {"type": "char", "value": [111]}]),
141
+ ('root ::= "foo" | "bar" | "baz" | "bazaar" | "barrington" ', "bazaa", [{"type": "char", "value": [114]}]),
142
+ ('root ::= ("bar" | "foo") "zyx"', "bar", [{"type": "char", "value": [122]}]),
143
+ (
144
+ 'root ::= "z" ("bar" | "foo") "zzzz"',
145
+ "z",
146
+ [{"type": "char", "value": [98]}, {"type": "char", "value": [102]}],
147
+ ),
148
+ ('root ::= "z" ("bar" | "foo") "zzz"', "zbar", [{"type": "char", "value": [122]}]),
149
+ (
150
+ "\nroot ::= termz ([-+*/] termz)* \ntermz ::= [0-9]+",
151
+ "1",
152
+ [{"type": "char", "value": [[48, 57]]}, {"type": "char", "value": [45, 43, 42, 47]}, {"type": "end"}],
153
+ ),
154
+ (
155
+ '\nroot ::= expr "=" termy \nexpr ::= termy ([-+*/] termy)*\ntermy ::= [0-9]+',
156
+ "1",
157
+ [
158
+ {"type": "char", "value": [[48, 57]]},
159
+ {"type": "char", "value": [45, 43, 42, 47]},
160
+ {"type": "char", "value": [61]},
161
+ ],
162
+ ),
163
+ (
164
+ '\nroot ::= (expr "=" terma "\n")+\nexpr ::= terma ([-+*/] terma)*\nterma ::= [0-9]+',
165
+ "1",
166
+ [
167
+ {"type": "char", "value": [[48, 57]]},
168
+ {"type": "char", "value": [45, 43, 42, 47]},
169
+ {"type": "char", "value": [61]},
170
+ ],
171
+ ),
172
+ (
173
+ 'root ::= (expr "=" termb "\n")+\nexpr ::= termb ([-+*/] termb)*\ntermb ::= [0-9]+',
174
+ "1+",
175
+ [{"type": "char", "value": [[48, 57]]}],
176
+ ),
177
+ (
178
+ '\nroot ::= (expr "=" termc "\n")+\nexpr ::= termc ([-+*/] termc)*\ntermc ::= [0-9]+',
179
+ "1=",
180
+ [{"type": "char", "value": [[48, 57]]}],
181
+ ),
182
+ (
183
+ 'root ::= (expr "=" termd "\n")+\nexpr ::= termd ([-+*/] termd)*\ntermd ::= [0-9]+',
184
+ "1+1",
185
+ [
186
+ {"type": "char", "value": [[48, 57]]},
187
+ {"type": "char", "value": [45, 43, 42, 47]},
188
+ {"type": "char", "value": [61]},
189
+ ],
190
+ ),
191
+ (
192
+ 'root ::= (expr "=" term "\n")+\nexpr ::= term ([-+*/] term)*\nterm ::= [0-9]+',
193
+ "1=1",
194
+ [{"type": "char", "value": [[48, 57]]}, {"type": "char", "value": [10]}],
195
+ ),
196
+ (
197
+ 'root ::= "\\"" ( [^\\"abcdefghA-Z])* ',
198
+ '"is not only in its lyrism, its vow to sustin it in its poo-sion, its r,v:l\'y, it\'s tory,',
199
+ [
200
+ {"type": "char_exclude", "value": [34, 97, 98, 99, 100, 101, 102, 103, 104, [65, 90]]},
201
+ {"type": "end"},
202
+ ],
203
+ ),
204
+ (
205
+ "root ::= ( [^abcdefgh] | [b-z])* ",
206
+ "bcdefghzyxw0123ABCZ",
207
+ [
208
+ {"type": "char_exclude", "value": [97, 98, 99, 100, 101, 102, 103, 104]},
209
+ {"type": "char", "value": [[98, 122]]},
210
+ {"type": "end"},
211
+ ],
212
+ ),
213
+ ]
214
+
215
+
216
+ def describe_iteration():
217
+ @pytest.mark.parametrize(("grammar", "input_string", "expected"), CASES)
218
+ def it_returns_parse_state_for_a_grammar_and_initial_string(grammar, input_string, expected):
219
+ state = GBNF(grammar)
220
+ state = state.add(input_string)
221
+ assert list(state) == expected
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/integration/validation/__init__.py ADDED
File without changes
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/integration/validation/test_validate_grammar.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/integration-tests/generated/validation/validate-grammar.test.ts.
2
+
3
+ The data tables were extracted mechanically from the TypeScript source."""
4
+
5
+ import pytest
6
+
7
+ from gbnf import GBNF, GrammarParseError
8
+
9
+ VALID_GRAMMARS = [
10
+ 'root ::= "foo"',
11
+ 'root ::= "foo" | "bar"',
12
+ 'root ::= ("foo" | "bar")',
13
+ 'root ::= ("foo" | "bar")?',
14
+ 'root ::= ("foo" | "bar")*',
15
+ 'root ::= ("foo" | "bar")+',
16
+ "root ::= [a-z]",
17
+ "root ::= [a-zA-Z]",
18
+ "root ::= [a-zA-Z0-9]",
19
+ "root ::= [a-zA-Z0-9]*",
20
+ "root ::= [a-zA-Z0-9]?",
21
+ "root ::= [a-z]+",
22
+ "root ::= [a-zA-Z0-9]+",
23
+ "root ::= ([a-zA-Z0-9])*",
24
+ "root ::= ([a-zA-Z0-9])?",
25
+ "root ::= ([a-zA-Z0-9])+",
26
+ '\n root ::= foo\n foo ::= "foo"',
27
+ '\n root ::= foo\n foo ::= "foo" | "bar" | ([a-z])?\n ',
28
+ ]
29
+
30
+ INVALID_GRAMMARS = [
31
+ ("", 0, "No rules were found"),
32
+ ('root = "foo"', 5, "Expecting ::= at 5"),
33
+ ('root ::= foo\nfoo := "foo"\n ', 17, "Expecting ::= at 17"),
34
+ ("root ::= foo", 9, 'Undefined rule identifier "foo"'),
35
+ ('root ::= foo\nbar ::= "bar"\n ', 9, 'Undefined rule identifier "foo"'),
36
+ ('root ::= foo\nfoo ::= baz\nbar ::= "bar"\n ', 21, 'Undefined rule identifier "baz"'),
37
+ ("root ::= foo ::= bar", 13, "Expecting newline or end at 13"),
38
+ ('root ::= ([a-z]\nfoo ::= "foo"\n ', 20, "Expecting ')' at 20"),
39
+ ]
40
+
41
+
42
+ def describe_validate_grammar():
43
+ @pytest.mark.parametrize("grammar", VALID_GRAMMARS)
44
+ def it_parses_a_grammar(grammar):
45
+ assert GBNF(grammar)
46
+
47
+ @pytest.mark.parametrize(("grammar", "error_pos", "error_reason"), INVALID_GRAMMARS)
48
+ def it_reports_an_error_for_an_invalid_grammar(grammar, error_pos, error_reason):
49
+ with pytest.raises(GrammarParseError) as excinfo:
50
+ GBNF(grammar)
51
+ assert str(excinfo.value) == str(GrammarParseError(grammar, error_pos, error_reason))
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/integration/validation/test_validate_input.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/integration-tests/generated/validation/validate-input.test.ts.
2
+
3
+ The data tables were extracted mechanically from the TypeScript source."""
4
+
5
+ import pytest
6
+
7
+ from gbnf import GBNF, InputParseError
8
+
9
+ VALID_INPUTS = [
10
+ ('root ::= "foo"', "f"),
11
+ ]
12
+
13
+ INVALID_INPUTS = [
14
+ ('root ::= "foo"', "2", 0),
15
+ ('root ::= "foo"', "b", 0),
16
+ ('root ::= "foo"', "f1", 1),
17
+ ('root ::= "foo"', "fo1", 2),
18
+ ('root ::= "foo"', "fooo", 3),
19
+ ('root ::= "foo"', "fooooooo", 3),
20
+ ('root ::= "foo" | "bar"', "1", 0),
21
+ ('root ::= "foo" | "bar"', "z", 0),
22
+ ('root ::= "foo" | "bar"', "f1", 1),
23
+ ('root ::= "foo" | "bar"', "b1", 1),
24
+ ('root ::= "foo" | "bar"', "fo1", 2),
25
+ ('root ::= "foo" | "bar"', "ba1", 2),
26
+ ('root ::= "foo" | "bar"', "fooo", 3),
27
+ ('root ::= "foo" | "bar"', "barrr", 3),
28
+ ('root ::= "foo" | "bar" | "baz"', "1", 0),
29
+ ('root ::= "foo" | "bar" | "baz"', "z", 0),
30
+ ('root ::= "foo" | "bar" | "baz"', "f1", 1),
31
+ ('root ::= "foo" | "bar" | "baz"', "b1", 1),
32
+ ('root ::= "foo" | "bar" | "baz"', "fo1", 2),
33
+ ('root ::= "foo" | "bar" | "baz"', "bal", 2),
34
+ ('root ::= "foo" | "bar" | "baz"', "fooo", 3),
35
+ ('root ::= "foo" | "bar" | "baz"', "bazrr", 3),
36
+ ("root ::= [^a]", "a", 0),
37
+ ("root ::= [^abc]", "b", 0),
38
+ ("root ::= [^a-z]", "z", 0),
39
+ ("root ::= [^a-zA-Z]", "X", 0),
40
+ ("root ::= [^a-zA-Z0-9]", "8", 0),
41
+ ('\n root ::= foo\n foo ::= "foo"', "1", 0),
42
+ ('\n root ::= foo\n foo ::= "foo"', "b", 0),
43
+ ('\n root ::= foo\n foo ::= "foo"', "f1", 1),
44
+ ('\n root ::= foo\n foo ::= "foo"', "fo1", 2),
45
+ ('\n root ::= foo\n foo ::= "foo"', "fooo", 3),
46
+ ('\n root::=foo|"bar"\n foo::="foo"', "1", 0),
47
+ ('\n root::=foo|"bar"\n foo::="foo"', "z", 0),
48
+ ('\n root::=foo|"bar"\n foo::="foo"', "f1", 1),
49
+ ('\n root::=foo|"bar"\n foo::="foo"', "b1", 1),
50
+ ('\n root::=foo|"bar"\n foo::="foo"', "fo1", 2),
51
+ ('\n root::=foo|"bar"\n foo::="foo"', "fooo", 3),
52
+ ('\n root::=foo|"bar"\n foo::="foo"', "barr", 3),
53
+ ('\n root::= foo | bar\n foo::="foo"\n bar::="bar"', "1", 0),
54
+ ('\n root::= foo | bar\n foo::="foo"\n bar::="bar"', "z", 0),
55
+ ('\n root::= foo | bar\n foo::="foo"\n bar::="bar"', "f1", 1),
56
+ ('\n root::= foo | bar\n foo::="foo"\n bar::="bar"', "b1", 1),
57
+ ('\n root::= foo | bar\n foo::="foo"\n bar::="bar"', "fo1", 2),
58
+ ('\n root::= foo | bar\n foo::="foo"\n bar::="bar"', "fooo", 3),
59
+ ('\n root::= foo | bar\n foo::="foo"\n bar::="bar"', "barr", 3),
60
+ (
61
+ '\n root ::= f | b\n f ::= fo\n b ::= ba\n fo ::= foo\n ba ::= bar | baz\n foo ::= "foo"\n bar ::= "bar"\n baz ::= "baz"\n ',
62
+ "1",
63
+ 0,
64
+ ),
65
+ (
66
+ '\n root ::= f | b\n f ::= fo\n b ::= ba\n fo ::= foo\n ba ::= bar | baz\n foo ::= "foo"\n bar ::= "bar"\n baz ::= "baz"',
67
+ "z",
68
+ 0,
69
+ ),
70
+ (
71
+ '\n root ::= f | b\n f ::= fo\n b ::= ba\n fo ::= foo\n ba ::= bar | baz\n foo ::= "foo"\n bar ::= "bar"\n baz ::= "baz"',
72
+ "f1",
73
+ 1,
74
+ ),
75
+ (
76
+ '\n root ::= f | b\n f ::= fo\n b ::= ba\n fo ::= foo\n ba ::= bar | baz\n foo ::= "foo"\n bar ::= "bar"\n baz ::= "baz"',
77
+ "b1",
78
+ 1,
79
+ ),
80
+ (
81
+ '\n root ::= f | b\n f ::= fo\n b ::= ba\n fo ::= foo\n ba ::= bar | baz\n foo ::= "foo"\n bar ::= "bar"\n baz ::= "baz"',
82
+ "fo1",
83
+ 2,
84
+ ),
85
+ (
86
+ '\n root ::= f | b\n f ::= fo\n b ::= ba\n fo ::= foo\n ba ::= bar | baz\n foo ::= "foo"\n bar ::= "bar"\n baz ::= "baz"',
87
+ "fooo",
88
+ 3,
89
+ ),
90
+ (
91
+ '\n root ::= f | b\n f ::= fo\n b ::= ba\n fo ::= foo\n ba ::= bar | baz\n foo ::= "foo"\n bar ::= "bar"\n baz ::= "baz"',
92
+ "barr",
93
+ 3,
94
+ ),
95
+ (
96
+ '\n root ::= f | b\n f ::= fo\n b ::= ba\n fo ::= foo\n ba ::= bar | baz\n foo ::= "foo"\n bar ::= "bar"\n baz ::= "baz"',
97
+ "bazr",
98
+ 3,
99
+ ),
100
+ ("root ::= [a-z]", "A", 0),
101
+ ("root ::= [a-z]", "0", 0),
102
+ ("root ::= [a-z]", "az", 1),
103
+ ("root ::= [a-z]?", "A", 0),
104
+ ("root ::= [a-z]?", "0", 0),
105
+ ("root ::= [a-z]?", "az", 1),
106
+ ("root ::= [a-z]+", "A", 0),
107
+ ("root ::= [a-z]+", "0", 0),
108
+ ("root ::= [a-z]+", "az0", 2),
109
+ ("root ::= [a-z]*", "A", 0),
110
+ ("root ::= [a-z]*", "0", 0),
111
+ ("root ::= [a-z]*", "az0", 2),
112
+ ("root ::= ( [^abcdefgh] | [b-z])*", "a", 0),
113
+ ]
114
+
115
+
116
+ def describe_validate_input():
117
+ @pytest.mark.parametrize(("grammar", "input_string"), VALID_INPUTS)
118
+ def it_parses_a_grammar_and_input(grammar, input_string):
119
+ assert GBNF(grammar, input_string)
120
+
121
+ @pytest.mark.parametrize(("grammar", "input_string", "error_pos"), INVALID_INPUTS)
122
+ def it_reports_an_error_for_an_invalid_input(grammar, input_string, error_pos):
123
+ error = InputParseError(input_string, error_pos, "")
124
+ with pytest.raises(InputParseError) as excinfo:
125
+ GBNF(grammar).add(input_string)
126
+ assert str(excinfo.value) == str(error)
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/__init__.py ADDED
File without changes
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/__init__.py ADDED
File without changes
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_colorize.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/src/grammar-graph/colorize.test.ts."""
2
+
3
+ from gbnf.grammar_graph.colorize import Color, colorize, plain
4
+
5
+
6
+ def describe_colorize():
7
+ def it_should_colorize_strings_correctly():
8
+ assert colorize("hello", Color.BLUE) == "\x1b[34mhello"
9
+ assert colorize("test", Color.CYAN) == "\x1b[36mtest"
10
+ assert colorize("example", Color.GREEN) == "\x1b[32mexample"
11
+
12
+ def it_should_colorize_numbers_correctly():
13
+ assert colorize(123, Color.RED) == "\x1b[31m123"
14
+ assert colorize(456, Color.GRAY) == "\x1b[90m456"
15
+ assert colorize(789, Color.YELLOW) == "\x1b[33m789"
16
+
17
+ def it_should_leave_values_plain():
18
+ assert plain("hello", Color.BLUE) == "hello"
19
+ assert plain(123, Color.RED) == "123"
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_generic_set.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/src/grammar-graph/generic-set.test.ts."""
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
+ generic_set = GenericSet(lambda el: el)
12
+ generic_set.add(1)
13
+ generic_set.add(2)
14
+ generic_set.add(3)
15
+ assert list(generic_set) == [1, 2, 3]
16
+
17
+ def it_skips_duplicates():
18
+ generic_set = GenericSet(lambda el: el)
19
+ generic_set.add(1)
20
+ generic_set.add(1)
21
+ generic_set.add(1)
22
+ assert list(generic_set) == [1]
23
+
24
+ def it_skips_duplicates_based_on_a_given_key():
25
+ generic_set = GenericSet(lambda el: el["id"])
26
+ generic_set.add({"id": 1})
27
+ generic_set.add({"id": 1, "foo": "foo"})
28
+ generic_set.add({"id": 1, "bar": "bar"})
29
+ assert list(generic_set) == [{"id": 1}]
30
+
31
+ def it_can_delete():
32
+ generic_set = GenericSet(lambda el: el["id"])
33
+ generic_set.add({"id": 1})
34
+ generic_set.delete({"id": 1})
35
+ generic_set.add({"id": 2, "foo": "foo"})
36
+ generic_set.add({"id": 2, "bar": "bar"})
37
+ assert list(generic_set) == [{"id": 2, "foo": "foo"}]
38
+
39
+ def it_reports_size_and_membership():
40
+ generic_set = GenericSet(lambda el: el["id"])
41
+ first = {"id": 1}
42
+ generic_set.add(first)
43
+ generic_set.add({"id": 2})
44
+ assert generic_set.size == 2
45
+ assert len(generic_set) == 2
46
+ assert generic_set.has(first)
47
+ assert {"id": 1} in generic_set
48
+ assert generic_set.get({"id": 1}) is first
49
+ assert generic_set.get({"id": 3}) is None
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_get_input_as_code_points.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/src/grammar-graph/get-input-as-code-points.test.ts."""
2
+
3
+ from gbnf.grammar_graph.get_input_as_code_points import get_input_as_code_points
4
+
5
+
6
+ def describe_get_input_as_code_points():
7
+ def it_returns_code_points_for_string():
8
+ assert get_input_as_code_points("abc") == [97, 98, 99]
9
+
10
+ def it_returns_code_points_for_number():
11
+ assert get_input_as_code_points(99) == [99]
12
+
13
+ def it_returns_code_points_for_array_of_number():
14
+ assert get_input_as_code_points([99, 100, 101]) == [99, 100, 101]
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_get_parent_stack_id.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/src/grammar-graph/get-parent-stack-id.test.ts."""
2
+
3
+ import json
4
+ from types import SimpleNamespace
5
+
6
+ from gbnf.grammar_graph.colorize import Color
7
+ from gbnf.grammar_graph.get_parent_stack_id import get_parent_stack_id
8
+ from gbnf.grammar_graph.graph_node import GraphNodeMeta
9
+
10
+
11
+ def _s(value: str) -> str:
12
+ return json.dumps(value)
13
+
14
+
15
+ def _mock_colorize(text, color):
16
+ return f"[{_s(color)}]:{text}"
17
+
18
+
19
+ RED = _s(Color.RED)
20
+ GRAY = _s(Color.GRAY)
21
+
22
+
23
+ def _create_mock_pointer(stack_id, path_id, step_id, parent=None):
24
+ return SimpleNamespace(
25
+ node=SimpleNamespace(meta=GraphNodeMeta(stack_id, path_id, step_id)),
26
+ parent=parent,
27
+ )
28
+
29
+
30
+ def describe_get_parent_stack_id():
31
+ def it_returns_an_empty_string_if_no_parents():
32
+ pointer = _create_mock_pointer(1, 1, 1)
33
+ assert get_parent_stack_id(pointer, _mock_colorize) == ""
34
+
35
+ def it_returns_a_single_parent_id_colored_correctly():
36
+ parent_pointer = _create_mock_pointer(1, 1, 1)
37
+ pointer = _create_mock_pointer(2, 2, 2, parent_pointer)
38
+ assert get_parent_stack_id(pointer, _mock_colorize) == 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
+ assert get_parent_stack_id(pointer, _mock_colorize) == f"[{RED}]:1,1,1[{GRAY}]:<-[{RED}]:0,0,0"
45
+
46
+ def it_handles_deep_nesting_of_pointers():
47
+ great_grandparent_pointer = _create_mock_pointer(0, 0, 0)
48
+ grandparent_pointer = _create_mock_pointer(1, 1, 1, great_grandparent_pointer)
49
+ parent_pointer = _create_mock_pointer(2, 2, 2, grandparent_pointer)
50
+ pointer = _create_mock_pointer(3, 3, 3, parent_pointer)
51
+ expected_output = f"[{RED}]:2,2,2[{GRAY}]:<-[{RED}]:1,1,1[{GRAY}]:<-[{RED}]:0,0,0"
52
+ assert get_parent_stack_id(pointer, _mock_colorize) == expected_output
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_get_serialized_rule_key.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/src/grammar-graph/get-serialized-rule-key.test.ts."""
2
+
3
+ import re
4
+
5
+ import pytest
6
+
7
+ from gbnf.grammar_graph.get_serialized_rule_key import KEY_TRANSLATION, get_serialized_rule_key
8
+ from gbnf.grammar_graph.rule_ref import RuleRef
9
+ from gbnf.grammar_graph.types import RuleType
10
+
11
+
12
+ def describe_get_serialized_rule_key():
13
+ def it_returns_type_for_end_rules():
14
+ rule = {"type": RuleType.END}
15
+ assert get_serialized_rule_key(rule) == f"{KEY_TRANSLATION[RuleType.END]}"
16
+
17
+ def it_returns_type_and_value_for_character_rules():
18
+ rule = {"type": RuleType.CHAR, "value": [97]}
19
+ assert get_serialized_rule_key(rule) == f"{KEY_TRANSLATION[RuleType.CHAR]}-[97]"
20
+
21
+ def it_returns_type_and_value_for_character_exclude_rules():
22
+ rule = {"type": RuleType.CHAR_EXCLUDE, "value": [97]}
23
+ assert get_serialized_rule_key(rule) == f"{KEY_TRANSLATION[RuleType.CHAR_EXCLUDE]}-[97]"
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(ValueError, match=re.escape('Unknown rule type: {"type":"UNKNOWN","value":"something"}')):
32
+ get_serialized_rule_key(rule)
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_graph.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/src/grammar-graph/graph.test.ts."""
2
+
3
+ import pytest
4
+
5
+ from gbnf.grammar_graph.generic_set import GenericSet
6
+ from gbnf.grammar_graph.graph import Graph
7
+ from gbnf.grammar_graph.rule_ref import RuleRef
8
+ from gbnf.grammar_graph.types import RuleType
9
+ from gbnf.utils.errors.input_parse_error import InputParseError
10
+
11
+
12
+ def describe_graph():
13
+ grammar = "example-grammar"
14
+ stacked_rules = [
15
+ [
16
+ [{"type": RuleType.CHAR, "value": [65, 66, 67]}],
17
+ [{"type": RuleType.CHAR, "value": [68, 69, 70]}],
18
+ ],
19
+ [
20
+ [{"type": RuleType.CHAR, "value": [71, 72, 73]}],
21
+ [{"type": RuleType.CHAR, "value": [74, 75, 76]}],
22
+ ],
23
+ ]
24
+ root_id = 0
25
+
26
+ def it_should_create_a_graph_instance():
27
+ graph = Graph(grammar, stacked_rules, root_id)
28
+ assert isinstance(graph, Graph)
29
+ assert graph.grammar == grammar
30
+
31
+ def it_should_get_the_root_node():
32
+ graph = Graph(grammar, stacked_rules, root_id)
33
+ root_node = graph._get_root_node(root_id)
34
+ assert isinstance(root_node, dict)
35
+ assert len(root_node) == 2
36
+
37
+ def it_should_throw_if_the_root_node_does_not_exist():
38
+ with pytest.raises(KeyError, match="Root node not found for value: 5"):
39
+ Graph(grammar, stacked_rules, 5)
40
+
41
+ def it_should_get_the_initial_pointers():
42
+ graph = Graph(grammar, stacked_rules, root_id)
43
+ pointers = graph._get_initial_pointers()
44
+ assert isinstance(pointers, GenericSet)
45
+ assert pointers.size == 2
46
+
47
+ def it_should_share_one_rule_object_between_identical_rules():
48
+ graph = Graph(
49
+ grammar,
50
+ [[[{"type": RuleType.CHAR, "value": [65]}, {"type": RuleType.END}], [{"type": RuleType.CHAR, "value": [65]}]]],
51
+ root_id,
52
+ )
53
+ first, second = graph._get_root_node(root_id).values()
54
+ assert first.rule is second.rule
55
+
56
+ def it_should_resolve_rule_references_to_the_referenced_nodes():
57
+ graph = Graph(
58
+ grammar,
59
+ [
60
+ [[RuleRef(1), {"type": RuleType.END}]],
61
+ [[{"type": RuleType.CHAR, "value": [65]}, {"type": RuleType.END}]],
62
+ ],
63
+ root_id,
64
+ )
65
+ (root,) = graph._get_root_node(0).values()
66
+ assert root.rule.nodes == list(graph._get_root_node(1).values())
67
+ assert [pointer.node.rule for pointer in graph._get_initial_pointers()] == [
68
+ {"type": RuleType.CHAR, "value": [65]},
69
+ ]
70
+
71
+ def describe_add():
72
+ complete_rules = [
73
+ [
74
+ [{"type": RuleType.CHAR, "value": [65]}, {"type": RuleType.CHAR, "value": [66]}, {"type": RuleType.END}],
75
+ [{"type": RuleType.CHAR, "value": [67]}, {"type": RuleType.END}],
76
+ ],
77
+ ]
78
+
79
+ def it_should_advance_pointers_over_matching_input():
80
+ graph = Graph(grammar, complete_rules, root_id)
81
+ pointers = graph.add("A")
82
+ assert [pointer.rule for pointer in pointers] == [{"type": RuleType.CHAR, "value": [66]}]
83
+ assert [pointer.rule for pointer in graph.add("B", pointers)] == [{"type": RuleType.END}]
84
+
85
+ def it_should_throw_on_input_that_matches_nothing():
86
+ graph = Graph(grammar, complete_rules, root_id)
87
+ with pytest.raises(InputParseError) as excinfo:
88
+ graph.add("AZ")
89
+ assert str(excinfo.value) == str(InputParseError("AZ", 1))
90
+
91
+ def describe_print():
92
+ def it_should_print_the_graph():
93
+ graph = Graph(grammar, stacked_rules, root_id)
94
+ printed_graph = graph.print(colors=True)
95
+ assert isinstance(printed_graph, str)
96
+ assert "ABC" in printed_graph
97
+ assert "DEF" in printed_graph
98
+ assert "GHI" in printed_graph
99
+
100
+ def it_should_print_without_colors_by_default():
101
+ graph = Graph(grammar, stacked_rules, root_id)
102
+ 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/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_graph_node.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/src/grammar-graph/graph-node.test.ts."""
2
+
3
+ from unittest.mock import patch
4
+
5
+ import pytest
6
+
7
+ from gbnf.grammar_graph.colorize import colorize
8
+ from gbnf.grammar_graph.graph_node import GraphNode, GraphNodeMeta
9
+ from gbnf.grammar_graph.rule_ref import RuleRef
10
+
11
+
12
+ def describe_graph_node():
13
+ meta = GraphNodeMeta(stack_id=1, path_id=2, step_id=3)
14
+ rule = RuleRef(42)
15
+
16
+ def it_should_construct_with_a_rule_and_meta():
17
+ node = GraphNode(rule, meta)
18
+ assert node.rule is rule
19
+ assert node.meta == meta
20
+
21
+ def it_should_throw_if_meta_is_undefined():
22
+ with pytest.raises(TypeError, match="Meta is undefined"):
23
+ GraphNode(rule, None)
24
+
25
+ def it_should_correctly_calculate_and_cache_its_id():
26
+ node = GraphNode(rule, meta)
27
+ assert node.id == "1,2,3"
28
+ node.meta = GraphNodeMeta(stack_id=4, path_id=5, step_id=6)
29
+ # Check if it uses cached value on second access
30
+ assert node.id == "1,2,3"
31
+
32
+ def it_should_delegate_print_to_the_print_graph_node_function():
33
+ node = GraphNode(rule, meta)
34
+ with patch("gbnf.grammar_graph.graph_node.print_graph_node", return_value="printed") as print_graph_node:
35
+ assert node.print(colorize=colorize, show_position=False) == "printed"
36
+ print_graph_node.assert_called_once_with(node, colorize=colorize, pointers=None, show_position=False)
37
+
38
+ def it_should_handle_next_node_linkage():
39
+ next_node = GraphNode(RuleRef(43), meta)
40
+ node = GraphNode(rule, meta, next_node)
41
+ assert node.next is next_node
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_graph_pointer.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/src/grammar-graph/graph-pointer.test.ts."""
2
+
3
+ import pytest
4
+
5
+ from gbnf.grammar_graph.graph_node import GraphNode, GraphNodeMeta
6
+ from gbnf.grammar_graph.graph_pointer import GraphPointer
7
+ from gbnf.grammar_graph.rule_ref import RuleRef
8
+ from gbnf.grammar_graph.types import RuleType
9
+
10
+
11
+ def describe_graph_pointer():
12
+ def describe_constructor():
13
+ def it_initializes_correctly_with_just_node_parameter():
14
+ node = GraphNode({"type": RuleType.END}, GraphNodeMeta(stack_id=1, path_id=2, step_id=3))
15
+ pointer = GraphPointer(node)
16
+ assert pointer.node is node
17
+ assert pointer.id == "1,2,3"
18
+ assert pointer.parent is None
19
+
20
+ def it_throws_an_error_if_node_is_undefined():
21
+ with pytest.raises(TypeError, match="Node is undefined"):
22
+ GraphPointer(None)
23
+
24
+ def it_initializes_correctly_with_node_and_parent():
25
+ parent_node = GraphNode({"type": RuleType.CHAR, "value": [97]}, GraphNodeMeta(1, 2, 3))
26
+ child_node = GraphNode({"type": RuleType.CHAR, "value": [98]}, GraphNodeMeta(4, 5, 6))
27
+ parent_pointer = GraphPointer(parent_node)
28
+ child_pointer = GraphPointer(child_node, parent_pointer)
29
+ assert child_pointer.parent is parent_pointer
30
+ assert child_pointer.id == "1,2,3-4,5,6"
31
+
32
+ def describe_resolve_method():
33
+ def it_throws_an_error_on_unknown_rule_types():
34
+ # Create a GraphNode with an unhandled rule type
35
+ unhandled_rule = {"type": "UNKNOWN_RULE_TYPE"}
36
+ node = GraphNode(unhandled_rule, GraphNodeMeta(1, 2, 3))
37
+ pointer = GraphPointer(node)
38
+
39
+ with pytest.raises(ValueError, match="Unknown rule"):
40
+ list(pointer.resolve())
41
+
42
+ def it_yields_a_char_rule():
43
+ rule = {"type": RuleType.CHAR, "value": [97]}
44
+ node = GraphNode(rule, GraphNodeMeta(1, 2, 3))
45
+ pointer = GraphPointer(node)
46
+ assert list(pointer.resolve()) == [pointer]
47
+
48
+ def it_yields_a_char_excluded_rule():
49
+ rule = {"type": RuleType.CHAR_EXCLUDE, "value": [97]}
50
+ node = GraphNode(rule, GraphNodeMeta(1, 2, 3))
51
+ pointer = GraphPointer(node)
52
+ assert list(pointer.resolve()) == [pointer]
53
+
54
+ def it_yields_an_end_rule_without_a_parent():
55
+ rule = {"type": RuleType.END}
56
+ node = GraphNode(rule, GraphNodeMeta(1, 2, 3))
57
+ pointer = GraphPointer(node)
58
+ assert list(pointer.resolve()) == [pointer]
59
+
60
+ def it_yields_an_end_rule_with_a_parent():
61
+ rule = {"type": RuleType.END}
62
+ parent_pointer = GraphPointer(GraphNode(rule, GraphNodeMeta(2, 1, 1)))
63
+ child_pointer = GraphPointer(GraphNode(rule, GraphNodeMeta(1, 1, 1)), parent_pointer)
64
+ assert list(child_pointer.resolve()) == [parent_pointer]
65
+
66
+ def it_yields_an_end_rule_with_a_grandparent():
67
+ rule = {"type": RuleType.END}
68
+ grand_parent_pointer = GraphPointer(GraphNode(rule, GraphNodeMeta(2, 1, 1)))
69
+ parent_pointer = GraphPointer(GraphNode(rule, GraphNodeMeta(2, 1, 1)), grand_parent_pointer)
70
+ child_pointer = GraphPointer(GraphNode(rule, GraphNodeMeta(1, 1, 1)), parent_pointer)
71
+ assert list(child_pointer.resolve()) == [grand_parent_pointer]
72
+
73
+ def it_yields_an_end_rules_parent_that_is_not_an_end_with_a_grandparent():
74
+ rule = {"type": RuleType.END}
75
+ grand_parent_pointer = GraphPointer(GraphNode(rule, GraphNodeMeta(2, 1, 1)))
76
+ parent_pointer = GraphPointer(
77
+ GraphNode({"type": RuleType.CHAR, "value": [97]}, GraphNodeMeta(2, 1, 1)), grand_parent_pointer
78
+ )
79
+ child_pointer = GraphPointer(GraphNode(rule, GraphNodeMeta(1, 1, 1)), parent_pointer)
80
+ assert list(child_pointer.resolve()) == [parent_pointer]
81
+
82
+ def it_descends_into_a_rule_reference():
83
+ referenced = GraphNode({"type": RuleType.CHAR, "value": [97]}, GraphNodeMeta(1, 0, 0))
84
+ rule_ref = RuleRef(1)
85
+ rule_ref.nodes = [referenced]
86
+ ref_node = GraphNode(rule_ref, GraphNodeMeta(0, 0, 0))
87
+ ref_pointer = GraphPointer(ref_node)
88
+ (resolved,) = ref_pointer.resolve()
89
+ assert resolved.node is referenced
90
+ assert resolved.parent is ref_pointer
91
+
92
+ def it_throws_when_a_resolved_reference_has_no_next_node():
93
+ ref_node = GraphNode(RuleRef(1), GraphNodeMeta(0, 0, 0))
94
+ with pytest.raises(RuntimeError, match="No next node"):
95
+ list(GraphPointer(ref_node).resolve(True))
96
+
97
+ def describe_fetch_next_method():
98
+ def it_yields_nothing_when_marked_invalid():
99
+ next_node = GraphNode({"type": RuleType.END}, GraphNodeMeta(0, 0, 1))
100
+ node = GraphNode({"type": RuleType.CHAR, "value": [97]}, GraphNodeMeta(0, 0, 0), next_node)
101
+ pointer = GraphPointer(node)
102
+ pointer.valid = False
103
+ assert list(pointer.fetch_next()) == []
104
+
105
+ def it_yields_the_resolved_next_node():
106
+ next_node = GraphNode({"type": RuleType.END}, GraphNodeMeta(0, 0, 1))
107
+ node = GraphNode({"type": RuleType.CHAR, "value": [97]}, GraphNodeMeta(0, 0, 0), next_node)
108
+ pointer = GraphPointer(node)
109
+ pointer.valid = True
110
+ (next_pointer,) = pointer.fetch_next()
111
+ assert next_pointer.node is next_node
112
+
113
+ def it_yields_nothing_for_an_end_node_without_a_parent():
114
+ pointer = GraphPointer(GraphNode({"type": RuleType.END}, GraphNodeMeta(0, 0, 0)))
115
+ assert list(pointer.fetch_next()) == []
116
+
117
+ def it_throws_when_there_is_no_next_node():
118
+ pointer = GraphPointer(GraphNode({"type": RuleType.CHAR, "value": [97]}, GraphNodeMeta(0, 0, 0)))
119
+ with pytest.raises(RuntimeError, match="No next node"):
120
+ list(pointer.fetch_next())
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_parse_state.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/src/grammar-graph/parse-state.test.ts."""
2
+
3
+ from types import SimpleNamespace
4
+ from unittest.mock import Mock
5
+
6
+ from gbnf.grammar_graph.parse_state import ParseState
7
+
8
+
9
+ # Mock implementations for Graph and Pointers
10
+ mock_graph = Mock(grammar="sample-grammar")
11
+ mock_pointers = [SimpleNamespace(rule={"type": "char", "value": [65]})] # ASCII 'A'
12
+
13
+
14
+ def describe_parse_state():
15
+ def it_constructs_with_given_graph_and_pointers():
16
+ parse_state = ParseState(mock_graph, mock_pointers)
17
+ assert isinstance(parse_state, ParseState)
18
+
19
+ def it_returns_unique_rules_from_pointers():
20
+ parse_state = ParseState(mock_graph, mock_pointers)
21
+ rules = list(parse_state.rules())
22
+ assert len(rules) == 1
23
+ assert rules[0]["value"] == [65]
24
+
25
+ def it_de_duplicates_rules_with_identical_content():
26
+ pointers = [
27
+ SimpleNamespace(rule={"type": "char", "value": [65]}),
28
+ SimpleNamespace(rule={"type": "char", "value": [65]}),
29
+ SimpleNamespace(rule={"type": "end"}),
30
+ ]
31
+ parse_state = ParseState(mock_graph, pointers)
32
+ assert list(parse_state) == [{"type": "char", "value": [65]}, {"type": "end"}]
33
+
34
+ def it_iterates_over_unique_rules_using_iter():
35
+ parse_state = ParseState(mock_graph, mock_pointers)
36
+ rules = [*parse_state]
37
+ assert len(rules) == 1
38
+ assert rules[0]["type"] == "char"
39
+
40
+ def it_adds_new_input_and_returns_new_parse_state_with_updated_pointers():
41
+ new_pointers = [SimpleNamespace(rule={"type": "char", "value": [66]})] # ASCII 'B'
42
+ mock_graph.add.return_value = new_pointers
43
+
44
+ parse_state = ParseState(mock_graph, mock_pointers)
45
+ new_state = parse_state.add("B")
46
+ assert isinstance(new_state, ParseState)
47
+ assert new_state is not parse_state
48
+ mock_graph.add.assert_called_with("B", mock_pointers)
49
+ assert list(new_state) == [{"type": "char", "value": [66]}]
50
+
51
+ def it_calculates_the_size_of_unique_rules_correctly():
52
+ parse_state = ParseState(mock_graph, mock_pointers)
53
+ assert parse_state.size == 1
54
+
55
+ def it_provides_access_to_the_underlying_graph_grammar():
56
+ parse_state = ParseState(mock_graph, mock_pointers)
57
+ assert parse_state.grammar == "sample-grammar"
58
+
59
+ def it_is_callable_as_a_function():
60
+ parse_state = ParseState(mock_graph, mock_pointers)
61
+ new_state = parse_state("B")
62
+ assert isinstance(new_state, ParseState)
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_print.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/src/grammar-graph/print.test.ts."""
2
+
3
+ from types import SimpleNamespace
4
+ from unittest.mock import patch
5
+
6
+ from gbnf.grammar_graph.colorize import Color
7
+ from gbnf.grammar_graph.print import print_graph_node, print_graph_pointer
8
+ from gbnf.grammar_graph.rule_ref import RuleRef
9
+ from gbnf.grammar_graph.types import RuleType
10
+
11
+ COLORS = {
12
+ Color.BLUE: "BLUE",
13
+ Color.CYAN: "CYAN",
14
+ Color.GREEN: "GREEN",
15
+ Color.RED: "RED",
16
+ Color.GRAY: "GRAY",
17
+ Color.YELLOW: "YELLOW",
18
+ }
19
+
20
+
21
+ def _mock_colorize(text, color):
22
+ return f"[{COLORS[color]}]:{text}"
23
+
24
+
25
+ def _create_mock_node(id_, rule, next_node=None):
26
+ return SimpleNamespace(id=id_, rule=rule, next=next_node, print=lambda **_kwargs: f"Node({id_})")
27
+
28
+
29
+ def _create_mock_graph_pointer(node):
30
+ return SimpleNamespace(node=node, parent=None, print=lambda **_kwargs: f"Pointer to {node.id}")
31
+
32
+
33
+ def describe_print():
34
+ def describe_print_graph_pointer():
35
+ def it_prints_graph_pointer_details_correctly():
36
+ mock_node = _create_mock_node(1, RuleRef(100))
37
+ mock_pointer = _create_mock_graph_pointer(mock_node)
38
+ with patch("gbnf.grammar_graph.print.get_parent_stack_id", return_value="foo"):
39
+ result = print_graph_pointer(mock_pointer, colorize=_mock_colorize)
40
+ assert result == "[RED]:*foo"
41
+
42
+ def describe_print_graph_node():
43
+ def it_prints_a_graph_node_with_a_character_rule():
44
+ rule_char = {"type": RuleType.CHAR, "value": [65]} # ASCII for 'A'
45
+ node = _create_mock_node(1, rule_char)
46
+ result = print_graph_node(node, colorize=_mock_colorize, show_position=False)
47
+ assert result == "[GRAY]:[[YELLOW]:A[GRAY]:]"
48
+
49
+ def it_prints_a_graph_node_with_a_range():
50
+ node = _create_mock_node(1, {"type": RuleType.CHAR, "value": [[97, 99], 10]})
51
+ result = print_graph_node(node, colorize=_mock_colorize)
52
+ assert result == "[GRAY]:[[YELLOW]:[YELLOW]:a,[YELLOW]:c\\n[GRAY]:]"
53
+
54
+ def it_prints_a_graph_node_with_a_rule_reference():
55
+ rule_ref = RuleRef(200)
56
+ node = _create_mock_node(1, rule_ref)
57
+ result = print_graph_node(node, colorize=_mock_colorize, show_position=True)
58
+ assert result == "[BLUE]:{[GRAY]:1[BLUE]:}[GRAY]:Ref([GREEN]:200[GRAY]:)"
59
+
60
+ def it_prints_an_end_rule_followed_by_the_next_node():
61
+ next_node = _create_mock_node(2, {"type": RuleType.END})
62
+ node = _create_mock_node(1, {"type": RuleType.END}, next_node)
63
+ result = print_graph_node(node, colorize=_mock_colorize)
64
+ assert result == "[YELLOW]:end[GRAY]:-> Node(2)"
65
+
66
+ def it_prints_pointers_located_at_the_node():
67
+ node = _create_mock_node(1, {"type": RuleType.END})
68
+ pointers = [_create_mock_graph_pointer(node), _create_mock_graph_pointer(_create_mock_node(2, {"type": RuleType.END}))]
69
+ result = print_graph_node(node, colorize=_mock_colorize, pointers=pointers)
70
+ assert result == "[YELLOW]:end[GRAY]:[Pointer to 1[GRAY]:]"
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_rule_ref.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/src/grammar-graph/rule-ref.test.ts."""
2
+
3
+ from types import SimpleNamespace
4
+
5
+ import pytest
6
+
7
+ from gbnf.grammar_graph.rule_ref import RuleRef
8
+
9
+
10
+ def describe_rule_ref_class():
11
+ def it_initializes_with_a_given_value():
12
+ rule_ref = RuleRef(123)
13
+ assert rule_ref.value == 123
14
+
15
+ def it_allows_setting_and_getting_nodes():
16
+ mock_nodes = [SimpleNamespace()]
17
+ rule_ref = RuleRef(123)
18
+ rule_ref.nodes = mock_nodes # Set the nodes
19
+ assert rule_ref.nodes is mock_nodes # Get the nodes
20
+
21
+ def it_throws_an_error_if_trying_to_get_nodes_before_setting():
22
+ rule_ref = RuleRef(123)
23
+ with pytest.raises(RuntimeError, match="Nodes are not set"):
24
+ rule_ref.nodes
25
+
26
+ def it_compares_equal_to_a_reference_with_the_same_value():
27
+ assert RuleRef(1) == RuleRef(1)
28
+ assert RuleRef(1) != RuleRef(2)
29
+ assert RuleRef(1) != {"value": 1}
30
+ assert len({RuleRef(1), RuleRef(1)}) == 1
31
+
32
+ def it_has_a_readable_repr():
33
+ assert repr(RuleRef(7)) == "RuleRef(7)"
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_graph/test_type_guards.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/src/grammar-graph/type-guards.test.ts."""
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.type_guards import (
7
+ is_graph_pointer_rule_char,
8
+ is_graph_pointer_rule_char_exclude,
9
+ is_graph_pointer_rule_end,
10
+ is_graph_pointer_rule_ref,
11
+ is_range,
12
+ is_rule,
13
+ is_rule_char,
14
+ is_rule_char_excluded,
15
+ is_rule_end,
16
+ is_rule_ref,
17
+ is_rule_type,
18
+ )
19
+ from gbnf.grammar_graph.types import RuleType
20
+
21
+ META = GraphNodeMeta(stack_id=1, path_id=2, step_id=3)
22
+
23
+
24
+ def describe_rule_type_guards():
25
+ def describe_is_graph_pointer_rule_ref():
26
+ def it_returns_true():
27
+ pointer = GraphPointer(GraphNode(RuleRef(1), META))
28
+ assert is_graph_pointer_rule_ref(pointer) is True
29
+
30
+ def it_returns_false():
31
+ pointer = GraphPointer(GraphNode({"type": RuleType.END}, META))
32
+ assert is_graph_pointer_rule_ref(pointer) is False
33
+
34
+ def describe_is_graph_pointer_rule_end():
35
+ def it_returns_false():
36
+ pointer = GraphPointer(GraphNode(RuleRef(1), META))
37
+ assert is_graph_pointer_rule_end(pointer) is False
38
+
39
+ def it_returns_true():
40
+ pointer = GraphPointer(GraphNode({"type": RuleType.END}, META))
41
+ assert is_graph_pointer_rule_end(pointer) is True
42
+
43
+ def describe_is_graph_pointer_rule_char():
44
+ def it_returns_true():
45
+ pointer = GraphPointer(GraphNode({"type": RuleType.CHAR, "value": [97]}, META))
46
+ assert is_graph_pointer_rule_char(pointer) is True
47
+
48
+ def it_returns_false():
49
+ pointer = GraphPointer(GraphNode({"type": RuleType.END}, META))
50
+ assert is_graph_pointer_rule_char(pointer) is False
51
+
52
+ def describe_is_graph_pointer_rule_char_exclude():
53
+ def it_returns_true():
54
+ pointer = GraphPointer(GraphNode({"type": RuleType.CHAR_EXCLUDE, "value": [97]}, META))
55
+ assert is_graph_pointer_rule_char_exclude(pointer) is True
56
+
57
+ def it_returns_false():
58
+ pointer = GraphPointer(GraphNode({"type": RuleType.CHAR, "value": [97]}, META))
59
+ assert is_graph_pointer_rule_char_exclude(pointer) is False
60
+
61
+ def describe_is_rule():
62
+ def it_should_return_true_for_valid_rule_objects():
63
+ rule_char = {"type": RuleType.CHAR, "value": [65, [66, 67]]}
64
+ assert is_rule(rule_char) is True
65
+
66
+ def it_should_return_true_for_plain_string_types():
67
+ assert is_rule({"type": "char", "value": [65]}) is True
68
+
69
+ def it_should_return_false_for_invalid_objects():
70
+ assert is_rule({"type": "invalid", "value": []}) is False
71
+ assert is_rule(RuleRef(1)) is False
72
+
73
+ def it_should_return_false_for_none():
74
+ assert is_rule(None) is False
75
+ assert is_rule() is False
76
+
77
+ def describe_is_rule_type():
78
+ def it_should_return_true_for_valid_rule_types():
79
+ assert is_rule_type(RuleType.CHAR) is True
80
+ assert is_rule_type(RuleType.END) is True
81
+ assert is_rule_type("char_exclude") is True
82
+
83
+ def it_should_return_false_for_invalid_rule_types():
84
+ assert is_rule_type("not_a_rule_type") is False
85
+ assert is_rule_type(None) is False
86
+ assert is_rule_type("") is False
87
+
88
+ def describe_is_rule_ref():
89
+ def it_should_recognize_rule_ref_instances():
90
+ assert is_rule_ref(RuleRef(1)) is True
91
+
92
+ def it_should_not_misidentify_other_rule_types():
93
+ rule_char = {"type": RuleType.CHAR, "value": [65]}
94
+ assert is_rule_ref(rule_char) is False
95
+
96
+ def describe_is_rule_end():
97
+ def it_recognizes_an_end_rule():
98
+ assert is_rule_end({"type": RuleType.END}) is True
99
+
100
+ def it_does_not_confuse_with_other_types():
101
+ assert is_rule_end({"type": RuleType.CHAR, "value": [65]}) is False
102
+ assert is_rule_end(RuleRef(1)) is False
103
+ assert is_rule_end(None) is False
104
+
105
+ def describe_is_rule_char_and_is_rule_char_excluded():
106
+ def it_identifies_character_rules():
107
+ rule_char = {"type": RuleType.CHAR, "value": [65]}
108
+ assert is_rule_char(rule_char) is True
109
+ assert is_rule_char_excluded(rule_char) is False
110
+
111
+ def it_identifies_character_exclusion_rules():
112
+ rule_char_exclude = {"type": RuleType.CHAR_EXCLUDE, "value": [65]}
113
+ assert is_rule_char_excluded(rule_char_exclude) is True
114
+ assert is_rule_char(rule_char_exclude) is False
115
+
116
+ def describe_is_range():
117
+ def it_identifies_valid_ranges():
118
+ assert is_range([1, 10]) is True
119
+ assert is_range((1, 10)) is True
120
+
121
+ def it_rejects_invalid_ranges():
122
+ assert is_range([1, "10"]) is False # Mixed types
123
+ assert is_range([1]) is False # Not enough elements
124
+ assert is_range(5) is False # Not an array
125
+ assert is_range([True, 10]) is False # Booleans are not code points
126
+ assert is_range(None) is False
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_parser/__init__.py ADDED
File without changes
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/grammar_parser/test_build_rule_stack.py ADDED
@@ -0,0 +1,371 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/src/grammar-parser/build-rule-stack.test.ts."""
2
+
3
+ import pytest
4
+
5
+ from gbnf.grammar_graph.rule_ref import RuleRef
6
+ from gbnf.grammar_graph.types import RuleType
7
+ from gbnf.grammar_parser.build_rule_stack import build_rule_stack
8
+ from gbnf.rules_builder.types import InternalRuleType
9
+
10
+
11
+ def describe_build_rule_stack():
12
+ def it_builds_rule_stack_for_a_single_path():
13
+ assert build_rule_stack([{"type": InternalRuleType.CHAR, "value": [120]}]) == [
14
+ [{"type": RuleType.CHAR, "value": [120]}, {"type": RuleType.END}],
15
+ ]
16
+
17
+ def it_builds_rule_stack_for_two_alternate_paths():
18
+ assert build_rule_stack(
19
+ [
20
+ {"type": InternalRuleType.CHAR, "value": [120]},
21
+ {"type": InternalRuleType.ALT},
22
+ {"type": InternalRuleType.CHAR, "value": [121]},
23
+ ]
24
+ ) == [
25
+ [{"type": RuleType.CHAR, "value": [120]}, {"type": RuleType.END}],
26
+ [{"type": RuleType.CHAR, "value": [121]}, {"type": RuleType.END}],
27
+ ]
28
+
29
+ def it_builds_rule_stack_for_three_alternate_paths():
30
+ assert build_rule_stack(
31
+ [
32
+ {"type": InternalRuleType.CHAR, "value": [120]},
33
+ {"type": InternalRuleType.ALT},
34
+ {"type": InternalRuleType.CHAR, "value": [121]},
35
+ {"type": InternalRuleType.ALT},
36
+ {"type": InternalRuleType.CHAR, "value": [122]},
37
+ ]
38
+ ) == [
39
+ [{"type": RuleType.CHAR, "value": [120]}, {"type": RuleType.END}],
40
+ [{"type": RuleType.CHAR, "value": [121]}, {"type": RuleType.END}],
41
+ [{"type": RuleType.CHAR, "value": [122]}, {"type": RuleType.END}],
42
+ ]
43
+
44
+ def it_builds_rule_stack_for_char_not():
45
+ assert build_rule_stack(
46
+ [
47
+ {"type": InternalRuleType.CHAR_NOT, "value": [120]},
48
+ {"type": InternalRuleType.ALT},
49
+ {"type": InternalRuleType.CHAR_NOT, "value": [121]},
50
+ {"type": InternalRuleType.ALT},
51
+ {"type": InternalRuleType.CHAR_NOT, "value": [122]},
52
+ ]
53
+ ) == [
54
+ [{"type": RuleType.CHAR_EXCLUDE, "value": [120]}, {"type": RuleType.END}],
55
+ [{"type": RuleType.CHAR_EXCLUDE, "value": [121]}, {"type": RuleType.END}],
56
+ [{"type": RuleType.CHAR_EXCLUDE, "value": [122]}, {"type": RuleType.END}],
57
+ ]
58
+
59
+ def it_builds_rule_stack_for_mixed_char_and_char_not():
60
+ assert build_rule_stack(
61
+ [
62
+ {"type": InternalRuleType.CHAR_NOT, "value": [120]},
63
+ {"type": InternalRuleType.ALT},
64
+ {"type": InternalRuleType.CHAR, "value": [121]},
65
+ {"type": InternalRuleType.ALT},
66
+ {"type": InternalRuleType.CHAR_NOT, "value": [122]},
67
+ ]
68
+ ) == [
69
+ [{"type": RuleType.CHAR_EXCLUDE, "value": [120]}, {"type": RuleType.END}],
70
+ [{"type": RuleType.CHAR, "value": [121]}, {"type": RuleType.END}],
71
+ [{"type": RuleType.CHAR_EXCLUDE, "value": [122]}, {"type": RuleType.END}],
72
+ ]
73
+
74
+ def it_builds_rule_stack_for_a_char_not_with_two_characters_and_a_range():
75
+ assert build_rule_stack(
76
+ [
77
+ {"type": InternalRuleType.CHAR_NOT, "value": [120]},
78
+ {"type": InternalRuleType.CHAR_ALT, "value": 121},
79
+ {"type": InternalRuleType.CHAR_ALT, "value": 122},
80
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": 130},
81
+ ]
82
+ ) == [
83
+ [{"type": RuleType.CHAR_EXCLUDE, "value": [120, 121, [122, 130]]}, {"type": RuleType.END}],
84
+ ]
85
+
86
+ def describe_ranges():
87
+ a_z = [ord("a"), ord("z")]
88
+ upper_a_z = [ord("A"), ord("Z")]
89
+ digits = [ord("0"), ord("9")]
90
+
91
+ def describe_no_modifiers():
92
+ @pytest.mark.parametrize(
93
+ ("grammar", "input_rules", "expected"),
94
+ [
95
+ (
96
+ "[a-z]",
97
+ [
98
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
99
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
100
+ {"type": InternalRuleType.END},
101
+ ],
102
+ [[{"type": RuleType.CHAR, "value": [a_z]}, {"type": RuleType.END}]],
103
+ ),
104
+ (
105
+ "[a-zA-Z]",
106
+ [
107
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
108
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
109
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("A")},
110
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("Z")},
111
+ {"type": InternalRuleType.END},
112
+ ],
113
+ [[{"type": RuleType.CHAR, "value": [a_z, upper_a_z]}, {"type": RuleType.END}]],
114
+ ),
115
+ (
116
+ "[a-zA-Z0-9]",
117
+ [
118
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
119
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
120
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("A")},
121
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("Z")},
122
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("0")},
123
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
124
+ {"type": InternalRuleType.END},
125
+ ],
126
+ [[{"type": RuleType.CHAR, "value": [a_z, upper_a_z, digits]}, {"type": RuleType.END}]],
127
+ ),
128
+ ],
129
+ ids=lambda value: value if isinstance(value, str) else "",
130
+ )
131
+ def it_builds_rule_stack_for_a_char(grammar, input_rules, expected):
132
+ assert build_rule_stack(input_rules) == expected
133
+
134
+ def describe_optional():
135
+ @pytest.mark.parametrize(
136
+ ("grammar", "input_rules", "expected"),
137
+ [
138
+ (
139
+ "[a-z]?",
140
+ [
141
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
142
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
143
+ {"type": InternalRuleType.ALT},
144
+ {"type": InternalRuleType.END},
145
+ ],
146
+ [
147
+ [{"type": RuleType.CHAR, "value": [a_z]}, {"type": RuleType.END}],
148
+ [{"type": RuleType.END}],
149
+ ],
150
+ ),
151
+ (
152
+ "[a-zA-Z]?",
153
+ [
154
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
155
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
156
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("A")},
157
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("Z")},
158
+ {"type": InternalRuleType.ALT},
159
+ {"type": InternalRuleType.END},
160
+ ],
161
+ [
162
+ [{"type": RuleType.CHAR, "value": [a_z, upper_a_z]}, {"type": RuleType.END}],
163
+ [{"type": RuleType.END}],
164
+ ],
165
+ ),
166
+ (
167
+ "[a-zA-Z0-9]?",
168
+ [
169
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
170
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
171
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("A")},
172
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("Z")},
173
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("0")},
174
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
175
+ {"type": InternalRuleType.ALT},
176
+ {"type": InternalRuleType.END},
177
+ ],
178
+ [
179
+ [{"type": RuleType.CHAR, "value": [a_z, upper_a_z, digits]}, {"type": RuleType.END}],
180
+ [{"type": RuleType.END}],
181
+ ],
182
+ ),
183
+ ],
184
+ ids=lambda value: value if isinstance(value, str) else "",
185
+ )
186
+ def it_builds_rule_stack_for_an_optional_char(grammar, input_rules, expected):
187
+ assert build_rule_stack(input_rules) == expected
188
+
189
+ def describe_one_or_more():
190
+ @pytest.mark.parametrize(
191
+ ("grammar", "input_rules", "expected"),
192
+ [
193
+ (
194
+ "[a-z]+",
195
+ [
196
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
197
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
198
+ {"type": InternalRuleType.RULE_REF, "value": 1},
199
+ {"type": InternalRuleType.ALT},
200
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
201
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
202
+ {"type": InternalRuleType.END},
203
+ ],
204
+ [
205
+ [{"type": RuleType.CHAR, "value": [a_z]}, RuleRef(1), {"type": RuleType.END}],
206
+ [{"type": RuleType.CHAR, "value": [a_z]}, {"type": RuleType.END}],
207
+ ],
208
+ ),
209
+ (
210
+ "[a-zA-Z]+",
211
+ [
212
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
213
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
214
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("A")},
215
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("Z")},
216
+ {"type": InternalRuleType.RULE_REF, "value": 1},
217
+ {"type": InternalRuleType.ALT},
218
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
219
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
220
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("A")},
221
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("Z")},
222
+ {"type": InternalRuleType.END},
223
+ ],
224
+ [
225
+ [{"type": RuleType.CHAR, "value": [a_z, upper_a_z]}, RuleRef(1), {"type": RuleType.END}],
226
+ [{"type": RuleType.CHAR, "value": [a_z, upper_a_z]}, {"type": RuleType.END}],
227
+ ],
228
+ ),
229
+ (
230
+ "[a-zA-Z0-9]+",
231
+ [
232
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
233
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
234
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("A")},
235
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("Z")},
236
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("0")},
237
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
238
+ {"type": InternalRuleType.RULE_REF, "value": 1},
239
+ {"type": InternalRuleType.ALT},
240
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
241
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
242
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("A")},
243
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("Z")},
244
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("0")},
245
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
246
+ {"type": InternalRuleType.END},
247
+ ],
248
+ [
249
+ [
250
+ {"type": RuleType.CHAR, "value": [a_z, upper_a_z, digits]},
251
+ RuleRef(1),
252
+ {"type": RuleType.END},
253
+ ],
254
+ [{"type": RuleType.CHAR, "value": [a_z, upper_a_z, digits]}, {"type": RuleType.END}],
255
+ ],
256
+ ),
257
+ ],
258
+ ids=lambda value: value if isinstance(value, str) else "",
259
+ )
260
+ def it_builds_rule_stack_for_a_repeating_char(grammar, input_rules, expected):
261
+ assert build_rule_stack(input_rules) == expected
262
+
263
+ def describe_zero_or_more():
264
+ @pytest.mark.parametrize(
265
+ ("grammar", "input_rules", "expected"),
266
+ [
267
+ (
268
+ "[a-z]*",
269
+ [
270
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
271
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
272
+ {"type": InternalRuleType.RULE_REF, "value": 1},
273
+ {"type": InternalRuleType.ALT},
274
+ {"type": InternalRuleType.END},
275
+ ],
276
+ [
277
+ [{"type": RuleType.CHAR, "value": [a_z]}, RuleRef(1), {"type": RuleType.END}],
278
+ [{"type": RuleType.END}],
279
+ ],
280
+ ),
281
+ (
282
+ "[a-zA-Z]*",
283
+ [
284
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
285
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
286
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("A")},
287
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("Z")},
288
+ {"type": InternalRuleType.RULE_REF, "value": 1},
289
+ {"type": InternalRuleType.ALT},
290
+ {"type": InternalRuleType.END},
291
+ ],
292
+ [
293
+ [{"type": RuleType.CHAR, "value": [a_z, upper_a_z]}, RuleRef(1), {"type": RuleType.END}],
294
+ [{"type": RuleType.END}],
295
+ ],
296
+ ),
297
+ (
298
+ "[a-zA-Z0-9]*",
299
+ [
300
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
301
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
302
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("A")},
303
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("Z")},
304
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("0")},
305
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
306
+ {"type": InternalRuleType.RULE_REF, "value": 1},
307
+ {"type": InternalRuleType.ALT},
308
+ {"type": InternalRuleType.END},
309
+ ],
310
+ [
311
+ [
312
+ {"type": RuleType.CHAR, "value": [a_z, upper_a_z, digits]},
313
+ RuleRef(1),
314
+ {"type": RuleType.END},
315
+ ],
316
+ [{"type": RuleType.END}],
317
+ ],
318
+ ),
319
+ ],
320
+ ids=lambda value: value if isinstance(value, str) else "",
321
+ )
322
+ def it_builds_rule_stack_for_an_optionally_repeating_char(grammar, input_rules, expected):
323
+ assert build_rule_stack(input_rules) == expected
324
+
325
+ def it_builds_a_char_with_range_and_a_single_char():
326
+ input_rules = [
327
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
328
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
329
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("A")},
330
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("Z")},
331
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("_")},
332
+ {"type": InternalRuleType.END},
333
+ ]
334
+ assert build_rule_stack(input_rules) == [
335
+ [{"type": RuleType.CHAR, "value": [a_z, upper_a_z, ord("_")]}, {"type": RuleType.END}],
336
+ ]
337
+
338
+ def it_builds_rule_stack_for_situation_root_is_repeated_ws():
339
+ # root ::= (ws )+\nws ::= [ \\t\\n]*
340
+ input_rules = [
341
+ {"type": InternalRuleType.CHAR, "value": [32]},
342
+ {"type": InternalRuleType.CHAR_ALT, "value": 92},
343
+ {"type": InternalRuleType.CHAR_ALT, "value": 110},
344
+ {"type": InternalRuleType.RULE_REF, "value": 4},
345
+ {"type": InternalRuleType.ALT},
346
+ {"type": InternalRuleType.END},
347
+ ]
348
+ assert build_rule_stack(input_rules) == [
349
+ [{"type": RuleType.CHAR, "value": [32, 92, 110]}, RuleRef(4), {"type": RuleType.END}],
350
+ [{"type": RuleType.END}],
351
+ ]
352
+
353
+ def it_builds_rule_stack_for_situation_root_is_repeated_letters():
354
+ # root ::= [a-zA-Z]+
355
+ input_rules = [
356
+ {"type": InternalRuleType.CHAR, "value": [97]},
357
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": 122},
358
+ {"type": InternalRuleType.CHAR_ALT, "value": 65},
359
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": 90},
360
+ {"type": InternalRuleType.RULE_REF, "value": 1},
361
+ {"type": InternalRuleType.ALT},
362
+ {"type": InternalRuleType.CHAR, "value": [97]},
363
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": 122},
364
+ {"type": InternalRuleType.CHAR_ALT, "value": 65},
365
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": 90},
366
+ {"type": InternalRuleType.END},
367
+ ]
368
+ assert build_rule_stack(input_rules) == [
369
+ [{"type": RuleType.CHAR, "value": [[97, 122], [65, 90]]}, RuleRef(1), {"type": RuleType.END}],
370
+ [{"type": RuleType.CHAR, "value": [[97, 122], [65, 90]]}, {"type": RuleType.END}],
371
+ ]
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/rules_builder/__init__.py ADDED
File without changes
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/rules_builder/test_is_word_char.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/src/rules-builder/is-word-char.test.ts."""
2
+
3
+ from gbnf.rules_builder.is_word_char import is_word_char
4
+
5
+
6
+ def describe_is_word_char_function():
7
+ def it_should_return_true_for_lowercase_letters():
8
+ assert is_word_char("a") is True
9
+ assert is_word_char("z") is True
10
+
11
+ def it_should_return_true_for_uppercase_letters():
12
+ assert is_word_char("A") is True
13
+ assert is_word_char("Z") is True
14
+
15
+ def it_should_return_false_for_digits():
16
+ assert is_word_char("0") is False
17
+ assert is_word_char("9") is False
18
+
19
+ def it_should_return_false_for_non_word_characters():
20
+ assert is_word_char("-") is False
21
+ assert is_word_char("@") is False
22
+ assert is_word_char("_") is False
23
+ assert is_word_char("?") is False
24
+ assert is_word_char(" ") is False
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/rules_builder/test_parse_char.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/src/rules-builder/parse-char.test.ts."""
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(("char", "code_point"), [("a", ord("a")), ("9", ord("9"))])
11
+ def it_parses_char(char, code_point):
12
+ grammar = f'root ::= "{char}" "foo"'
13
+ assert parse_char(grammar, len('root ::= "')) == (code_point, 1)
14
+
15
+ @pytest.mark.parametrize(
16
+ ("key", "escaped_char", "code_point", "inc_pos"),
17
+ [
18
+ ("escaped 8-bit unicode char", "\\x2A", ord("\x2a"), 4),
19
+ ("escaped 16-bit unicode char", "\\u006F", ord("o"), 6),
20
+ ("escaped 32-bit unicode char", "\\U0001F4A9", 128169, 10),
21
+ ("escaped tab char", "\\t", ord("\t"), 2),
22
+ ("escaped new line char", "\\n", ord("\n"), 2),
23
+ ("escaped \\r char", "\\r", ord("\r"), 2),
24
+ ("escaped quote char", '\\"', ord('"'), 2),
25
+ ("escaped [ char", "\\[", ord("["), 2),
26
+ ("escaped ] char", "\\]", ord("]"), 2),
27
+ ("escaped \\\\ char", "\\\\", ord("\\"), 2),
28
+ ],
29
+ ids=lambda value: value if isinstance(value, str) and value.startswith("escaped") else "",
30
+ )
31
+ def it_parses_escaped_char(key, escaped_char, code_point, inc_pos):
32
+ grammar = f'root ::= "{escaped_char}" "foo"'
33
+ assert parse_char(grammar, len('root ::= "')) == (code_point, inc_pos)
34
+
35
+ def it_throws_on_invalid_input():
36
+ with pytest.raises(GrammarParseError):
37
+ parse_char("", 0)
38
+ with pytest.raises(GrammarParseError):
39
+ parse_char("a", 1)
40
+
41
+ def it_throws_on_an_unknown_escape():
42
+ with pytest.raises(GrammarParseError):
43
+ parse_char("\\q", 0)
44
+
45
+ def it_throws_on_a_malformed_hex_escape():
46
+ with pytest.raises(GrammarParseError):
47
+ parse_char("\\x2G", 0)
48
+ with pytest.raises(GrammarParseError):
49
+ parse_char("\\u00", 0)
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/rules_builder/test_parse_name.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/src/rules-builder/parse-name.test.ts."""
2
+
3
+ import pytest
4
+
5
+ from gbnf.rules_builder.parse_name import INVALID_CHAR_ERROR, PARSE_NAME_ERROR, parse_name
6
+ from gbnf.utils.errors.grammar_parse_error import GrammarParseError
7
+
8
+
9
+ def describe_parse_name():
10
+ @pytest.mark.parametrize(
11
+ "src",
12
+ ["v", "valid", "valid-name", "valid-name-foo", "validName", "validName-foo"],
13
+ )
14
+ def it_should_return_correct_position_when_encountering_a_valid_name(src):
15
+ name = parse_name(src, 0)
16
+ assert name == src
17
+
18
+ @pytest.mark.parametrize(
19
+ ("src", "position", "expectation"),
20
+ [
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
+ )
29
+ def it_should_return_correct_position_for_a_valid_name_starting_at_a_non_zero_position(
30
+ src, position, expectation
31
+ ):
32
+ name = parse_name(src.replace("\\n", "\n"), position)
33
+ assert name == expectation
34
+
35
+ @pytest.mark.parametrize(
36
+ ("src", "error"),
37
+ [
38
+ ("123", GrammarParseError("123", 0, PARSE_NAME_ERROR)),
39
+ ("valid_name", GrammarParseError("valid_name", 5, INVALID_CHAR_ERROR.format(char="_"))),
40
+ ("valid123", GrammarParseError("valid123", 5, INVALID_CHAR_ERROR.format(char="1"))),
41
+ ],
42
+ ids=lambda value: value if isinstance(value, str) else "",
43
+ )
44
+ def it_should_throw_an_error_when_encountering_an_invalid_name(src, error):
45
+ with pytest.raises(GrammarParseError) as excinfo:
46
+ parse_name(src, 0)
47
+ assert str(excinfo.value) == str(error)
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/rules_builder/test_parse_space.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/src/rules-builder/parse-space.test.ts."""
2
+
3
+ from gbnf.rules_builder.parse_space import parse_space
4
+
5
+
6
+ def describe_parse_space_function():
7
+ def it_should_return_the_input_string_when_no_whitespace_or_comments_are_present():
8
+ input_string = "abcdefghijk"
9
+ pos = parse_space(input_string, 0, True)
10
+ assert input_string[pos:] == input_string
11
+
12
+ def it_should_skip_leading_spaces_and_tabs():
13
+ input_string = " \t abcdefghijk"
14
+ pos = parse_space(input_string, 0, True)
15
+ assert input_string[pos:] == "abcdefghijk"
16
+
17
+ def it_should_skip_leading_newline_characters_when_newline_ok_is_true():
18
+ input_string = "\n\n\r\n\r\nabcdefghijk"
19
+ pos = parse_space(input_string, 0, True)
20
+ assert input_string[pos:] == "abcdefghijk"
21
+
22
+ def it_should_not_skip_leading_newline_characters_when_newline_ok_is_false():
23
+ input_string = "\n\n\r\n\r\nabcdefghijk"
24
+ pos = parse_space(input_string, 0, False)
25
+ assert input_string[pos:] == "\n\n\r\n\r\nabcdefghijk"
26
+
27
+ def it_should_skip_comments_and_leading_spaces():
28
+ input_string = " # This is a comment\n\t abcdefghijk"
29
+ pos = parse_space(input_string, 0, True)
30
+ assert input_string[pos:] == "abcdefghijk"
31
+
32
+ def it_should_skip_comments_and_leading_newline_characters_when_newline_ok_is_true():
33
+ input_string = "\n\n # This is a comment\n\r\n\r\nabcdefghijk"
34
+ pos = parse_space(input_string, 0, True)
35
+ assert input_string[pos:] == "abcdefghijk"
36
+
37
+ def it_should_skip_comments_and_leading_newline_characters_when_newline_ok_is_false():
38
+ input_string = "\n\n # This is a comment\n\r\n\r\nabcdefghijk"
39
+ pos = parse_space(input_string, 0, False)
40
+ assert input_string[pos:] == "\n\n # This is a comment\n\r\n\r\nabcdefghijk"
41
+
42
+ def it_should_return_an_empty_string_if_the_input_is_all_whitespace_and_comments():
43
+ input_string = " \t# Comment\n# Another comment\n\n"
44
+ pos = parse_space(input_string, 0, True)
45
+ assert input_string[pos:] == ""
46
+
47
+ def it_should_return_an_empty_string_for_an_empty_input_string():
48
+ input_string = ""
49
+ pos = parse_space(input_string, 0, True)
50
+ assert input_string[pos:] == ""
v2-models-effort/runs/20261109-214231-delicate-darkness/ported_implementation/python/tests/unit/rules_builder/test_rules_builder.py ADDED
@@ -0,0 +1,1380 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ported from javascript/src/rules-builder/rules-builder.test.ts.
2
+
3
+ The data tables were extracted mechanically from the TypeScript source.
4
+ Grammar lines are stored already trimmed, as the original test trims them before parsing."""
5
+
6
+ import pytest
7
+
8
+ from gbnf.rules_builder.rules_builder import RulesBuilder
9
+ from gbnf.rules_builder.types import InternalRuleType
10
+
11
+ GRAMMARS = [
12
+ (
13
+ "single-string",
14
+ 'root ::= "foo"',
15
+ {
16
+ "symbol_ids": [("root", 0)],
17
+ "rules": [
18
+ [
19
+ {"type": InternalRuleType.CHAR, "value": [ord("f")]},
20
+ {"type": InternalRuleType.CHAR, "value": [ord("o")]},
21
+ {"type": InternalRuleType.CHAR, "value": [ord("o")]},
22
+ {"type": InternalRuleType.END},
23
+ ],
24
+ ],
25
+ },
26
+ ),
27
+ (
28
+ "quote character",
29
+ 'root::= "\\""',
30
+ {
31
+ "symbol_ids": [("root", 0)],
32
+ "rules": [[{"type": InternalRuleType.CHAR, "value": [ord('"')]}, {"type": InternalRuleType.END}]],
33
+ },
34
+ ),
35
+ (
36
+ "two-lines-referencing-expression",
37
+ 'root ::= foo\nfoo ::= "bar"\n',
38
+ {
39
+ "symbol_ids": [("root", 0), ("foo", 1)],
40
+ "rules": [
41
+ [{"type": InternalRuleType.RULE_REF, "value": 1}, {"type": InternalRuleType.END}],
42
+ [
43
+ {"type": InternalRuleType.CHAR, "value": [ord("b")]},
44
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
45
+ {"type": InternalRuleType.CHAR, "value": [ord("r")]},
46
+ {"type": InternalRuleType.END},
47
+ ],
48
+ ],
49
+ },
50
+ ),
51
+ (
52
+ "expression with dash",
53
+ 'root ::= foo-bar\nfoo-bar ::= "bar"\n',
54
+ {
55
+ "symbol_ids": [("root", 0), ("foo-bar", 1)],
56
+ "rules": [
57
+ [{"type": InternalRuleType.RULE_REF, "value": 1}, {"type": InternalRuleType.END}],
58
+ [
59
+ {"type": InternalRuleType.CHAR, "value": [ord("b")]},
60
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
61
+ {"type": InternalRuleType.CHAR, "value": [ord("r")]},
62
+ {"type": InternalRuleType.END},
63
+ ],
64
+ ],
65
+ },
66
+ ),
67
+ (
68
+ "simple-grammar",
69
+ '\nroot ::= (expr "=" term "\n")+\nexpr ::= term ([-+*/] term)*\nterm ::= [0-9]+\n\n',
70
+ {
71
+ "symbol_ids": [
72
+ ("root", 0),
73
+ ("root_1", 1),
74
+ ("expr", 2),
75
+ ("term", 3),
76
+ ("root_4", 4),
77
+ ("expr_5", 5),
78
+ ("expr_6", 6),
79
+ ("term_7", 7),
80
+ ],
81
+ "rules": [
82
+ [{"type": InternalRuleType.RULE_REF, "value": 4}, {"type": InternalRuleType.END}],
83
+ [
84
+ {"type": InternalRuleType.RULE_REF, "value": 2},
85
+ {"type": InternalRuleType.CHAR, "value": [ord("=")]},
86
+ {"type": InternalRuleType.RULE_REF, "value": 3},
87
+ {"type": InternalRuleType.CHAR, "value": [10]},
88
+ {"type": InternalRuleType.END},
89
+ ],
90
+ [
91
+ {"type": InternalRuleType.RULE_REF, "value": 3},
92
+ {"type": InternalRuleType.RULE_REF, "value": 6},
93
+ {"type": InternalRuleType.END},
94
+ ],
95
+ [{"type": InternalRuleType.RULE_REF, "value": 7}, {"type": InternalRuleType.END}],
96
+ [
97
+ {"type": InternalRuleType.RULE_REF, "value": 1},
98
+ {"type": InternalRuleType.RULE_REF, "value": 4},
99
+ {"type": InternalRuleType.ALT},
100
+ {"type": InternalRuleType.RULE_REF, "value": 1},
101
+ {"type": InternalRuleType.END},
102
+ ],
103
+ [
104
+ {"type": InternalRuleType.CHAR, "value": [ord("-")]},
105
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("+")},
106
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("*")},
107
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("/")},
108
+ {"type": InternalRuleType.RULE_REF, "value": 3},
109
+ {"type": InternalRuleType.END},
110
+ ],
111
+ [
112
+ {"type": InternalRuleType.RULE_REF, "value": 5},
113
+ {"type": InternalRuleType.RULE_REF, "value": 6},
114
+ {"type": InternalRuleType.ALT},
115
+ {"type": InternalRuleType.END},
116
+ ],
117
+ [
118
+ {"type": InternalRuleType.CHAR, "value": [ord("0")]},
119
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
120
+ {"type": InternalRuleType.RULE_REF, "value": 7},
121
+ {"type": InternalRuleType.ALT},
122
+ {"type": InternalRuleType.CHAR, "value": [ord("0")]},
123
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
124
+ {"type": InternalRuleType.END},
125
+ ],
126
+ ],
127
+ },
128
+ ),
129
+ (
130
+ "longer-grammar",
131
+ 'root ::= (expr "=" ws term "\\n")+\nexpr ::= term ([-+*/] term)*\nterm ::= ident | num | "(" ws expr ")" ws\nident ::= [a-z] [a-z0-9_]* ws\nnum ::= [0-9]+ ws\nws ::= [ \\t\\n]*',
132
+ {
133
+ "symbol_ids": [
134
+ ("root", 0),
135
+ ("root_1", 1),
136
+ ("expr", 2),
137
+ ("ws", 3),
138
+ ("term", 4),
139
+ ("root_5", 5),
140
+ ("expr_6", 6),
141
+ ("expr_7", 7),
142
+ ("ident", 8),
143
+ ("num", 9),
144
+ ("ident_10", 10),
145
+ ("num_11", 11),
146
+ ("ws_12", 12),
147
+ ],
148
+ "rules": [
149
+ [{"value": 5, "type": InternalRuleType.RULE_REF}, {"type": InternalRuleType.END}],
150
+ [
151
+ {"value": 2, "type": InternalRuleType.RULE_REF},
152
+ {"value": [ord("=")], "type": InternalRuleType.CHAR},
153
+ {"value": 3, "type": InternalRuleType.RULE_REF},
154
+ {"value": 4, "type": InternalRuleType.RULE_REF},
155
+ {"value": [10], "type": InternalRuleType.CHAR},
156
+ {"type": InternalRuleType.END},
157
+ ],
158
+ [
159
+ {"value": 4, "type": InternalRuleType.RULE_REF},
160
+ {"value": 7, "type": InternalRuleType.RULE_REF},
161
+ {"type": InternalRuleType.END},
162
+ ],
163
+ [{"value": 12, "type": InternalRuleType.RULE_REF}, {"type": InternalRuleType.END}],
164
+ [
165
+ {"value": 8, "type": InternalRuleType.RULE_REF},
166
+ {"type": InternalRuleType.ALT},
167
+ {"value": 9, "type": InternalRuleType.RULE_REF},
168
+ {"type": InternalRuleType.ALT},
169
+ {"value": [ord("(")], "type": InternalRuleType.CHAR},
170
+ {"value": 3, "type": InternalRuleType.RULE_REF},
171
+ {"value": 2, "type": InternalRuleType.RULE_REF},
172
+ {"value": [ord(")")], "type": InternalRuleType.CHAR},
173
+ {"value": 3, "type": InternalRuleType.RULE_REF},
174
+ {"type": InternalRuleType.END},
175
+ ],
176
+ [
177
+ {"value": 1, "type": InternalRuleType.RULE_REF},
178
+ {"value": 5, "type": InternalRuleType.RULE_REF},
179
+ {"type": InternalRuleType.ALT},
180
+ {"value": 1, "type": InternalRuleType.RULE_REF},
181
+ {"type": InternalRuleType.END},
182
+ ],
183
+ [
184
+ {"value": [ord("-")], "type": InternalRuleType.CHAR},
185
+ {"value": ord("+"), "type": InternalRuleType.CHAR_ALT},
186
+ {"value": ord("*"), "type": InternalRuleType.CHAR_ALT},
187
+ {"value": ord("/"), "type": InternalRuleType.CHAR_ALT},
188
+ {"value": 4, "type": InternalRuleType.RULE_REF},
189
+ {"type": InternalRuleType.END},
190
+ ],
191
+ [
192
+ {"value": 6, "type": InternalRuleType.RULE_REF},
193
+ {"value": 7, "type": InternalRuleType.RULE_REF},
194
+ {"type": InternalRuleType.ALT},
195
+ {"type": InternalRuleType.END},
196
+ ],
197
+ [
198
+ {"value": [ord("a")], "type": InternalRuleType.CHAR},
199
+ {"value": ord("z"), "type": InternalRuleType.CHAR_RNG_UPPER},
200
+ {"value": 10, "type": InternalRuleType.RULE_REF},
201
+ {"value": 3, "type": InternalRuleType.RULE_REF},
202
+ {"type": InternalRuleType.END},
203
+ ],
204
+ [
205
+ {"value": 11, "type": InternalRuleType.RULE_REF},
206
+ {"value": 3, "type": InternalRuleType.RULE_REF},
207
+ {"type": InternalRuleType.END},
208
+ ],
209
+ [
210
+ {"value": [ord("a")], "type": InternalRuleType.CHAR},
211
+ {"value": ord("z"), "type": InternalRuleType.CHAR_RNG_UPPER},
212
+ {"value": ord("0"), "type": InternalRuleType.CHAR_ALT},
213
+ {"value": ord("9"), "type": InternalRuleType.CHAR_RNG_UPPER},
214
+ {"value": ord("_"), "type": InternalRuleType.CHAR_ALT},
215
+ {"value": 10, "type": InternalRuleType.RULE_REF},
216
+ {"type": InternalRuleType.ALT},
217
+ {"type": InternalRuleType.END},
218
+ ],
219
+ [
220
+ {"value": [ord("0")], "type": InternalRuleType.CHAR},
221
+ {"value": ord("9"), "type": InternalRuleType.CHAR_RNG_UPPER},
222
+ {"value": 11, "type": InternalRuleType.RULE_REF},
223
+ {"type": InternalRuleType.ALT},
224
+ {"value": [ord("0")], "type": InternalRuleType.CHAR},
225
+ {"value": ord("9"), "type": InternalRuleType.CHAR_RNG_UPPER},
226
+ {"type": InternalRuleType.END},
227
+ ],
228
+ [
229
+ {"value": [ord(" ")], "type": InternalRuleType.CHAR},
230
+ {"value": 9, "type": InternalRuleType.CHAR_ALT},
231
+ {"value": 10, "type": InternalRuleType.CHAR_ALT},
232
+ {"value": 12, "type": InternalRuleType.RULE_REF},
233
+ {"type": InternalRuleType.ALT},
234
+ {"type": InternalRuleType.END},
235
+ ],
236
+ ],
237
+ },
238
+ ),
239
+ (
240
+ "character unicode grammar",
241
+ "root ::= [ぁ-ゟ]",
242
+ {
243
+ "symbol_ids": [("root", 0)],
244
+ "rules": [
245
+ [
246
+ {"type": InternalRuleType.CHAR, "value": [12353]},
247
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": 12447},
248
+ {"type": InternalRuleType.END},
249
+ ],
250
+ ],
251
+ },
252
+ ),
253
+ (
254
+ "character alts grammar",
255
+ "root ::= [az]",
256
+ {
257
+ "symbol_ids": [("root", 0)],
258
+ "rules": [
259
+ [
260
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
261
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("z")},
262
+ {"type": InternalRuleType.END},
263
+ ],
264
+ ],
265
+ },
266
+ ),
267
+ (
268
+ "character range grammar",
269
+ "root ::= [a-zA-Z0-9]",
270
+ {
271
+ "symbol_ids": [("root", 0)],
272
+ "rules": [
273
+ [
274
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
275
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
276
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("A")},
277
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("Z")},
278
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("0")},
279
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
280
+ {"type": InternalRuleType.END},
281
+ ],
282
+ ],
283
+ },
284
+ ),
285
+ (
286
+ "character range grammar with dash at end",
287
+ "root ::= [a-z-]",
288
+ {
289
+ "symbol_ids": [("root", 0)],
290
+ "rules": [
291
+ [
292
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
293
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
294
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("-")},
295
+ {"type": InternalRuleType.END},
296
+ ],
297
+ ],
298
+ },
299
+ ),
300
+ (
301
+ "grouping",
302
+ 'root ::= "f" ("b" | "a")',
303
+ {
304
+ "symbol_ids": [("root", 0), ("root_1", 1)],
305
+ "rules": [
306
+ [
307
+ {"type": InternalRuleType.CHAR, "value": [ord("f")]},
308
+ {"type": InternalRuleType.RULE_REF, "value": 1},
309
+ {"type": InternalRuleType.END},
310
+ ],
311
+ [
312
+ {"type": InternalRuleType.CHAR, "value": [ord("b")]},
313
+ {"type": InternalRuleType.ALT},
314
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
315
+ {"type": InternalRuleType.END},
316
+ ],
317
+ ],
318
+ },
319
+ ),
320
+ (
321
+ "optional",
322
+ 'root ::= "f"?',
323
+ {
324
+ "symbol_ids": [("root", 0), ("root_1", 1)],
325
+ "rules": [
326
+ [{"type": InternalRuleType.RULE_REF, "value": 1}, {"type": InternalRuleType.END}],
327
+ [
328
+ {"type": InternalRuleType.CHAR, "value": [ord("f")]},
329
+ {"type": InternalRuleType.ALT},
330
+ {"type": InternalRuleType.END},
331
+ ],
332
+ ],
333
+ },
334
+ ),
335
+ (
336
+ "repeating",
337
+ 'root ::= "f"*',
338
+ {
339
+ "symbol_ids": [("root", 0), ("root_1", 1)],
340
+ "rules": [
341
+ [{"type": InternalRuleType.RULE_REF, "value": 1}, {"type": InternalRuleType.END}],
342
+ [
343
+ {"type": InternalRuleType.CHAR, "value": [ord("f")]},
344
+ {"type": InternalRuleType.RULE_REF, "value": 1},
345
+ {"type": InternalRuleType.ALT},
346
+ {"type": InternalRuleType.END},
347
+ ],
348
+ ],
349
+ },
350
+ ),
351
+ (
352
+ "repeating-at-least-once",
353
+ 'root ::= "f"+',
354
+ {
355
+ "symbol_ids": [("root", 0), ("root_1", 1)],
356
+ "rules": [
357
+ [{"type": InternalRuleType.RULE_REF, "value": 1}, {"type": InternalRuleType.END}],
358
+ [
359
+ {"type": InternalRuleType.CHAR, "value": [ord("f")]},
360
+ {"type": InternalRuleType.RULE_REF, "value": 1},
361
+ {"type": InternalRuleType.ALT},
362
+ {"type": InternalRuleType.CHAR, "value": [ord("f")]},
363
+ {"type": InternalRuleType.END},
364
+ ],
365
+ ],
366
+ },
367
+ ),
368
+ (
369
+ "character range with optional repeating",
370
+ "root ::= [a-zA-Z0-9]*",
371
+ {
372
+ "symbol_ids": [("root", 0), ("root_1", 1)],
373
+ "rules": [
374
+ [{"type": InternalRuleType.RULE_REF, "value": 1}, {"type": InternalRuleType.END}],
375
+ [
376
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
377
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
378
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("A")},
379
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("Z")},
380
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("0")},
381
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
382
+ {"type": InternalRuleType.RULE_REF, "value": 1},
383
+ {"type": InternalRuleType.ALT},
384
+ {"type": InternalRuleType.END},
385
+ ],
386
+ ],
387
+ },
388
+ ),
389
+ (
390
+ "grouping-repeating",
391
+ 'root ::= "f" ("b" | "a")*',
392
+ {
393
+ "symbol_ids": [("root", 0), ("root_1", 1), ("root_2", 2)],
394
+ "rules": [
395
+ [
396
+ {"type": InternalRuleType.CHAR, "value": [ord("f")]},
397
+ {"type": InternalRuleType.RULE_REF, "value": 2},
398
+ {"type": InternalRuleType.END},
399
+ ],
400
+ [
401
+ {"type": InternalRuleType.CHAR, "value": [ord("b")]},
402
+ {"type": InternalRuleType.ALT},
403
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
404
+ {"type": InternalRuleType.END},
405
+ ],
406
+ [
407
+ {"type": InternalRuleType.RULE_REF, "value": 1},
408
+ {"type": InternalRuleType.RULE_REF, "value": 2},
409
+ {"type": InternalRuleType.ALT},
410
+ {"type": InternalRuleType.END},
411
+ ],
412
+ ],
413
+ },
414
+ ),
415
+ (
416
+ "negation",
417
+ "root ::= [^\n]",
418
+ {
419
+ "symbol_ids": [("root", 0)],
420
+ "rules": [[{"type": InternalRuleType.CHAR_NOT, "value": [10]}, {"type": InternalRuleType.END}]],
421
+ },
422
+ ),
423
+ (
424
+ "negation of range",
425
+ "root ::= [^0-9]",
426
+ {
427
+ "symbol_ids": [("root", 0)],
428
+ "rules": [
429
+ [
430
+ {"type": InternalRuleType.CHAR_NOT, "value": [ord("0")]},
431
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
432
+ {"type": InternalRuleType.END},
433
+ ],
434
+ ],
435
+ },
436
+ ),
437
+ (
438
+ "negation with after",
439
+ 'root ::= [^\n]+ "\n"',
440
+ {
441
+ "symbol_ids": [("root", 0), ("root_1", 1)],
442
+ "rules": [
443
+ [
444
+ {"type": InternalRuleType.RULE_REF, "value": 1},
445
+ {"type": InternalRuleType.CHAR, "value": [10]},
446
+ {"type": InternalRuleType.END},
447
+ ],
448
+ [
449
+ {"type": InternalRuleType.CHAR_NOT, "value": [10]},
450
+ {"type": InternalRuleType.RULE_REF, "value": 1},
451
+ {"type": InternalRuleType.ALT},
452
+ {"type": InternalRuleType.CHAR_NOT, "value": [10]},
453
+ {"type": InternalRuleType.END},
454
+ ],
455
+ ],
456
+ },
457
+ ),
458
+ (
459
+ "longer negation",
460
+ 'root ::= "\\"" ( [^"abcdefgh])*',
461
+ {
462
+ "symbol_ids": [("root", 0), ("root_1", 1), ("root_2", 2)],
463
+ "rules": [
464
+ [
465
+ {"type": InternalRuleType.CHAR, "value": [ord('"')]},
466
+ {"type": InternalRuleType.RULE_REF, "value": 2},
467
+ {"type": InternalRuleType.END},
468
+ ],
469
+ [
470
+ {"type": InternalRuleType.CHAR_NOT, "value": [ord('"')]},
471
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("a")},
472
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("b")},
473
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("c")},
474
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("d")},
475
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("e")},
476
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("f")},
477
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("g")},
478
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("h")},
479
+ {"type": InternalRuleType.END},
480
+ ],
481
+ [
482
+ {"type": InternalRuleType.RULE_REF, "value": 1},
483
+ {"type": InternalRuleType.RULE_REF, "value": 2},
484
+ {"type": InternalRuleType.ALT},
485
+ {"type": InternalRuleType.END},
486
+ ],
487
+ ],
488
+ },
489
+ ),
490
+ (
491
+ "longer negation with a range",
492
+ 'root ::= "\\"" ( [^"abcdefghA-Z])*',
493
+ {
494
+ "symbol_ids": [("root", 0), ("root_1", 1), ("root_2", 2)],
495
+ "rules": [
496
+ [
497
+ {"type": InternalRuleType.CHAR, "value": [ord('"')]},
498
+ {"type": InternalRuleType.RULE_REF, "value": 2},
499
+ {"type": InternalRuleType.END},
500
+ ],
501
+ [
502
+ {"type": InternalRuleType.CHAR_NOT, "value": [ord('"')]},
503
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("a")},
504
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("b")},
505
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("c")},
506
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("d")},
507
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("e")},
508
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("f")},
509
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("g")},
510
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("h")},
511
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("A")},
512
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("Z")},
513
+ {"type": InternalRuleType.END},
514
+ ],
515
+ [
516
+ {"type": InternalRuleType.RULE_REF, "value": 1},
517
+ {"type": InternalRuleType.RULE_REF, "value": 2},
518
+ {"type": InternalRuleType.ALT},
519
+ {"type": InternalRuleType.END},
520
+ ],
521
+ ],
522
+ },
523
+ ),
524
+ (
525
+ "escaped 8-bit unicode char",
526
+ 'root ::= "\\x2A"',
527
+ {
528
+ "symbol_ids": [("root", 0)],
529
+ "rules": [[{"type": InternalRuleType.CHAR, "value": [ord("*")]}, {"type": InternalRuleType.END}]],
530
+ },
531
+ ),
532
+ (
533
+ "escaped 16-bit unicode char",
534
+ 'root ::= "\\u006F"',
535
+ {
536
+ "symbol_ids": [("root", 0)],
537
+ "rules": [[{"type": InternalRuleType.CHAR, "value": [ord("o")]}, {"type": InternalRuleType.END}]],
538
+ },
539
+ ),
540
+ (
541
+ "escaped 32-bit unicode char",
542
+ 'root ::= "\\U0001F4A9"',
543
+ {
544
+ "symbol_ids": [("root", 0)],
545
+ "rules": [[{"type": InternalRuleType.CHAR, "value": [128169]}, {"type": InternalRuleType.END}]],
546
+ },
547
+ ),
548
+ (
549
+ "escaped tab char",
550
+ 'root ::= "\\t"',
551
+ {
552
+ "symbol_ids": [("root", 0)],
553
+ "rules": [[{"type": InternalRuleType.CHAR, "value": [9]}, {"type": InternalRuleType.END}]],
554
+ },
555
+ ),
556
+ (
557
+ "escaped new line char",
558
+ 'root ::= "\\n"',
559
+ {
560
+ "symbol_ids": [("root", 0)],
561
+ "rules": [[{"type": InternalRuleType.CHAR, "value": [10]}, {"type": InternalRuleType.END}]],
562
+ },
563
+ ),
564
+ (
565
+ "escaped \r char",
566
+ 'root ::= "\\r"',
567
+ {
568
+ "symbol_ids": [("root", 0)],
569
+ "rules": [[{"type": InternalRuleType.CHAR, "value": [13]}, {"type": InternalRuleType.END}]],
570
+ },
571
+ ),
572
+ (
573
+ "escaped quote char",
574
+ 'root ::= "\\""',
575
+ {
576
+ "symbol_ids": [("root", 0)],
577
+ "rules": [[{"type": InternalRuleType.CHAR, "value": [ord('"')]}, {"type": InternalRuleType.END}]],
578
+ },
579
+ ),
580
+ (
581
+ "escaped [ char",
582
+ 'root ::= "\\["',
583
+ {
584
+ "symbol_ids": [("root", 0)],
585
+ "rules": [[{"type": InternalRuleType.CHAR, "value": [ord("[")]}, {"type": InternalRuleType.END}]],
586
+ },
587
+ ),
588
+ (
589
+ "escaped ] char",
590
+ 'root ::= "\\]"',
591
+ {
592
+ "symbol_ids": [("root", 0)],
593
+ "rules": [[{"type": InternalRuleType.CHAR, "value": [ord("]")]}, {"type": InternalRuleType.END}]],
594
+ },
595
+ ),
596
+ (
597
+ "escaped \\ char",
598
+ 'root ::= "\\\\"',
599
+ {
600
+ "symbol_ids": [("root", 0)],
601
+ "rules": [[{"type": InternalRuleType.CHAR, "value": [ord("\\")]}, {"type": InternalRuleType.END}]],
602
+ },
603
+ ),
604
+ (
605
+ "simple arithmetic",
606
+ '\nroot ::= (expr "=" term "\n")+\nexpr ::= term ([-+*/] term)*\nterm ::= [0-9]+\n',
607
+ {
608
+ "symbol_ids": [
609
+ ("root", 0),
610
+ ("root_1", 1),
611
+ ("expr", 2),
612
+ ("term", 3),
613
+ ("root_4", 4),
614
+ ("expr_5", 5),
615
+ ("expr_6", 6),
616
+ ("term_7", 7),
617
+ ],
618
+ "rules": [
619
+ [{"type": InternalRuleType.RULE_REF, "value": 4}, {"type": InternalRuleType.END}],
620
+ [
621
+ {"type": InternalRuleType.RULE_REF, "value": 2},
622
+ {"type": InternalRuleType.CHAR, "value": [ord("=")]},
623
+ {"type": InternalRuleType.RULE_REF, "value": 3},
624
+ {"type": InternalRuleType.CHAR, "value": [10]},
625
+ {"type": InternalRuleType.END},
626
+ ],
627
+ [
628
+ {"type": InternalRuleType.RULE_REF, "value": 3},
629
+ {"type": InternalRuleType.RULE_REF, "value": 6},
630
+ {"type": InternalRuleType.END},
631
+ ],
632
+ [{"type": InternalRuleType.RULE_REF, "value": 7}, {"type": InternalRuleType.END}],
633
+ [
634
+ {"type": InternalRuleType.RULE_REF, "value": 1},
635
+ {"type": InternalRuleType.RULE_REF, "value": 4},
636
+ {"type": InternalRuleType.ALT},
637
+ {"type": InternalRuleType.RULE_REF, "value": 1},
638
+ {"type": InternalRuleType.END},
639
+ ],
640
+ [
641
+ {"type": InternalRuleType.CHAR, "value": [ord("-")]},
642
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("+")},
643
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("*")},
644
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("/")},
645
+ {"type": InternalRuleType.RULE_REF, "value": 3},
646
+ {"type": InternalRuleType.END},
647
+ ],
648
+ [
649
+ {"type": InternalRuleType.RULE_REF, "value": 5},
650
+ {"type": InternalRuleType.RULE_REF, "value": 6},
651
+ {"type": InternalRuleType.ALT},
652
+ {"type": InternalRuleType.END},
653
+ ],
654
+ [
655
+ {"type": InternalRuleType.CHAR, "value": [ord("0")]},
656
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
657
+ {"type": InternalRuleType.RULE_REF, "value": 7},
658
+ {"type": InternalRuleType.ALT},
659
+ {"type": InternalRuleType.CHAR, "value": [ord("0")]},
660
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
661
+ {"type": InternalRuleType.END},
662
+ ],
663
+ ],
664
+ },
665
+ ),
666
+ (
667
+ "ranges with chars",
668
+ "\nroot ::= [a-z0-9_]*\n",
669
+ {
670
+ "symbol_ids": [("root", 0), ("root_1", 1)],
671
+ "rules": [
672
+ [{"type": InternalRuleType.RULE_REF, "value": 1}, {"type": InternalRuleType.END}],
673
+ [
674
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
675
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
676
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("0")},
677
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
678
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("_")},
679
+ {"type": InternalRuleType.RULE_REF, "value": 1},
680
+ {"type": InternalRuleType.ALT},
681
+ {"type": InternalRuleType.END},
682
+ ],
683
+ ],
684
+ },
685
+ ),
686
+ (
687
+ "nested ranges with chars",
688
+ "\nroot ::= [a-z] [a-z0-9_]*\n",
689
+ {
690
+ "symbol_ids": [("root", 0), ("root_1", 1)],
691
+ "rules": [
692
+ [
693
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
694
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
695
+ {"type": InternalRuleType.RULE_REF, "value": 1},
696
+ {"type": InternalRuleType.END},
697
+ ],
698
+ [
699
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
700
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
701
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("0")},
702
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
703
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("_")},
704
+ {"type": InternalRuleType.RULE_REF, "value": 1},
705
+ {"type": InternalRuleType.ALT},
706
+ {"type": InternalRuleType.END},
707
+ ],
708
+ ],
709
+ },
710
+ ),
711
+ (
712
+ "expression with nested range with chars",
713
+ "\nroot ::= ident\nident ::= [a-z] [a-z0-9_]* ws\nws ::= [ \\t\\n]*\n",
714
+ {
715
+ "symbol_ids": [("root", 0), ("ident", 1), ("ident_2", 2), ("ws", 3), ("ws_4", 4)],
716
+ "rules": [
717
+ [{"type": InternalRuleType.RULE_REF, "value": 1}, {"type": InternalRuleType.END}],
718
+ [
719
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
720
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
721
+ {"type": InternalRuleType.RULE_REF, "value": 2},
722
+ {"type": InternalRuleType.RULE_REF, "value": 3},
723
+ {"type": InternalRuleType.END},
724
+ ],
725
+ [
726
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
727
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
728
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("0")},
729
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
730
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("_")},
731
+ {"type": InternalRuleType.RULE_REF, "value": 2},
732
+ {"type": InternalRuleType.ALT},
733
+ {"type": InternalRuleType.END},
734
+ ],
735
+ [{"type": InternalRuleType.RULE_REF, "value": 4}, {"type": InternalRuleType.END}],
736
+ [
737
+ {"type": InternalRuleType.CHAR, "value": [ord(" ")]},
738
+ {"type": InternalRuleType.CHAR_ALT, "value": 9},
739
+ {"type": InternalRuleType.CHAR_ALT, "value": 10},
740
+ {"type": InternalRuleType.RULE_REF, "value": 4},
741
+ {"type": InternalRuleType.ALT},
742
+ {"type": InternalRuleType.END},
743
+ ],
744
+ ],
745
+ },
746
+ ),
747
+ (
748
+ "lots of escapes",
749
+ 'root ::= "\\x2A" "\\u006F" "\\U0001F4A9" "\\t" "\\n" "\\r" "\\"" "\\[" "\\]" "\\\\"',
750
+ {
751
+ "symbol_ids": [("root", 0)],
752
+ "rules": [
753
+ [
754
+ {"type": InternalRuleType.CHAR, "value": [ord("*")]},
755
+ {"type": InternalRuleType.CHAR, "value": [ord("o")]},
756
+ {"type": InternalRuleType.CHAR, "value": [128169]},
757
+ {"type": InternalRuleType.CHAR, "value": [9]},
758
+ {"type": InternalRuleType.CHAR, "value": [10]},
759
+ {"type": InternalRuleType.CHAR, "value": [13]},
760
+ {"type": InternalRuleType.CHAR, "value": [ord('"')]},
761
+ {"type": InternalRuleType.CHAR, "value": [ord("[")]},
762
+ {"type": InternalRuleType.CHAR, "value": [ord("]")]},
763
+ {"type": InternalRuleType.CHAR, "value": [ord("\\")]},
764
+ {"type": InternalRuleType.END},
765
+ ],
766
+ ],
767
+ },
768
+ ),
769
+ (
770
+ "lots of escape and alternate escapes",
771
+ 'root ::= "\\x2A" "\\u006F" "\\U0001F4A9" "\\t" "\\n" "\\r" "\\"" "\\[" "\\]" "\\\\" ("\\x2A" | "\\u006F" | "\\U0001F4A9" | "\\t" | "\\n" | "\\r" | "\\"" | "\\[" | "\\]" | "\\\\" )',
772
+ {
773
+ "symbol_ids": [("root", 0), ("root_1", 1)],
774
+ "rules": [
775
+ [
776
+ {"type": InternalRuleType.CHAR, "value": [ord("*")]},
777
+ {"type": InternalRuleType.CHAR, "value": [ord("o")]},
778
+ {"type": InternalRuleType.CHAR, "value": [128169]},
779
+ {"type": InternalRuleType.CHAR, "value": [9]},
780
+ {"type": InternalRuleType.CHAR, "value": [10]},
781
+ {"type": InternalRuleType.CHAR, "value": [13]},
782
+ {"type": InternalRuleType.CHAR, "value": [ord('"')]},
783
+ {"type": InternalRuleType.CHAR, "value": [ord("[")]},
784
+ {"type": InternalRuleType.CHAR, "value": [ord("]")]},
785
+ {"type": InternalRuleType.CHAR, "value": [ord("\\")]},
786
+ {"type": InternalRuleType.RULE_REF, "value": 1},
787
+ {"type": InternalRuleType.END},
788
+ ],
789
+ [
790
+ {"type": InternalRuleType.CHAR, "value": [ord("*")]},
791
+ {"type": InternalRuleType.ALT},
792
+ {"type": InternalRuleType.CHAR, "value": [ord("o")]},
793
+ {"type": InternalRuleType.ALT},
794
+ {"type": InternalRuleType.CHAR, "value": [128169]},
795
+ {"type": InternalRuleType.ALT},
796
+ {"type": InternalRuleType.CHAR, "value": [9]},
797
+ {"type": InternalRuleType.ALT},
798
+ {"type": InternalRuleType.CHAR, "value": [10]},
799
+ {"type": InternalRuleType.ALT},
800
+ {"type": InternalRuleType.CHAR, "value": [13]},
801
+ {"type": InternalRuleType.ALT},
802
+ {"type": InternalRuleType.CHAR, "value": [ord('"')]},
803
+ {"type": InternalRuleType.ALT},
804
+ {"type": InternalRuleType.CHAR, "value": [ord("[")]},
805
+ {"type": InternalRuleType.ALT},
806
+ {"type": InternalRuleType.CHAR, "value": [ord("]")]},
807
+ {"type": InternalRuleType.ALT},
808
+ {"type": InternalRuleType.CHAR, "value": [ord("\\")]},
809
+ {"type": InternalRuleType.END},
810
+ ],
811
+ ],
812
+ },
813
+ ),
814
+ (
815
+ "arithmetic",
816
+ '\nroot ::= (expr "=" ws term "\\n")+\nexpr ::= term ([-+*/] term)*\nterm ::= ident | num | "(" ws expr ")" ws\nident ::= [a-z] [a-z0-9_]* ws\nnum ::= [0-9]+ ws\nws ::= [ \\t\\n]*\n',
817
+ {
818
+ "symbol_ids": [
819
+ ("root", 0),
820
+ ("root_1", 1),
821
+ ("expr", 2),
822
+ ("ws", 3),
823
+ ("term", 4),
824
+ ("root_5", 5),
825
+ ("expr_6", 6),
826
+ ("expr_7", 7),
827
+ ("ident", 8),
828
+ ("num", 9),
829
+ ("ident_10", 10),
830
+ ("num_11", 11),
831
+ ("ws_12", 12),
832
+ ],
833
+ "rules": [
834
+ [{"type": InternalRuleType.RULE_REF, "value": 5}, {"type": InternalRuleType.END}],
835
+ [
836
+ {"type": InternalRuleType.RULE_REF, "value": 2},
837
+ {"type": InternalRuleType.CHAR, "value": [ord("=")]},
838
+ {"type": InternalRuleType.RULE_REF, "value": 3},
839
+ {"type": InternalRuleType.RULE_REF, "value": 4},
840
+ {"type": InternalRuleType.CHAR, "value": [10]},
841
+ {"type": InternalRuleType.END},
842
+ ],
843
+ [
844
+ {"type": InternalRuleType.RULE_REF, "value": 4},
845
+ {"type": InternalRuleType.RULE_REF, "value": 7},
846
+ {"type": InternalRuleType.END},
847
+ ],
848
+ [{"type": InternalRuleType.RULE_REF, "value": 12}, {"type": InternalRuleType.END}],
849
+ [
850
+ {"type": InternalRuleType.RULE_REF, "value": 8},
851
+ {"type": InternalRuleType.ALT},
852
+ {"type": InternalRuleType.RULE_REF, "value": 9},
853
+ {"type": InternalRuleType.ALT},
854
+ {"type": InternalRuleType.CHAR, "value": [ord("(")]},
855
+ {"type": InternalRuleType.RULE_REF, "value": 3},
856
+ {"type": InternalRuleType.RULE_REF, "value": 2},
857
+ {"type": InternalRuleType.CHAR, "value": [ord(")")]},
858
+ {"type": InternalRuleType.RULE_REF, "value": 3},
859
+ {"type": InternalRuleType.END},
860
+ ],
861
+ [
862
+ {"type": InternalRuleType.RULE_REF, "value": 1},
863
+ {"type": InternalRuleType.RULE_REF, "value": 5},
864
+ {"type": InternalRuleType.ALT},
865
+ {"type": InternalRuleType.RULE_REF, "value": 1},
866
+ {"type": InternalRuleType.END},
867
+ ],
868
+ [
869
+ {"type": InternalRuleType.CHAR, "value": [ord("-")]},
870
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("+")},
871
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("*")},
872
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("/")},
873
+ {"type": InternalRuleType.RULE_REF, "value": 4},
874
+ {"type": InternalRuleType.END},
875
+ ],
876
+ [
877
+ {"type": InternalRuleType.RULE_REF, "value": 6},
878
+ {"type": InternalRuleType.RULE_REF, "value": 7},
879
+ {"type": InternalRuleType.ALT},
880
+ {"type": InternalRuleType.END},
881
+ ],
882
+ [
883
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
884
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
885
+ {"type": InternalRuleType.RULE_REF, "value": 10},
886
+ {"type": InternalRuleType.RULE_REF, "value": 3},
887
+ {"type": InternalRuleType.END},
888
+ ],
889
+ [
890
+ {"type": InternalRuleType.RULE_REF, "value": 11},
891
+ {"type": InternalRuleType.RULE_REF, "value": 3},
892
+ {"type": InternalRuleType.END},
893
+ ],
894
+ [
895
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
896
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("z")},
897
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("0")},
898
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
899
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("_")},
900
+ {"type": InternalRuleType.RULE_REF, "value": 10},
901
+ {"type": InternalRuleType.ALT},
902
+ {"type": InternalRuleType.END},
903
+ ],
904
+ [
905
+ {"type": InternalRuleType.CHAR, "value": [ord("0")]},
906
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
907
+ {"type": InternalRuleType.RULE_REF, "value": 11},
908
+ {"type": InternalRuleType.ALT},
909
+ {"type": InternalRuleType.CHAR, "value": [ord("0")]},
910
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
911
+ {"type": InternalRuleType.END},
912
+ ],
913
+ [
914
+ {"type": InternalRuleType.CHAR, "value": [ord(" ")]},
915
+ {"type": InternalRuleType.CHAR_ALT, "value": 9},
916
+ {"type": InternalRuleType.CHAR_ALT, "value": 10},
917
+ {"type": InternalRuleType.RULE_REF, "value": 12},
918
+ {"type": InternalRuleType.ALT},
919
+ {"type": InternalRuleType.END},
920
+ ],
921
+ ],
922
+ },
923
+ ),
924
+ (
925
+ "json.gbnf (string)",
926
+ '\nroot ::=\n"\\"" (\n[^"\\\\\x7f\\x00-\\x1F] |\n"\\\\" (["\\\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes\n)* "\\""\n',
927
+ {
928
+ "symbol_ids": [("root", 0), ("root_1", 1), ("root_2", 2), ("root_3", 3)],
929
+ "rules": [
930
+ [
931
+ {"type": InternalRuleType.CHAR, "value": [ord('"')]},
932
+ {"type": InternalRuleType.RULE_REF, "value": 3},
933
+ {"type": InternalRuleType.CHAR, "value": [ord('"')]},
934
+ {"type": InternalRuleType.END},
935
+ ],
936
+ [
937
+ {"type": InternalRuleType.CHAR_NOT, "value": [ord('"')]},
938
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("\\")},
939
+ {"type": InternalRuleType.CHAR_ALT, "value": 127},
940
+ {"type": InternalRuleType.CHAR_ALT, "value": 0},
941
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": 31},
942
+ {"type": InternalRuleType.ALT},
943
+ {"type": InternalRuleType.CHAR, "value": [ord("\\")]},
944
+ {"type": InternalRuleType.RULE_REF, "value": 2},
945
+ {"type": InternalRuleType.END},
946
+ ],
947
+ [
948
+ {"type": InternalRuleType.CHAR, "value": [ord('"')]},
949
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("\\")},
950
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("/")},
951
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("b")},
952
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("f")},
953
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("n")},
954
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("r")},
955
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("t")},
956
+ {"type": InternalRuleType.ALT},
957
+ {"type": InternalRuleType.CHAR, "value": [ord("u")]},
958
+ {"type": InternalRuleType.CHAR, "value": [ord("0")]},
959
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
960
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("a")},
961
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("f")},
962
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("A")},
963
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("F")},
964
+ {"type": InternalRuleType.CHAR, "value": [ord("0")]},
965
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
966
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("a")},
967
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("f")},
968
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("A")},
969
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("F")},
970
+ {"type": InternalRuleType.CHAR, "value": [ord("0")]},
971
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
972
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("a")},
973
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("f")},
974
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("A")},
975
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("F")},
976
+ {"type": InternalRuleType.CHAR, "value": [ord("0")]},
977
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
978
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("a")},
979
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("f")},
980
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("A")},
981
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("F")},
982
+ {"type": InternalRuleType.END},
983
+ ],
984
+ [
985
+ {"type": InternalRuleType.RULE_REF, "value": 1},
986
+ {"type": InternalRuleType.RULE_REF, "value": 3},
987
+ {"type": InternalRuleType.ALT},
988
+ {"type": InternalRuleType.END},
989
+ ],
990
+ ],
991
+ },
992
+ ),
993
+ (
994
+ "json.gbnf (full)",
995
+ '\nroot ::= object\nvalue ::= object | array | string | number | ("true" | "false" | "null") ws\n\nobject ::=\n"{" ws (\nstring ":" ws value\n("," ws string ":" ws value)*\n)? "}" ws\n\narray ::=\n"[" ws (\nvalue\n("," ws value)*\n)? "]" ws\n\nstring ::=\n"\\"" (\n[^"\\\\\x7f\\x00-\\x1F] |\n"\\\\" (["\\\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes\n)* "\\"" ws\n\nnumber ::= ("-"? ([0-9] | [1-9] [0-9]*)) ("." [0-9]+)? ([eE] [-+]? [0-9]+)? ws\n\n# Optional space: by convention, applied in this grammar after literal chars when allowed\nws ::= ([ \\t\\n] ws)?\n\n',
996
+ {
997
+ "symbol_ids": [
998
+ ("root", 0),
999
+ ("object", 1),
1000
+ ("value", 2),
1001
+ ("array", 3),
1002
+ ("string", 4),
1003
+ ("number", 5),
1004
+ ("value_6", 6),
1005
+ ("ws", 7),
1006
+ ("object_8", 8),
1007
+ ("object_9", 9),
1008
+ ("object_10", 10),
1009
+ ("object_11", 11),
1010
+ ("array_12", 12),
1011
+ ("array_13", 13),
1012
+ ("array_14", 14),
1013
+ ("array_15", 15),
1014
+ ("string_16", 16),
1015
+ ("string_17", 17),
1016
+ ("string_18", 18),
1017
+ ("number_19", 19),
1018
+ ("number_20", 20),
1019
+ ("number_21", 21),
1020
+ ("number_22", 22),
1021
+ ("number_23", 23),
1022
+ ("number_24", 24),
1023
+ ("number_25", 25),
1024
+ ("number_26", 26),
1025
+ ("number_27", 27),
1026
+ ("number_28", 28),
1027
+ ("number_29", 29),
1028
+ ("ws_30", 30),
1029
+ ("ws_31", 31),
1030
+ ],
1031
+ "rules": [
1032
+ [{"type": InternalRuleType.RULE_REF, "value": 1}, {"type": InternalRuleType.END}],
1033
+ [
1034
+ {"type": InternalRuleType.CHAR, "value": [ord("{")]},
1035
+ {"type": InternalRuleType.RULE_REF, "value": 7},
1036
+ {"type": InternalRuleType.RULE_REF, "value": 11},
1037
+ {"type": InternalRuleType.CHAR, "value": [ord("}")]},
1038
+ {"type": InternalRuleType.RULE_REF, "value": 7},
1039
+ {"type": InternalRuleType.END},
1040
+ ],
1041
+ [
1042
+ {"type": InternalRuleType.RULE_REF, "value": 1},
1043
+ {"type": InternalRuleType.ALT},
1044
+ {"type": InternalRuleType.RULE_REF, "value": 3},
1045
+ {"type": InternalRuleType.ALT},
1046
+ {"type": InternalRuleType.RULE_REF, "value": 4},
1047
+ {"type": InternalRuleType.ALT},
1048
+ {"type": InternalRuleType.RULE_REF, "value": 5},
1049
+ {"type": InternalRuleType.ALT},
1050
+ {"type": InternalRuleType.RULE_REF, "value": 6},
1051
+ {"type": InternalRuleType.RULE_REF, "value": 7},
1052
+ {"type": InternalRuleType.END},
1053
+ ],
1054
+ [
1055
+ {"type": InternalRuleType.CHAR, "value": [ord("[")]},
1056
+ {"type": InternalRuleType.RULE_REF, "value": 7},
1057
+ {"type": InternalRuleType.RULE_REF, "value": 15},
1058
+ {"type": InternalRuleType.CHAR, "value": [ord("]")]},
1059
+ {"type": InternalRuleType.RULE_REF, "value": 7},
1060
+ {"type": InternalRuleType.END},
1061
+ ],
1062
+ [
1063
+ {"type": InternalRuleType.CHAR, "value": [ord('"')]},
1064
+ {"type": InternalRuleType.RULE_REF, "value": 18},
1065
+ {"type": InternalRuleType.CHAR, "value": [ord('"')]},
1066
+ {"type": InternalRuleType.RULE_REF, "value": 7},
1067
+ {"type": InternalRuleType.END},
1068
+ ],
1069
+ [
1070
+ {"type": InternalRuleType.RULE_REF, "value": 19},
1071
+ {"type": InternalRuleType.RULE_REF, "value": 25},
1072
+ {"type": InternalRuleType.RULE_REF, "value": 29},
1073
+ {"type": InternalRuleType.RULE_REF, "value": 7},
1074
+ {"type": InternalRuleType.END},
1075
+ ],
1076
+ [
1077
+ {"type": InternalRuleType.CHAR, "value": [ord("t")]},
1078
+ {"type": InternalRuleType.CHAR, "value": [ord("r")]},
1079
+ {"type": InternalRuleType.CHAR, "value": [ord("u")]},
1080
+ {"type": InternalRuleType.CHAR, "value": [ord("e")]},
1081
+ {"type": InternalRuleType.ALT},
1082
+ {"type": InternalRuleType.CHAR, "value": [ord("f")]},
1083
+ {"type": InternalRuleType.CHAR, "value": [ord("a")]},
1084
+ {"type": InternalRuleType.CHAR, "value": [ord("l")]},
1085
+ {"type": InternalRuleType.CHAR, "value": [ord("s")]},
1086
+ {"type": InternalRuleType.CHAR, "value": [ord("e")]},
1087
+ {"type": InternalRuleType.ALT},
1088
+ {"type": InternalRuleType.CHAR, "value": [ord("n")]},
1089
+ {"type": InternalRuleType.CHAR, "value": [ord("u")]},
1090
+ {"type": InternalRuleType.CHAR, "value": [ord("l")]},
1091
+ {"type": InternalRuleType.CHAR, "value": [ord("l")]},
1092
+ {"type": InternalRuleType.END},
1093
+ ],
1094
+ [{"type": InternalRuleType.RULE_REF, "value": 31}, {"type": InternalRuleType.END}],
1095
+ [
1096
+ {"type": InternalRuleType.RULE_REF, "value": 4},
1097
+ {"type": InternalRuleType.CHAR, "value": [ord(":")]},
1098
+ {"type": InternalRuleType.RULE_REF, "value": 7},
1099
+ {"type": InternalRuleType.RULE_REF, "value": 2},
1100
+ {"type": InternalRuleType.RULE_REF, "value": 10},
1101
+ {"type": InternalRuleType.END},
1102
+ ],
1103
+ [
1104
+ {"type": InternalRuleType.CHAR, "value": [ord(",")]},
1105
+ {"type": InternalRuleType.RULE_REF, "value": 7},
1106
+ {"type": InternalRuleType.RULE_REF, "value": 4},
1107
+ {"type": InternalRuleType.CHAR, "value": [ord(":")]},
1108
+ {"type": InternalRuleType.RULE_REF, "value": 7},
1109
+ {"type": InternalRuleType.RULE_REF, "value": 2},
1110
+ {"type": InternalRuleType.END},
1111
+ ],
1112
+ [
1113
+ {"type": InternalRuleType.RULE_REF, "value": 9},
1114
+ {"type": InternalRuleType.RULE_REF, "value": 10},
1115
+ {"type": InternalRuleType.ALT},
1116
+ {"type": InternalRuleType.END},
1117
+ ],
1118
+ [
1119
+ {"type": InternalRuleType.RULE_REF, "value": 8},
1120
+ {"type": InternalRuleType.ALT},
1121
+ {"type": InternalRuleType.END},
1122
+ ],
1123
+ [
1124
+ {"type": InternalRuleType.RULE_REF, "value": 2},
1125
+ {"type": InternalRuleType.RULE_REF, "value": 14},
1126
+ {"type": InternalRuleType.END},
1127
+ ],
1128
+ [
1129
+ {"type": InternalRuleType.CHAR, "value": [ord(",")]},
1130
+ {"type": InternalRuleType.RULE_REF, "value": 7},
1131
+ {"type": InternalRuleType.RULE_REF, "value": 2},
1132
+ {"type": InternalRuleType.END},
1133
+ ],
1134
+ [
1135
+ {"type": InternalRuleType.RULE_REF, "value": 13},
1136
+ {"type": InternalRuleType.RULE_REF, "value": 14},
1137
+ {"type": InternalRuleType.ALT},
1138
+ {"type": InternalRuleType.END},
1139
+ ],
1140
+ [
1141
+ {"type": InternalRuleType.RULE_REF, "value": 12},
1142
+ {"type": InternalRuleType.ALT},
1143
+ {"type": InternalRuleType.END},
1144
+ ],
1145
+ [
1146
+ {"type": InternalRuleType.CHAR_NOT, "value": [ord('"')]},
1147
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("\\")},
1148
+ {"type": InternalRuleType.CHAR_ALT, "value": 127},
1149
+ {"type": InternalRuleType.CHAR_ALT, "value": 0},
1150
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": 31},
1151
+ {"type": InternalRuleType.ALT},
1152
+ {"type": InternalRuleType.CHAR, "value": [ord("\\")]},
1153
+ {"type": InternalRuleType.RULE_REF, "value": 17},
1154
+ {"type": InternalRuleType.END},
1155
+ ],
1156
+ [
1157
+ {"type": InternalRuleType.CHAR, "value": [ord('"')]},
1158
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("\\")},
1159
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("/")},
1160
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("b")},
1161
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("f")},
1162
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("n")},
1163
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("r")},
1164
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("t")},
1165
+ {"type": InternalRuleType.ALT},
1166
+ {"type": InternalRuleType.CHAR, "value": [ord("u")]},
1167
+ {"type": InternalRuleType.CHAR, "value": [ord("0")]},
1168
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
1169
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("a")},
1170
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("f")},
1171
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("A")},
1172
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("F")},
1173
+ {"type": InternalRuleType.CHAR, "value": [ord("0")]},
1174
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
1175
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("a")},
1176
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("f")},
1177
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("A")},
1178
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("F")},
1179
+ {"type": InternalRuleType.CHAR, "value": [ord("0")]},
1180
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
1181
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("a")},
1182
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("f")},
1183
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("A")},
1184
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("F")},
1185
+ {"type": InternalRuleType.CHAR, "value": [ord("0")]},
1186
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
1187
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("a")},
1188
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("f")},
1189
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("A")},
1190
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("F")},
1191
+ {"type": InternalRuleType.END},
1192
+ ],
1193
+ [
1194
+ {"type": InternalRuleType.RULE_REF, "value": 16},
1195
+ {"type": InternalRuleType.RULE_REF, "value": 18},
1196
+ {"type": InternalRuleType.ALT},
1197
+ {"type": InternalRuleType.END},
1198
+ ],
1199
+ [
1200
+ {"type": InternalRuleType.RULE_REF, "value": 20},
1201
+ {"type": InternalRuleType.RULE_REF, "value": 21},
1202
+ {"type": InternalRuleType.END},
1203
+ ],
1204
+ [
1205
+ {"type": InternalRuleType.CHAR, "value": [ord("-")]},
1206
+ {"type": InternalRuleType.ALT},
1207
+ {"type": InternalRuleType.END},
1208
+ ],
1209
+ [
1210
+ {"type": InternalRuleType.CHAR, "value": [ord("0")]},
1211
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
1212
+ {"type": InternalRuleType.ALT},
1213
+ {"type": InternalRuleType.CHAR, "value": [ord("1")]},
1214
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
1215
+ {"type": InternalRuleType.RULE_REF, "value": 22},
1216
+ {"type": InternalRuleType.END},
1217
+ ],
1218
+ [
1219
+ {"type": InternalRuleType.CHAR, "value": [ord("0")]},
1220
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
1221
+ {"type": InternalRuleType.RULE_REF, "value": 22},
1222
+ {"type": InternalRuleType.ALT},
1223
+ {"type": InternalRuleType.END},
1224
+ ],
1225
+ [
1226
+ {"type": InternalRuleType.CHAR, "value": [ord(".")]},
1227
+ {"type": InternalRuleType.RULE_REF, "value": 24},
1228
+ {"type": InternalRuleType.END},
1229
+ ],
1230
+ [
1231
+ {"type": InternalRuleType.CHAR, "value": [ord("0")]},
1232
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
1233
+ {"type": InternalRuleType.RULE_REF, "value": 24},
1234
+ {"type": InternalRuleType.ALT},
1235
+ {"type": InternalRuleType.CHAR, "value": [ord("0")]},
1236
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
1237
+ {"type": InternalRuleType.END},
1238
+ ],
1239
+ [
1240
+ {"type": InternalRuleType.RULE_REF, "value": 23},
1241
+ {"type": InternalRuleType.ALT},
1242
+ {"type": InternalRuleType.END},
1243
+ ],
1244
+ [
1245
+ {"type": InternalRuleType.CHAR, "value": [ord("e")]},
1246
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("E")},
1247
+ {"type": InternalRuleType.RULE_REF, "value": 27},
1248
+ {"type": InternalRuleType.RULE_REF, "value": 28},
1249
+ {"type": InternalRuleType.END},
1250
+ ],
1251
+ [
1252
+ {"type": InternalRuleType.CHAR, "value": [ord("-")]},
1253
+ {"type": InternalRuleType.CHAR_ALT, "value": ord("+")},
1254
+ {"type": InternalRuleType.ALT},
1255
+ {"type": InternalRuleType.END},
1256
+ ],
1257
+ [
1258
+ {"type": InternalRuleType.CHAR, "value": [ord("0")]},
1259
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
1260
+ {"type": InternalRuleType.RULE_REF, "value": 28},
1261
+ {"type": InternalRuleType.ALT},
1262
+ {"type": InternalRuleType.CHAR, "value": [ord("0")]},
1263
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": ord("9")},
1264
+ {"type": InternalRuleType.END},
1265
+ ],
1266
+ [
1267
+ {"type": InternalRuleType.RULE_REF, "value": 26},
1268
+ {"type": InternalRuleType.ALT},
1269
+ {"type": InternalRuleType.END},
1270
+ ],
1271
+ [
1272
+ {"type": InternalRuleType.CHAR, "value": [ord(" ")]},
1273
+ {"type": InternalRuleType.CHAR_ALT, "value": 9},
1274
+ {"type": InternalRuleType.CHAR_ALT, "value": 10},
1275
+ {"type": InternalRuleType.RULE_REF, "value": 7},
1276
+ {"type": InternalRuleType.END},
1277
+ ],
1278
+ [
1279
+ {"type": InternalRuleType.RULE_REF, "value": 30},
1280
+ {"type": InternalRuleType.ALT},
1281
+ {"type": InternalRuleType.END},
1282
+ ],
1283
+ ],
1284
+ },
1285
+ ),
1286
+ (
1287
+ "japanese",
1288
+ "\n# A probably incorrect grammar for Japanese\nroot ::= jp-char+ ([ \\t\\n] jp-char+)*\njp-char ::= hiragana | katakana | punctuation | cjk\nhiragana ::= [ぁ-ゟ]\nkatakana ::= [ァ-ヿ]\npunctuation ::= [、-〾]\ncjk ::= [一-鿿]\n\n",
1289
+ {
1290
+ "symbol_ids": [
1291
+ ("root", 0),
1292
+ ("jp-char", 1),
1293
+ ("root_2", 2),
1294
+ ("root_3", 3),
1295
+ ("root_4", 4),
1296
+ ("root_5", 5),
1297
+ ("hiragana", 6),
1298
+ ("katakana", 7),
1299
+ ("punctuation", 8),
1300
+ ("cjk", 9),
1301
+ ],
1302
+ "rules": [
1303
+ [
1304
+ {"type": InternalRuleType.RULE_REF, "value": 2},
1305
+ {"type": InternalRuleType.RULE_REF, "value": 5},
1306
+ {"type": InternalRuleType.END},
1307
+ ],
1308
+ [
1309
+ {"type": InternalRuleType.RULE_REF, "value": 6},
1310
+ {"type": InternalRuleType.ALT},
1311
+ {"type": InternalRuleType.RULE_REF, "value": 7},
1312
+ {"type": InternalRuleType.ALT},
1313
+ {"type": InternalRuleType.RULE_REF, "value": 8},
1314
+ {"type": InternalRuleType.ALT},
1315
+ {"type": InternalRuleType.RULE_REF, "value": 9},
1316
+ {"type": InternalRuleType.END},
1317
+ ],
1318
+ [
1319
+ {"type": InternalRuleType.RULE_REF, "value": 1},
1320
+ {"type": InternalRuleType.RULE_REF, "value": 2},
1321
+ {"type": InternalRuleType.ALT},
1322
+ {"type": InternalRuleType.RULE_REF, "value": 1},
1323
+ {"type": InternalRuleType.END},
1324
+ ],
1325
+ [
1326
+ {"type": InternalRuleType.CHAR, "value": [ord(" ")]},
1327
+ {"type": InternalRuleType.CHAR_ALT, "value": 9},
1328
+ {"type": InternalRuleType.CHAR_ALT, "value": 10},
1329
+ {"type": InternalRuleType.RULE_REF, "value": 4},
1330
+ {"type": InternalRuleType.END},
1331
+ ],
1332
+ [
1333
+ {"type": InternalRuleType.RULE_REF, "value": 1},
1334
+ {"type": InternalRuleType.RULE_REF, "value": 4},
1335
+ {"type": InternalRuleType.ALT},
1336
+ {"type": InternalRuleType.RULE_REF, "value": 1},
1337
+ {"type": InternalRuleType.END},
1338
+ ],
1339
+ [
1340
+ {"type": InternalRuleType.RULE_REF, "value": 3},
1341
+ {"type": InternalRuleType.RULE_REF, "value": 5},
1342
+ {"type": InternalRuleType.ALT},
1343
+ {"type": InternalRuleType.END},
1344
+ ],
1345
+ [
1346
+ {"type": InternalRuleType.CHAR, "value": [12353]},
1347
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": 12447},
1348
+ {"type": InternalRuleType.END},
1349
+ ],
1350
+ [
1351
+ {"type": InternalRuleType.CHAR, "value": [12449]},
1352
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": 12543},
1353
+ {"type": InternalRuleType.END},
1354
+ ],
1355
+ [
1356
+ {"type": InternalRuleType.CHAR, "value": [12289]},
1357
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": 12350},
1358
+ {"type": InternalRuleType.END},
1359
+ ],
1360
+ [
1361
+ {"type": InternalRuleType.CHAR, "value": [19968]},
1362
+ {"type": InternalRuleType.CHAR_RNG_UPPER, "value": 40959},
1363
+ {"type": InternalRuleType.END},
1364
+ ],
1365
+ ],
1366
+ },
1367
+ ),
1368
+ ]
1369
+
1370
+
1371
+ def describe_grammar_parser_tests():
1372
+ @pytest.mark.parametrize(
1373
+ ("grammar", "expectation"),
1374
+ [(grammar, expectation) for _key, grammar, expectation in GRAMMARS],
1375
+ ids=[key for key, _grammar, _expectation in GRAMMARS],
1376
+ )
1377
+ def it_parses_grammar(grammar, expectation):
1378
+ parsed_grammar = RulesBuilder(grammar)
1379
+ assert parsed_grammar.rules == expectation["rules"]
1380
+ assert list(parsed_grammar.symbol_ids) == expectation["symbol_ids"]