Upload experiments v1, v2-models-effort, v2-tests-configuration as on disk 2026-11-11 (part 65)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- v2-tests-configuration/runs/20261012T035221Z_69800922/ported_implementation/tests/generated/iteration/iteration.test.ts +53 -0
- v2-tests-configuration/runs/20261012T035221Z_69800922/ported_implementation/tests/generated/iteration/iteration_with_additional_strings.test.ts +178 -0
- v2-tests-configuration/runs/20261012T035221Z_69800922/ported_implementation/tests/generated/iteration/iteration_with_an_initial_string.test.ts +125 -0
- v2-tests-configuration/runs/20261012T035221Z_69800922/ported_implementation/tests/generated/validation/validate_grammar.test.ts +55 -0
- v2-tests-configuration/runs/20261012T035221Z_69800922/ported_implementation/tests/generated/validation/validate_input.test.ts +96 -0
- v2-tests-configuration/runs/20261012T035221Z_69800922/ported_implementation/tsconfig.json +12 -0
- v2-tests-configuration/runs/20261012T035221Z_69800922/ported_implementation/vitest.config.ts +7 -0
- v2-tests-configuration/runs/20261012T035221Z_69800922/proxy.log +27 -0
- v2-tests-configuration/runs/20261012T035221Z_69800922/result.json +1 -0
- v2-tests-configuration/runs/20261012T035221Z_69800922/run.json +11 -0
- v2-tests-configuration/runs/20261012T035221Z_69800922/stderr.txt +0 -0
- v2-tests-configuration/runs/20261012T035221Z_69800922/transcript/.claude/projects/-target/75cbe6ae-9c9f-498d-932c-5417de6987cf.jsonl +0 -0
- v2-tests-configuration/runs/20261012T035221Z_69800922/transcript/.claude/projects/-target/75cbe6ae-9c9f-498d-932c-5417de6987cf/subagents/agent-a268528f7241df89a.jsonl +0 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/manifest.json +113 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/.gitignore +3 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/Makefile +52 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/README.md +36 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/conftest.py +70 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/__init__.py +22 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/gbnf.py +32 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/gbnf_test.py +105 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/__init__.py +3 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/colorize.py +14 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/colorize_test.py +13 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/generic_set.py +41 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/generic_set_test.py +38 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/get_input_as_code_points.py +12 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/get_input_as_code_points_test.py +13 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/get_parent_stack_id.py +19 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/get_parent_stack_id_test.py +49 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/get_serialized_rule_key.py +27 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/get_serialized_rule_key_test.py +54 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/graph.py +172 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/graph_node.py +47 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/graph_node_test.py +49 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/graph_pointer.py +97 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/graph_pointer_test.py +112 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/graph_test.py +52 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/parse_state.py +52 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/parse_state_test.py +67 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/print.py +67 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/print_test.py +47 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/rule_ref.py +25 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/rule_ref_test.py +23 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/type_guards.py +64 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/type_guards_test.py +113 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/types.py +52 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_parser/__init__.py +3 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_parser/build_rule_stack.py +82 -0
- v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_parser/build_rule_stack_test.py +282 -0
v2-tests-configuration/runs/20261012T035221Z_69800922/ported_implementation/tests/generated/iteration/iteration.test.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, test } from 'vitest';
|
| 2 |
+
import { GBNF, RuleChar, RuleCharExclude, RuleEnd } from '../../../src/index.ts';
|
| 3 |
+
|
| 4 |
+
type ExpectedRule = { type: 'char' | 'char_exclude' | 'end'; value?: (number | [number, number])[] };
|
| 5 |
+
|
| 6 |
+
const transformExpected = (e: ExpectedRule): RuleChar | RuleCharExclude | RuleEnd => {
|
| 7 |
+
if (e.type === 'char') {
|
| 8 |
+
return new RuleChar(e.value!);
|
| 9 |
+
}
|
| 10 |
+
if (e.type === 'char_exclude') {
|
| 11 |
+
return new RuleCharExclude(e.value!);
|
| 12 |
+
}
|
| 13 |
+
if (e.type === 'end') {
|
| 14 |
+
return new RuleEnd();
|
| 15 |
+
}
|
| 16 |
+
throw new Error(`Unknown type found in expectation array: ${(e as ExpectedRule).type}`);
|
| 17 |
+
};
|
| 18 |
+
|
| 19 |
+
const sortRules = <R extends { type: string; value?: unknown }>(rules: R[]): R[] => {
|
| 20 |
+
const key = (r: R) => JSON.stringify({ type: r.type, value: r.value });
|
| 21 |
+
return [...rules].sort((a, b) => (key(a) < key(b) ? -1 : key(a) > key(b) ? 1 : 0));
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
const RETURNS_PARSE_STATE_CASES: [string, ExpectedRule[]][] = [
|
| 25 |
+
["root ::= \"foo\"", [{"type": "char", "value": [102]}]],
|
| 26 |
+
["root ::= \"foo\" | \"bar\" ", [{"type": "char", "value": [102]}, {"type": "char", "value": [98]}]],
|
| 27 |
+
["root ::= \"foo\" | \"bar\" | \"gaz\"", [{"type": "char", "value": [102]}, {"type": "char", "value": [98]}, {"type": "char", "value": [103]}]],
|
| 28 |
+
["root ::= \"foo\" | \"bar\" | \"baz\"", [{"type": "char", "value": [102]}, {"type": "char", "value": [98]}]],
|
| 29 |
+
["root ::= [^x]", [{"type": "char_exclude", "value": [120]}]],
|
| 30 |
+
["root ::= [^f] \"o\"", [{"type": "char_exclude", "value": [102]}]],
|
| 31 |
+
["root ::= [^A-Z]", [{"type": "char_exclude", "value": [[65, 90]]}]],
|
| 32 |
+
["root ::= [^A-Z0-9]", [{"type": "char_exclude", "value": [[65, 90], [48, 57]]}]],
|
| 33 |
+
["root ::= [^A-Z0-9_-]", [{"type": "char_exclude", "value": [[65, 90], [48, 57], 95, 45]}]],
|
| 34 |
+
["\nroot ::= foo\nfoo ::= \"foo\"\n ", [{"type": "char", "value": [102]}]],
|
| 35 |
+
["\nroot ::= f\nf ::= foo\nfoo ::= \"foo\"\n ", [{"type": "char", "value": [102]}]],
|
| 36 |
+
["\nroot ::= foo | \"bar\"\nfoo ::= \"foo\"", [{"type": "char", "value": [102]}, {"type": "char", "value": [98]}]],
|
| 37 |
+
["\nroot ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"\n ", [{"type": "char", "value": [102]}, {"type": "char", "value": [98]}]],
|
| 38 |
+
["\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"\n ", [{"type": "char", "value": [102]}, {"type": "char", "value": [98]}]],
|
| 39 |
+
["root ::= [a-z]", [{"type": "char", "value": [[97, 122]]}]],
|
| 40 |
+
["root ::= [a-zA-Z]", [{"type": "char", "value": [[97, 122], [65, 90]]}]],
|
| 41 |
+
["root ::= [a-z]?", [{"type": "char", "value": [[97, 122]]}, {"type": "end"}]],
|
| 42 |
+
["root ::= [a-z]+", [{"type": "char", "value": [[97, 122]]}]],
|
| 43 |
+
["root ::= [a-z]*", [{"type": "char", "value": [[97, 122]]}, {"type": "end"}]],
|
| 44 |
+
["\n root ::= \"foo\"\n foo ::= \"foo\"", [{"type": "char", "value": [102]}]],
|
| 45 |
+
["\n root ::= (expr \"=\" term \"\")+\n expr ::= term ([-+*/] term)*\n term ::= [0-9]+\n ", [{"type": "char", "value": [[48, 57]]}]],
|
| 46 |
+
];
|
| 47 |
+
|
| 48 |
+
describe('iteration', () => {
|
| 49 |
+
test.each(RETURNS_PARSE_STATE_CASES)('it returns parse state for grammar %j', (grammar, expected) => {
|
| 50 |
+
const state = [...GBNF(grammar)];
|
| 51 |
+
expect(state).toEqual(expected.map(transformExpected));
|
| 52 |
+
});
|
| 53 |
+
});
|
v2-tests-configuration/runs/20261012T035221Z_69800922/ported_implementation/tests/generated/iteration/iteration_with_additional_strings.test.ts
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, test } from 'vitest';
|
| 2 |
+
import { GBNF, InputParseError, RuleChar, RuleCharExclude, RuleEnd } from '../../../src/index.ts';
|
| 3 |
+
|
| 4 |
+
type ExpectedRule = { type: 'char' | 'char_exclude' | 'end'; value?: (number | [number, number])[] };
|
| 5 |
+
|
| 6 |
+
const transformExpected = (e: ExpectedRule): RuleChar | RuleCharExclude | RuleEnd => {
|
| 7 |
+
if (e.type === 'char') {
|
| 8 |
+
return new RuleChar(e.value!);
|
| 9 |
+
}
|
| 10 |
+
if (e.type === 'char_exclude') {
|
| 11 |
+
return new RuleCharExclude(e.value!);
|
| 12 |
+
}
|
| 13 |
+
if (e.type === 'end') {
|
| 14 |
+
return new RuleEnd();
|
| 15 |
+
}
|
| 16 |
+
throw new Error(`Unknown type found in expectation array: ${(e as ExpectedRule).type}`);
|
| 17 |
+
};
|
| 18 |
+
|
| 19 |
+
const sortRules = <R extends { type: string; value?: unknown }>(rules: R[]): R[] => {
|
| 20 |
+
const key = (r: R) => JSON.stringify({ type: r.type, value: r.value });
|
| 21 |
+
return [...rules].sort((a, b) => (key(a) < key(b) ? -1 : key(a) > key(b) ? 1 : 0));
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
const RAISES_CASES: [string, string, string][] = [
|
| 25 |
+
["root ::= \"foo\"", "f", "1"],
|
| 26 |
+
["root ::= \"foo\"", "f", "b"],
|
| 27 |
+
["root ::= \"foo\"", "f", "o1"],
|
| 28 |
+
["root ::= \"foo\"", "f", "ooo"],
|
| 29 |
+
["root ::= \"foo\"", "f", "ooooooo"],
|
| 30 |
+
["root ::= \"foo\" | \"bar\"", "f", "1"],
|
| 31 |
+
["root ::= \"foo\" | \"bar\"", "b", "1"],
|
| 32 |
+
["root ::= \"foo\" | \"bar\"", "f", "o1"],
|
| 33 |
+
["root ::= \"foo\" | \"bar\"", "b", "a1"],
|
| 34 |
+
["root ::= \"foo\" | \"bar\"", "f", "ooo"],
|
| 35 |
+
["root ::= \"foo\" | \"bar\"", "b", "arrr"],
|
| 36 |
+
["root ::= \"foo\" | \"bar\" | \"baz\"", "f", "1"],
|
| 37 |
+
["root ::= \"foo\" | \"bar\" | \"baz\"", "f", "z"],
|
| 38 |
+
["root ::= \"foo\" | \"bar\" | \"baz\"", "b", "b1"],
|
| 39 |
+
["root ::= \"foo\" | \"bar\" | \"baz\"", "f", "o1"],
|
| 40 |
+
["root ::= \"foo\" | \"bar\" | \"baz\"", "b", "al"],
|
| 41 |
+
["root ::= \"foo\" | \"bar\" | \"baz\"", "f", "ooo"],
|
| 42 |
+
["root ::= \"foo\" | \"bar\" | \"baz\"", "b", "azrr"],
|
| 43 |
+
["root ::= foo\nfoo ::=\"foo\"", "f", "1"],
|
| 44 |
+
["root ::= foo\nfoo ::=\"foo\"", "f", "b"],
|
| 45 |
+
["root ::= foo\nfoo ::=\"foo\"", "f", "o1"],
|
| 46 |
+
["root ::= foo\nfoo ::=\"foo\"", "f", "ooo"],
|
| 47 |
+
["root::=foo|\"bar\"\nfoo::=\"foo\"", "f", "1"],
|
| 48 |
+
["root::=foo|\"bar\"\nfoo::=\"foo\"", "f", "z"],
|
| 49 |
+
["root::=foo|\"bar\"\nfoo::=\"foo\"", "b", "b1"],
|
| 50 |
+
["root::=foo|\"bar\"\nfoo::=\"foo\"", "f", "o1"],
|
| 51 |
+
["root::=foo|\"bar\"\nfoo::=\"foo\"", "f", "ooo"],
|
| 52 |
+
["root::=foo|\"bar\"\nfoo::=\"foo\"", "b", "arr"],
|
| 53 |
+
["root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"", "f", "1"],
|
| 54 |
+
["root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"", "f", "z"],
|
| 55 |
+
["root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"", "b", "1"],
|
| 56 |
+
["root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"", "f", "o1"],
|
| 57 |
+
["root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"", "f", "ooo"],
|
| 58 |
+
["root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"", "b", "arr"],
|
| 59 |
+
["root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"", "f", "1"],
|
| 60 |
+
["root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"", "f", "z"],
|
| 61 |
+
["root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"", "b", "1"],
|
| 62 |
+
["root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"", "f", "o1"],
|
| 63 |
+
["root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"", "f", "ooo"],
|
| 64 |
+
["root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"", "b", "arr"],
|
| 65 |
+
["root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"", "b", "azr"],
|
| 66 |
+
["root ::= [a-z]", "a", "A"],
|
| 67 |
+
["root ::= [a-z]", "a", "0"],
|
| 68 |
+
["root ::= [a-z]", "a", "az"],
|
| 69 |
+
["root ::= [a-z]?", "a", "A"],
|
| 70 |
+
["root ::= [a-z]?", "a", "0"],
|
| 71 |
+
["root ::= [a-z]?", "a", "az"],
|
| 72 |
+
["root ::= [a-z]+", "a", "A"],
|
| 73 |
+
["root ::= [a-z]+", "a", "0"],
|
| 74 |
+
["root ::= [a-z]+", "a", "z0"],
|
| 75 |
+
["root ::= [a-z]*", "a", "A"],
|
| 76 |
+
["root ::= [a-z]*", "a", "0"],
|
| 77 |
+
["root ::= [a-z]*", "a", "z0"],
|
| 78 |
+
];
|
| 79 |
+
|
| 80 |
+
const PARSES_CASES: [string, string, string, ExpectedRule[]][] = [
|
| 81 |
+
["root ::= \"foo\"", "", "f", [{"type": "char", "value": [111]}]],
|
| 82 |
+
["root ::= \"foo\"", "f", "o", [{"type": "char", "value": [111]}]],
|
| 83 |
+
["root ::= \"foo\"", "fo", "o", [{"type": "end"}]],
|
| 84 |
+
["root ::= \"foo\" | \"bar\" ", "", "f", [{"type": "char", "value": [111]}]],
|
| 85 |
+
["root ::= \"foo\" | \"bar\" ", "f", "o", [{"type": "char", "value": [111]}]],
|
| 86 |
+
["root ::= \"foo\" | \"bar\" ", "fo", "o", [{"type": "end"}]],
|
| 87 |
+
["root ::= \"foo\" | \"bar\" ", "", "b", [{"type": "char", "value": [97]}]],
|
| 88 |
+
["root ::= \"foo\" | \"bar\" ", "b", "a", [{"type": "char", "value": [114]}]],
|
| 89 |
+
["root ::= \"foo\" | \"bar\" ", "ba", "r", [{"type": "end"}]],
|
| 90 |
+
["root ::= \"foo\" | \"bar\" | \"baz\"", "b", "a", [{"type": "char", "value": [114]}, {"type": "char", "value": [122]}]],
|
| 91 |
+
["root ::= [^f] \"o\"", "g", "o", [{"type": "end"}]],
|
| 92 |
+
["root ::= [^A-Z]", "", "a", [{"type": "end"}]],
|
| 93 |
+
["root ::= [^A-Z0-9]", "", "a", [{"type": "end"}]],
|
| 94 |
+
["root ::= [^A-Z0-9_-]", "", "a", [{"type": "end"}]],
|
| 95 |
+
["root ::= foo\nfoo ::= \"foo\"", "", "f", [{"type": "char", "value": [111]}]],
|
| 96 |
+
["root ::= foo\nfoo ::= \"foo\"", "f", "o", [{"type": "char", "value": [111]}]],
|
| 97 |
+
["root ::= foo\nfoo ::= \"foo\"", "fo", "o", [{"type": "end"}]],
|
| 98 |
+
["root ::= foo | \"bar\"\nfoo ::= \"foo\"", "", "f", [{"type": "char", "value": [111]}]],
|
| 99 |
+
["root ::= foo | \"bar\"\nfoo ::= \"foo\"", "f", "o", [{"type": "char", "value": [111]}]],
|
| 100 |
+
["root ::= foo | \"bar\"\nfoo ::= \"foo\"", "fo", "o", [{"type": "end"}]],
|
| 101 |
+
["root ::= foo | \"bar\"\nfoo ::= \"foo\"", "", "b", [{"type": "char", "value": [97]}]],
|
| 102 |
+
["root ::= foo | \"bar\"\nfoo ::= \"foo\"", "b", "a", [{"type": "char", "value": [114]}]],
|
| 103 |
+
["root ::= foo | \"bar\"\nfoo ::= \"foo\"", "ba", "r", [{"type": "end"}]],
|
| 104 |
+
["root ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"", "", "f", [{"type": "char", "value": [111]}]],
|
| 105 |
+
["\nroot ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"", "f", "o", [{"type": "char", "value": [111]}]],
|
| 106 |
+
["\nroot ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"", "fo", "o", [{"type": "end"}]],
|
| 107 |
+
["root ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"", "", "b", [{"type": "char", "value": [97]}]],
|
| 108 |
+
["root ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"", "b", "a", [{"type": "char", "value": [114]}]],
|
| 109 |
+
["root ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"", "ba", "r", [{"type": "end"}]],
|
| 110 |
+
["\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"", "", "f", [{"type": "char", "value": [111]}]],
|
| 111 |
+
["root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"", "f", "o", [{"type": "char", "value": [111]}]],
|
| 112 |
+
["root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"", "fo", "o", [{"type": "end"}]],
|
| 113 |
+
["root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"", "", "b", [{"type": "char", "value": [97]}]],
|
| 114 |
+
["\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"", "b", "a", [{"type": "char", "value": [114]}, {"type": "char", "value": [122]}]],
|
| 115 |
+
["\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"", "ba", "r", [{"type": "end"}]],
|
| 116 |
+
["\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"", "ba", "z", [{"type": "end"}]],
|
| 117 |
+
["root ::= [a-z]", "", "a", [{"type": "end"}]],
|
| 118 |
+
["root ::= [a-z]", "", "m", [{"type": "end"}]],
|
| 119 |
+
["root ::= [a-z]", "", "z", [{"type": "end"}]],
|
| 120 |
+
["root ::= [a-zA-Z]", "", "a", [{"type": "end"}]],
|
| 121 |
+
["root ::= [a-zA-Z]", "", "Z", [{"type": "end"}]],
|
| 122 |
+
["root ::= [a-zA-Z0-9]", "", "Z", [{"type": "end"}]],
|
| 123 |
+
["root ::= [a-zA-Z0-9]", "", "0", [{"type": "end"}]],
|
| 124 |
+
["root ::= [a-zA-Z0-9]", "", "9", [{"type": "end"}]],
|
| 125 |
+
["root ::= [a-z]?", "", "a", [{"type": "end"}]],
|
| 126 |
+
["root ::= [a-zA-Z]?", "", "Z", [{"type": "end"}]],
|
| 127 |
+
["root ::= [a-zA-Z0-9]?", "", "0", [{"type": "end"}]],
|
| 128 |
+
["root ::= [a-z]+", "", "a", [{"type": "char", "value": [[97, 122]]}, {"type": "end"}]],
|
| 129 |
+
["root ::= [a-zA-Z]+", "", "Z", [{"type": "char", "value": [[97, 122], [65, 90]]}, {"type": "end"}]],
|
| 130 |
+
["root ::= [a-zA-Z]+", "azA", "Z", [{"type": "char", "value": [[97, 122], [65, 90]]}, {"type": "end"}]],
|
| 131 |
+
["root ::= [a-z]*", "", "a", [{"type": "char", "value": [[97, 122]]}, {"type": "end"}]],
|
| 132 |
+
["root ::= [a-zA-Z]+", "", "Z", [{"type": "char", "value": [[97, 122], [65, 90]]}, {"type": "end"}]],
|
| 133 |
+
["root ::= [a-zA-Z]+", "acczABC", "Z", [{"type": "char", "value": [[97, 122], [65, 90]]}, {"type": "end"}]],
|
| 134 |
+
["root ::= \"foo\" | \"bar\" | \"baz\" | \"bazaar\" | \"barrington\" ", "baza", "a", [{"type": "char", "value": [114]}]],
|
| 135 |
+
["root ::= [^f]+ \"o\"", "aaa", "a", [{"type": "char_exclude", "value": [102]}, {"type": "char", "value": [111]}]],
|
| 136 |
+
["root ::= [^A-Z]+", "abc", "d", [{"type": "char_exclude", "value": [[65, 90]]}, {"type": "end"}]],
|
| 137 |
+
["root ::= [^A-Z0-9]*", "abc", "d", [{"type": "char_exclude", "value": [[65, 90], [48, 57]]}, {"type": "end"}]],
|
| 138 |
+
["root ::= [^A-Z0-9_-]*", "abc", "d", [{"type": "char_exclude", "value": [[65, 90], [48, 57], 95, 45]}, {"type": "end"}]],
|
| 139 |
+
];
|
| 140 |
+
|
| 141 |
+
describe('iteration', () => {
|
| 142 |
+
test.each(RAISES_CASES)(
|
| 143 |
+
'it raises if encountering grammar %j with starting %j and additional %j',
|
| 144 |
+
(grammar, starting, additional) => {
|
| 145 |
+
const state = GBNF(grammar, starting);
|
| 146 |
+
expect(() => state.add(additional)).toThrow(InputParseError);
|
| 147 |
+
},
|
| 148 |
+
);
|
| 149 |
+
|
| 150 |
+
test.each(PARSES_CASES)(
|
| 151 |
+
'it parses a grammar %j with starting %j and additional %j',
|
| 152 |
+
(grammar, starting, additional, expected) => {
|
| 153 |
+
let state = GBNF(grammar, starting);
|
| 154 |
+
state = state.add(additional);
|
| 155 |
+
expect(sortRules([...state])).toEqual(sortRules(expected.map(transformExpected)));
|
| 156 |
+
},
|
| 157 |
+
);
|
| 158 |
+
|
| 159 |
+
test.each([false, true])('it raises a particular error (error for most recent input: %s)', (errorForMostRecentInput) => {
|
| 160 |
+
const grammar = 'root ::= "bar"';
|
| 161 |
+
let state = GBNF(grammar);
|
| 162 |
+
state = state.add('b');
|
| 163 |
+
state = state.add('a');
|
| 164 |
+
let err: InputParseError | undefined;
|
| 165 |
+
try {
|
| 166 |
+
state.add('z');
|
| 167 |
+
} catch (e) {
|
| 168 |
+
err = e as InputParseError;
|
| 169 |
+
}
|
| 170 |
+
expect(err).toBeInstanceOf(InputParseError);
|
| 171 |
+
const expectedError = new InputParseError('z', 0, 'ba');
|
| 172 |
+
if (errorForMostRecentInput) {
|
| 173 |
+
expect(err!.errorForMostRecentInput).toEqual(expectedError.errorForMostRecentInput);
|
| 174 |
+
} else {
|
| 175 |
+
expect(String(err)).toEqual(String(expectedError));
|
| 176 |
+
}
|
| 177 |
+
});
|
| 178 |
+
});
|
v2-tests-configuration/runs/20261012T035221Z_69800922/ported_implementation/tests/generated/iteration/iteration_with_an_initial_string.test.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, test } from 'vitest';
|
| 2 |
+
import { GBNF, RuleChar, RuleCharExclude, RuleEnd } from '../../../src/index.ts';
|
| 3 |
+
|
| 4 |
+
type ExpectedRule = { type: 'char' | 'char_exclude' | 'end'; value?: (number | [number, number])[] };
|
| 5 |
+
|
| 6 |
+
const transformExpected = (e: ExpectedRule): RuleChar | RuleCharExclude | RuleEnd => {
|
| 7 |
+
if (e.type === 'char') {
|
| 8 |
+
return new RuleChar(e.value!);
|
| 9 |
+
}
|
| 10 |
+
if (e.type === 'char_exclude') {
|
| 11 |
+
return new RuleCharExclude(e.value!);
|
| 12 |
+
}
|
| 13 |
+
if (e.type === 'end') {
|
| 14 |
+
return new RuleEnd();
|
| 15 |
+
}
|
| 16 |
+
throw new Error(`Unknown type found in expectation array: ${(e as ExpectedRule).type}`);
|
| 17 |
+
};
|
| 18 |
+
|
| 19 |
+
const sortRules = <R extends { type: string; value?: unknown }>(rules: R[]): R[] => {
|
| 20 |
+
const key = (r: R) => JSON.stringify({ type: r.type, value: r.value });
|
| 21 |
+
return [...rules].sort((a, b) => (key(a) < key(b) ? -1 : key(a) > key(b) ? 1 : 0));
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
const CASES: [string, string, ExpectedRule[]][] = [
|
| 25 |
+
["root ::= \"foo\"", "", [{"type": "char", "value": [102]}]],
|
| 26 |
+
["root ::= \"foo\"", "f", [{"type": "char", "value": [111]}]],
|
| 27 |
+
["root ::= \"foo\"", "fo", [{"type": "char", "value": [111]}]],
|
| 28 |
+
["root ::= \"foo\"", "foo", [{"type": "end"}]],
|
| 29 |
+
["root ::= \"foo\" | \"bar\" ", "", [{"type": "char", "value": [102]}, {"type": "char", "value": [98]}]],
|
| 30 |
+
["root ::= \"foo\" | \"bar\" ", "f", [{"type": "char", "value": [111]}]],
|
| 31 |
+
["root ::= \"foo\" | \"bar\" ", "fo", [{"type": "char", "value": [111]}]],
|
| 32 |
+
["root ::= \"foo\" | \"bar\" ", "foo", [{"type": "end"}]],
|
| 33 |
+
["root ::= \"foo\" | \"bar\" ", "b", [{"type": "char", "value": [97]}]],
|
| 34 |
+
["root ::= \"foo\" | \"bar\" ", "ba", [{"type": "char", "value": [114]}]],
|
| 35 |
+
["root ::= \"foo\" | \"bar\" ", "bar", [{"type": "end"}]],
|
| 36 |
+
["root ::= \"foo\" | \"bar\" | \"baz\"", "ba", [{"type": "char", "value": [114]}, {"type": "char", "value": [122]}]],
|
| 37 |
+
["root ::= \"[\"", "[", [{"type": "end"}]],
|
| 38 |
+
["root ::= \"]\"", "]", [{"type": "end"}]],
|
| 39 |
+
["root ::= \"[]\"", "[]", [{"type": "end"}]],
|
| 40 |
+
["root ::= \"{\"", "{", [{"type": "end"}]],
|
| 41 |
+
["root ::= \"}\"", "}", [{"type": "end"}]],
|
| 42 |
+
["root ::= \"{}\"", "{}", [{"type": "end"}]],
|
| 43 |
+
["root ::= \"[{}]\"", "[{}]", [{"type": "end"}]],
|
| 44 |
+
["root ::= [^f] \"o\"", "", [{"type": "char_exclude", "value": [102]}]],
|
| 45 |
+
["root ::= [^f] \"o\"", "a", [{"type": "char", "value": [111]}]],
|
| 46 |
+
["root ::= [^A-Z]", "", [{"type": "char_exclude", "value": [[65, 90]]}]],
|
| 47 |
+
["root ::= [^A-Z0-9]", "", [{"type": "char_exclude", "value": [[65, 90], [48, 57]]}]],
|
| 48 |
+
["root ::= [^A-Z0-9_-]", "", [{"type": "char_exclude", "value": [[65, 90], [48, 57], 95, 45]}]],
|
| 49 |
+
["\nroot ::= foo\nfoo ::= \"foo\"", "", [{"type": "char", "value": [102]}]],
|
| 50 |
+
["root ::= foo\nfoo ::= \"foo\"\n", "f", [{"type": "char", "value": [111]}]],
|
| 51 |
+
["\nroot ::= foo\nfoo ::= \"foo\"\n", "fo", [{"type": "char", "value": [111]}]],
|
| 52 |
+
["root ::= foo\nfoo ::= \"foo\"", "foo", [{"type": "end"}]],
|
| 53 |
+
["root ::= f\nf ::= foo\nfoo ::= \"foo\"", "", [{"type": "char", "value": [102]}]],
|
| 54 |
+
["\nroot ::= foo | \"bar\"\nfoo ::= \"foo\"\n", "", [{"type": "char", "value": [102]}, {"type": "char", "value": [98]}]],
|
| 55 |
+
["\nroot ::= foo | \"bar\"\nfoo ::= \"foo\"", "f", [{"type": "char", "value": [111]}]],
|
| 56 |
+
["root ::= foo | \"bar\"\nfoo ::= \"foo\"", "fo", [{"type": "char", "value": [111]}]],
|
| 57 |
+
["root ::= foo | \"bar\"\nfoo ::= \"foo\"", "foo", [{"type": "end"}]],
|
| 58 |
+
["root ::= foo | \"bar\"\nfoo ::= \"foo\"", "b", [{"type": "char", "value": [97]}]],
|
| 59 |
+
["root ::= foo | \"bar\"\nfoo ::= \"foo\"", "ba", [{"type": "char", "value": [114]}]],
|
| 60 |
+
["root ::= foo | \"bar\"\nfoo ::= \"foo\"", "bar", [{"type": "end"}]],
|
| 61 |
+
["\nroot ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"", "", [{"type": "char", "value": [102]}, {"type": "char", "value": [98]}]],
|
| 62 |
+
["\nroot ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"", "f", [{"type": "char", "value": [111]}]],
|
| 63 |
+
["\nroot ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"", "fo", [{"type": "char", "value": [111]}]],
|
| 64 |
+
["\nroot ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"", "foo", [{"type": "end"}]],
|
| 65 |
+
["\nroot ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"", "b", [{"type": "char", "value": [97]}]],
|
| 66 |
+
["\nroot ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"", "ba", [{"type": "char", "value": [114]}]],
|
| 67 |
+
["\nroot ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"", "bar", [{"type": "end"}]],
|
| 68 |
+
["\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"", "", [{"type": "char", "value": [102]}, {"type": "char", "value": [98]}]],
|
| 69 |
+
["\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"", "f", [{"type": "char", "value": [111]}]],
|
| 70 |
+
["\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"", "fo", [{"type": "char", "value": [111]}]],
|
| 71 |
+
["\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"", "foo", [{"type": "end"}]],
|
| 72 |
+
["\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"", "b", [{"type": "char", "value": [97]}]],
|
| 73 |
+
["\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"", "ba", [{"type": "char", "value": [114]}, {"type": "char", "value": [122]}]],
|
| 74 |
+
["\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"", "bar", [{"type": "end"}]],
|
| 75 |
+
["\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"", "baz", [{"type": "end"}]],
|
| 76 |
+
["root ::= [a-z]", "", [{"type": "char", "value": [[97, 122]]}]],
|
| 77 |
+
["root ::= [a-zA-Z]", "", [{"type": "char", "value": [[97, 122], [65, 90]]}]],
|
| 78 |
+
["root ::= [a-z]", "a", [{"type": "end"}]],
|
| 79 |
+
["root ::= [a-z]", "m", [{"type": "end"}]],
|
| 80 |
+
["root ::= [a-z]", "z", [{"type": "end"}]],
|
| 81 |
+
["root ::= [a-zA-Z]", "a", [{"type": "end"}]],
|
| 82 |
+
["root ::= [a-zA-Z]", "Z", [{"type": "end"}]],
|
| 83 |
+
["root ::= [a-zA-Z0-9]", "Z", [{"type": "end"}]],
|
| 84 |
+
["root ::= [a-zA-Z0-9]", "0", [{"type": "end"}]],
|
| 85 |
+
["root ::= [a-zA-Z0-9]", "9", [{"type": "end"}]],
|
| 86 |
+
["root ::= [a-z]?", "", [{"type": "char", "value": [[97, 122]]}, {"type": "end"}]],
|
| 87 |
+
["root ::= [a-z]?", "a", [{"type": "end"}]],
|
| 88 |
+
["root ::= [a-zA-Z]?", "Z", [{"type": "end"}]],
|
| 89 |
+
["root ::= [a-zA-Z0-9]?", "0", [{"type": "end"}]],
|
| 90 |
+
["root ::= [a-z]+", "", [{"type": "char", "value": [[97, 122]]}]],
|
| 91 |
+
["root ::= [a-z]+", "l", [{"type": "char", "value": [[97, 122]]}, {"type": "end"}]],
|
| 92 |
+
["root ::= [a-zA-Z]+", "Z", [{"type": "char", "value": [[97, 122], [65, 90]]}, {"type": "end"}]],
|
| 93 |
+
["root ::= [a-zA-Z]+", "aZ", [{"type": "char", "value": [[97, 122], [65, 90]]}, {"type": "end"}]],
|
| 94 |
+
["root ::= [a-zA-Z]+", "azAZ", [{"type": "char", "value": [[97, 122], [65, 90]]}, {"type": "end"}]],
|
| 95 |
+
["root ::= [a-z]*", "", [{"type": "char", "value": [[97, 122]]}, {"type": "end"}]],
|
| 96 |
+
["root ::= [a-z]*", "a", [{"type": "char", "value": [[97, 122]]}, {"type": "end"}]],
|
| 97 |
+
["root ::= [a-zA-Z]+", "Z", [{"type": "char", "value": [[97, 122], [65, 90]]}, {"type": "end"}]],
|
| 98 |
+
["root ::= [a-zA-Z]+", "abczABCZ", [{"type": "char", "value": [[97, 122], [65, 90]]}, {"type": "end"}]],
|
| 99 |
+
["root ::= [^f]+ \"o\"", "aaa", [{"type": "char_exclude", "value": [102]}, {"type": "char", "value": [111]}]],
|
| 100 |
+
["root ::= [^A-Z]+", "abc", [{"type": "char_exclude", "value": [[65, 90]]}, {"type": "end"}]],
|
| 101 |
+
["root ::= [^A-Z0-9]*", "abc", [{"type": "char_exclude", "value": [[65, 90], [48, 57]]}, {"type": "end"}]],
|
| 102 |
+
["root ::= [^A-Z0-9_-]*", "abc", [{"type": "char_exclude", "value": [[65, 90], [48, 57], 95, 45]}, {"type": "end"}]],
|
| 103 |
+
["\nroot ::= [a-z] | xyx\nxyx ::= \"foo\"", "f", [{"type": "end"}, {"type": "char", "value": [111]}]],
|
| 104 |
+
["root ::= \"foo\" | \"bar\" | \"baz\" | \"bazaar\" | \"barrington\" ", "bazaa", [{"type": "char", "value": [114]}]],
|
| 105 |
+
["root ::= (\"bar\" | \"foo\") \"zyx\"", "bar", [{"type": "char", "value": [122]}]],
|
| 106 |
+
["root ::= \"z\" (\"bar\" | \"foo\") \"zzzz\"", "z", [{"type": "char", "value": [98]}, {"type": "char", "value": [102]}]],
|
| 107 |
+
["root ::= \"z\" (\"bar\" | \"foo\") \"zzz\"", "zbar", [{"type": "char", "value": [122]}]],
|
| 108 |
+
["\nroot ::= termz ([-+*/] termz)* \ntermz ::= [0-9]+", "1", [{"type": "char", "value": [[48, 57]]}, {"type": "char", "value": [45, 43, 42, 47]}, {"type": "end"}]],
|
| 109 |
+
["\nroot ::= expr \"=\" termy \nexpr ::= termy ([-+*/] termy)*\ntermy ::= [0-9]+", "1", [{"type": "char", "value": [[48, 57]]}, {"type": "char", "value": [45, 43, 42, 47]}, {"type": "char", "value": [61]}]],
|
| 110 |
+
["\nroot ::= (expr \"=\" terma \"\n\")+\nexpr ::= terma ([-+*/] terma)*\nterma ::= [0-9]+", "1", [{"type": "char", "value": [[48, 57]]}, {"type": "char", "value": [45, 43, 42, 47]}, {"type": "char", "value": [61]}]],
|
| 111 |
+
["root ::= (expr \"=\" termb \"\n\")+\nexpr ::= termb ([-+*/] termb)*\ntermb ::= [0-9]+", "1+", [{"type": "char", "value": [[48, 57]]}]],
|
| 112 |
+
["\nroot ::= (expr \"=\" termc \"\n\")+\nexpr ::= termc ([-+*/] termc)*\ntermc ::= [0-9]+", "1=", [{"type": "char", "value": [[48, 57]]}]],
|
| 113 |
+
["root ::= (expr \"=\" termd \"\n\")+\nexpr ::= termd ([-+*/] termd)*\ntermd ::= [0-9]+", "1+1", [{"type": "char", "value": [[48, 57]]}, {"type": "char", "value": [45, 43, 42, 47]}, {"type": "char", "value": [61]}]],
|
| 114 |
+
["root ::= (expr \"=\" term \"\n\")+\nexpr ::= term ([-+*/] term)*\nterm ::= [0-9]+", "1=1", [{"type": "char", "value": [[48, 57]]}, {"type": "char", "value": [10]}]],
|
| 115 |
+
["root ::= \"\\\"\" ( [^\\\"abcdefghA-Z])* ", "\"is not only in its lyrism, its vow to sustin it in its poo-sion, its r,v:l'y, it's tory,", [{"type": "char_exclude", "value": [34, 97, 98, 99, 100, 101, 102, 103, 104, [65, 90]]}, {"type": "end"}]],
|
| 116 |
+
["root ::= ( [^abcdefgh] | [b-z])* ", "bcdefghzyxw0123ABCZ", [{"type": "char_exclude", "value": [97, 98, 99, 100, 101, 102, 103, 104]}, {"type": "char", "value": [[98, 122]]}, {"type": "end"}]],
|
| 117 |
+
];
|
| 118 |
+
|
| 119 |
+
describe('iteration', () => {
|
| 120 |
+
test.each(CASES)('it returns parse state for grammar %j and initial string %j', (grammar, inputString, expected) => {
|
| 121 |
+
let state = GBNF(grammar);
|
| 122 |
+
state = state.add(inputString);
|
| 123 |
+
expect(sortRules([...state])).toEqual(sortRules(expected.map(transformExpected)));
|
| 124 |
+
});
|
| 125 |
+
});
|
v2-tests-configuration/runs/20261012T035221Z_69800922/ported_implementation/tests/generated/validation/validate_grammar.test.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, test } from 'vitest';
|
| 2 |
+
import { GBNF, GrammarParseError } from '../../../src/index.ts';
|
| 3 |
+
|
| 4 |
+
const VALID_GRAMMARS: string[] = [
|
| 5 |
+
"root ::= \"foo\"",
|
| 6 |
+
"root ::= \"foo\" | \"bar\"",
|
| 7 |
+
"root ::= (\"foo\" | \"bar\")",
|
| 8 |
+
"root ::= (\"foo\" | \"bar\")?",
|
| 9 |
+
"root ::= (\"foo\" | \"bar\")*",
|
| 10 |
+
"root ::= (\"foo\" | \"bar\")+",
|
| 11 |
+
"root ::= [a-z]",
|
| 12 |
+
"root ::= [a-zA-Z]",
|
| 13 |
+
"root ::= [a-zA-Z0-9]",
|
| 14 |
+
"root ::= [a-zA-Z0-9]*",
|
| 15 |
+
"root ::= [a-zA-Z0-9]?",
|
| 16 |
+
"root ::= [a-z]+",
|
| 17 |
+
"root ::= [a-zA-Z0-9]+",
|
| 18 |
+
"root ::= ([a-zA-Z0-9])*",
|
| 19 |
+
"root ::= ([a-zA-Z0-9])?",
|
| 20 |
+
"root ::= ([a-zA-Z0-9])+",
|
| 21 |
+
"\n root ::= foo\n foo ::= \"foo\"",
|
| 22 |
+
"\n root ::= foo\n foo ::= \"foo\" | \"bar\" | ([a-z])?\n ",
|
| 23 |
+
];
|
| 24 |
+
|
| 25 |
+
const INVALID_GRAMMARS: [string, number, string][] = [
|
| 26 |
+
["", 0, "No rules were found"],
|
| 27 |
+
["root = \"foo\"", 5, "Expecting ::= at 5"],
|
| 28 |
+
["root ::= foo\nfoo := \"foo\"\n ", 17, "Expecting ::= at 17"],
|
| 29 |
+
["root ::= foo", 9, "Undefined rule identifier \"foo\""],
|
| 30 |
+
["root ::= foo\nbar ::= \"bar\"\n ", 9, "Undefined rule identifier \"foo\""],
|
| 31 |
+
["root ::= foo\nfoo ::= baz\nbar ::= \"bar\"\n ", 21, "Undefined rule identifier \"baz\""],
|
| 32 |
+
["root ::= foo ::= bar", 13, "Expecting newline or end at 13"],
|
| 33 |
+
["root ::= ([a-z]\nfoo ::= \"foo\"\n ", 20, "Expecting ')' at 20"],
|
| 34 |
+
];
|
| 35 |
+
|
| 36 |
+
describe('validate grammar', () => {
|
| 37 |
+
test.each(VALID_GRAMMARS)('it parses a grammar %j', (grammar) => {
|
| 38 |
+
GBNF(grammar);
|
| 39 |
+
});
|
| 40 |
+
|
| 41 |
+
test.each(INVALID_GRAMMARS)(
|
| 42 |
+
'it reports an error for an invalid grammar %j (pos %d: %s)',
|
| 43 |
+
(grammar, errorPos, errorReason) => {
|
| 44 |
+
const expected = new GrammarParseError(grammar, errorPos, errorReason);
|
| 45 |
+
let err: unknown;
|
| 46 |
+
try {
|
| 47 |
+
GBNF(grammar);
|
| 48 |
+
} catch (e) {
|
| 49 |
+
err = e;
|
| 50 |
+
}
|
| 51 |
+
expect(err).toBeInstanceOf(GrammarParseError);
|
| 52 |
+
expect(String(err)).toEqual(String(expected));
|
| 53 |
+
},
|
| 54 |
+
);
|
| 55 |
+
});
|
v2-tests-configuration/runs/20261012T035221Z_69800922/ported_implementation/tests/generated/validation/validate_input.test.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { describe, expect, test } from 'vitest';
|
| 2 |
+
import { GBNF, InputParseError } from '../../../src/index.ts';
|
| 3 |
+
|
| 4 |
+
const VALID_INPUTS: [string, string][] = [
|
| 5 |
+
["root ::= \"foo\"", "f"],
|
| 6 |
+
];
|
| 7 |
+
|
| 8 |
+
const INVALID_INPUTS: [string, string, number][] = [
|
| 9 |
+
["root ::= \"foo\"", "2", 0],
|
| 10 |
+
["root ::= \"foo\"", "b", 0],
|
| 11 |
+
["root ::= \"foo\"", "f1", 1],
|
| 12 |
+
["root ::= \"foo\"", "fo1", 2],
|
| 13 |
+
["root ::= \"foo\"", "fooo", 3],
|
| 14 |
+
["root ::= \"foo\"", "fooooooo", 3],
|
| 15 |
+
["root ::= \"foo\" | \"bar\"", "1", 0],
|
| 16 |
+
["root ::= \"foo\" | \"bar\"", "z", 0],
|
| 17 |
+
["root ::= \"foo\" | \"bar\"", "f1", 1],
|
| 18 |
+
["root ::= \"foo\" | \"bar\"", "b1", 1],
|
| 19 |
+
["root ::= \"foo\" | \"bar\"", "fo1", 2],
|
| 20 |
+
["root ::= \"foo\" | \"bar\"", "ba1", 2],
|
| 21 |
+
["root ::= \"foo\" | \"bar\"", "fooo", 3],
|
| 22 |
+
["root ::= \"foo\" | \"bar\"", "barrr", 3],
|
| 23 |
+
["root ::= \"foo\" | \"bar\" | \"baz\"", "1", 0],
|
| 24 |
+
["root ::= \"foo\" | \"bar\" | \"baz\"", "z", 0],
|
| 25 |
+
["root ::= \"foo\" | \"bar\" | \"baz\"", "f1", 1],
|
| 26 |
+
["root ::= \"foo\" | \"bar\" | \"baz\"", "b1", 1],
|
| 27 |
+
["root ::= \"foo\" | \"bar\" | \"baz\"", "fo1", 2],
|
| 28 |
+
["root ::= \"foo\" | \"bar\" | \"baz\"", "bal", 2],
|
| 29 |
+
["root ::= \"foo\" | \"bar\" | \"baz\"", "fooo", 3],
|
| 30 |
+
["root ::= \"foo\" | \"bar\" | \"baz\"", "bazrr", 3],
|
| 31 |
+
["root ::= [^a]", "a", 0],
|
| 32 |
+
["root ::= [^abc]", "b", 0],
|
| 33 |
+
["root ::= [^a-z]", "z", 0],
|
| 34 |
+
["root ::= [^a-zA-Z]", "X", 0],
|
| 35 |
+
["root ::= [^a-zA-Z0-9]", "8", 0],
|
| 36 |
+
["\n root ::= foo\n foo ::= \"foo\"", "1", 0],
|
| 37 |
+
["\n root ::= foo\n foo ::= \"foo\"", "b", 0],
|
| 38 |
+
["\n root ::= foo\n foo ::= \"foo\"", "f1", 1],
|
| 39 |
+
["\n root ::= foo\n foo ::= \"foo\"", "fo1", 2],
|
| 40 |
+
["\n root ::= foo\n foo ::= \"foo\"", "fooo", 3],
|
| 41 |
+
["\n root::=foo|\"bar\"\n foo::=\"foo\"", "1", 0],
|
| 42 |
+
["\n root::=foo|\"bar\"\n foo::=\"foo\"", "z", 0],
|
| 43 |
+
["\n root::=foo|\"bar\"\n foo::=\"foo\"", "f1", 1],
|
| 44 |
+
["\n root::=foo|\"bar\"\n foo::=\"foo\"", "b1", 1],
|
| 45 |
+
["\n root::=foo|\"bar\"\n foo::=\"foo\"", "fo1", 2],
|
| 46 |
+
["\n root::=foo|\"bar\"\n foo::=\"foo\"", "fooo", 3],
|
| 47 |
+
["\n root::=foo|\"bar\"\n foo::=\"foo\"", "barr", 3],
|
| 48 |
+
["\n root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"", "1", 0],
|
| 49 |
+
["\n root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"", "z", 0],
|
| 50 |
+
["\n root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"", "f1", 1],
|
| 51 |
+
["\n root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"", "b1", 1],
|
| 52 |
+
["\n root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"", "fo1", 2],
|
| 53 |
+
["\n root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"", "fooo", 3],
|
| 54 |
+
["\n root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"", "barr", 3],
|
| 55 |
+
["\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 ", "1", 0],
|
| 56 |
+
["\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\"", "z", 0],
|
| 57 |
+
["\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\"", "f1", 1],
|
| 58 |
+
["\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\"", "b1", 1],
|
| 59 |
+
["\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\"", "fo1", 2],
|
| 60 |
+
["\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\"", "fooo", 3],
|
| 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\"", "barr", 3],
|
| 62 |
+
["\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\"", "bazr", 3],
|
| 63 |
+
["root ::= [a-z]", "A", 0],
|
| 64 |
+
["root ::= [a-z]", "0", 0],
|
| 65 |
+
["root ::= [a-z]", "az", 1],
|
| 66 |
+
["root ::= [a-z]?", "A", 0],
|
| 67 |
+
["root ::= [a-z]?", "0", 0],
|
| 68 |
+
["root ::= [a-z]?", "az", 1],
|
| 69 |
+
["root ::= [a-z]+", "A", 0],
|
| 70 |
+
["root ::= [a-z]+", "0", 0],
|
| 71 |
+
["root ::= [a-z]+", "az0", 2],
|
| 72 |
+
["root ::= [a-z]*", "A", 0],
|
| 73 |
+
["root ::= [a-z]*", "0", 0],
|
| 74 |
+
["root ::= [a-z]*", "az0", 2],
|
| 75 |
+
["root ::= ( [^abcdefgh] | [b-z])*", "a", 0],
|
| 76 |
+
];
|
| 77 |
+
|
| 78 |
+
describe('validate input', () => {
|
| 79 |
+
test.each(VALID_INPUTS)('it parses a grammar %j with input %j', (grammar, input) => {
|
| 80 |
+
const graph = GBNF(grammar, input);
|
| 81 |
+
expect(Boolean(graph)).toBe(true);
|
| 82 |
+
});
|
| 83 |
+
|
| 84 |
+
test.each(INVALID_INPUTS)('it reports an error for grammar %j and invalid input %j at %d', (grammar, inputText, errorPos) => {
|
| 85 |
+
const graph = GBNF(grammar);
|
| 86 |
+
const expected = new InputParseError(inputText, errorPos);
|
| 87 |
+
let err: unknown;
|
| 88 |
+
try {
|
| 89 |
+
graph.add(inputText);
|
| 90 |
+
} catch (e) {
|
| 91 |
+
err = e;
|
| 92 |
+
}
|
| 93 |
+
expect(err).toBeInstanceOf(InputParseError);
|
| 94 |
+
expect(String(err)).toEqual(String(expected));
|
| 95 |
+
});
|
| 96 |
+
});
|
v2-tests-configuration/runs/20261012T035221Z_69800922/ported_implementation/tsconfig.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"compilerOptions": {
|
| 3 |
+
"target": "ES2022",
|
| 4 |
+
"module": "ESNext",
|
| 5 |
+
"moduleResolution": "Bundler",
|
| 6 |
+
"strict": true,
|
| 7 |
+
"noEmit": true,
|
| 8 |
+
"allowImportingTsExtensions": true,
|
| 9 |
+
"skipLibCheck": true
|
| 10 |
+
},
|
| 11 |
+
"include": ["src", "tests"]
|
| 12 |
+
}
|
v2-tests-configuration/runs/20261012T035221Z_69800922/ported_implementation/vitest.config.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { defineConfig } from 'vitest/config';
|
| 2 |
+
|
| 3 |
+
export default defineConfig({
|
| 4 |
+
test: {
|
| 5 |
+
include: ['src/**/*.test.ts', 'tests/**/*.test.ts'],
|
| 6 |
+
},
|
| 7 |
+
});
|
v2-tests-configuration/runs/20261012T035221Z_69800922/proxy.log
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
NOTICE Oct 12 03:52:39.882 [1]: Initializing tinyproxy ...
|
| 2 |
+
NOTICE Oct 12 03:52:39.915 [1]: Reloading config file (/etc/tinyproxy/tinyproxy.conf)
|
| 3 |
+
INFO Oct 12 03:52:39.917 [1]: Added address [0.0.0.0] to listen addresses.
|
| 4 |
+
INFO Oct 12 03:52:39.920 [1]: Adding Port [443] to the list allowed by CONNECT
|
| 5 |
+
NOTICE Oct 12 03:52:39.922 [1]: Reloading config file finished
|
| 6 |
+
INFO Oct 12 03:52:39.923 [1]: listen_sock called with addr = '0.0.0.0'
|
| 7 |
+
INFO Oct 12 03:52:39.926 [1]: trying to listen on host[0.0.0.0], family[2], socktype[1], proto[6]
|
| 8 |
+
INFO Oct 12 03:52:39.928 [1]: listening on fd [3]
|
| 9 |
+
INFO Oct 12 03:52:39.931 [1]: Not running as root, so not changing UID/GID.
|
| 10 |
+
INFO Oct 12 03:52:39.932 [1]: Setting the various signals.
|
| 11 |
+
INFO Oct 12 03:52:39.933 [1]: Starting main loop. Accepting connections.
|
| 12 |
+
CONNECT Oct 12 03:52:40.492 [1]: Connect (file descriptor 5): 192.168.32.3
|
| 13 |
+
CONNECT Oct 12 03:52:40.495 [1]: Request (file descriptor 5): CONNECT api.anthropic.com:443 HTTP/1.1
|
| 14 |
+
INFO Oct 12 03:52:40.496 [1]: No upstream proxy for api.anthropic.com
|
| 15 |
+
INFO Oct 12 03:52:40.499 [1]: opensock: opening connection to api.anthropic.com:443
|
| 16 |
+
INFO Oct 12 03:52:40.502 [1]: opensock: getaddrinfo returned for api.anthropic.com:443
|
| 17 |
+
CONNECT Oct 12 03:52:40.618 [1]: Established connection to host "api.anthropic.com" using file descriptor 6.
|
| 18 |
+
INFO Oct 12 03:52:40.622 [1]: Not sending client headers to remote machine
|
| 19 |
+
CONNECT Oct 12 03:58:04.870 [1]: Connect (file descriptor 7): 192.168.32.3
|
| 20 |
+
CONNECT Oct 12 03:58:04.871 [1]: Request (file descriptor 7): CONNECT api.anthropic.com:443 HTTP/1.1
|
| 21 |
+
INFO Oct 12 03:58:04.873 [1]: No upstream proxy for api.anthropic.com
|
| 22 |
+
INFO Oct 12 03:58:04.874 [1]: opensock: opening connection to api.anthropic.com:443
|
| 23 |
+
INFO Oct 12 03:58:04.877 [1]: opensock: getaddrinfo returned for api.anthropic.com:443
|
| 24 |
+
CONNECT Oct 12 03:58:04.882 [1]: Established connection to host "api.anthropic.com" using file descriptor 8.
|
| 25 |
+
INFO Oct 12 03:58:04.884 [1]: Not sending client headers to remote machine
|
| 26 |
+
INFO Oct 12 04:01:29.327 [1]: Closed connection between local client (fd:7) and remote client (fd:8)
|
| 27 |
+
INFO Oct 12 04:01:29.327 [1]: Closed connection between local client (fd:5) and remote client (fd:6)
|
v2-tests-configuration/runs/20261012T035221Z_69800922/result.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"duration_api_ms":602587,"stop_reason":"end_turn","session_id":"75cbe6ae-9c9f-498d-932c-5417de6987cf","total_cost_usd":3.0610042,"usage":{"input_tokens":6,"cache_creation_input_tokens":2397,"cache_read_input_tokens":258343,"output_tokens":1106,"output_tokens_details":{"thinking_tokens":46},"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0},"service_tier":"standard","cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":2397},"inference_geo":"global","iterations":[],"speed":"standard","fallback_credit":null},"modelUsage":{"claude-opus-5-5":{"inputTokens":86,"outputTokens":71709,"cacheReadInputTokens":3315176,"cacheCreationInputTokens":192689,"webSearchRequests":0,"costUSD":3.0610042,"contextWindow":1000000,"maxOutputTokens":128000,"thinkingTokens":14901,"canonicalModel":"claude-opus-5-5","provider":"firstParty","costBasis":"list"}},"permission_denials":[],"terminal_reason":"completed","fast_mode_state":"off","fast_mode_disabled_reason":"sdk_opt_in_required","origin":{"kind":"task-notification","producer":"session-task","runId":"0mv4q1vlc-4f6af79b"},"subagent_stats":{"spawned":1,"requested":{"background":1,"foreground":0,"unset":0},"started_in_background":1,"max_depth":1,"spawned_by_subagents":0,"completed":1,"failed":0,"killed":{"parent":0,"user":0,"system":0},"refused":{"depth_limit":0,"concurrency_limit":0,"budget":0},"by_type":{"general-purpose":1}},"safety_stops":0,"is_error":false,"num_turns":2,"subtype":"success","api_error_status":null,"result":"The port is in `/target` and all **508 tests pass** (`vitest run`, 27 files). There is no TypeScript compiler on this machine, so nothing has been type-checked. Every source module does load and run directly under Node 24, which strips the types. To make that work I avoided enums and constructor parameter properties.\n\n**Layout.** `src/` copies the Python package structure with the same snake_case file names (`grammar_graph/`, `grammar_parser/`, `rules_builder/`, `utils/errors/`). The entry point is `src/index.ts`, which exports `GBNF` (named and default), `ParseState`, the rule classes, `RuleType`, the type guards and the two error classes. Identifiers are camelCase, e.g. `fetch_next` → `fetchNext` and `error_for_most_recent_input` → `errorForMostRecentInput`. `README.md` has the full Python → TypeScript mapping.\n\n**API choices:**\n- Each rule has a `type` field (`'char'`, `'char_exclude'` or `'end'`) plus `value`, so it compares equal to the plain `{type, value}` objects the generated tests expect.\n- `ParseState` is iterable and has `add()`, `rules()`, `size` and `grammar`. TypeScript has no equivalent of Python's `state + \"x\"` or `state(\"x\")`, so only `state.add(\"x\")` exists. The two Python unit tests for those operators were dropped, which is why the unit test count is 2 lower.\n- Python's `ValueError` becomes a plain `Error`.\n\n**Behaviour differences from the Python:**\n- **Unicode:** input is processed by code point, like Python, and `\\U0001F4A9` or literal emoji in grammars parse correctly. Grammar error positions, however, count UTF-16 units. They only differ from Python if an emoji or other astral character comes before the error.\n- **Error types:** in a few malformed cases Python crashes with an `IndexError` or `KeyError`, such as a grammar with no `root` rule. The port raises a `GrammarParseError` there instead.\n- **Rule order:** the port returns referenced rules in the order they're defined in the grammar. Python stores them in a set, so its order is effectively arbitrary.\n\n**Tests:**\n- **Integration tests** (`tests/generated/`): all 320 cases, converted mechanically from the Python test data.\n- **`rules_builder` test**: all 41 cases, generated from the Python test data.\n- **Remaining unit tests**: hand-ported next to the source as `*.test.ts`. The Python `patch` mocks became `vi.mock`.\n\nWithout network access I couldn't install vitest locally. `node_modules/vitest` is a symlink to the global install; `pnpm install` will replace it with the real dev dependency in `package.json`.","ttft_ms":3082,"type":"result","duration_ms":16343,"uuid":"815d9d7d-1d30-430e-8727-28e3de28a9ac","ttft_stream_ms":2111,"time_to_request_ms":18,"first_content_frame_ms":2111,"queued_turn_count":0,"result_index":1}
|
v2-tests-configuration/runs/20261012T035221Z_69800922/run.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"params": {
|
| 3 |
+
"source-language": "python",
|
| 4 |
+
"include-unit-tests": true,
|
| 5 |
+
"include-source-integration-tests": true,
|
| 6 |
+
"include-target-integration-tests": false
|
| 7 |
+
},
|
| 8 |
+
"created": "2026-10-12T03:52:21Z",
|
| 9 |
+
"ended": "2026-10-12T04:01:30Z",
|
| 10 |
+
"exit_code": 0
|
| 11 |
+
}
|
v2-tests-configuration/runs/20261012T035221Z_69800922/stderr.txt
ADDED
|
File without changes
|
v2-tests-configuration/runs/20261012T035221Z_69800922/transcript/.claude/projects/-target/75cbe6ae-9c9f-498d-932c-5417de6987cf.jsonl
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
v2-tests-configuration/runs/20261012T035221Z_69800922/transcript/.claude/projects/-target/75cbe6ae-9c9f-498d-932c-5417de6987cf/subagents/agent-a268528f7241df89a.jsonl
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/manifest.json
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"timestamp": "2026-10-12T03:56:27Z",
|
| 3 |
+
"completed_at": "2026-10-12T04:04:41Z",
|
| 4 |
+
"exit_code": 0,
|
| 5 |
+
"study": "v2-test-configurations",
|
| 6 |
+
"condition": {
|
| 7 |
+
"name": "source-javascript_unit-tests_source-integration-tests_target-integration-tests_effort-medium_model-claude-opus-5-5",
|
| 8 |
+
"source_language": "javascript",
|
| 9 |
+
"target_language": "python",
|
| 10 |
+
"include_unit_tests": true,
|
| 11 |
+
"include_source_integration_tests": true,
|
| 12 |
+
"include_target_integration_tests": true,
|
| 13 |
+
"effort": "medium",
|
| 14 |
+
"model": "claude-opus-5-5"
|
| 15 |
+
},
|
| 16 |
+
"derivation": {
|
| 17 |
+
"gbnf_commit": "13f1aca495d11e160fffd68c4ba299a2415909d8"
|
| 18 |
+
},
|
| 19 |
+
"reference_implementation": {
|
| 20 |
+
"included": [
|
| 21 |
+
"javascript/.eslintrc.cjs",
|
| 22 |
+
"javascript/.gitignore",
|
| 23 |
+
"javascript/.npmignore",
|
| 24 |
+
"javascript/README.md",
|
| 25 |
+
"javascript/integration-tests/.gitignore",
|
| 26 |
+
"javascript/integration-tests/generated/iteration/grammars.test.ts",
|
| 27 |
+
"javascript/integration-tests/generated/iteration/iteration-with-additional-strings.test.ts",
|
| 28 |
+
"javascript/integration-tests/generated/iteration/iteration-with-an-initial-string.test.ts",
|
| 29 |
+
"javascript/integration-tests/generated/iteration/iteration.test.ts",
|
| 30 |
+
"javascript/integration-tests/generated/validation/validate-grammar.test.ts",
|
| 31 |
+
"javascript/integration-tests/generated/validation/validate-input.test.ts",
|
| 32 |
+
"javascript/package.json",
|
| 33 |
+
"javascript/src/gbnf.test.ts",
|
| 34 |
+
"javascript/src/gbnf.ts",
|
| 35 |
+
"javascript/src/grammar-graph/colorize.test.ts",
|
| 36 |
+
"javascript/src/grammar-graph/colorize.ts",
|
| 37 |
+
"javascript/src/grammar-graph/generic-set.test.ts",
|
| 38 |
+
"javascript/src/grammar-graph/generic-set.ts",
|
| 39 |
+
"javascript/src/grammar-graph/get-input-as-code-points.test.ts",
|
| 40 |
+
"javascript/src/grammar-graph/get-input-as-code-points.ts",
|
| 41 |
+
"javascript/src/grammar-graph/get-parent-stack-id.test.ts",
|
| 42 |
+
"javascript/src/grammar-graph/get-parent-stack-id.ts",
|
| 43 |
+
"javascript/src/grammar-graph/get-serialized-rule-key.test.ts",
|
| 44 |
+
"javascript/src/grammar-graph/get-serialized-rule-key.ts",
|
| 45 |
+
"javascript/src/grammar-graph/graph-node.test.ts",
|
| 46 |
+
"javascript/src/grammar-graph/graph-node.ts",
|
| 47 |
+
"javascript/src/grammar-graph/graph-pointer.test.ts",
|
| 48 |
+
"javascript/src/grammar-graph/graph-pointer.ts",
|
| 49 |
+
"javascript/src/grammar-graph/graph.test.ts",
|
| 50 |
+
"javascript/src/grammar-graph/graph.ts",
|
| 51 |
+
"javascript/src/grammar-graph/index.ts",
|
| 52 |
+
"javascript/src/grammar-graph/parse-state.test.ts",
|
| 53 |
+
"javascript/src/grammar-graph/parse-state.ts",
|
| 54 |
+
"javascript/src/grammar-graph/print.test.ts",
|
| 55 |
+
"javascript/src/grammar-graph/print.ts",
|
| 56 |
+
"javascript/src/grammar-graph/rule-ref.test.ts",
|
| 57 |
+
"javascript/src/grammar-graph/rule-ref.ts",
|
| 58 |
+
"javascript/src/grammar-graph/type-guards.test.ts",
|
| 59 |
+
"javascript/src/grammar-graph/type-guards.ts",
|
| 60 |
+
"javascript/src/grammar-graph/types.ts",
|
| 61 |
+
"javascript/src/grammar-parser/build-rule-stack.test.ts",
|
| 62 |
+
"javascript/src/grammar-parser/build-rule-stack.ts",
|
| 63 |
+
"javascript/src/index.ts",
|
| 64 |
+
"javascript/src/rules-builder/index.ts",
|
| 65 |
+
"javascript/src/rules-builder/is-word-char.test.ts",
|
| 66 |
+
"javascript/src/rules-builder/is-word-char.ts",
|
| 67 |
+
"javascript/src/rules-builder/parse-char.test.ts",
|
| 68 |
+
"javascript/src/rules-builder/parse-char.ts",
|
| 69 |
+
"javascript/src/rules-builder/parse-name.test.ts",
|
| 70 |
+
"javascript/src/rules-builder/parse-name.ts",
|
| 71 |
+
"javascript/src/rules-builder/parse-space.test.ts",
|
| 72 |
+
"javascript/src/rules-builder/parse-space.ts",
|
| 73 |
+
"javascript/src/rules-builder/rules-builder.test.ts",
|
| 74 |
+
"javascript/src/rules-builder/rules-builder.ts",
|
| 75 |
+
"javascript/src/rules-builder/symbol-ids.ts",
|
| 76 |
+
"javascript/src/rules-builder/type-guards.ts",
|
| 77 |
+
"javascript/src/rules-builder/types.ts",
|
| 78 |
+
"javascript/src/umd.ts",
|
| 79 |
+
"javascript/src/utils/errors/build-error-position.test.ts",
|
| 80 |
+
"javascript/src/utils/errors/build-error-position.ts",
|
| 81 |
+
"javascript/src/utils/errors/get-input-as-string.test.ts",
|
| 82 |
+
"javascript/src/utils/errors/get-input-as-string.ts",
|
| 83 |
+
"javascript/src/utils/errors/grammar-parse-error.test.ts",
|
| 84 |
+
"javascript/src/utils/errors/grammar-parse-error.ts",
|
| 85 |
+
"javascript/src/utils/errors/input-parse-error.test.ts",
|
| 86 |
+
"javascript/src/utils/errors/input-parse-error.ts",
|
| 87 |
+
"javascript/src/utils/is-point-in-range.test.ts",
|
| 88 |
+
"javascript/src/utils/is-point-in-range.ts",
|
| 89 |
+
"javascript/tsconfig.json",
|
| 90 |
+
"javascript/tsconfig.test.json",
|
| 91 |
+
"javascript/vite.config.esm.ts",
|
| 92 |
+
"javascript/vite.config.ts",
|
| 93 |
+
"javascript/vite.config.umd.ts",
|
| 94 |
+
"javascript/vitest.config.integration.ts",
|
| 95 |
+
"javascript/vitest.config.unit.ts",
|
| 96 |
+
"python/Makefile",
|
| 97 |
+
"python/pyproject.toml",
|
| 98 |
+
"python/tests/generated/iteration/iteration_test.py",
|
| 99 |
+
"python/tests/generated/iteration/iteration_with_additional_strings_test.py",
|
| 100 |
+
"python/tests/generated/iteration/iteration_with_an_initial_string_test.py",
|
| 101 |
+
"python/tests/generated/validation/validate_grammar_test.py",
|
| 102 |
+
"python/tests/generated/validation/validate_input_test.py",
|
| 103 |
+
"python/uv.lock"
|
| 104 |
+
]
|
| 105 |
+
},
|
| 106 |
+
"sandbox": {
|
| 107 |
+
"image_id": "sha256:63a173aaefbf8fd2107009d2662adeffc93cc2002636f1042a154f2d88dd8397"
|
| 108 |
+
},
|
| 109 |
+
"harness": {
|
| 110 |
+
"commit": "353d5b38f59a4ff542135ffe337cbfdf9199c5c5",
|
| 111 |
+
"dirty": true
|
| 112 |
+
}
|
| 113 |
+
}
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.venv/
|
| 2 |
+
__pycache__/
|
| 3 |
+
.pytest_cache/
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/Makefile
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
SHELL := /bin/bash
|
| 2 |
+
|
| 3 |
+
init:
|
| 4 |
+
uv venv --python python3.12 && uv sync
|
| 5 |
+
|
| 6 |
+
lint:
|
| 7 |
+
uvx ruff check . --fix
|
| 8 |
+
|
| 9 |
+
copy_assets:
|
| 10 |
+
cp ../../../LICENSE ./
|
| 11 |
+
|
| 12 |
+
build_all:
|
| 13 |
+
make copy_assets && make build_package
|
| 14 |
+
|
| 15 |
+
build_package:
|
| 16 |
+
uv run --with build -m build
|
| 17 |
+
|
| 18 |
+
upgrade_dev_dependencies:
|
| 19 |
+
./upgrade_dev_dependencies.sh
|
| 20 |
+
|
| 21 |
+
build_test_writer:
|
| 22 |
+
cd ../../test-writer && pnpm build
|
| 23 |
+
|
| 24 |
+
write_integration_tests:
|
| 25 |
+
@echo "Writing integration tests..."
|
| 26 |
+
@$(MAKE) -s build_test_writer
|
| 27 |
+
@node --no-warnings ../../test-writer/dist/test-writer.mjs \
|
| 28 |
+
validation \
|
| 29 |
+
iteration/iteration \
|
| 30 |
+
--testDir ../test \
|
| 31 |
+
--targetDir ./tests/generated \
|
| 32 |
+
--language python
|
| 33 |
+
|
| 34 |
+
test_integration:
|
| 35 |
+
@$(MAKE) -s write_integration_tests
|
| 36 |
+
@$(MAKE) -s test_integration_only
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
test_integration_only:
|
| 40 |
+
uv run pytest ./tests/generated
|
| 41 |
+
|
| 42 |
+
# test_integration_watch:
|
| 43 |
+
# uv run ptw --now tests/generated
|
| 44 |
+
|
| 45 |
+
test:
|
| 46 |
+
make test_unit
|
| 47 |
+
|
| 48 |
+
test_unit:
|
| 49 |
+
uv run pytest ./gbnf/**/*_test.py
|
| 50 |
+
|
| 51 |
+
test_unit_watch:
|
| 52 |
+
uv run ptw --now .
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# GBNF (Python)
|
| 2 |
+
|
| 3 |
+
A library for parsing `.gbnf` grammar files in Python. Port of the `gbnf` JavaScript package.
|
| 4 |
+
|
| 5 |
+
## Usage
|
| 6 |
+
|
| 7 |
+
```python
|
| 8 |
+
from gbnf import GBNF, RuleChar, RuleEnd
|
| 9 |
+
|
| 10 |
+
state = GBNF('root ::= "yes" | "no"')
|
| 11 |
+
print(list(state)) # [RuleChar(value=[121]), RuleChar(value=[110])]
|
| 12 |
+
```
|
| 13 |
+
|
| 14 |
+
If the grammar is invalid, `GBNF` raises `GrammarParseError`.
|
| 15 |
+
|
| 16 |
+
States are _immutable_. To parse more input, call `state.add(...)` (or `state + "..."`, or `state("...")`):
|
| 17 |
+
|
| 18 |
+
```python
|
| 19 |
+
state = GBNF('root ::= "I like green eggs and ham"')
|
| 20 |
+
state = state.add("I li")
|
| 21 |
+
print(list(state)) # [RuleChar(value=[ord("k")])]
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
Invalid input raises `InputParseError`. An initial string may be passed as `GBNF(grammar, "initial")`.
|
| 25 |
+
|
| 26 |
+
Rules returned:
|
| 27 |
+
|
| 28 |
+
- `RuleChar` (`type == "char"`): `value` is a list of code points or `[start, end]` ranges to match.
|
| 29 |
+
- `RuleCharExclude` (`type == "char_exclude"`): code points / ranges that must _not_ match.
|
| 30 |
+
- `RuleEnd` (`type == "end"`): a valid end of the input.
|
| 31 |
+
|
| 32 |
+
## Tests
|
| 33 |
+
|
| 34 |
+
```bash
|
| 35 |
+
pytest gbnf tests
|
| 36 |
+
```
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/conftest.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Minimal stand-in for the pytest-describe plugin (unavailable offline).
|
| 2 |
+
|
| 3 |
+
Collects `describe_*` functions: each is executed once, and the functions it
|
| 4 |
+
defines locally become test items (nested `describe_*` blocks are supported).
|
| 5 |
+
If the real pytest-describe plugin is installed, it is used instead.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import sys
|
| 9 |
+
import types
|
| 10 |
+
|
| 11 |
+
import pytest
|
| 12 |
+
from _pytest.python import PyCollector
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
import pytest_describe # noqa: F401
|
| 16 |
+
|
| 17 |
+
HAS_PYTEST_DESCRIBE = True
|
| 18 |
+
except ImportError:
|
| 19 |
+
HAS_PYTEST_DESCRIBE = False
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def _evaluate_describe(func):
|
| 23 |
+
captured = {}
|
| 24 |
+
|
| 25 |
+
def profiler(frame, event, _arg):
|
| 26 |
+
if event == "return" and frame.f_code is func.__code__:
|
| 27 |
+
captured.update(frame.f_locals)
|
| 28 |
+
|
| 29 |
+
previous = sys.getprofile()
|
| 30 |
+
sys.setprofile(profiler)
|
| 31 |
+
try:
|
| 32 |
+
func()
|
| 33 |
+
finally:
|
| 34 |
+
sys.setprofile(previous)
|
| 35 |
+
return captured
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class DescribeBlock(pytest.Module):
|
| 39 |
+
@classmethod
|
| 40 |
+
def from_function(cls, parent, func):
|
| 41 |
+
block = cls.from_parent(parent, path=parent.path, name=func.__name__)
|
| 42 |
+
block._describe_func = func
|
| 43 |
+
return block
|
| 44 |
+
|
| 45 |
+
def _getobj(self):
|
| 46 |
+
module = types.ModuleType(self.name)
|
| 47 |
+
module.__dict__.update(_evaluate_describe(self._describe_func))
|
| 48 |
+
return module
|
| 49 |
+
|
| 50 |
+
def _register_setup_module_fixture(self):
|
| 51 |
+
pass
|
| 52 |
+
|
| 53 |
+
def _register_setup_function_fixture(self):
|
| 54 |
+
pass
|
| 55 |
+
|
| 56 |
+
def collect(self):
|
| 57 |
+
self.session._fixturemanager.parsefactories(self)
|
| 58 |
+
return PyCollector.collect(self)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
if not HAS_PYTEST_DESCRIBE:
|
| 62 |
+
|
| 63 |
+
@pytest.hookimpl(tryfirst=True)
|
| 64 |
+
def pytest_pycollect_makeitem(collector, name, obj):
|
| 65 |
+
if isinstance(obj, types.FunctionType) and name.startswith("describe_"):
|
| 66 |
+
return DescribeBlock.from_function(collector, obj)
|
| 67 |
+
return None
|
| 68 |
+
|
| 69 |
+
def pytest_configure(config):
|
| 70 |
+
config.addinivalue_line("python_functions", "test_*")
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/__init__.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .gbnf import GBNF
|
| 2 |
+
from .grammar_graph.parse_state import ParseState
|
| 3 |
+
from .grammar_graph.type_guards import is_range
|
| 4 |
+
from .grammar_graph.types import Range, ResolvedRule, RuleChar, RuleCharExclude, RuleEnd, RuleType
|
| 5 |
+
from .utils.errors.grammar_parse_error import GrammarParseError
|
| 6 |
+
from .utils.errors.input_parse_error import InputParseError
|
| 7 |
+
|
| 8 |
+
Rule = ResolvedRule
|
| 9 |
+
|
| 10 |
+
__all__ = [
|
| 11 |
+
"GBNF",
|
| 12 |
+
"GrammarParseError",
|
| 13 |
+
"InputParseError",
|
| 14 |
+
"ParseState",
|
| 15 |
+
"Range",
|
| 16 |
+
"Rule",
|
| 17 |
+
"RuleChar",
|
| 18 |
+
"RuleCharExclude",
|
| 19 |
+
"RuleEnd",
|
| 20 |
+
"RuleType",
|
| 21 |
+
"is_range",
|
| 22 |
+
]
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/gbnf.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
from typing import TYPE_CHECKING
|
| 5 |
+
|
| 6 |
+
from .grammar_graph.graph import Graph
|
| 7 |
+
from .grammar_graph.parse_state import ParseState
|
| 8 |
+
from .grammar_parser.build_rule_stack import build_rule_stack
|
| 9 |
+
from .rules_builder.rules_builder import RulesBuilder
|
| 10 |
+
from .utils.errors.grammar_parse_error import GrammarParseError
|
| 11 |
+
|
| 12 |
+
if TYPE_CHECKING:
|
| 13 |
+
from .grammar_graph.types import ValidInput
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def GBNF(grammar_input: object, initial_string: ValidInput = "") -> ParseState: # noqa: N802
|
| 17 |
+
grammar = grammar_input if isinstance(grammar_input, str) else str(grammar_input)
|
| 18 |
+
builder = RulesBuilder(grammar)
|
| 19 |
+
rules, symbol_ids = builder.rules, builder.symbol_ids
|
| 20 |
+
if len(rules) == 0:
|
| 21 |
+
raise GrammarParseError(grammar, 0, "No rules were found")
|
| 22 |
+
if not symbol_ids.has("root"):
|
| 23 |
+
raise GrammarParseError(
|
| 24 |
+
grammar,
|
| 25 |
+
0,
|
| 26 |
+
f"Grammar does not contain a root symbol. Available symbols are: {json.dumps(list(symbol_ids.keys()))}",
|
| 27 |
+
)
|
| 28 |
+
root_id = symbol_ids.get("root")
|
| 29 |
+
|
| 30 |
+
stacked_rules = [build_rule_stack(rule) for rule in rules if rule is not None]
|
| 31 |
+
graph = Graph(grammar, stacked_rules, root_id)
|
| 32 |
+
return ParseState(graph, graph.add(initial_string))
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/gbnf_test.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from types import SimpleNamespace
|
| 3 |
+
from unittest.mock import MagicMock, patch
|
| 4 |
+
|
| 5 |
+
import pytest
|
| 6 |
+
|
| 7 |
+
from .gbnf import GBNF
|
| 8 |
+
from .rules_builder.symbol_ids import SymbolIds
|
| 9 |
+
from .rules_builder.types import InternalRuleDefChar, InternalRuleDefEnd
|
| 10 |
+
from .utils.errors.grammar_parse_error import GrammarParseError
|
| 11 |
+
|
| 12 |
+
MODULE = "gbnf.gbnf"
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def make_symbol_ids(entries):
|
| 16 |
+
symbol_ids = SymbolIds()
|
| 17 |
+
for pos, (key, value) in enumerate(entries):
|
| 18 |
+
symbol_ids.set(key, value, pos)
|
| 19 |
+
return symbol_ids
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@pytest.fixture
|
| 23 |
+
def mocks():
|
| 24 |
+
with (
|
| 25 |
+
patch(f"{MODULE}.RulesBuilder") as rules_builder,
|
| 26 |
+
patch(f"{MODULE}.build_rule_stack") as build_rule_stack,
|
| 27 |
+
patch(f"{MODULE}.Graph") as graph,
|
| 28 |
+
patch(f"{MODULE}.ParseState") as parse_state,
|
| 29 |
+
):
|
| 30 |
+
# default: a rules builder with no rules and no symbols
|
| 31 |
+
rules_builder.side_effect = lambda _grammar: SimpleNamespace(rules=[], symbol_ids=SymbolIds())
|
| 32 |
+
yield SimpleNamespace(
|
| 33 |
+
RulesBuilder=rules_builder,
|
| 34 |
+
build_rule_stack=build_rule_stack,
|
| 35 |
+
Graph=graph,
|
| 36 |
+
ParseState=parse_state,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def use_rules_builder(mocks, rules, symbol_ids):
|
| 41 |
+
mocks.RulesBuilder.side_effect = lambda _grammar: SimpleNamespace(rules=rules, symbol_ids=symbol_ids)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def test_should_throw_if_rules_are_empty(mocks):
|
| 45 |
+
with pytest.raises(GrammarParseError) as exc_info:
|
| 46 |
+
GBNF("")
|
| 47 |
+
assert exc_info.value == GrammarParseError("", 0, "No rules were found")
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def test_should_throw_if_symbol_ids_does_not_have_root(mocks):
|
| 51 |
+
symbol_ids = make_symbol_ids([("root1", 1), ("root2", 1)])
|
| 52 |
+
use_rules_builder(mocks, [[InternalRuleDefEnd()]], symbol_ids)
|
| 53 |
+
with pytest.raises(GrammarParseError) as exc_info:
|
| 54 |
+
GBNF("")
|
| 55 |
+
# TS does `JSON.stringify(symbolIds.keys())`, which serializes a Map iterator as "{}";
|
| 56 |
+
# the Python port lists the available symbol names instead.
|
| 57 |
+
assert exc_info.value == GrammarParseError(
|
| 58 |
+
"",
|
| 59 |
+
0,
|
| 60 |
+
f"Grammar does not contain a root symbol. Available symbols are: {json.dumps(['root1', 'root2'])}",
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def _setup_parse_state(mocks, rules):
|
| 65 |
+
use_rules_builder(mocks, rules, make_symbol_ids([("root", 1)]))
|
| 66 |
+
mock_graph = MagicMock()
|
| 67 |
+
mock_graph.add.return_value = "foo"
|
| 68 |
+
mocks.Graph.return_value = mock_graph
|
| 69 |
+
mock_parse_state = object()
|
| 70 |
+
mocks.ParseState.return_value = mock_parse_state
|
| 71 |
+
mocks.build_rule_stack.side_effect = lambda rule: rule
|
| 72 |
+
return mock_graph, mock_parse_state
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def test_it_builds_parse_state(mocks):
|
| 76 |
+
grammar = "grammar"
|
| 77 |
+
initial_string = "foo"
|
| 78 |
+
rules = [[InternalRuleDefEnd()], [InternalRuleDefChar([97])]]
|
| 79 |
+
mock_graph, mock_parse_state = _setup_parse_state(mocks, rules)
|
| 80 |
+
|
| 81 |
+
assert GBNF(grammar, initial_string) is mock_parse_state
|
| 82 |
+
# TS passes (rule, index, rules) via Array.prototype.map; Python calls build_rule_stack(rule)
|
| 83 |
+
mocks.build_rule_stack.assert_any_call(rules[0])
|
| 84 |
+
mocks.build_rule_stack.assert_any_call(rules[1])
|
| 85 |
+
mocks.RulesBuilder.assert_called_once_with(grammar)
|
| 86 |
+
mocks.Graph.assert_called_once_with(grammar, rules, 1)
|
| 87 |
+
mock_graph.add.assert_called_once_with(initial_string)
|
| 88 |
+
mocks.ParseState.assert_called_once_with(mock_graph, "foo")
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def test_it_builds_parse_state_with_a_gbnf_rule_as_input(mocks):
|
| 92 |
+
# The TS test uses the `g` template tag from ./builder/template-tags.js, which is not part of
|
| 93 |
+
# the port; any object with __str__ stands in for a GBNFRule.
|
| 94 |
+
class GBNFRuleLike:
|
| 95 |
+
def __str__(self):
|
| 96 |
+
return '"foo"'
|
| 97 |
+
|
| 98 |
+
grammar = GBNFRuleLike()
|
| 99 |
+
initial_string = "foo"
|
| 100 |
+
rules = [[InternalRuleDefEnd()], [InternalRuleDefChar([97])]]
|
| 101 |
+
_mock_graph, mock_parse_state = _setup_parse_state(mocks, rules)
|
| 102 |
+
|
| 103 |
+
assert GBNF(grammar, initial_string) is mock_parse_state
|
| 104 |
+
mocks.RulesBuilder.assert_called_once_with(str(grammar))
|
| 105 |
+
mocks.Graph.assert_called_once_with(str(grammar), rules, 1)
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/__init__.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .graph import Graph
|
| 2 |
+
|
| 3 |
+
__all__ = ["Graph"]
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/colorize.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from enum import StrEnum
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class Color(StrEnum):
|
| 5 |
+
BLUE = "\x1b[34m"
|
| 6 |
+
CYAN = "\x1b[36m"
|
| 7 |
+
GREEN = "\x1b[32m"
|
| 8 |
+
RED = "\x1b[31m"
|
| 9 |
+
GRAY = "\x1b[90m"
|
| 10 |
+
YELLOW = "\x1b[33m"
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def colorize(s: str | int, color: str) -> str:
|
| 14 |
+
return f"{color}{s}"
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/colorize_test.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .colorize import Color, colorize
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def test_should_colorize_strings_correctly():
|
| 5 |
+
assert colorize("hello", Color.BLUE) == "\x1b[34mhello"
|
| 6 |
+
assert colorize("test", Color.CYAN) == "\x1b[36mtest"
|
| 7 |
+
assert colorize("example", Color.GREEN) == "\x1b[32mexample"
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def test_should_colorize_numbers_correctly():
|
| 11 |
+
assert colorize(123, Color.RED) == "\x1b[31m123"
|
| 12 |
+
assert colorize(456, Color.GRAY) == "\x1b[90m456"
|
| 13 |
+
assert colorize(789, Color.YELLOW) == "\x1b[33m789"
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/generic_set.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from collections.abc import Callable, Iterator
|
| 2 |
+
from typing import Generic, TypeVar
|
| 3 |
+
|
| 4 |
+
T = TypeVar("T")
|
| 5 |
+
K = TypeVar("K")
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class GenericSet(Generic[T, K]):
|
| 9 |
+
"""An insertion-ordered set whose membership is determined by a key function."""
|
| 10 |
+
|
| 11 |
+
def __init__(self, get_key: Callable[[T], K]):
|
| 12 |
+
self.get_key = get_key
|
| 13 |
+
self._keys: dict[K, T] = {}
|
| 14 |
+
|
| 15 |
+
def add(self, el: T) -> None:
|
| 16 |
+
key = self.get_key(el)
|
| 17 |
+
if key not in self._keys:
|
| 18 |
+
self._keys[key] = el
|
| 19 |
+
|
| 20 |
+
def delete(self, el: T) -> None:
|
| 21 |
+
key = self.get_key(el)
|
| 22 |
+
if key not in self._keys:
|
| 23 |
+
raise KeyError("Could not get ref")
|
| 24 |
+
del self._keys[key]
|
| 25 |
+
|
| 26 |
+
def has(self, el: T) -> bool:
|
| 27 |
+
key = self.get_key(el)
|
| 28 |
+
return key in self._keys and self._keys[key] is el
|
| 29 |
+
|
| 30 |
+
def get(self, el: T) -> T | None:
|
| 31 |
+
return self._keys.get(self.get_key(el))
|
| 32 |
+
|
| 33 |
+
def __iter__(self) -> Iterator[T]:
|
| 34 |
+
return iter(list(self._keys.values()))
|
| 35 |
+
|
| 36 |
+
def __len__(self) -> int:
|
| 37 |
+
return len(self._keys)
|
| 38 |
+
|
| 39 |
+
@property
|
| 40 |
+
def size(self) -> int:
|
| 41 |
+
return len(self._keys)
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/generic_set_test.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .generic_set import GenericSet
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def test_it_instantiates():
|
| 5 |
+
assert isinstance(GenericSet(lambda _el: "foo"), GenericSet)
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def test_it_iterates():
|
| 9 |
+
s = GenericSet(lambda el: el)
|
| 10 |
+
s.add(1)
|
| 11 |
+
s.add(2)
|
| 12 |
+
s.add(3)
|
| 13 |
+
assert list(s) == [1, 2, 3]
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def test_it_skips_duplicates():
|
| 17 |
+
s = GenericSet(lambda el: el)
|
| 18 |
+
s.add(1)
|
| 19 |
+
s.add(1)
|
| 20 |
+
s.add(1)
|
| 21 |
+
assert list(s) == [1]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def test_it_skips_duplicates_based_on_a_given_key():
|
| 25 |
+
s = GenericSet(lambda el: el["id"])
|
| 26 |
+
s.add({"id": 1})
|
| 27 |
+
s.add({"id": 1, "foo": "foo"})
|
| 28 |
+
s.add({"id": 1, "bar": "bar"})
|
| 29 |
+
assert list(s) == [{"id": 1}]
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def test_can_delete():
|
| 33 |
+
s = GenericSet(lambda el: el["id"])
|
| 34 |
+
s.add({"id": 1})
|
| 35 |
+
s.delete({"id": 1})
|
| 36 |
+
s.add({"id": 2, "foo": "foo"})
|
| 37 |
+
s.add({"id": 2, "bar": "bar"})
|
| 38 |
+
assert list(s) == [{"id": 2, "foo": "foo"}]
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/get_input_as_code_points.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from typing import TYPE_CHECKING
|
| 4 |
+
|
| 5 |
+
if TYPE_CHECKING:
|
| 6 |
+
from .types import ValidInput
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def get_input_as_code_points(src: ValidInput) -> list[int]:
|
| 10 |
+
if not isinstance(src, str):
|
| 11 |
+
return list(src) if isinstance(src, (list, tuple)) else [src]
|
| 12 |
+
return [ord(char) for char in src]
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/get_input_as_code_points_test.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .get_input_as_code_points import get_input_as_code_points
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def test_it_returns_code_points_for_string():
|
| 5 |
+
assert get_input_as_code_points("abc") == [97, 98, 99]
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def test_it_returns_code_points_for_number():
|
| 9 |
+
assert get_input_as_code_points(99) == [99]
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def test_it_returns_code_points_for_array_of_number():
|
| 13 |
+
assert get_input_as_code_points([99, 100, 101]) == [99, 100, 101]
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/get_parent_stack_id.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from typing import TYPE_CHECKING
|
| 4 |
+
|
| 5 |
+
from .colorize import Color
|
| 6 |
+
|
| 7 |
+
if TYPE_CHECKING:
|
| 8 |
+
from .graph_pointer import GraphPointer
|
| 9 |
+
from .types import Colorize
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def get_parent_stack_id(pointer: GraphPointer, col: Colorize) -> str:
|
| 13 |
+
stack_ids: list[str] = []
|
| 14 |
+
parent = pointer.parent
|
| 15 |
+
while parent:
|
| 16 |
+
meta = parent.node.meta
|
| 17 |
+
stack_ids.append(f"{meta.stack_id},{meta.path_id},{meta.step_id}")
|
| 18 |
+
parent = parent.parent
|
| 19 |
+
return col("<-", Color.GRAY).join(col(stack_id, Color.RED) for stack_id in stack_ids)
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/get_parent_stack_id_test.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from types import SimpleNamespace
|
| 3 |
+
|
| 4 |
+
from .colorize import Color
|
| 5 |
+
from .get_parent_stack_id import get_parent_stack_id
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def s(string):
|
| 9 |
+
return json.dumps(string)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def mock_colorize(text, color):
|
| 13 |
+
return f"[{s(color)}]:{text}"
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
RED = s(Color.RED)
|
| 17 |
+
GRAY = s(Color.GRAY)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def create_mock_pointer(stack_id, path_id, step_id, parent=None):
|
| 21 |
+
return SimpleNamespace(
|
| 22 |
+
node=SimpleNamespace(meta=SimpleNamespace(stack_id=stack_id, path_id=path_id, step_id=step_id)),
|
| 23 |
+
parent=parent,
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def test_returns_an_empty_string_if_no_parents():
|
| 28 |
+
assert get_parent_stack_id(create_mock_pointer(1, 1, 1), mock_colorize) == ""
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def test_returns_a_single_parent_id_colored_correctly():
|
| 32 |
+
pointer = create_mock_pointer(2, 2, 2, create_mock_pointer(1, 1, 1))
|
| 33 |
+
assert get_parent_stack_id(pointer, mock_colorize) == f"[{RED}]:1,1,1"
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def test_returns_multiple_parent_ids_separated_by_colored_arrows():
|
| 37 |
+
grandparent = create_mock_pointer(0, 0, 0)
|
| 38 |
+
parent = create_mock_pointer(1, 1, 1, grandparent)
|
| 39 |
+
pointer = create_mock_pointer(2, 2, 2, parent)
|
| 40 |
+
assert get_parent_stack_id(pointer, mock_colorize) == f"[{RED}]:1,1,1[{GRAY}]:<-[{RED}]:0,0,0"
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def test_handles_deep_nesting_of_pointers():
|
| 44 |
+
great_grandparent = create_mock_pointer(0, 0, 0)
|
| 45 |
+
grandparent = create_mock_pointer(1, 1, 1, great_grandparent)
|
| 46 |
+
parent = create_mock_pointer(2, 2, 2, grandparent)
|
| 47 |
+
pointer = create_mock_pointer(3, 3, 3, parent)
|
| 48 |
+
expected = f"[{RED}]:2,2,2[{GRAY}]:<-[{RED}]:1,1,1[{GRAY}]:<-[{RED}]:0,0,0"
|
| 49 |
+
assert get_parent_stack_id(pointer, mock_colorize) == expected
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/get_serialized_rule_key.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
from typing import TYPE_CHECKING
|
| 5 |
+
|
| 6 |
+
from .type_guards import is_rule_char, is_rule_char_excluded, is_rule_end, is_rule_ref
|
| 7 |
+
from .types import RuleType
|
| 8 |
+
|
| 9 |
+
if TYPE_CHECKING:
|
| 10 |
+
from .types import UnresolvedRule
|
| 11 |
+
|
| 12 |
+
KEY_TRANSLATION = {
|
| 13 |
+
RuleType.END: 0,
|
| 14 |
+
RuleType.CHAR: 1,
|
| 15 |
+
RuleType.CHAR_EXCLUDE: 2,
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def get_serialized_rule_key(rule: UnresolvedRule) -> str:
|
| 20 |
+
if is_rule_end(rule):
|
| 21 |
+
return f"{KEY_TRANSLATION[RuleType.END]}"
|
| 22 |
+
|
| 23 |
+
if is_rule_char(rule) or is_rule_char_excluded(rule):
|
| 24 |
+
return f"{KEY_TRANSLATION[rule.type]}-{json.dumps(rule.value, separators=(',', ':'))}"
|
| 25 |
+
if is_rule_ref(rule):
|
| 26 |
+
return f"3-{rule.value}"
|
| 27 |
+
raise ValueError(f"Unknown rule type: {rule!r}")
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/get_serialized_rule_key_test.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from unittest.mock import patch
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
from .get_serialized_rule_key import KEY_TRANSLATION, get_serialized_rule_key
|
| 6 |
+
from .rule_ref import RuleRef
|
| 7 |
+
from .types import RuleChar, RuleCharExclude, RuleEnd, RuleType
|
| 8 |
+
|
| 9 |
+
MODULE = "gbnf.grammar_graph.get_serialized_rule_key"
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@pytest.fixture(autouse=True)
|
| 13 |
+
def guards():
|
| 14 |
+
with (
|
| 15 |
+
patch(f"{MODULE}.is_rule_end", return_value=False) as is_rule_end,
|
| 16 |
+
patch(f"{MODULE}.is_rule_char", return_value=False) as is_rule_char,
|
| 17 |
+
patch(f"{MODULE}.is_rule_char_excluded", return_value=False) as is_rule_char_excluded,
|
| 18 |
+
patch(f"{MODULE}.is_rule_ref", return_value=False) as is_rule_ref,
|
| 19 |
+
):
|
| 20 |
+
yield {
|
| 21 |
+
"is_rule_end": is_rule_end,
|
| 22 |
+
"is_rule_char": is_rule_char,
|
| 23 |
+
"is_rule_char_excluded": is_rule_char_excluded,
|
| 24 |
+
"is_rule_ref": is_rule_ref,
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def test_returns_type_for_end_rules(guards):
|
| 29 |
+
guards["is_rule_end"].return_value = True
|
| 30 |
+
assert get_serialized_rule_key(RuleEnd()) == f"{KEY_TRANSLATION[RuleType.END]}"
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def test_returns_type_and_value_for_character_rules(guards):
|
| 34 |
+
guards["is_rule_char"].return_value = True
|
| 35 |
+
assert get_serialized_rule_key(RuleChar([97])) == f"{KEY_TRANSLATION[RuleType.CHAR]}-[97]"
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def test_returns_type_and_value_for_character_exclude_rules(guards):
|
| 39 |
+
guards["is_rule_char_excluded"].return_value = True
|
| 40 |
+
rule = RuleCharExclude([97])
|
| 41 |
+
assert get_serialized_rule_key(rule) == f"{KEY_TRANSLATION[RuleType.CHAR_EXCLUDE]}-[97]"
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def test_returns_ref_type_with_value_for_reference_rules(guards):
|
| 45 |
+
guards["is_rule_ref"].return_value = True
|
| 46 |
+
assert get_serialized_rule_key(RuleRef(99)) == "3-99"
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def test_throws_an_error_for_unknown_rule_types():
|
| 50 |
+
rule = {"type": "UNKNOWN", "value": "something"}
|
| 51 |
+
# Python uses repr() of the rule in the message, rather than JSON.stringify
|
| 52 |
+
with pytest.raises(ValueError, match="Unknown rule type: ") as exc_info:
|
| 53 |
+
get_serialized_rule_key(rule)
|
| 54 |
+
assert repr(rule) in str(exc_info.value)
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/graph.py
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from collections.abc import Iterator
|
| 4 |
+
from typing import TYPE_CHECKING
|
| 5 |
+
|
| 6 |
+
from ..utils.errors.input_parse_error import InputParseError
|
| 7 |
+
from ..utils.is_point_in_range import is_point_in_range
|
| 8 |
+
from .colorize import colorize
|
| 9 |
+
from .generic_set import GenericSet
|
| 10 |
+
from .get_input_as_code_points import get_input_as_code_points
|
| 11 |
+
from .get_serialized_rule_key import get_serialized_rule_key
|
| 12 |
+
from .graph_node import GraphNode, GraphNodeMeta
|
| 13 |
+
from .graph_pointer import GraphPointer
|
| 14 |
+
from .type_guards import is_range, is_rule_char, is_rule_char_excluded, is_rule_end, is_rule_ref
|
| 15 |
+
|
| 16 |
+
if TYPE_CHECKING:
|
| 17 |
+
from .rule_ref import RuleRef
|
| 18 |
+
from .types import Pointers, UnresolvedRule, ValidInput
|
| 19 |
+
|
| 20 |
+
RootNode = dict[int, GraphNode]
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def make_pointers() -> GenericSet[GraphPointer, str]:
|
| 24 |
+
return GenericSet(lambda p: p.id)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class Graph:
|
| 28 |
+
def __init__(self, grammar: str, stacked_rules: list[list[list[UnresolvedRule]]], root_id: int):
|
| 29 |
+
self.grammar = grammar
|
| 30 |
+
self._roots: dict[int, RootNode] = {}
|
| 31 |
+
self._previous_code_points: list[int] = []
|
| 32 |
+
rule_refs: list[RuleRef] = []
|
| 33 |
+
unique_rules: GenericSet[UnresolvedRule, str] = GenericSet(get_serialized_rule_key)
|
| 34 |
+
for stack_id, stack in enumerate(stacked_rules):
|
| 35 |
+
nodes: RootNode = {}
|
| 36 |
+
for path_id, path in enumerate(stack):
|
| 37 |
+
node: GraphNode | None = None
|
| 38 |
+
for step_id in range(len(path) - 1, -1, -1):
|
| 39 |
+
next_node = node
|
| 40 |
+
rule = path[step_id]
|
| 41 |
+
unique_rules.add(rule)
|
| 42 |
+
if is_rule_ref(rule):
|
| 43 |
+
rule_refs.append(rule)
|
| 44 |
+
# rules coming in may be identical but have different references.
|
| 45 |
+
# here, we ensure we always use the same reference for an identical rule.
|
| 46 |
+
# this makes future comparisons easier.
|
| 47 |
+
unique_rule = unique_rules.get(rule)
|
| 48 |
+
if unique_rule is None:
|
| 49 |
+
raise ValueError("Could not get unique rule")
|
| 50 |
+
node = GraphNode(unique_rule, GraphNodeMeta(stack_id, path_id, step_id), next_node)
|
| 51 |
+
if node is None:
|
| 52 |
+
raise ValueError("Could not get node")
|
| 53 |
+
nodes[path_id] = node
|
| 54 |
+
|
| 55 |
+
self._roots[stack_id] = nodes
|
| 56 |
+
if root_id not in self._roots:
|
| 57 |
+
raise ValueError(f"Root node not found for value: {root_id}")
|
| 58 |
+
self._root_node: RootNode = self._roots[root_id]
|
| 59 |
+
|
| 60 |
+
for rule_ref in rule_refs:
|
| 61 |
+
rule_ref.nodes = list(self._get_root_node(rule_ref.value).values())
|
| 62 |
+
|
| 63 |
+
def _get_root_node(self, value: int) -> RootNode:
|
| 64 |
+
root_node = self._roots.get(value)
|
| 65 |
+
if not root_node:
|
| 66 |
+
raise ValueError(f"Root node not found for value: {value}")
|
| 67 |
+
return root_node
|
| 68 |
+
|
| 69 |
+
def _get_initial_pointers(self) -> Pointers:
|
| 70 |
+
pointers = make_pointers()
|
| 71 |
+
|
| 72 |
+
for node, parent in self._fetch_nodes_for_root_node(self._root_node):
|
| 73 |
+
pointer = GraphPointer(node, parent)
|
| 74 |
+
for resolved_pointer in self._resolve_pointer(pointer):
|
| 75 |
+
pointers.add(resolved_pointer)
|
| 76 |
+
return pointers
|
| 77 |
+
|
| 78 |
+
@staticmethod
|
| 79 |
+
def _set_valid(pointers: list[GraphPointer], valid: bool) -> None:
|
| 80 |
+
for pointer in pointers:
|
| 81 |
+
pointer.valid = valid
|
| 82 |
+
|
| 83 |
+
def _parse(self, current_pointers: Pointers, code_point: int) -> Pointers:
|
| 84 |
+
for rule, graph_pointers in self._iterate_over_pointers(current_pointers):
|
| 85 |
+
if is_rule_char(rule):
|
| 86 |
+
valid = any(
|
| 87 |
+
is_point_in_range(code_point, possible) if is_range(possible) else code_point == possible
|
| 88 |
+
for possible in rule.value
|
| 89 |
+
)
|
| 90 |
+
self._set_valid(graph_pointers, valid)
|
| 91 |
+
elif is_rule_char_excluded(rule):
|
| 92 |
+
valid = all(
|
| 93 |
+
not is_point_in_range(code_point, possible) if is_range(possible) else code_point != possible
|
| 94 |
+
for possible in rule.value
|
| 95 |
+
)
|
| 96 |
+
self._set_valid(graph_pointers, valid)
|
| 97 |
+
elif not is_rule_end(rule):
|
| 98 |
+
raise ValueError(f"Unsupported rule: {rule!r}")
|
| 99 |
+
|
| 100 |
+
# a pointer's id is the sum of its node's id and its parent's id chain.
|
| 101 |
+
# if two pointers share the same id, it means they point to the same node and have identical parent chains.
|
| 102 |
+
# for the purposes of walking the graph, we only need to keep one of them.
|
| 103 |
+
next_pointers = make_pointers()
|
| 104 |
+
for current_pointer in current_pointers:
|
| 105 |
+
for unresolved_next_pointer in current_pointer.fetch_next():
|
| 106 |
+
for resolved_next_pointer in self._resolve_pointer(unresolved_next_pointer):
|
| 107 |
+
next_pointers.add(resolved_next_pointer)
|
| 108 |
+
return next_pointers
|
| 109 |
+
|
| 110 |
+
@staticmethod
|
| 111 |
+
def _resolve_pointer(unresolved_pointer: GraphPointer) -> Iterator[GraphPointer]:
|
| 112 |
+
for resolved_pointer in unresolved_pointer.resolve():
|
| 113 |
+
if is_rule_ref(resolved_pointer.node.rule):
|
| 114 |
+
raise ValueError("Encountered a reference rule when building pointers to the graph")
|
| 115 |
+
if is_rule_end(resolved_pointer.node.rule) and resolved_pointer.parent:
|
| 116 |
+
raise ValueError("Encountered an ending rule with a parent when building pointers to the graph")
|
| 117 |
+
yield resolved_pointer
|
| 118 |
+
|
| 119 |
+
def add(self, src: ValidInput, pointers: Pointers | None = None) -> Pointers:
|
| 120 |
+
if pointers is None:
|
| 121 |
+
pointers = self._get_initial_pointers()
|
| 122 |
+
code_points = get_input_as_code_points(src)
|
| 123 |
+
for code_point_pos, code_point in enumerate(code_points):
|
| 124 |
+
pointers = self._parse(pointers, code_point)
|
| 125 |
+
if pointers.size == 0:
|
| 126 |
+
raise InputParseError(code_points, code_point_pos, list(self._previous_code_points))
|
| 127 |
+
self._previous_code_points.extend(code_points)
|
| 128 |
+
return pointers
|
| 129 |
+
|
| 130 |
+
# generator that yields either the node, or if a reference rule, the referenced node.
|
| 131 |
+
# we need this function, as distinct from leveraging the logic in GraphPointer,
|
| 132 |
+
# because that needs a rule ref with already defined nodes; this function is used to _set_ those nodes
|
| 133 |
+
def _fetch_nodes_for_root_node(
|
| 134 |
+
self,
|
| 135 |
+
root_nodes: RootNode,
|
| 136 |
+
parent: GraphPointer | None = None,
|
| 137 |
+
) -> Iterator[tuple[GraphNode, GraphPointer | None]]:
|
| 138 |
+
for node in root_nodes.values():
|
| 139 |
+
if is_rule_ref(node.rule):
|
| 140 |
+
yield from self._fetch_nodes_for_root_node(
|
| 141 |
+
self._get_root_node(node.rule.value), GraphPointer(node, parent)
|
| 142 |
+
)
|
| 143 |
+
else:
|
| 144 |
+
yield node, parent
|
| 145 |
+
|
| 146 |
+
def __repr__(self) -> str:
|
| 147 |
+
return self.print(colors=True)
|
| 148 |
+
|
| 149 |
+
def print(self, pointers: Pointers | None = None, colors: bool = False) -> str:
|
| 150 |
+
col = colorize if colors else (lambda s, _color: f"{s}")
|
| 151 |
+
graph_view = [
|
| 152 |
+
node.print(pointers=pointers, show_position=True, colorize=col)
|
| 153 |
+
for nodes in self._roots.values()
|
| 154 |
+
for node in nodes.values()
|
| 155 |
+
]
|
| 156 |
+
return "\n" + "\n".join(graph_view)
|
| 157 |
+
|
| 158 |
+
@staticmethod
|
| 159 |
+
def _iterate_over_pointers(pointers: Pointers) -> Iterator[tuple[UnresolvedRule, list[GraphPointer]]]:
|
| 160 |
+
# rules are unique by reference (see the constructor), so group pointers by rule identity
|
| 161 |
+
seen_rules: dict[int, tuple[UnresolvedRule, list[GraphPointer]]] = {}
|
| 162 |
+
for pointer in pointers:
|
| 163 |
+
rule = pointer.rule
|
| 164 |
+
if is_rule_ref(rule):
|
| 165 |
+
raise ValueError("Encountered a reference rule in the graph, this should not happen")
|
| 166 |
+
entry = seen_rules.get(id(rule))
|
| 167 |
+
if entry is None:
|
| 168 |
+
seen_rules[id(rule)] = (rule, [pointer])
|
| 169 |
+
else:
|
| 170 |
+
entry[1].append(pointer)
|
| 171 |
+
|
| 172 |
+
yield from seen_rules.values()
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/graph_node.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from dataclasses import dataclass
|
| 4 |
+
from typing import TYPE_CHECKING, Generic, TypeVar
|
| 5 |
+
|
| 6 |
+
from .colorize import colorize as default_colorize
|
| 7 |
+
from .print import print_graph_node
|
| 8 |
+
|
| 9 |
+
if TYPE_CHECKING:
|
| 10 |
+
from .types import Colorize, Pointers
|
| 11 |
+
|
| 12 |
+
R = TypeVar("R")
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@dataclass
|
| 16 |
+
class GraphNodeMeta:
|
| 17 |
+
stack_id: int
|
| 18 |
+
path_id: int
|
| 19 |
+
step_id: int
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class GraphNode(Generic[R]):
|
| 23 |
+
def __init__(self, rule: R, meta: GraphNodeMeta, next: GraphNode | None = None): # noqa: A002
|
| 24 |
+
self.rule = rule
|
| 25 |
+
if meta is None:
|
| 26 |
+
raise ValueError("Meta is undefined")
|
| 27 |
+
self.meta = meta
|
| 28 |
+
self.next = next
|
| 29 |
+
self._id: str | None = None
|
| 30 |
+
|
| 31 |
+
@property
|
| 32 |
+
def id(self) -> str:
|
| 33 |
+
if self._id is None:
|
| 34 |
+
self._id = f"{self.meta.stack_id},{self.meta.path_id},{self.meta.step_id}"
|
| 35 |
+
return self._id
|
| 36 |
+
|
| 37 |
+
def print(
|
| 38 |
+
self,
|
| 39 |
+
*,
|
| 40 |
+
colorize: Colorize,
|
| 41 |
+
pointers: Pointers | None = None,
|
| 42 |
+
show_position: bool = False,
|
| 43 |
+
) -> str:
|
| 44 |
+
return print_graph_node(self, colorize=colorize, pointers=pointers, show_position=show_position)
|
| 45 |
+
|
| 46 |
+
def __repr__(self) -> str:
|
| 47 |
+
return self.print(colorize=default_colorize, show_position=False)
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/graph_node_test.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from unittest.mock import patch
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
from .colorize import colorize
|
| 6 |
+
from .graph_node import GraphNode, GraphNodeMeta
|
| 7 |
+
from .rule_ref import RuleRef
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@pytest.fixture
|
| 11 |
+
def meta():
|
| 12 |
+
return GraphNodeMeta(stack_id=1, path_id=2, step_id=3)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@pytest.fixture
|
| 16 |
+
def rule():
|
| 17 |
+
return RuleRef(42)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def test_should_construct_with_a_rule_and_meta(rule, meta):
|
| 21 |
+
node = GraphNode(rule, meta)
|
| 22 |
+
assert node.rule is rule
|
| 23 |
+
assert node.meta == GraphNodeMeta(1, 2, 3)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def test_should_throw_if_meta_is_undefined(rule):
|
| 27 |
+
with pytest.raises(ValueError, match="Meta is undefined"):
|
| 28 |
+
GraphNode(rule, None)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def test_should_correctly_calculate_and_cache_its_id(rule, meta):
|
| 32 |
+
node = GraphNode(rule, meta)
|
| 33 |
+
assert node.id == "1,2,3"
|
| 34 |
+
node.meta = GraphNodeMeta(4, 5, 6)
|
| 35 |
+
# Check if it uses cached value on second access
|
| 36 |
+
assert node.id == "1,2,3"
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def test_should_delegate_print_to_the_print_graph_node_function(rule, meta):
|
| 40 |
+
node = GraphNode(rule, meta)
|
| 41 |
+
with patch("gbnf.grammar_graph.graph_node.print_graph_node", return_value="printed") as print_graph_node:
|
| 42 |
+
assert node.print(colorize=colorize, show_position=False) == "printed"
|
| 43 |
+
print_graph_node.assert_called_once_with(node, colorize=colorize, pointers=None, show_position=False)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def test_should_handle_next_node_linkage(rule, meta):
|
| 47 |
+
next_node = GraphNode(RuleRef(43), meta)
|
| 48 |
+
node = GraphNode(rule, meta, next_node)
|
| 49 |
+
assert node.next is next_node
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/graph_pointer.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from collections.abc import Iterator
|
| 4 |
+
from typing import TYPE_CHECKING
|
| 5 |
+
|
| 6 |
+
from .colorize import colorize as default_colorize
|
| 7 |
+
from .print import print_graph_pointer
|
| 8 |
+
from .type_guards import (
|
| 9 |
+
is_graph_pointer_rule_char,
|
| 10 |
+
is_graph_pointer_rule_char_exclude,
|
| 11 |
+
is_graph_pointer_rule_end,
|
| 12 |
+
is_graph_pointer_rule_ref,
|
| 13 |
+
is_rule_end,
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
if TYPE_CHECKING:
|
| 17 |
+
from .graph_node import GraphNode
|
| 18 |
+
from .types import Colorize, UnresolvedRule
|
| 19 |
+
|
| 20 |
+
GraphPointerKey = str
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class GraphPointer:
|
| 24 |
+
def __init__(self, node: GraphNode, parent: GraphPointer | None = None):
|
| 25 |
+
if node is None:
|
| 26 |
+
raise ValueError("Node is undefined")
|
| 27 |
+
self.node = node
|
| 28 |
+
self.parent = parent
|
| 29 |
+
self._valid: bool | None = None
|
| 30 |
+
self.id: str = f"{parent.id}-{node.id}" if parent else node.id
|
| 31 |
+
|
| 32 |
+
def resolve(self, resolved: bool = False) -> Iterator[GraphPointer]:
|
| 33 |
+
"""
|
| 34 |
+
1. If the current node is an end node, and the pointer has a parent, resolve the parent's next node;
|
| 35 |
+
else yield this pointer.
|
| 36 |
+
2. If the current node is a rule ref, yield the referenced nodes, _unless_ resolved is true,
|
| 37 |
+
in which case it resolves next.
|
| 38 |
+
3. If the current node is a char or range, yield this pointer.
|
| 39 |
+
|
| 40 |
+
Implemented iteratively (a depth-first walk with an explicit stack) so that deeply nested
|
| 41 |
+
parent chains do not hit Python's recursion limit.
|
| 42 |
+
"""
|
| 43 |
+
stack: list[tuple[GraphPointer, bool]] = [(self, resolved)]
|
| 44 |
+
while stack:
|
| 45 |
+
pointer, is_resolved = stack.pop()
|
| 46 |
+
if is_graph_pointer_rule_ref(pointer):
|
| 47 |
+
if is_resolved:
|
| 48 |
+
if pointer.node.next is None:
|
| 49 |
+
raise ValueError(f"No next node: {pointer.node!r}")
|
| 50 |
+
stack.append((GraphPointer(pointer.node.next, pointer.parent), False))
|
| 51 |
+
else:
|
| 52 |
+
children = [(GraphPointer(node, pointer), False) for node in pointer.node.rule.nodes]
|
| 53 |
+
stack.extend(reversed(children))
|
| 54 |
+
elif is_graph_pointer_rule_end(pointer):
|
| 55 |
+
if not pointer.parent:
|
| 56 |
+
yield pointer
|
| 57 |
+
else:
|
| 58 |
+
stack.append((pointer.parent, True))
|
| 59 |
+
elif is_graph_pointer_rule_char(pointer) or is_graph_pointer_rule_char_exclude(pointer):
|
| 60 |
+
yield pointer
|
| 61 |
+
else:
|
| 62 |
+
raise ValueError(f"Unknown rule: {pointer.node.rule!r}")
|
| 63 |
+
|
| 64 |
+
def fetch_next(self) -> Iterator[GraphPointer]:
|
| 65 |
+
pointer: GraphPointer | None = self
|
| 66 |
+
while pointer is not None:
|
| 67 |
+
# if this pointer is invalid, then we don't return any new pointers
|
| 68 |
+
if pointer._valid is False:
|
| 69 |
+
return
|
| 70 |
+
|
| 71 |
+
# if this pointer is an end node, we return the parent's next node. If no parent exists,
|
| 72 |
+
# we return nothing, since it's the end of the line.
|
| 73 |
+
if is_rule_end(pointer.node.rule):
|
| 74 |
+
pointer = pointer.parent
|
| 75 |
+
continue
|
| 76 |
+
if pointer.node.next is None:
|
| 77 |
+
raise ValueError(f"No next node: {pointer.node!r}")
|
| 78 |
+
yield from GraphPointer(pointer.node.next, pointer.parent).resolve()
|
| 79 |
+
return
|
| 80 |
+
|
| 81 |
+
@property
|
| 82 |
+
def rule(self) -> UnresolvedRule:
|
| 83 |
+
return self.node.rule
|
| 84 |
+
|
| 85 |
+
@property
|
| 86 |
+
def valid(self) -> bool | None:
|
| 87 |
+
return self._valid
|
| 88 |
+
|
| 89 |
+
@valid.setter
|
| 90 |
+
def valid(self, valid: bool) -> None:
|
| 91 |
+
self._valid = valid
|
| 92 |
+
|
| 93 |
+
def print(self, *, colorize: Colorize) -> str:
|
| 94 |
+
return print_graph_pointer(self, colorize=colorize)
|
| 95 |
+
|
| 96 |
+
def __repr__(self) -> str:
|
| 97 |
+
return self.print(colorize=default_colorize)
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/graph_pointer_test.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from unittest.mock import patch
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
from .graph_node import GraphNode, GraphNodeMeta
|
| 6 |
+
from .graph_pointer import GraphPointer
|
| 7 |
+
from .types import RuleChar, RuleCharExclude, RuleEnd, RuleType
|
| 8 |
+
|
| 9 |
+
MODULE = "gbnf.grammar_graph.graph_pointer"
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@pytest.fixture
|
| 13 |
+
def guards():
|
| 14 |
+
with (
|
| 15 |
+
patch(f"{MODULE}.is_graph_pointer_rule_ref") as is_ref,
|
| 16 |
+
patch(f"{MODULE}.is_graph_pointer_rule_end") as is_end,
|
| 17 |
+
patch(f"{MODULE}.is_graph_pointer_rule_char") as is_char,
|
| 18 |
+
patch(f"{MODULE}.is_graph_pointer_rule_char_exclude") as is_char_exclude,
|
| 19 |
+
):
|
| 20 |
+
yield is_ref, is_end, is_char, is_char_exclude
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def set_guards(guards, ref=False, end=False, char=False, char_exclude=False):
|
| 24 |
+
for mock, value in zip(guards, (ref, end, char, char_exclude), strict=True):
|
| 25 |
+
if callable(value):
|
| 26 |
+
mock.side_effect = value
|
| 27 |
+
else:
|
| 28 |
+
mock.return_value = value
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# constructor
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def test_initializes_correctly_with_just_node_parameter():
|
| 35 |
+
node = GraphNode(RuleEnd(), GraphNodeMeta(1, 2, 3))
|
| 36 |
+
pointer = GraphPointer(node)
|
| 37 |
+
assert pointer.node is node
|
| 38 |
+
assert pointer.id == "1,2,3"
|
| 39 |
+
assert pointer.parent is None
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def test_throws_an_error_if_node_is_undefined():
|
| 43 |
+
with pytest.raises(ValueError, match="Node is undefined"):
|
| 44 |
+
GraphPointer(None)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def test_initializes_correctly_with_node_and_parent():
|
| 48 |
+
parent_node = GraphNode(RuleChar([97]), GraphNodeMeta(1, 2, 3))
|
| 49 |
+
child_node = GraphNode(RuleChar([98]), GraphNodeMeta(4, 5, 6))
|
| 50 |
+
parent_pointer = GraphPointer(parent_node)
|
| 51 |
+
child_pointer = GraphPointer(child_node, parent_pointer)
|
| 52 |
+
assert child_pointer.parent is parent_pointer
|
| 53 |
+
assert child_pointer.id == "1,2,3-4,5,6"
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# resolve
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def test_resolve_throws_an_error_on_unknown_rule_types(guards):
|
| 60 |
+
unhandled_rule = {"type": "UNKNOWN_RULE_TYPE"}
|
| 61 |
+
pointer = GraphPointer(GraphNode(unhandled_rule, GraphNodeMeta(1, 2, 3)))
|
| 62 |
+
set_guards(guards)
|
| 63 |
+
with pytest.raises(ValueError, match="Unknown rule"):
|
| 64 |
+
list(pointer.resolve())
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def test_resolve_yields_a_char_rule(guards):
|
| 68 |
+
pointer = GraphPointer(GraphNode(RuleChar([97]), GraphNodeMeta(1, 2, 3)))
|
| 69 |
+
set_guards(guards, char=True)
|
| 70 |
+
assert list(pointer.resolve()) == [pointer]
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def test_resolve_yields_a_char_excluded_rule(guards):
|
| 74 |
+
pointer = GraphPointer(GraphNode(RuleCharExclude([97]), GraphNodeMeta(1, 2, 3)))
|
| 75 |
+
set_guards(guards, char_exclude=True)
|
| 76 |
+
assert list(pointer.resolve()) == [pointer]
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def test_resolve_yields_an_end_rule_without_a_parent(guards):
|
| 80 |
+
pointer = GraphPointer(GraphNode(RuleEnd(), GraphNodeMeta(1, 2, 3)))
|
| 81 |
+
set_guards(guards, end=True)
|
| 82 |
+
assert list(pointer.resolve()) == [pointer]
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def test_resolve_yields_an_end_rule_with_a_parent(guards):
|
| 86 |
+
rule = RuleEnd()
|
| 87 |
+
parent_pointer = GraphPointer(GraphNode(rule, GraphNodeMeta(2, 1, 1)))
|
| 88 |
+
child_pointer = GraphPointer(GraphNode(rule, GraphNodeMeta(1, 1, 1)), parent_pointer)
|
| 89 |
+
set_guards(guards, end=True)
|
| 90 |
+
assert list(child_pointer.resolve()) == [parent_pointer]
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def test_resolve_yields_an_end_rule_with_a_grandparent(guards):
|
| 94 |
+
rule = RuleEnd()
|
| 95 |
+
grand_parent_pointer = GraphPointer(GraphNode(rule, GraphNodeMeta(2, 1, 1)))
|
| 96 |
+
parent_pointer = GraphPointer(GraphNode(rule, GraphNodeMeta(2, 1, 1)), grand_parent_pointer)
|
| 97 |
+
child_pointer = GraphPointer(GraphNode(rule, GraphNodeMeta(1, 1, 1)), parent_pointer)
|
| 98 |
+
set_guards(guards, end=True)
|
| 99 |
+
assert list(child_pointer.resolve()) == [grand_parent_pointer]
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def test_resolve_yields_an_end_rules_parent_that_is_not_an_end_with_a_grandparent(guards):
|
| 103 |
+
rule = RuleEnd()
|
| 104 |
+
grand_parent_pointer = GraphPointer(GraphNode(rule, GraphNodeMeta(2, 1, 1)))
|
| 105 |
+
parent_pointer = GraphPointer(GraphNode(RuleChar([97]), GraphNodeMeta(2, 1, 1)), grand_parent_pointer)
|
| 106 |
+
child_pointer = GraphPointer(GraphNode(rule, GraphNodeMeta(1, 1, 1)), parent_pointer)
|
| 107 |
+
set_guards(
|
| 108 |
+
guards,
|
| 109 |
+
end=lambda pointer: getattr(pointer.rule, "type", None) == RuleType.END,
|
| 110 |
+
char=lambda pointer: getattr(pointer.rule, "type", None) == RuleType.CHAR,
|
| 111 |
+
)
|
| 112 |
+
assert list(child_pointer.resolve()) == [parent_pointer]
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/graph_test.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from .generic_set import GenericSet
|
| 4 |
+
from .graph import Graph
|
| 5 |
+
from .types import RuleChar
|
| 6 |
+
|
| 7 |
+
GRAMMAR = "example-grammar"
|
| 8 |
+
ROOT_ID = 0
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@pytest.fixture
|
| 12 |
+
def stacked_rules():
|
| 13 |
+
return [
|
| 14 |
+
[
|
| 15 |
+
[RuleChar([65, 66, 67])],
|
| 16 |
+
[RuleChar([68, 69, 70])],
|
| 17 |
+
],
|
| 18 |
+
[
|
| 19 |
+
[RuleChar([71, 72, 73])],
|
| 20 |
+
[RuleChar([74, 75, 76])],
|
| 21 |
+
],
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def test_should_create_a_graph_instance(stacked_rules):
|
| 26 |
+
graph = Graph(GRAMMAR, stacked_rules, ROOT_ID)
|
| 27 |
+
assert isinstance(graph, Graph)
|
| 28 |
+
assert graph.grammar == GRAMMAR
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def test_should_get_the_root_node(stacked_rules):
|
| 32 |
+
graph = Graph(GRAMMAR, stacked_rules, ROOT_ID)
|
| 33 |
+
# root nodes are a dict in Python (a Map in TS); the accessor is private
|
| 34 |
+
root_node = graph._get_root_node(ROOT_ID)
|
| 35 |
+
assert isinstance(root_node, dict)
|
| 36 |
+
assert len(root_node) == 2
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def test_should_get_the_initial_pointers(stacked_rules):
|
| 40 |
+
graph = Graph(GRAMMAR, stacked_rules, ROOT_ID)
|
| 41 |
+
pointers = graph._get_initial_pointers()
|
| 42 |
+
assert isinstance(pointers, GenericSet)
|
| 43 |
+
assert pointers.size == 2
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def test_print_should_print_the_graph(stacked_rules):
|
| 47 |
+
graph = Graph(GRAMMAR, stacked_rules, ROOT_ID)
|
| 48 |
+
printed_graph = graph.print(colors=True)
|
| 49 |
+
assert isinstance(printed_graph, str)
|
| 50 |
+
assert "ABC" in printed_graph
|
| 51 |
+
assert "DEF" in printed_graph
|
| 52 |
+
assert "GHI" in printed_graph
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/parse_state.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from collections.abc import Iterator
|
| 4 |
+
from typing import TYPE_CHECKING
|
| 5 |
+
|
| 6 |
+
from .get_serialized_rule_key import get_serialized_rule_key
|
| 7 |
+
|
| 8 |
+
if TYPE_CHECKING:
|
| 9 |
+
from .graph import Graph
|
| 10 |
+
from .types import Pointers, ResolvedRule, ValidInput
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class ParseState:
|
| 14 |
+
def __init__(self, graph: Graph, pointers: Pointers):
|
| 15 |
+
self._graph = graph
|
| 16 |
+
self._pointers = pointers
|
| 17 |
+
|
| 18 |
+
def __call__(self, src: ValidInput) -> ParseState:
|
| 19 |
+
return self.add(src)
|
| 20 |
+
|
| 21 |
+
def __add__(self, src: ValidInput) -> ParseState:
|
| 22 |
+
return self.add(src)
|
| 23 |
+
|
| 24 |
+
def __iter__(self) -> Iterator[ResolvedRule]:
|
| 25 |
+
return self.rules()
|
| 26 |
+
|
| 27 |
+
def rules(self) -> Iterator[ResolvedRule]:
|
| 28 |
+
seen: set[str] = set()
|
| 29 |
+
for pointer in self._pointers:
|
| 30 |
+
rule = pointer.rule
|
| 31 |
+
key = get_serialized_rule_key(rule)
|
| 32 |
+
if key not in seen:
|
| 33 |
+
seen.add(key)
|
| 34 |
+
yield rule
|
| 35 |
+
|
| 36 |
+
def add(self, src: ValidInput) -> ParseState:
|
| 37 |
+
pointers = self._graph.add(src, self._pointers)
|
| 38 |
+
return ParseState(self._graph, pointers)
|
| 39 |
+
|
| 40 |
+
@property
|
| 41 |
+
def size(self) -> int:
|
| 42 |
+
return sum(1 for _ in self.rules())
|
| 43 |
+
|
| 44 |
+
def __len__(self) -> int:
|
| 45 |
+
return self.size
|
| 46 |
+
|
| 47 |
+
def __bool__(self) -> bool:
|
| 48 |
+
return True
|
| 49 |
+
|
| 50 |
+
@property
|
| 51 |
+
def grammar(self) -> str:
|
| 52 |
+
return self._graph.grammar
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/parse_state_test.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from types import SimpleNamespace
|
| 2 |
+
from unittest.mock import MagicMock
|
| 3 |
+
|
| 4 |
+
import pytest
|
| 5 |
+
|
| 6 |
+
from .parse_state import ParseState
|
| 7 |
+
from .types import RuleChar, RuleType
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@pytest.fixture
|
| 11 |
+
def mock_graph():
|
| 12 |
+
graph = MagicMock()
|
| 13 |
+
graph.grammar = "sample-grammar"
|
| 14 |
+
return graph
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@pytest.fixture
|
| 18 |
+
def mock_pointers():
|
| 19 |
+
return [SimpleNamespace(rule=RuleChar([65]))] # ASCII 'A'
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def test_constructs_with_given_graph_and_pointers(mock_graph, mock_pointers):
|
| 23 |
+
assert isinstance(ParseState(mock_graph, mock_pointers), ParseState)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def test_returns_unique_rules_from_pointers(mock_graph, mock_pointers):
|
| 27 |
+
rules = list(ParseState(mock_graph, mock_pointers).rules())
|
| 28 |
+
assert len(rules) == 1
|
| 29 |
+
assert rules[0].value == [65]
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def test_iterates_over_unique_rules(mock_graph, mock_pointers):
|
| 33 |
+
rules = list(ParseState(mock_graph, mock_pointers))
|
| 34 |
+
assert len(rules) == 1
|
| 35 |
+
assert rules[0].type == RuleType.CHAR
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def test_adds_new_input_and_returns_new_parse_state_with_updated_pointers(mock_graph, mock_pointers):
|
| 39 |
+
new_pointers = [SimpleNamespace(rule=RuleChar([66]))] # ASCII 'B'
|
| 40 |
+
mock_graph.add.return_value = new_pointers
|
| 41 |
+
|
| 42 |
+
parse_state = ParseState(mock_graph, mock_pointers)
|
| 43 |
+
new_state = parse_state.add("B")
|
| 44 |
+
assert isinstance(new_state, ParseState)
|
| 45 |
+
mock_graph.add.assert_called_with("B", mock_pointers)
|
| 46 |
+
assert [rule.value for rule in new_state] == [[66]]
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def test_calculates_the_size_of_unique_rules_correctly(mock_graph, mock_pointers):
|
| 50 |
+
assert ParseState(mock_graph, mock_pointers).size == 1
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def test_provides_access_to_the_underlying_graph_grammar(mock_graph, mock_pointers):
|
| 54 |
+
assert ParseState(mock_graph, mock_pointers).grammar == "sample-grammar"
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def test_is_callable_as_a_function(mock_graph, mock_pointers):
|
| 58 |
+
# TS uses a Proxy/Function trick; Python uses __call__
|
| 59 |
+
new_state = ParseState(mock_graph, mock_pointers)("B")
|
| 60 |
+
assert isinstance(new_state, ParseState)
|
| 61 |
+
mock_graph.add.assert_called_with("B", mock_pointers)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def test_supports_the_plus_operator(mock_graph, mock_pointers):
|
| 65 |
+
new_state = ParseState(mock_graph, mock_pointers) + "B"
|
| 66 |
+
assert isinstance(new_state, ParseState)
|
| 67 |
+
mock_graph.add.assert_called_with("B", mock_pointers)
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/print.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from typing import TYPE_CHECKING
|
| 4 |
+
|
| 5 |
+
from .colorize import Color
|
| 6 |
+
from .get_parent_stack_id import get_parent_stack_id
|
| 7 |
+
from .type_guards import is_range, is_rule_char, is_rule_ref
|
| 8 |
+
|
| 9 |
+
if TYPE_CHECKING:
|
| 10 |
+
from .graph_node import GraphNode
|
| 11 |
+
from .graph_pointer import GraphPointer
|
| 12 |
+
from .types import Colorize, Pointers
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def print_graph_pointer(pointer: GraphPointer, *, colorize: Colorize) -> str:
|
| 16 |
+
return colorize(f"*{get_parent_stack_id(pointer, colorize)}", Color.RED)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _get_char(char_code: int) -> str:
|
| 20 |
+
char = chr(char_code)
|
| 21 |
+
if char == "\n":
|
| 22 |
+
return "\\n"
|
| 23 |
+
return char
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def print_graph_node(
|
| 27 |
+
node: GraphNode,
|
| 28 |
+
*,
|
| 29 |
+
colorize: Colorize,
|
| 30 |
+
pointers: Pointers | None = None,
|
| 31 |
+
show_position: bool = False,
|
| 32 |
+
) -> str:
|
| 33 |
+
col = colorize
|
| 34 |
+
rule = node.rule
|
| 35 |
+
|
| 36 |
+
parts: list[str] = []
|
| 37 |
+
if show_position:
|
| 38 |
+
parts.extend([
|
| 39 |
+
col("{", Color.BLUE),
|
| 40 |
+
col(node.id, Color.GRAY),
|
| 41 |
+
col("}", Color.BLUE),
|
| 42 |
+
])
|
| 43 |
+
if is_rule_char(rule):
|
| 44 |
+
chars = [
|
| 45 |
+
",".join(col(chr(val), Color.YELLOW) for val in v) if is_range(v) else _get_char(v) for v in rule.value
|
| 46 |
+
]
|
| 47 |
+
parts.extend([
|
| 48 |
+
col("[", Color.GRAY),
|
| 49 |
+
col("".join(chars), Color.YELLOW),
|
| 50 |
+
col("]", Color.GRAY),
|
| 51 |
+
])
|
| 52 |
+
elif is_rule_ref(rule):
|
| 53 |
+
parts.append(col("Ref(", Color.GRAY) + col(f"{rule.value}", Color.GREEN) + col(")", Color.GRAY))
|
| 54 |
+
else:
|
| 55 |
+
parts.append(col(rule.type.value, Color.YELLOW))
|
| 56 |
+
|
| 57 |
+
if pointers:
|
| 58 |
+
for pointer in pointers:
|
| 59 |
+
if pointer.node is node:
|
| 60 |
+
parts.append(col("[", Color.GRAY))
|
| 61 |
+
parts.append(pointer.print(colorize=col))
|
| 62 |
+
parts.append(col("]", Color.GRAY))
|
| 63 |
+
|
| 64 |
+
segments = ["".join(parts)]
|
| 65 |
+
if node.next is not None:
|
| 66 |
+
segments.append(node.next.print(pointers=pointers, colorize=col, show_position=show_position))
|
| 67 |
+
return col("-> ", Color.GRAY).join(s for s in segments if s)
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/print_test.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from types import SimpleNamespace
|
| 2 |
+
from unittest.mock import patch
|
| 3 |
+
|
| 4 |
+
from .print import print_graph_node, print_graph_pointer
|
| 5 |
+
from .rule_ref import RuleRef
|
| 6 |
+
from .types import RuleChar
|
| 7 |
+
|
| 8 |
+
COLORS = {
|
| 9 |
+
"\x1b[34m": "BLUE",
|
| 10 |
+
"\x1b[36m": "CYAN",
|
| 11 |
+
"\x1b[32m": "GREEN",
|
| 12 |
+
"\x1b[31m": "RED",
|
| 13 |
+
"\x1b[90m": "GRAY",
|
| 14 |
+
"\x1b[33m": "YELLOW",
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def mock_colorize(text, color):
|
| 19 |
+
return f"[{COLORS[color]}]:{text}"
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def create_mock_node(id_, rule, next_=None):
|
| 23 |
+
return SimpleNamespace(id=id_, rule=rule, next=next_, print=lambda **_kwargs: f"Node({id_})")
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def create_mock_graph_pointer(node):
|
| 27 |
+
return SimpleNamespace(node=node, parent=None, print=lambda **_kwargs: f"Pointer to {node.id}")
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def test_print_graph_pointer_prints_graph_pointer_details_correctly():
|
| 31 |
+
mock_pointer = create_mock_graph_pointer(create_mock_node(1, RuleRef(100)))
|
| 32 |
+
with patch("gbnf.grammar_graph.print.get_parent_stack_id", return_value="foo") as get_parent_stack_id:
|
| 33 |
+
result = print_graph_pointer(mock_pointer, colorize=mock_colorize)
|
| 34 |
+
assert result == "[RED]:*foo"
|
| 35 |
+
get_parent_stack_id.assert_called_once_with(mock_pointer, mock_colorize)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def test_print_graph_node_prints_a_graph_node_with_a_character_rule():
|
| 39 |
+
node = create_mock_node(1, RuleChar([65])) # ASCII for 'A'
|
| 40 |
+
result = print_graph_node(node, colorize=mock_colorize, show_position=False)
|
| 41 |
+
assert result == "[GRAY]:[[YELLOW]:A[GRAY]:]"
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def test_print_graph_node_prints_a_graph_node_with_a_rule_reference():
|
| 45 |
+
node = create_mock_node(1, RuleRef(200))
|
| 46 |
+
result = print_graph_node(node, colorize=mock_colorize, show_position=True)
|
| 47 |
+
assert result == "[BLUE]:{[GRAY]:1[BLUE]:}[GRAY]:Ref([GREEN]:200[GRAY]:)"
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/rule_ref.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from typing import TYPE_CHECKING
|
| 4 |
+
|
| 5 |
+
if TYPE_CHECKING:
|
| 6 |
+
from .graph_node import GraphNode
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class RuleRef:
|
| 10 |
+
def __init__(self, value: int):
|
| 11 |
+
self.value = value
|
| 12 |
+
self._nodes: list[GraphNode] | None = None
|
| 13 |
+
|
| 14 |
+
@property
|
| 15 |
+
def nodes(self) -> list[GraphNode]:
|
| 16 |
+
if self._nodes is None:
|
| 17 |
+
raise ValueError("Nodes are not set")
|
| 18 |
+
return self._nodes
|
| 19 |
+
|
| 20 |
+
@nodes.setter
|
| 21 |
+
def nodes(self, nodes: list[GraphNode]) -> None:
|
| 22 |
+
self._nodes = nodes
|
| 23 |
+
|
| 24 |
+
def __repr__(self) -> str:
|
| 25 |
+
return f"RuleRef({self.value})"
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/rule_ref_test.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from unittest.mock import MagicMock
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
from .rule_ref import RuleRef
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def test_initializes_with_a_given_value():
|
| 9 |
+
assert RuleRef(123).value == 123
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def test_allows_setting_and_getting_nodes():
|
| 13 |
+
# RuleRef.nodes is a list in the Python port (a Set in TS)
|
| 14 |
+
mock_nodes = [MagicMock()]
|
| 15 |
+
rule_ref = RuleRef(123)
|
| 16 |
+
rule_ref.nodes = mock_nodes
|
| 17 |
+
assert rule_ref.nodes is mock_nodes
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def test_throws_an_error_if_trying_to_get_nodes_before_setting():
|
| 21 |
+
rule_ref = RuleRef(123)
|
| 22 |
+
with pytest.raises(ValueError, match="Nodes are not set"):
|
| 23 |
+
_ = rule_ref.nodes
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/type_guards.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from typing import TYPE_CHECKING, Any
|
| 4 |
+
|
| 5 |
+
from .rule_ref import RuleRef
|
| 6 |
+
from .types import RuleType
|
| 7 |
+
|
| 8 |
+
if TYPE_CHECKING:
|
| 9 |
+
from .graph_pointer import GraphPointer
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def is_rule_type(type_: Any = None) -> bool:
|
| 13 |
+
return bool(type_) and type_ in list(RuleType)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def is_rule(rule: Any = None) -> bool:
|
| 17 |
+
if not rule:
|
| 18 |
+
return False
|
| 19 |
+
rule_type = rule.get("type") if isinstance(rule, dict) else getattr(rule, "type", None)
|
| 20 |
+
return is_rule_type(rule_type)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def is_rule_ref(rule: Any = None) -> bool:
|
| 24 |
+
return isinstance(rule, RuleRef)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def _is(rule: Any, rule_type: RuleType) -> bool:
|
| 28 |
+
return rule is not None and not is_rule_ref(rule) and getattr(rule, "type", None) == rule_type
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def is_rule_end(rule: Any = None) -> bool:
|
| 32 |
+
return _is(rule, RuleType.END)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def is_rule_char(rule: Any = None) -> bool:
|
| 36 |
+
return _is(rule, RuleType.CHAR)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def is_rule_char_excluded(rule: Any = None) -> bool:
|
| 40 |
+
return _is(rule, RuleType.CHAR_EXCLUDE)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def is_range(range_: Any = None) -> bool:
|
| 44 |
+
return (
|
| 45 |
+
isinstance(range_, (list, tuple))
|
| 46 |
+
and len(range_) == 2
|
| 47 |
+
and all(isinstance(n, (int, float)) and not isinstance(n, bool) for n in range_)
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def is_graph_pointer_rule_ref(pointer: GraphPointer) -> bool:
|
| 52 |
+
return is_rule_ref(pointer.rule)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def is_graph_pointer_rule_end(pointer: GraphPointer) -> bool:
|
| 56 |
+
return is_rule_end(pointer.rule)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def is_graph_pointer_rule_char(pointer: GraphPointer) -> bool:
|
| 60 |
+
return is_rule_char(pointer.rule)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def is_graph_pointer_rule_char_exclude(pointer: GraphPointer) -> bool:
|
| 64 |
+
return is_rule_char_excluded(pointer.rule)
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/type_guards_test.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .graph_node import GraphNode, GraphNodeMeta
|
| 2 |
+
from .graph_pointer import GraphPointer
|
| 3 |
+
from .rule_ref import RuleRef
|
| 4 |
+
from .type_guards import (
|
| 5 |
+
is_graph_pointer_rule_char,
|
| 6 |
+
is_graph_pointer_rule_char_exclude,
|
| 7 |
+
is_graph_pointer_rule_end,
|
| 8 |
+
is_graph_pointer_rule_ref,
|
| 9 |
+
is_range,
|
| 10 |
+
is_rule,
|
| 11 |
+
is_rule_char,
|
| 12 |
+
is_rule_char_excluded,
|
| 13 |
+
is_rule_end,
|
| 14 |
+
is_rule_ref,
|
| 15 |
+
is_rule_type,
|
| 16 |
+
)
|
| 17 |
+
from .types import RuleChar, RuleCharExclude, RuleEnd, RuleType
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def make_pointer(rule):
|
| 21 |
+
return GraphPointer(GraphNode(rule, GraphNodeMeta(1, 2, 3)))
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def test_is_graph_pointer_rule_ref_returns_true():
|
| 25 |
+
assert is_graph_pointer_rule_ref(make_pointer(RuleRef(1))) is True
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def test_is_graph_pointer_rule_ref_returns_false():
|
| 29 |
+
assert is_graph_pointer_rule_ref(make_pointer(RuleEnd())) is False
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def test_is_graph_pointer_rule_end_returns_false():
|
| 33 |
+
assert is_graph_pointer_rule_end(make_pointer(RuleRef(1))) is False
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def test_is_graph_pointer_rule_end_returns_true():
|
| 37 |
+
assert is_graph_pointer_rule_end(make_pointer(RuleEnd())) is True
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def test_is_graph_pointer_rule_char_returns_true():
|
| 41 |
+
assert is_graph_pointer_rule_char(make_pointer(RuleChar([97]))) is True
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def test_is_graph_pointer_rule_char_returns_false():
|
| 45 |
+
assert is_graph_pointer_rule_char(make_pointer(RuleEnd())) is False
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def test_is_graph_pointer_rule_char_exclude_returns_true():
|
| 49 |
+
assert is_graph_pointer_rule_char_exclude(make_pointer(RuleCharExclude([97]))) is True
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def test_is_graph_pointer_rule_char_exclude_returns_false():
|
| 53 |
+
assert is_graph_pointer_rule_char_exclude(make_pointer(RuleChar([97]))) is False
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def test_is_rule_should_return_true_for_valid_rule_objects():
|
| 57 |
+
assert is_rule(RuleChar([65, [66, 67]])) is True
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def test_is_rule_should_return_false_for_invalid_objects():
|
| 61 |
+
assert is_rule({"type": "invalid", "value": []}) is False
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def test_is_rule_should_return_false_for_none():
|
| 65 |
+
assert is_rule(None) is False
|
| 66 |
+
assert is_rule() is False
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def test_is_rule_type_should_return_true_for_valid_rule_types():
|
| 70 |
+
assert is_rule_type(RuleType.CHAR) is True
|
| 71 |
+
assert is_rule_type(RuleType.END) is True
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def test_is_rule_type_should_return_false_for_invalid_rule_types():
|
| 75 |
+
assert is_rule_type("not_a_rule_type") is False
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def test_is_rule_ref_should_recognize_rule_ref_instances():
|
| 79 |
+
assert is_rule_ref(RuleRef(1)) is True
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def test_is_rule_ref_should_not_misidentify_other_rule_types():
|
| 83 |
+
assert is_rule_ref(RuleChar([65])) is False
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def test_is_rule_end_recognizes_an_end_rule():
|
| 87 |
+
assert is_rule_end(RuleEnd()) is True
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def test_is_rule_end_does_not_confuse_with_other_types():
|
| 91 |
+
assert is_rule_end(RuleChar([65])) is False
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def test_identifies_character_rules():
|
| 95 |
+
rule = RuleChar([65])
|
| 96 |
+
assert is_rule_char(rule) is True
|
| 97 |
+
assert is_rule_char_excluded(rule) is False
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def test_identifies_character_exclusion_rules():
|
| 101 |
+
rule = RuleCharExclude([65])
|
| 102 |
+
assert is_rule_char_excluded(rule) is True
|
| 103 |
+
assert is_rule_char(rule) is False
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def test_is_range_identifies_valid_ranges():
|
| 107 |
+
assert is_range([1, 10]) is True
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def test_is_range_rejects_invalid_ranges():
|
| 111 |
+
assert is_range([1, "10"]) is False # Mixed types
|
| 112 |
+
assert is_range([1]) is False # Not enough elements
|
| 113 |
+
assert is_range(5) is False # Not an array
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_graph/types.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from dataclasses import dataclass, field
|
| 4 |
+
from enum import StrEnum
|
| 5 |
+
from typing import TYPE_CHECKING, Protocol, Union
|
| 6 |
+
|
| 7 |
+
if TYPE_CHECKING:
|
| 8 |
+
from .generic_set import GenericSet
|
| 9 |
+
from .graph_pointer import GraphPointer
|
| 10 |
+
from .rule_ref import RuleRef
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class RuleType(StrEnum):
|
| 14 |
+
CHAR = "char"
|
| 15 |
+
CHAR_EXCLUDE = "char_exclude"
|
| 16 |
+
END = "end"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
Range = list[int]
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
@dataclass
|
| 23 |
+
class RuleChar:
|
| 24 |
+
value: list[int | Range]
|
| 25 |
+
type: RuleType = field(default=RuleType.CHAR, init=False)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
@dataclass
|
| 29 |
+
class RuleCharExclude:
|
| 30 |
+
value: list[int | Range]
|
| 31 |
+
type: RuleType = field(default=RuleType.CHAR_EXCLUDE, init=False)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
@dataclass
|
| 35 |
+
class RuleEnd:
|
| 36 |
+
type: RuleType = field(default=RuleType.END, init=False)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
UnresolvedRule = Union[RuleChar, RuleCharExclude, "RuleRef", RuleEnd]
|
| 40 |
+
# RuleRefs should never be exposed to the end user.
|
| 41 |
+
ResolvedRule = RuleChar | RuleCharExclude | RuleEnd
|
| 42 |
+
ResolvedGraphPointer = "GraphPointer"
|
| 43 |
+
Pointers = "GenericSet[GraphPointer, str]"
|
| 44 |
+
|
| 45 |
+
# ValidInput can either be a string, or a number indicating a code point.
|
| 46 |
+
# It CANNOT be a number representing a number; a number being a "number" (like "8")
|
| 47 |
+
# should be passed in as a string.
|
| 48 |
+
ValidInput = str | int | list[int]
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
class Colorize(Protocol):
|
| 52 |
+
def __call__(self, s: str | int, color: str) -> str: ...
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_parser/__init__.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .build_rule_stack import build_rule_stack
|
| 2 |
+
|
| 3 |
+
__all__ = ["build_rule_stack"]
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_parser/build_rule_stack.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
from typing import TYPE_CHECKING
|
| 5 |
+
|
| 6 |
+
from ..grammar_graph.rule_ref import RuleRef
|
| 7 |
+
from ..grammar_graph.type_guards import is_range, is_rule_end
|
| 8 |
+
from ..grammar_graph.types import RuleChar, RuleCharExclude, RuleEnd
|
| 9 |
+
from ..rules_builder.type_guards import (
|
| 10 |
+
is_rule_def_alt,
|
| 11 |
+
is_rule_def_char,
|
| 12 |
+
is_rule_def_char_alt,
|
| 13 |
+
is_rule_def_char_not,
|
| 14 |
+
is_rule_def_char_rng_upper,
|
| 15 |
+
is_rule_def_end,
|
| 16 |
+
is_rule_def_ref,
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
if TYPE_CHECKING:
|
| 20 |
+
from ..grammar_graph.types import UnresolvedRule
|
| 21 |
+
from ..rules_builder.types import InternalRuleDef, InternalRuleDefChar, InternalRuleDefCharNot
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def _make_char_rule(rule_def: InternalRuleDefChar | InternalRuleDefCharNot) -> RuleChar | RuleCharExclude:
|
| 25 |
+
if is_rule_def_char_not(rule_def):
|
| 26 |
+
return RuleCharExclude([*rule_def.value])
|
| 27 |
+
return RuleChar([*rule_def.value])
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def build_rule_stack(linear_rules: list[InternalRuleDef]) -> list[list[UnresolvedRule]]:
|
| 31 |
+
paths: list[UnresolvedRule] = []
|
| 32 |
+
|
| 33 |
+
stack: list[list[UnresolvedRule]] = []
|
| 34 |
+
|
| 35 |
+
idx = 0
|
| 36 |
+
while idx < len(linear_rules):
|
| 37 |
+
rule_def = linear_rules[idx]
|
| 38 |
+
if is_rule_def_char(rule_def) or is_rule_def_char_not(rule_def):
|
| 39 |
+
# this could be a single char, or a range, or a sequence of alts; we don't know until we step through it.
|
| 40 |
+
char_rule = _make_char_rule(rule_def)
|
| 41 |
+
idx += 1
|
| 42 |
+
while idx < len(linear_rules) and (
|
| 43 |
+
is_rule_def_char_rng_upper(linear_rules[idx]) or is_rule_def_char_alt(linear_rules[idx])
|
| 44 |
+
):
|
| 45 |
+
rule = linear_rules[idx]
|
| 46 |
+
if is_rule_def_char_rng_upper(rule):
|
| 47 |
+
# previous rule value should be a number
|
| 48 |
+
prev_value = char_rule.value.pop() if char_rule.value else None
|
| 49 |
+
if is_range(prev_value):
|
| 50 |
+
raise ValueError(
|
| 51 |
+
f"Unexpected range, expected a number but got an array: {json.dumps(prev_value)}"
|
| 52 |
+
)
|
| 53 |
+
if prev_value is None:
|
| 54 |
+
raise ValueError("Unexpected undefined value")
|
| 55 |
+
char_rule.value.append([prev_value, rule.value])
|
| 56 |
+
if is_rule_def_char_alt(rule):
|
| 57 |
+
char_rule.value.append(rule.value)
|
| 58 |
+
idx += 1
|
| 59 |
+
paths.append(char_rule)
|
| 60 |
+
else:
|
| 61 |
+
if is_rule_def_alt(rule_def):
|
| 62 |
+
if not paths:
|
| 63 |
+
raise ValueError("Encountered alt without anything before it")
|
| 64 |
+
paths.append(RuleEnd())
|
| 65 |
+
stack.append(paths)
|
| 66 |
+
paths = []
|
| 67 |
+
elif is_rule_def_end(rule_def):
|
| 68 |
+
paths.append(RuleEnd())
|
| 69 |
+
elif is_rule_def_ref(rule_def):
|
| 70 |
+
paths.append(RuleRef(rule_def.value))
|
| 71 |
+
elif is_rule_def_char_alt(rule_def):
|
| 72 |
+
raise ValueError(f"Encountered char alt, should be handled by above block: {rule_def.type}")
|
| 73 |
+
else:
|
| 74 |
+
raise ValueError(f"Unsupported rule type: {getattr(rule_def, 'type', rule_def)}")
|
| 75 |
+
idx += 1
|
| 76 |
+
|
| 77 |
+
if not paths or not is_rule_end(paths[-1]):
|
| 78 |
+
paths.append(RuleEnd())
|
| 79 |
+
|
| 80 |
+
stack.append(paths)
|
| 81 |
+
|
| 82 |
+
return stack
|
v2-tests-configuration/runs/20261012T035627Z_6d495db0/ported_implementation/gbnf/grammar_parser/build_rule_stack_test.py
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from ..grammar_graph.rule_ref import RuleRef
|
| 4 |
+
from ..grammar_graph.types import RuleChar, RuleCharExclude, RuleEnd
|
| 5 |
+
from ..rules_builder.types import (
|
| 6 |
+
InternalRuleDefAlt,
|
| 7 |
+
InternalRuleDefChar,
|
| 8 |
+
InternalRuleDefCharAlt,
|
| 9 |
+
InternalRuleDefCharNot,
|
| 10 |
+
InternalRuleDefCharRngUpper,
|
| 11 |
+
InternalRuleDefEnd,
|
| 12 |
+
InternalRuleDefReference,
|
| 13 |
+
)
|
| 14 |
+
from .build_rule_stack import build_rule_stack
|
| 15 |
+
|
| 16 |
+
# short aliases for internal rule defs
|
| 17 |
+
CHAR = InternalRuleDefChar
|
| 18 |
+
CHAR_NOT = InternalRuleDefCharNot
|
| 19 |
+
CHAR_ALT = InternalRuleDefCharAlt
|
| 20 |
+
RNG_UPPER = InternalRuleDefCharRngUpper
|
| 21 |
+
REF = InternalRuleDefReference
|
| 22 |
+
ALT = InternalRuleDefAlt
|
| 23 |
+
END = InternalRuleDefEnd
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def o(c):
|
| 27 |
+
return ord(c)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class Ref:
|
| 31 |
+
"""Expected-value stand-in for a RuleRef (RuleRef has no __eq__; TS toEqual compares structurally)."""
|
| 32 |
+
|
| 33 |
+
def __init__(self, value):
|
| 34 |
+
self.value = value
|
| 35 |
+
|
| 36 |
+
def __eq__(self, other):
|
| 37 |
+
return isinstance(other, RuleRef) and other.value == self.value
|
| 38 |
+
|
| 39 |
+
def __repr__(self):
|
| 40 |
+
return f"Ref({self.value})"
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def test_it_builds_rule_stack_for_a_single_path():
|
| 44 |
+
assert build_rule_stack([CHAR([120])]) == [[RuleChar([120]), RuleEnd()]]
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def test_it_builds_rule_stack_for_two_alternate_paths():
|
| 48 |
+
assert build_rule_stack([CHAR([120]), ALT(), CHAR([121])]) == [
|
| 49 |
+
[RuleChar([120]), RuleEnd()],
|
| 50 |
+
[RuleChar([121]), RuleEnd()],
|
| 51 |
+
]
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def test_it_builds_rule_stack_for_three_alternate_paths():
|
| 55 |
+
assert build_rule_stack([CHAR([120]), ALT(), CHAR([121]), ALT(), CHAR([122])]) == [
|
| 56 |
+
[RuleChar([120]), RuleEnd()],
|
| 57 |
+
[RuleChar([121]), RuleEnd()],
|
| 58 |
+
[RuleChar([122]), RuleEnd()],
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def test_it_builds_rule_stack_for_char_not():
|
| 63 |
+
assert build_rule_stack([CHAR_NOT([120]), ALT(), CHAR_NOT([121]), ALT(), CHAR_NOT([122])]) == [
|
| 64 |
+
[RuleCharExclude([120]), RuleEnd()],
|
| 65 |
+
[RuleCharExclude([121]), RuleEnd()],
|
| 66 |
+
[RuleCharExclude([122]), RuleEnd()],
|
| 67 |
+
]
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def test_it_builds_rule_stack_for_mixed_char_and_char_not():
|
| 71 |
+
assert build_rule_stack([CHAR_NOT([120]), ALT(), CHAR([121]), ALT(), CHAR_NOT([122])]) == [
|
| 72 |
+
[RuleCharExclude([120]), RuleEnd()],
|
| 73 |
+
[RuleChar([121]), RuleEnd()],
|
| 74 |
+
[RuleCharExclude([122]), RuleEnd()],
|
| 75 |
+
]
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def test_it_builds_rule_stack_for_a_char_not_with_two_characters_and_a_range():
|
| 79 |
+
assert build_rule_stack([CHAR_NOT([120]), CHAR_ALT(121), CHAR_ALT(122), RNG_UPPER(130)]) == [
|
| 80 |
+
[RuleCharExclude([120, 121, [122, 130]]), RuleEnd()],
|
| 81 |
+
]
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
A_Z = [o("a"), o("z")]
|
| 85 |
+
UPPER_A_Z = [o("A"), o("Z")]
|
| 86 |
+
DIGITS = [o("0"), o("9")]
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
@pytest.mark.parametrize(
|
| 90 |
+
("_grammar", "input_", "expected"),
|
| 91 |
+
[
|
| 92 |
+
(
|
| 93 |
+
"[a-z]",
|
| 94 |
+
[CHAR([o("a")]), RNG_UPPER(o("z")), END()],
|
| 95 |
+
[[RuleChar([A_Z]), RuleEnd()]],
|
| 96 |
+
),
|
| 97 |
+
(
|
| 98 |
+
"[a-zA-Z]",
|
| 99 |
+
[CHAR([o("a")]), RNG_UPPER(o("z")), CHAR_ALT(o("A")), RNG_UPPER(o("Z")), END()],
|
| 100 |
+
[[RuleChar([A_Z, UPPER_A_Z]), RuleEnd()]],
|
| 101 |
+
),
|
| 102 |
+
(
|
| 103 |
+
"[a-zA-Z0-9]",
|
| 104 |
+
[
|
| 105 |
+
CHAR([o("a")]),
|
| 106 |
+
RNG_UPPER(o("z")),
|
| 107 |
+
CHAR_ALT(o("A")),
|
| 108 |
+
RNG_UPPER(o("Z")),
|
| 109 |
+
CHAR_ALT(o("0")),
|
| 110 |
+
RNG_UPPER(o("9")),
|
| 111 |
+
END(),
|
| 112 |
+
],
|
| 113 |
+
[[RuleChar([A_Z, UPPER_A_Z, DIGITS]), RuleEnd()]],
|
| 114 |
+
),
|
| 115 |
+
],
|
| 116 |
+
)
|
| 117 |
+
def test_ranges_no_modifiers(_grammar, input_, expected):
|
| 118 |
+
assert build_rule_stack(input_) == expected
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
@pytest.mark.parametrize(
|
| 122 |
+
("_grammar", "input_", "expected"),
|
| 123 |
+
[
|
| 124 |
+
(
|
| 125 |
+
"[a-z]?",
|
| 126 |
+
[CHAR([o("a")]), RNG_UPPER(o("z")), ALT(), END()],
|
| 127 |
+
[[RuleChar([A_Z]), RuleEnd()], [RuleEnd()]],
|
| 128 |
+
),
|
| 129 |
+
(
|
| 130 |
+
"[a-zA-Z]?",
|
| 131 |
+
[CHAR([o("a")]), RNG_UPPER(o("z")), CHAR_ALT(o("A")), RNG_UPPER(o("Z")), ALT(), END()],
|
| 132 |
+
[[RuleChar([A_Z, UPPER_A_Z]), RuleEnd()], [RuleEnd()]],
|
| 133 |
+
),
|
| 134 |
+
(
|
| 135 |
+
"[a-zA-Z0-9]?",
|
| 136 |
+
[
|
| 137 |
+
CHAR([o("a")]),
|
| 138 |
+
RNG_UPPER(o("z")),
|
| 139 |
+
CHAR_ALT(o("A")),
|
| 140 |
+
RNG_UPPER(o("Z")),
|
| 141 |
+
CHAR_ALT(o("0")),
|
| 142 |
+
RNG_UPPER(o("9")),
|
| 143 |
+
ALT(),
|
| 144 |
+
END(),
|
| 145 |
+
],
|
| 146 |
+
[[RuleChar([A_Z, UPPER_A_Z, DIGITS]), RuleEnd()], [RuleEnd()]],
|
| 147 |
+
),
|
| 148 |
+
],
|
| 149 |
+
)
|
| 150 |
+
def test_ranges_optional(_grammar, input_, expected):
|
| 151 |
+
assert build_rule_stack(input_) == expected
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
@pytest.mark.parametrize(
|
| 155 |
+
("_grammar", "input_", "expected"),
|
| 156 |
+
[
|
| 157 |
+
(
|
| 158 |
+
"[a-z]+",
|
| 159 |
+
[CHAR([o("a")]), RNG_UPPER(o("z")), REF(1), ALT(), CHAR([o("a")]), RNG_UPPER(o("z")), END()],
|
| 160 |
+
[
|
| 161 |
+
[RuleChar([A_Z]), Ref(1), RuleEnd()],
|
| 162 |
+
[RuleChar([A_Z]), RuleEnd()],
|
| 163 |
+
],
|
| 164 |
+
),
|
| 165 |
+
(
|
| 166 |
+
"[a-zA-Z]+",
|
| 167 |
+
[
|
| 168 |
+
CHAR([o("a")]),
|
| 169 |
+
RNG_UPPER(o("z")),
|
| 170 |
+
CHAR_ALT(o("A")),
|
| 171 |
+
RNG_UPPER(o("Z")),
|
| 172 |
+
REF(1),
|
| 173 |
+
ALT(),
|
| 174 |
+
CHAR([o("a")]),
|
| 175 |
+
RNG_UPPER(o("z")),
|
| 176 |
+
CHAR_ALT(o("A")),
|
| 177 |
+
RNG_UPPER(o("Z")),
|
| 178 |
+
END(),
|
| 179 |
+
],
|
| 180 |
+
[
|
| 181 |
+
[RuleChar([A_Z, UPPER_A_Z]), Ref(1), RuleEnd()],
|
| 182 |
+
[RuleChar([A_Z, UPPER_A_Z]), RuleEnd()],
|
| 183 |
+
],
|
| 184 |
+
),
|
| 185 |
+
(
|
| 186 |
+
"[a-zA-Z0-9]+",
|
| 187 |
+
[
|
| 188 |
+
CHAR([o("a")]),
|
| 189 |
+
RNG_UPPER(o("z")),
|
| 190 |
+
CHAR_ALT(o("A")),
|
| 191 |
+
RNG_UPPER(o("Z")),
|
| 192 |
+
CHAR_ALT(o("0")),
|
| 193 |
+
RNG_UPPER(o("9")),
|
| 194 |
+
REF(1),
|
| 195 |
+
ALT(),
|
| 196 |
+
CHAR([o("a")]),
|
| 197 |
+
RNG_UPPER(o("z")),
|
| 198 |
+
CHAR_ALT(o("A")),
|
| 199 |
+
RNG_UPPER(o("Z")),
|
| 200 |
+
CHAR_ALT(o("0")),
|
| 201 |
+
RNG_UPPER(o("9")),
|
| 202 |
+
END(),
|
| 203 |
+
],
|
| 204 |
+
[
|
| 205 |
+
[RuleChar([A_Z, UPPER_A_Z, DIGITS]), Ref(1), RuleEnd()],
|
| 206 |
+
[RuleChar([A_Z, UPPER_A_Z, DIGITS]), RuleEnd()],
|
| 207 |
+
],
|
| 208 |
+
),
|
| 209 |
+
],
|
| 210 |
+
)
|
| 211 |
+
def test_ranges_repeating(_grammar, input_, expected):
|
| 212 |
+
assert build_rule_stack(input_) == expected
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
@pytest.mark.parametrize(
|
| 216 |
+
("_grammar", "input_", "expected"),
|
| 217 |
+
[
|
| 218 |
+
(
|
| 219 |
+
"[a-z]*",
|
| 220 |
+
[CHAR([o("a")]), RNG_UPPER(o("z")), REF(1), ALT(), END()],
|
| 221 |
+
[[RuleChar([A_Z]), Ref(1), RuleEnd()], [RuleEnd()]],
|
| 222 |
+
),
|
| 223 |
+
(
|
| 224 |
+
"[a-zA-Z]*",
|
| 225 |
+
[CHAR([o("a")]), RNG_UPPER(o("z")), CHAR_ALT(o("A")), RNG_UPPER(o("Z")), REF(1), ALT(), END()],
|
| 226 |
+
[[RuleChar([A_Z, UPPER_A_Z]), Ref(1), RuleEnd()], [RuleEnd()]],
|
| 227 |
+
),
|
| 228 |
+
(
|
| 229 |
+
"[a-zA-Z0-9]*",
|
| 230 |
+
[
|
| 231 |
+
CHAR([o("a")]),
|
| 232 |
+
RNG_UPPER(o("z")),
|
| 233 |
+
CHAR_ALT(o("A")),
|
| 234 |
+
RNG_UPPER(o("Z")),
|
| 235 |
+
CHAR_ALT(o("0")),
|
| 236 |
+
RNG_UPPER(o("9")),
|
| 237 |
+
REF(1),
|
| 238 |
+
ALT(),
|
| 239 |
+
END(),
|
| 240 |
+
],
|
| 241 |
+
[[RuleChar([A_Z, UPPER_A_Z, DIGITS]), Ref(1), RuleEnd()], [RuleEnd()]],
|
| 242 |
+
),
|
| 243 |
+
],
|
| 244 |
+
)
|
| 245 |
+
def test_ranges_optionally_repeating(_grammar, input_, expected):
|
| 246 |
+
assert build_rule_stack(input_) == expected
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
def test_char_with_range():
|
| 250 |
+
# `[a-zA-Z_]`
|
| 251 |
+
input_ = [CHAR([o("a")]), RNG_UPPER(o("z")), CHAR_ALT(o("A")), RNG_UPPER(o("Z")), CHAR_ALT(o("_")), END()]
|
| 252 |
+
assert build_rule_stack(input_) == [[RuleChar([A_Z, UPPER_A_Z, o("_")]), RuleEnd()]]
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
def test_it_builds_rule_stack_for_ws_situation():
|
| 256 |
+
# `root ::= (ws )+\nws ::= [ \\t\\n]*`
|
| 257 |
+
input_ = [CHAR([32]), CHAR_ALT(92), CHAR_ALT(110), REF(4), ALT(), END()]
|
| 258 |
+
assert build_rule_stack(input_) == [
|
| 259 |
+
[RuleChar([32, 92, 110]), Ref(4), RuleEnd()],
|
| 260 |
+
[RuleEnd()],
|
| 261 |
+
]
|
| 262 |
+
|
| 263 |
+
|
| 264 |
+
def test_it_builds_rule_stack_for_repeated_range_situation():
|
| 265 |
+
# `root ::= [a-zA-Z]+`
|
| 266 |
+
input_ = [
|
| 267 |
+
CHAR([97]),
|
| 268 |
+
RNG_UPPER(122),
|
| 269 |
+
CHAR_ALT(65),
|
| 270 |
+
RNG_UPPER(90),
|
| 271 |
+
REF(1),
|
| 272 |
+
ALT(),
|
| 273 |
+
CHAR([97]),
|
| 274 |
+
RNG_UPPER(122),
|
| 275 |
+
CHAR_ALT(65),
|
| 276 |
+
RNG_UPPER(90),
|
| 277 |
+
END(),
|
| 278 |
+
]
|
| 279 |
+
assert build_rule_stack(input_) == [
|
| 280 |
+
[RuleChar([[97, 122], [65, 90]]), Ref(1), RuleEnd()],
|
| 281 |
+
[RuleChar([[97, 122], [65, 90]]), RuleEnd()],
|
| 282 |
+
]
|