Upload experiments v1, v2-models-effort, v2-tests-configuration as on disk 2026-11-11 (part 40)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/data/grammars.json +0 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/data/iteration.json +1 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/data/iteration_additional.json +1 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/data/iteration_additional_errors.json +1 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/data/iteration_initial.json +1 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/data/rules_builder.json +1 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/data/validate_grammar_err.json +1 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/data/validate_grammar_ok.json +1 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/data/validate_input_err.json +1 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/data/validate_input_ok.json +1 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/helpers.py +14 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/integration/__init__.py +0 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/integration/test_edge_cases.py +82 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/integration/test_iteration.py +58 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/integration/test_validation.py +31 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/unit/__init__.py +0 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/unit/test_build_rule_stack.py +112 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/unit/test_grammar_graph_helpers.py +173 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/unit/test_graph.py +191 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/unit/test_parse_state_and_gbnf.py +137 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/unit/test_print.py +76 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/unit/test_rules_builder.py +75 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/unit/test_rules_builder_helpers.py +104 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/unit/test_type_guards.py +87 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/unit/test_utils.py +98 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/proxy.log +35 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/result.json +1 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/run.json +10 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/stderr.txt +0 -0
- v2-models-effort/runs/20261017T184432Z_db5d720b/transcript/.claude/projects/-target/b24bf51f-6b2c-460b-ad53-87df3e5a5965.jsonl +0 -0
- v2-models-effort/runs/20261017T185204Z_a9c4712f/manifest.json +99 -0
- v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/GBNF.ts +19 -0
- v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/colorize.ts +12 -0
- v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/get_input_as_code_points.ts +21 -0
- v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/get_parent_stack_id.ts +13 -0
- v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/get_serialized_rule_key.ts +18 -0
- v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/grammar_graph_types.ts +37 -0
- v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/graph.ts +190 -0
- v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/graph_node.ts +33 -0
- v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/graph_pointer.ts +74 -0
- v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/parse_state.ts +43 -0
- v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/pointers.ts +25 -0
- v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/print.ts +63 -0
- v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/rule_ref.ts +23 -0
- v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/type_guards.ts +18 -0
- v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_parser/build_rule_stack.ts +77 -0
- v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/index.ts +3 -0
- v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/rules_builder/index.ts +2 -0
- v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/rules_builder/is_word_char.ts +3 -0
- v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/rules_builder/parse_char.ts +29 -0
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/data/grammars.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/data/iteration.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
[["root ::= \"foo\"",[{"type":"char","value":[102]}]],["root ::= \"foo\" | \"bar\" ",[{"type":"char","value":[102]},{"type":"char","value":[98]}]],["root ::= \"foo\" | \"bar\" | \"gaz\"",[{"type":"char","value":[102]},{"type":"char","value":[98]},{"type":"char","value":[103]}]],["root ::= \"foo\" | \"bar\" | \"baz\"",[{"type":"char","value":[102]},{"type":"char","value":[98]}]],["root ::= [^x]",[{"type":"char_exclude","value":[120]}]],["root ::= [^f] \"o\"",[{"type":"char_exclude","value":[102]}]],["root ::= [^A-Z]",[{"type":"char_exclude","value":[[65,90]]}]],["root ::= [^A-Z0-9]",[{"type":"char_exclude","value":[[65,90],[48,57]]}]],["root ::= [^A-Z0-9_-]",[{"type":"char_exclude","value":[[65,90],[48,57],95,45]}]],["\nroot ::= foo\nfoo ::= \"foo\"\n ",[{"type":"char","value":[102]}]],["\nroot ::= f\nf ::= foo\nfoo ::= \"foo\"\n ",[{"type":"char","value":[102]}]],["\nroot ::= foo | \"bar\"\nfoo ::= \"foo\"",[{"type":"char","value":[102]},{"type":"char","value":[98]}]],["\nroot ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"\n ",[{"type":"char","value":[102]},{"type":"char","value":[98]}]],["\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]}]],["root ::= [a-z]",[{"type":"char","value":[[97,122]]}]],["root ::= [a-zA-Z]",[{"type":"char","value":[[97,122],[65,90]]}]],["root ::= [a-z]?",[{"type":"char","value":[[97,122]]},{"type":"end"}]],["root ::= [a-z]+",[{"type":"char","value":[[97,122]]}]],["root ::= [a-z]*",[{"type":"char","value":[[97,122]]},{"type":"end"}]],["\n root ::= \"foo\"\n foo ::= \"foo\"",[{"type":"char","value":[102]}]],["\n root ::= (expr \"=\" term \"\\n\")+\n expr ::= term ([-+*/] term)*\n term ::= [0-9]+\n ",[{"type":"char","value":[[48,57]]}]]]
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/data/iteration_additional.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
[["root ::= \"foo\"","","f",[{"type":"char","value":[111]}]],["root ::= \"foo\"","f","o",[{"type":"char","value":[111]}]],["root ::= \"foo\"","fo","o",[{"type":"end"}]],["root ::= \"foo\" | \"bar\" ","","f",[{"type":"char","value":[111]}]],["root ::= \"foo\" | \"bar\" ","f","o",[{"type":"char","value":[111]}]],["root ::= \"foo\" | \"bar\" ","fo","o",[{"type":"end"}]],["root ::= \"foo\" | \"bar\" ","","b",[{"type":"char","value":[97]}]],["root ::= \"foo\" | \"bar\" ","b","a",[{"type":"char","value":[114]}]],["root ::= \"foo\" | \"bar\" ","ba","r",[{"type":"end"}]],["root ::= \"foo\" | \"bar\" | \"baz\"","b","a",[{"type":"char","value":[114]},{"type":"char","value":[122]}]],["root ::= [^f] \"o\"","g","o",[{"type":"end"}]],["root ::= [^A-Z]","","a",[{"type":"end"}]],["root ::= [^A-Z0-9]","","a",[{"type":"end"}]],["root ::= [^A-Z0-9_-]","","a",[{"type":"end"}]],["root ::= foo\nfoo ::= \"foo\"","","f",[{"type":"char","value":[111]}]],["root ::= foo\nfoo ::= \"foo\"","f","o",[{"type":"char","value":[111]}]],["root ::= foo\nfoo ::= \"foo\"","fo","o",[{"type":"end"}]],["root ::= foo | \"bar\"\nfoo ::= \"foo\"","","f",[{"type":"char","value":[111]}]],["root ::= foo | \"bar\"\nfoo ::= \"foo\"","f","o",[{"type":"char","value":[111]}]],["root ::= foo | \"bar\"\nfoo ::= \"foo\"","fo","o",[{"type":"end"}]],["root ::= foo | \"bar\"\nfoo ::= \"foo\"","","b",[{"type":"char","value":[97]}]],["root ::= foo | \"bar\"\nfoo ::= \"foo\"","b","a",[{"type":"char","value":[114]}]],["root ::= foo | \"bar\"\nfoo ::= \"foo\"","ba","r",[{"type":"end"}]],["root ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"","","f",[{"type":"char","value":[111]}]],["\nroot ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"","f","o",[{"type":"char","value":[111]}]],["\nroot ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"","fo","o",[{"type":"end"}]],["root ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"","","b",[{"type":"char","value":[97]}]],["root ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"","b","a",[{"type":"char","value":[114]}]],["root ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"","ba","r",[{"type":"end"}]],["\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"","","f",[{"type":"char","value":[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]}]],["root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"","fo","o",[{"type":"end"}]],["root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"","","b",[{"type":"char","value":[97]}]],["\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]}]],["\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"","ba","r",[{"type":"end"}]],["\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"","ba","z",[{"type":"end"}]],["root ::= [a-z]","","a",[{"type":"end"}]],["root ::= [a-z]","","m",[{"type":"end"}]],["root ::= [a-z]","","z",[{"type":"end"}]],["root ::= [a-zA-Z]","","a",[{"type":"end"}]],["root ::= [a-zA-Z]","","Z",[{"type":"end"}]],["root ::= [a-zA-Z0-9]","","Z",[{"type":"end"}]],["root ::= [a-zA-Z0-9]","","0",[{"type":"end"}]],["root ::= [a-zA-Z0-9]","","9",[{"type":"end"}]],["root ::= [a-z]?","","a",[{"type":"end"}]],["root ::= [a-zA-Z]?","","Z",[{"type":"end"}]],["root ::= [a-zA-Z0-9]?","","0",[{"type":"end"}]],["root ::= [a-z]+","","a",[{"type":"char","value":[[97,122]]},{"type":"end"}]],["root ::= [a-zA-Z]+","","Z",[{"type":"char","value":[[97,122],[65,90]]},{"type":"end"}]],["root ::= [a-zA-Z]+","azA","Z",[{"type":"char","value":[[97,122],[65,90]]},{"type":"end"}]],["root ::= [a-z]*","","a",[{"type":"char","value":[[97,122]]},{"type":"end"}]],["root ::= [a-zA-Z]+","","Z",[{"type":"char","value":[[97,122],[65,90]]},{"type":"end"}]],["root ::= [a-zA-Z]+","acczABC","Z",[{"type":"char","value":[[97,122],[65,90]]},{"type":"end"}]],["root ::= \"foo\" | \"bar\" | \"baz\" | \"bazaar\" | \"barrington\" ","baza","a",[{"type":"char","value":[114]}]],["root ::= [^f]+ \"o\"","aaa","a",[{"type":"char_exclude","value":[102]},{"type":"char","value":[111]}]],["root ::= [^A-Z]+","abc","d",[{"type":"char_exclude","value":[[65,90]]},{"type":"end"}]],["root ::= [^A-Z0-9]*","abc","d",[{"type":"char_exclude","value":[[65,90],[48,57]]},{"type":"end"}]],["root ::= [^A-Z0-9_-]*","abc","d",[{"type":"char_exclude","value":[[65,90],[48,57],95,45]},{"type":"end"}]]]
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/data/iteration_additional_errors.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
[["root ::= \"foo\"","f","1"],["root ::= \"foo\"","f","b"],["root ::= \"foo\"","f","o1"],["root ::= \"foo\"","f","ooo"],["root ::= \"foo\"","f","ooooooo"],["root ::= \"foo\" | \"bar\"","f","1"],["root ::= \"foo\" | \"bar\"","b","1"],["root ::= \"foo\" | \"bar\"","f","o1"],["root ::= \"foo\" | \"bar\"","b","a1"],["root ::= \"foo\" | \"bar\"","f","ooo"],["root ::= \"foo\" | \"bar\"","b","arrr"],["root ::= \"foo\" | \"bar\" | \"baz\"","f","1"],["root ::= \"foo\" | \"bar\" | \"baz\"","f","z"],["root ::= \"foo\" | \"bar\" | \"baz\"","b","b1"],["root ::= \"foo\" | \"bar\" | \"baz\"","f","o1"],["root ::= \"foo\" | \"bar\" | \"baz\"","b","al"],["root ::= \"foo\" | \"bar\" | \"baz\"","f","ooo"],["root ::= \"foo\" | \"bar\" | \"baz\"","b","azrr"],["root ::= foo\nfoo ::=\"foo\"","f","1"],["root ::= foo\nfoo ::=\"foo\"","f","b"],["root ::= foo\nfoo ::=\"foo\"","f","o1"],["root ::= foo\nfoo ::=\"foo\"","f","ooo"],["root::=foo|\"bar\"\nfoo::=\"foo\"","f","1"],["root::=foo|\"bar\"\nfoo::=\"foo\"","f","z"],["root::=foo|\"bar\"\nfoo::=\"foo\"","b","b1"],["root::=foo|\"bar\"\nfoo::=\"foo\"","f","o1"],["root::=foo|\"bar\"\nfoo::=\"foo\"","f","ooo"],["root::=foo|\"bar\"\nfoo::=\"foo\"","b","arr"],["root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"","f","1"],["root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"","f","z"],["root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"","b","1"],["root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"","f","o1"],["root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"","f","ooo"],["root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"","b","arr"],["root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"","f","1"],["root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"","f","z"],["root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"","b","1"],["root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"","f","o1"],["root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"","f","ooo"],["root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"","b","arr"],["root ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"","b","azr"],["root ::= [a-z]","a","A"],["root ::= [a-z]","a","0"],["root ::= [a-z]","a","az"],["root ::= [a-z]?","a","A"],["root ::= [a-z]?","a","0"],["root ::= [a-z]?","a","az"],["root ::= [a-z]+","a","A"],["root ::= [a-z]+","a","0"],["root ::= [a-z]+","a","z0"],["root ::= [a-z]*","a","A"],["root ::= [a-z]*","a","0"],["root ::= [a-z]*","a","z0"]]
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/data/iteration_initial.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
[["root ::= \"foo\"","",[{"type":"char","value":[102]}]],["root ::= \"foo\"","f",[{"type":"char","value":[111]}]],["root ::= \"foo\"","fo",[{"type":"char","value":[111]}]],["root ::= \"foo\"","foo",[{"type":"end"}]],["root ::= \"foo\" | \"bar\" ","",[{"type":"char","value":[102]},{"type":"char","value":[98]}]],["root ::= \"foo\" | \"bar\" ","f",[{"type":"char","value":[111]}]],["root ::= \"foo\" | \"bar\" ","fo",[{"type":"char","value":[111]}]],["root ::= \"foo\" | \"bar\" ","foo",[{"type":"end"}]],["root ::= \"foo\" | \"bar\" ","b",[{"type":"char","value":[97]}]],["root ::= \"foo\" | \"bar\" ","ba",[{"type":"char","value":[114]}]],["root ::= \"foo\" | \"bar\" ","bar",[{"type":"end"}]],["root ::= \"foo\" | \"bar\" | \"baz\"","ba",[{"type":"char","value":[114]},{"type":"char","value":[122]}]],["root ::= \"[\"","[",[{"type":"end"}]],["root ::= \"]\"","]",[{"type":"end"}]],["root ::= \"[]\"","[]",[{"type":"end"}]],["root ::= \"{\"","{",[{"type":"end"}]],["root ::= \"}\"","}",[{"type":"end"}]],["root ::= \"{}\"","{}",[{"type":"end"}]],["root ::= \"[{}]\"","[{}]",[{"type":"end"}]],["root ::= [^f] \"o\"","",[{"type":"char_exclude","value":[102]}]],["root ::= [^f] \"o\"","a",[{"type":"char","value":[111]}]],["root ::= [^A-Z]","",[{"type":"char_exclude","value":[[65,90]]}]],["root ::= [^A-Z0-9]","",[{"type":"char_exclude","value":[[65,90],[48,57]]}]],["root ::= [^A-Z0-9_-]","",[{"type":"char_exclude","value":[[65,90],[48,57],95,45]}]],["\nroot ::= foo\nfoo ::= \"foo\"","",[{"type":"char","value":[102]}]],["root ::= foo\nfoo ::= \"foo\"\n","f",[{"type":"char","value":[111]}]],["\nroot ::= foo\nfoo ::= \"foo\"\n","fo",[{"type":"char","value":[111]}]],["root ::= foo\nfoo ::= \"foo\"","foo",[{"type":"end"}]],["root ::= f\nf ::= foo\nfoo ::= \"foo\"","",[{"type":"char","value":[102]}]],["\nroot ::= foo | \"bar\"\nfoo ::= \"foo\"\n","",[{"type":"char","value":[102]},{"type":"char","value":[98]}]],["\nroot ::= foo | \"bar\"\nfoo ::= \"foo\"","f",[{"type":"char","value":[111]}]],["root ::= foo | \"bar\"\nfoo ::= \"foo\"","fo",[{"type":"char","value":[111]}]],["root ::= foo | \"bar\"\nfoo ::= \"foo\"","foo",[{"type":"end"}]],["root ::= foo | \"bar\"\nfoo ::= \"foo\"","b",[{"type":"char","value":[97]}]],["root ::= foo | \"bar\"\nfoo ::= \"foo\"","ba",[{"type":"char","value":[114]}]],["root ::= foo | \"bar\"\nfoo ::= \"foo\"","bar",[{"type":"end"}]],["\nroot ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"","",[{"type":"char","value":[102]},{"type":"char","value":[98]}]],["\nroot ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"","f",[{"type":"char","value":[111]}]],["\nroot ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"","fo",[{"type":"char","value":[111]}]],["\nroot ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"","foo",[{"type":"end"}]],["\nroot ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"","b",[{"type":"char","value":[97]}]],["\nroot ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"","ba",[{"type":"char","value":[114]}]],["\nroot ::= foo | bar\nfoo ::= \"foo\"\nbar ::= \"bar\"","bar",[{"type":"end"}]],["\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]}]],["\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"","f",[{"type":"char","value":[111]}]],["\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"","fo",[{"type":"char","value":[111]}]],["\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"","foo",[{"type":"end"}]],["\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"","b",[{"type":"char","value":[97]}]],["\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]}]],["\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"","bar",[{"type":"end"}]],["\nroot ::= f | b\nf ::= fo\nb ::= ba\nfo ::= foo\nba ::= bar | baz\nfoo ::= \"foo\"\nbar ::= \"bar\"\nbaz ::= \"baz\"","baz",[{"type":"end"}]],["root ::= [a-z]","",[{"type":"char","value":[[97,122]]}]],["root ::= [a-zA-Z]","",[{"type":"char","value":[[97,122],[65,90]]}]],["root ::= [a-z]","a",[{"type":"end"}]],["root ::= [a-z]","m",[{"type":"end"}]],["root ::= [a-z]","z",[{"type":"end"}]],["root ::= [a-zA-Z]","a",[{"type":"end"}]],["root ::= [a-zA-Z]","Z",[{"type":"end"}]],["root ::= [a-zA-Z0-9]","Z",[{"type":"end"}]],["root ::= [a-zA-Z0-9]","0",[{"type":"end"}]],["root ::= [a-zA-Z0-9]","9",[{"type":"end"}]],["root ::= [a-z]?","",[{"type":"char","value":[[97,122]]},{"type":"end"}]],["root ::= [a-z]?","a",[{"type":"end"}]],["root ::= [a-zA-Z]?","Z",[{"type":"end"}]],["root ::= [a-zA-Z0-9]?","0",[{"type":"end"}]],["root ::= [a-z]+","",[{"type":"char","value":[[97,122]]}]],["root ::= [a-z]+","l",[{"type":"char","value":[[97,122]]},{"type":"end"}]],["root ::= [a-zA-Z]+","Z",[{"type":"char","value":[[97,122],[65,90]]},{"type":"end"}]],["root ::= [a-zA-Z]+","aZ",[{"type":"char","value":[[97,122],[65,90]]},{"type":"end"}]],["root ::= [a-zA-Z]+","azAZ",[{"type":"char","value":[[97,122],[65,90]]},{"type":"end"}]],["root ::= [a-z]*","",[{"type":"char","value":[[97,122]]},{"type":"end"}]],["root ::= [a-z]*","a",[{"type":"char","value":[[97,122]]},{"type":"end"}]],["root ::= [a-zA-Z]+","Z",[{"type":"char","value":[[97,122],[65,90]]},{"type":"end"}]],["root ::= [a-zA-Z]+","abczABCZ",[{"type":"char","value":[[97,122],[65,90]]},{"type":"end"}]],["root ::= [^f]+ \"o\"","aaa",[{"type":"char_exclude","value":[102]},{"type":"char","value":[111]}]],["root ::= [^A-Z]+","abc",[{"type":"char_exclude","value":[[65,90]]},{"type":"end"}]],["root ::= [^A-Z0-9]*","abc",[{"type":"char_exclude","value":[[65,90],[48,57]]},{"type":"end"}]],["root ::= [^A-Z0-9_-]*","abc",[{"type":"char_exclude","value":[[65,90],[48,57],95,45]},{"type":"end"}]],["\nroot ::= [a-z] | xyx\nxyx ::= \"foo\"","f",[{"type":"end"},{"type":"char","value":[111]}]],["root ::= \"foo\" | \"bar\" | \"baz\" | \"bazaar\" | \"barrington\" ","bazaa",[{"type":"char","value":[114]}]],["root ::= (\"bar\" | \"foo\") \"zyx\"","bar",[{"type":"char","value":[122]}]],["root ::= \"z\" (\"bar\" | \"foo\") \"zzzz\"","z",[{"type":"char","value":[98]},{"type":"char","value":[102]}]],["root ::= \"z\" (\"bar\" | \"foo\") \"zzz\"","zbar",[{"type":"char","value":[122]}]],["\nroot ::= termz ([-+*/] termz)* \ntermz ::= [0-9]+","1",[{"type":"char","value":[[48,57]]},{"type":"char","value":[45,43,42,47]},{"type":"end"}]],["\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]}]],["\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]}]],["root ::= (expr \"=\" termb \"\n\")+\nexpr ::= termb ([-+*/] termb)*\ntermb ::= [0-9]+","1+",[{"type":"char","value":[[48,57]]}]],["\nroot ::= (expr \"=\" termc \"\n\")+\nexpr ::= termc ([-+*/] termc)*\ntermc ::= [0-9]+","1=",[{"type":"char","value":[[48,57]]}]],["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]}]],["root ::= (expr \"=\" term \"\n\")+\nexpr ::= term ([-+*/] term)*\nterm ::= [0-9]+","1=1",[{"type":"char","value":[[48,57]]},{"type":"char","value":[10]}]],["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"}]],["root ::= ( [^abcdefgh] | [b-z])* ","bcdefghzyxw0123ABCZ",[{"type":"char_exclude","value":[97,98,99,100,101,102,103,104]},{"type":"char","value":[[98,122]]},{"type":"end"}]]]
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/data/rules_builder.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
[["single-string","root ::= \"foo\"",{"symbolIds":[["root",0]],"rules":[[{"type":"CHAR","value":[102]},{"type":"CHAR","value":[111]},{"type":"CHAR","value":[111]},{"type":"END"}]]}],["quote character","root::= \"\\\"\"",{"symbolIds":[["root",0]],"rules":[[{"type":"CHAR","value":[34]},{"type":"END"}]]}],["two-lines-referencing-expression","root ::= foo\\nfoo ::= \"bar\"\\n",{"symbolIds":[["root",0],["foo",1]],"rules":[[{"type":"RULE_REF","value":1},{"type":"END"}],[{"type":"CHAR","value":[98]},{"type":"CHAR","value":[97]},{"type":"CHAR","value":[114]},{"type":"END"}]]}],["expression with dash","root ::= foo-bar\\nfoo-bar ::= \"bar\"\\n",{"symbolIds":[["root",0],["foo-bar",1]],"rules":[[{"type":"RULE_REF","value":1},{"type":"END"}],[{"type":"CHAR","value":[98]},{"type":"CHAR","value":[97]},{"type":"CHAR","value":[114]},{"type":"END"}]]}],["simple-grammar","\\nroot ::= (expr \"=\" term \"\\n\")+\\nexpr ::= term ([-+*/] term)*\\nterm ::= [0-9]+\\n\\n",{"symbolIds":[["root",0],["root_1",1],["expr",2],["term",3],["root_4",4],["expr_5",5],["expr_6",6],["term_7",7]],"rules":[[{"type":"RULE_REF","value":4},{"type":"END"}],[{"type":"RULE_REF","value":2},{"type":"CHAR","value":[61]},{"type":"RULE_REF","value":3},{"type":"CHAR","value":[10]},{"type":"END"}],[{"type":"RULE_REF","value":3},{"type":"RULE_REF","value":6},{"type":"END"}],[{"type":"RULE_REF","value":7},{"type":"END"}],[{"type":"RULE_REF","value":1},{"type":"RULE_REF","value":4},{"type":"ALT"},{"type":"RULE_REF","value":1},{"type":"END"}],[{"type":"CHAR","value":[45]},{"type":"CHAR_ALT","value":43},{"type":"CHAR_ALT","value":42},{"type":"CHAR_ALT","value":47},{"type":"RULE_REF","value":3},{"type":"END"}],[{"type":"RULE_REF","value":5},{"type":"RULE_REF","value":6},{"type":"ALT"},{"type":"END"}],[{"type":"CHAR","value":[48]},{"type":"CHAR_RNG_UPPER","value":57},{"type":"RULE_REF","value":7},{"type":"ALT"},{"type":"CHAR","value":[48]},{"type":"CHAR_RNG_UPPER","value":57},{"type":"END"}]]}],["longer-grammar","root ::= (expr \"=\" ws term \"\\n\")+\\nexpr ::= term ([-+*/] term)*\\nterm ::= ident | num | \"(\" ws expr \")\" ws\\nident ::= [a-z] [a-z0-9_]* ws\\nnum ::= [0-9]+ ws\\nws ::= [ \\t\\n]*",{"symbolIds":[["root",0],["root_1",1],["expr",2],["ws",3],["term",4],["root_5",5],["expr_6",6],["expr_7",7],["ident",8],["num",9],["ident_10",10],["num_11",11],["ws_12",12]],"rules":[[{"value":5,"type":"RULE_REF"},{"type":"END"}],[{"value":2,"type":"RULE_REF"},{"value":[61],"type":"CHAR"},{"value":3,"type":"RULE_REF"},{"value":4,"type":"RULE_REF"},{"value":[10],"type":"CHAR"},{"type":"END"}],[{"value":4,"type":"RULE_REF"},{"value":7,"type":"RULE_REF"},{"type":"END"}],[{"value":12,"type":"RULE_REF"},{"type":"END"}],[{"value":8,"type":"RULE_REF"},{"type":"ALT"},{"value":9,"type":"RULE_REF"},{"type":"ALT"},{"value":[40],"type":"CHAR"},{"value":3,"type":"RULE_REF"},{"value":2,"type":"RULE_REF"},{"value":[41],"type":"CHAR"},{"value":3,"type":"RULE_REF"},{"type":"END"}],[{"value":1,"type":"RULE_REF"},{"value":5,"type":"RULE_REF"},{"type":"ALT"},{"value":1,"type":"RULE_REF"},{"type":"END"}],[{"value":[45],"type":"CHAR"},{"value":43,"type":"CHAR_ALT"},{"value":42,"type":"CHAR_ALT"},{"value":47,"type":"CHAR_ALT"},{"value":4,"type":"RULE_REF"},{"type":"END"}],[{"value":6,"type":"RULE_REF"},{"value":7,"type":"RULE_REF"},{"type":"ALT"},{"type":"END"}],[{"value":[97],"type":"CHAR"},{"value":122,"type":"CHAR_RNG_UPPER"},{"value":10,"type":"RULE_REF"},{"value":3,"type":"RULE_REF"},{"type":"END"}],[{"value":11,"type":"RULE_REF"},{"value":3,"type":"RULE_REF"},{"type":"END"}],[{"value":[97],"type":"CHAR"},{"value":122,"type":"CHAR_RNG_UPPER"},{"value":48,"type":"CHAR_ALT"},{"value":57,"type":"CHAR_RNG_UPPER"},{"value":95,"type":"CHAR_ALT"},{"value":10,"type":"RULE_REF"},{"type":"ALT"},{"type":"END"}],[{"value":[48],"type":"CHAR"},{"value":57,"type":"CHAR_RNG_UPPER"},{"value":11,"type":"RULE_REF"},{"type":"ALT"},{"value":[48],"type":"CHAR"},{"value":57,"type":"CHAR_RNG_UPPER"},{"type":"END"}],[{"value":[32],"type":"CHAR"},{"value":9,"type":"CHAR_ALT"},{"value":10,"type":"CHAR_ALT"},{"value":12,"type":"RULE_REF"},{"type":"ALT"},{"type":"END"}]]}],["character unicode grammar","root ::= [ぁ-ゟ]",{"symbolIds":[["root",0]],"rules":[[{"type":"CHAR","value":[12353]},{"type":"CHAR_RNG_UPPER","value":12447},{"type":"END"}]]}],["character alts grammar","root ::= [az]",{"symbolIds":[["root",0]],"rules":[[{"type":"CHAR","value":[97]},{"type":"CHAR_ALT","value":122},{"type":"END"}]]}],["character range grammar","root ::= [a-zA-Z0-9]",{"symbolIds":[["root",0]],"rules":[[{"type":"CHAR","value":[97]},{"type":"CHAR_RNG_UPPER","value":122},{"type":"CHAR_ALT","value":65},{"type":"CHAR_RNG_UPPER","value":90},{"type":"CHAR_ALT","value":48},{"type":"CHAR_RNG_UPPER","value":57},{"type":"END"}]]}],["character range grammar with dash at end","root ::= [a-z-]",{"symbolIds":[["root",0]],"rules":[[{"type":"CHAR","value":[97]},{"type":"CHAR_RNG_UPPER","value":122},{"type":"CHAR_ALT","value":45},{"type":"END"}]]}],["grouping","root ::= \"f\" (\"b\" | \"a\")",{"symbolIds":[["root",0],["root_1",1]],"rules":[[{"type":"CHAR","value":[102]},{"type":"RULE_REF","value":1},{"type":"END"}],[{"type":"CHAR","value":[98]},{"type":"ALT"},{"type":"CHAR","value":[97]},{"type":"END"}]]}],["optional","root ::= \"f\"?",{"symbolIds":[["root",0],["root_1",1]],"rules":[[{"type":"RULE_REF","value":1},{"type":"END"}],[{"type":"CHAR","value":[102]},{"type":"ALT"},{"type":"END"}]]}],["repeating","root ::= \"f\"*",{"symbolIds":[["root",0],["root_1",1]],"rules":[[{"type":"RULE_REF","value":1},{"type":"END"}],[{"type":"CHAR","value":[102]},{"type":"RULE_REF","value":1},{"type":"ALT"},{"type":"END"}]]}],["repeating-at-least-once","root ::= \"f\"+",{"symbolIds":[["root",0],["root_1",1]],"rules":[[{"type":"RULE_REF","value":1},{"type":"END"}],[{"type":"CHAR","value":[102]},{"type":"RULE_REF","value":1},{"type":"ALT"},{"type":"CHAR","value":[102]},{"type":"END"}]]}],["character range with optional repeating","root ::= [a-zA-Z0-9]*",{"symbolIds":[["root",0],["root_1",1]],"rules":[[{"type":"RULE_REF","value":1},{"type":"END"}],[{"type":"CHAR","value":[97]},{"type":"CHAR_RNG_UPPER","value":122},{"type":"CHAR_ALT","value":65},{"type":"CHAR_RNG_UPPER","value":90},{"type":"CHAR_ALT","value":48},{"type":"CHAR_RNG_UPPER","value":57},{"type":"RULE_REF","value":1},{"type":"ALT"},{"type":"END"}]]}],["grouping-repeating","root ::= \"f\" (\"b\" | \"a\")*",{"symbolIds":[["root",0],["root_1",1],["root_2",2]],"rules":[[{"type":"CHAR","value":[102]},{"type":"RULE_REF","value":2},{"type":"END"}],[{"type":"CHAR","value":[98]},{"type":"ALT"},{"type":"CHAR","value":[97]},{"type":"END"}],[{"type":"RULE_REF","value":1},{"type":"RULE_REF","value":2},{"type":"ALT"},{"type":"END"}]]}],["negation","root ::= [^\\n]",{"symbolIds":[["root",0]],"rules":[[{"type":"CHAR_NOT","value":[10]},{"type":"END"}]]}],["negation of range","root ::= [^0-9]",{"symbolIds":[["root",0]],"rules":[[{"type":"CHAR_NOT","value":[48]},{"type":"CHAR_RNG_UPPER","value":57},{"type":"END"}]]}],["negation with after","root ::= [^\\n]+ \"\\n\"",{"symbolIds":[["root",0],["root_1",1]],"rules":[[{"type":"RULE_REF","value":1},{"type":"CHAR","value":[10]},{"type":"END"}],[{"type":"CHAR_NOT","value":[10]},{"type":"RULE_REF","value":1},{"type":"ALT"},{"type":"CHAR_NOT","value":[10]},{"type":"END"}]]}],["longer negation","root ::= \"\\\"\" ( [^\"abcdefgh])*",{"symbolIds":[["root",0],["root_1",1],["root_2",2]],"rules":[[{"type":"CHAR","value":[34]},{"type":"RULE_REF","value":2},{"type":"END"}],[{"type":"CHAR_NOT","value":[34]},{"type":"CHAR_ALT","value":97},{"type":"CHAR_ALT","value":98},{"type":"CHAR_ALT","value":99},{"type":"CHAR_ALT","value":100},{"type":"CHAR_ALT","value":101},{"type":"CHAR_ALT","value":102},{"type":"CHAR_ALT","value":103},{"type":"CHAR_ALT","value":104},{"type":"END"}],[{"type":"RULE_REF","value":1},{"type":"RULE_REF","value":2},{"type":"ALT"},{"type":"END"}]]}],["longer negation with a range","root ::= \"\\\"\" ( [^\"abcdefghA-Z])*",{"symbolIds":[["root",0],["root_1",1],["root_2",2]],"rules":[[{"type":"CHAR","value":[34]},{"type":"RULE_REF","value":2},{"type":"END"}],[{"type":"CHAR_NOT","value":[34]},{"type":"CHAR_ALT","value":97},{"type":"CHAR_ALT","value":98},{"type":"CHAR_ALT","value":99},{"type":"CHAR_ALT","value":100},{"type":"CHAR_ALT","value":101},{"type":"CHAR_ALT","value":102},{"type":"CHAR_ALT","value":103},{"type":"CHAR_ALT","value":104},{"type":"CHAR_ALT","value":65},{"type":"CHAR_RNG_UPPER","value":90},{"type":"END"}],[{"type":"RULE_REF","value":1},{"type":"RULE_REF","value":2},{"type":"ALT"},{"type":"END"}]]}],["escaped 8-bit unicode char","root ::= \"\\x2A\"",{"symbolIds":[["root",0]],"rules":[[{"type":"CHAR","value":[42]},{"type":"END"}]]}],["escaped 16-bit unicode char","root ::= \"\\u006F\"",{"symbolIds":[["root",0]],"rules":[[{"type":"CHAR","value":[111]},{"type":"END"}]]}],["escaped 32-bit unicode char","root ::= \"\\U0001F4A9\"",{"symbolIds":[["root",0]],"rules":[[{"type":"CHAR","value":[128169]},{"type":"END"}]]}],["escaped tab char","root ::= \"\\t\"",{"symbolIds":[["root",0]],"rules":[[{"type":"CHAR","value":[9]},{"type":"END"}]]}],["escaped new line char","root ::= \"\\n\"",{"symbolIds":[["root",0]],"rules":[[{"type":"CHAR","value":[10]},{"type":"END"}]]}],["escaped \r char","root ::= \"\\r\"",{"symbolIds":[["root",0]],"rules":[[{"type":"CHAR","value":[13]},{"type":"END"}]]}],["escaped quote char","root ::= \"\\\"\"",{"symbolIds":[["root",0]],"rules":[[{"type":"CHAR","value":[34]},{"type":"END"}]]}],["escaped [ char","root ::= \"\\[\"",{"symbolIds":[["root",0]],"rules":[[{"type":"CHAR","value":[91]},{"type":"END"}]]}],["escaped ] char","root ::= \"\\]\"",{"symbolIds":[["root",0]],"rules":[[{"type":"CHAR","value":[93]},{"type":"END"}]]}],["escaped \\ char","root ::= \"\\\\\"",{"symbolIds":[["root",0]],"rules":[[{"type":"CHAR","value":[92]},{"type":"END"}]]}],["simple arithmetic","\\nroot ::= (expr \"=\" term \"\\n\")+\\nexpr ::= term ([-+*/] term)*\\nterm ::= [0-9]+\\n",{"symbolIds":[["root",0],["root_1",1],["expr",2],["term",3],["root_4",4],["expr_5",5],["expr_6",6],["term_7",7]],"rules":[[{"type":"RULE_REF","value":4},{"type":"END"}],[{"type":"RULE_REF","value":2},{"type":"CHAR","value":[61]},{"type":"RULE_REF","value":3},{"type":"CHAR","value":[10]},{"type":"END"}],[{"type":"RULE_REF","value":3},{"type":"RULE_REF","value":6},{"type":"END"}],[{"type":"RULE_REF","value":7},{"type":"END"}],[{"type":"RULE_REF","value":1},{"type":"RULE_REF","value":4},{"type":"ALT"},{"type":"RULE_REF","value":1},{"type":"END"}],[{"type":"CHAR","value":[45]},{"type":"CHAR_ALT","value":43},{"type":"CHAR_ALT","value":42},{"type":"CHAR_ALT","value":47},{"type":"RULE_REF","value":3},{"type":"END"}],[{"type":"RULE_REF","value":5},{"type":"RULE_REF","value":6},{"type":"ALT"},{"type":"END"}],[{"type":"CHAR","value":[48]},{"type":"CHAR_RNG_UPPER","value":57},{"type":"RULE_REF","value":7},{"type":"ALT"},{"type":"CHAR","value":[48]},{"type":"CHAR_RNG_UPPER","value":57},{"type":"END"}]]}],["ranges with chars","\\nroot ::= [a-z0-9_]*\\n",{"symbolIds":[["root",0],["root_1",1]],"rules":[[{"type":"RULE_REF","value":1},{"type":"END"}],[{"type":"CHAR","value":[97]},{"type":"CHAR_RNG_UPPER","value":122},{"type":"CHAR_ALT","value":48},{"type":"CHAR_RNG_UPPER","value":57},{"type":"CHAR_ALT","value":95},{"type":"RULE_REF","value":1},{"type":"ALT"},{"type":"END"}]]}],["nested ranges with chars","\\nroot ::= [a-z] [a-z0-9_]*\\n",{"symbolIds":[["root",0],["root_1",1]],"rules":[[{"type":"CHAR","value":[97]},{"type":"CHAR_RNG_UPPER","value":122},{"type":"RULE_REF","value":1},{"type":"END"}],[{"type":"CHAR","value":[97]},{"type":"CHAR_RNG_UPPER","value":122},{"type":"CHAR_ALT","value":48},{"type":"CHAR_RNG_UPPER","value":57},{"type":"CHAR_ALT","value":95},{"type":"RULE_REF","value":1},{"type":"ALT"},{"type":"END"}]]}],["expression with nested range with chars","\\nroot ::= ident\\nident ::= [a-z] [a-z0-9_]* ws\\nws ::= [ \\t\\n]*\\n",{"symbolIds":[["root",0],["ident",1],["ident_2",2],["ws",3],["ws_4",4]],"rules":[[{"type":"RULE_REF","value":1},{"type":"END"}],[{"type":"CHAR","value":[97]},{"type":"CHAR_RNG_UPPER","value":122},{"type":"RULE_REF","value":2},{"type":"RULE_REF","value":3},{"type":"END"}],[{"type":"CHAR","value":[97]},{"type":"CHAR_RNG_UPPER","value":122},{"type":"CHAR_ALT","value":48},{"type":"CHAR_RNG_UPPER","value":57},{"type":"CHAR_ALT","value":95},{"type":"RULE_REF","value":2},{"type":"ALT"},{"type":"END"}],[{"type":"RULE_REF","value":4},{"type":"END"}],[{"type":"CHAR","value":[32]},{"type":"CHAR_ALT","value":9},{"type":"CHAR_ALT","value":10},{"type":"RULE_REF","value":4},{"type":"ALT"},{"type":"END"}]]}],["lots of escapes","root ::= \"\\x2A\" \"\\u006F\" \"\\U0001F4A9\" \"\\t\" \"\\n\" \"\\r\" \"\\\"\" \"\\[\" \"\\]\" \"\\\\\"",{"symbolIds":[["root",0]],"rules":[[{"type":"CHAR","value":[42]},{"type":"CHAR","value":[111]},{"type":"CHAR","value":[128169]},{"type":"CHAR","value":[9]},{"type":"CHAR","value":[10]},{"type":"CHAR","value":[13]},{"type":"CHAR","value":[34]},{"type":"CHAR","value":[91]},{"type":"CHAR","value":[93]},{"type":"CHAR","value":[92]},{"type":"END"}]]}],["lots of escape and alternate escapes","root ::= \"\\x2A\" \"\\u006F\" \"\\U0001F4A9\" \"\\t\" \"\\n\" \"\\r\" \"\\\"\" \"\\[\" \"\\]\" \"\\\\\" (\"\\x2A\" | \"\\u006F\" | \"\\U0001F4A9\" | \"\\t\" | \"\\n\" | \"\\r\" | \"\\\"\" | \"\\[\" | \"\\]\" | \"\\\\\" )",{"symbolIds":[["root",0],["root_1",1]],"rules":[[{"type":"CHAR","value":[42]},{"type":"CHAR","value":[111]},{"type":"CHAR","value":[128169]},{"type":"CHAR","value":[9]},{"type":"CHAR","value":[10]},{"type":"CHAR","value":[13]},{"type":"CHAR","value":[34]},{"type":"CHAR","value":[91]},{"type":"CHAR","value":[93]},{"type":"CHAR","value":[92]},{"type":"RULE_REF","value":1},{"type":"END"}],[{"type":"CHAR","value":[42]},{"type":"ALT"},{"type":"CHAR","value":[111]},{"type":"ALT"},{"type":"CHAR","value":[128169]},{"type":"ALT"},{"type":"CHAR","value":[9]},{"type":"ALT"},{"type":"CHAR","value":[10]},{"type":"ALT"},{"type":"CHAR","value":[13]},{"type":"ALT"},{"type":"CHAR","value":[34]},{"type":"ALT"},{"type":"CHAR","value":[91]},{"type":"ALT"},{"type":"CHAR","value":[93]},{"type":"ALT"},{"type":"CHAR","value":[92]},{"type":"END"}]]}],["arithmetic","\\nroot ::= (expr \"=\" ws term \"\\n\")+\\nexpr ::= term ([-+*/] term)*\\nterm ::= ident | num | \"(\" ws expr \")\" ws\\nident ::= [a-z] [a-z0-9_]* ws\\nnum ::= [0-9]+ ws\\nws ::= [ \\t\\n]*\\n",{"symbolIds":[["root",0],["root_1",1],["expr",2],["ws",3],["term",4],["root_5",5],["expr_6",6],["expr_7",7],["ident",8],["num",9],["ident_10",10],["num_11",11],["ws_12",12]],"rules":[[{"type":"RULE_REF","value":5},{"type":"END"}],[{"type":"RULE_REF","value":2},{"type":"CHAR","value":[61]},{"type":"RULE_REF","value":3},{"type":"RULE_REF","value":4},{"type":"CHAR","value":[10]},{"type":"END"}],[{"type":"RULE_REF","value":4},{"type":"RULE_REF","value":7},{"type":"END"}],[{"type":"RULE_REF","value":12},{"type":"END"}],[{"type":"RULE_REF","value":8},{"type":"ALT"},{"type":"RULE_REF","value":9},{"type":"ALT"},{"type":"CHAR","value":[40]},{"type":"RULE_REF","value":3},{"type":"RULE_REF","value":2},{"type":"CHAR","value":[41]},{"type":"RULE_REF","value":3},{"type":"END"}],[{"type":"RULE_REF","value":1},{"type":"RULE_REF","value":5},{"type":"ALT"},{"type":"RULE_REF","value":1},{"type":"END"}],[{"type":"CHAR","value":[45]},{"type":"CHAR_ALT","value":43},{"type":"CHAR_ALT","value":42},{"type":"CHAR_ALT","value":47},{"type":"RULE_REF","value":4},{"type":"END"}],[{"type":"RULE_REF","value":6},{"type":"RULE_REF","value":7},{"type":"ALT"},{"type":"END"}],[{"type":"CHAR","value":[97]},{"type":"CHAR_RNG_UPPER","value":122},{"type":"RULE_REF","value":10},{"type":"RULE_REF","value":3},{"type":"END"}],[{"type":"RULE_REF","value":11},{"type":"RULE_REF","value":3},{"type":"END"}],[{"type":"CHAR","value":[97]},{"type":"CHAR_RNG_UPPER","value":122},{"type":"CHAR_ALT","value":48},{"type":"CHAR_RNG_UPPER","value":57},{"type":"CHAR_ALT","value":95},{"type":"RULE_REF","value":10},{"type":"ALT"},{"type":"END"}],[{"type":"CHAR","value":[48]},{"type":"CHAR_RNG_UPPER","value":57},{"type":"RULE_REF","value":11},{"type":"ALT"},{"type":"CHAR","value":[48]},{"type":"CHAR_RNG_UPPER","value":57},{"type":"END"}],[{"type":"CHAR","value":[32]},{"type":"CHAR_ALT","value":9},{"type":"CHAR_ALT","value":10},{"type":"RULE_REF","value":12},{"type":"ALT"},{"type":"END"}]]}],["json.gbnf (string)","\\nroot ::=\\n\"\\\"\" (\\n[^\"\\\\\\x00-\\x1F] |\\n\"\\\\\" ([\"\\\\/bfnrt] | \"u\" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes\\n)* \"\\\"\"\\n",{"symbolIds":[["root",0],["root_1",1],["root_2",2],["root_3",3]],"rules":[[{"type":"CHAR","value":[34]},{"type":"RULE_REF","value":3},{"type":"CHAR","value":[34]},{"type":"END"}],[{"type":"CHAR_NOT","value":[34]},{"type":"CHAR_ALT","value":92},{"type":"CHAR_ALT","value":127},{"type":"CHAR_ALT","value":0},{"type":"CHAR_RNG_UPPER","value":31},{"type":"ALT"},{"type":"CHAR","value":[92]},{"type":"RULE_REF","value":2},{"type":"END"}],[{"type":"CHAR","value":[34]},{"type":"CHAR_ALT","value":92},{"type":"CHAR_ALT","value":47},{"type":"CHAR_ALT","value":98},{"type":"CHAR_ALT","value":102},{"type":"CHAR_ALT","value":110},{"type":"CHAR_ALT","value":114},{"type":"CHAR_ALT","value":116},{"type":"ALT"},{"type":"CHAR","value":[117]},{"type":"CHAR","value":[48]},{"type":"CHAR_RNG_UPPER","value":57},{"type":"CHAR_ALT","value":97},{"type":"CHAR_RNG_UPPER","value":102},{"type":"CHAR_ALT","value":65},{"type":"CHAR_RNG_UPPER","value":70},{"type":"CHAR","value":[48]},{"type":"CHAR_RNG_UPPER","value":57},{"type":"CHAR_ALT","value":97},{"type":"CHAR_RNG_UPPER","value":102},{"type":"CHAR_ALT","value":65},{"type":"CHAR_RNG_UPPER","value":70},{"type":"CHAR","value":[48]},{"type":"CHAR_RNG_UPPER","value":57},{"type":"CHAR_ALT","value":97},{"type":"CHAR_RNG_UPPER","value":102},{"type":"CHAR_ALT","value":65},{"type":"CHAR_RNG_UPPER","value":70},{"type":"CHAR","value":[48]},{"type":"CHAR_RNG_UPPER","value":57},{"type":"CHAR_ALT","value":97},{"type":"CHAR_RNG_UPPER","value":102},{"type":"CHAR_ALT","value":65},{"type":"CHAR_RNG_UPPER","value":70},{"type":"END"}],[{"type":"RULE_REF","value":1},{"type":"RULE_REF","value":3},{"type":"ALT"},{"type":"END"}]]}],["json.gbnf (full)","\\nroot ::= object\\nvalue ::= object | array | string | number | (\"true\" | \"false\" | \"null\") ws\\n\\nobject ::=\\n\"{\" ws (\\nstring \":\" ws value\\n(\",\" ws string \":\" ws value)*\\n)? \"}\" ws\\n\\narray ::=\\n\"[\" ws (\\nvalue\\n(\",\" ws value)*\\n)? \"]\" ws\\n\\nstring ::=\\n\"\\\"\" (\\n[^\"\\\\\\x00-\\x1F] |\\n\"\\\\\" ([\"\\\\/bfnrt] | \"u\" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F]) # escapes\\n)* \"\\\"\" ws\\n\\nnumber ::= (\"-\"? ([0-9] | [1-9] [0-9]*)) (\".\" [0-9]+)? ([eE] [-+]? [0-9]+)? ws\\n\\n# Optional space: by convention, applied in this grammar after literal chars when allowed\\nws ::= ([ \\t\\n] ws)?\\n\\n",{"symbolIds":[["root",0],["object",1],["value",2],["array",3],["string",4],["number",5],["value_6",6],["ws",7],["object_8",8],["object_9",9],["object_10",10],["object_11",11],["array_12",12],["array_13",13],["array_14",14],["array_15",15],["string_16",16],["string_17",17],["string_18",18],["number_19",19],["number_20",20],["number_21",21],["number_22",22],["number_23",23],["number_24",24],["number_25",25],["number_26",26],["number_27",27],["number_28",28],["number_29",29],["ws_30",30],["ws_31",31]],"rules":[[{"type":"RULE_REF","value":1},{"type":"END"}],[{"type":"CHAR","value":[123]},{"type":"RULE_REF","value":7},{"type":"RULE_REF","value":11},{"type":"CHAR","value":[125]},{"type":"RULE_REF","value":7},{"type":"END"}],[{"type":"RULE_REF","value":1},{"type":"ALT"},{"type":"RULE_REF","value":3},{"type":"ALT"},{"type":"RULE_REF","value":4},{"type":"ALT"},{"type":"RULE_REF","value":5},{"type":"ALT"},{"type":"RULE_REF","value":6},{"type":"RULE_REF","value":7},{"type":"END"}],[{"type":"CHAR","value":[91]},{"type":"RULE_REF","value":7},{"type":"RULE_REF","value":15},{"type":"CHAR","value":[93]},{"type":"RULE_REF","value":7},{"type":"END"}],[{"type":"CHAR","value":[34]},{"type":"RULE_REF","value":18},{"type":"CHAR","value":[34]},{"type":"RULE_REF","value":7},{"type":"END"}],[{"type":"RULE_REF","value":19},{"type":"RULE_REF","value":25},{"type":"RULE_REF","value":29},{"type":"RULE_REF","value":7},{"type":"END"}],[{"type":"CHAR","value":[116]},{"type":"CHAR","value":[114]},{"type":"CHAR","value":[117]},{"type":"CHAR","value":[101]},{"type":"ALT"},{"type":"CHAR","value":[102]},{"type":"CHAR","value":[97]},{"type":"CHAR","value":[108]},{"type":"CHAR","value":[115]},{"type":"CHAR","value":[101]},{"type":"ALT"},{"type":"CHAR","value":[110]},{"type":"CHAR","value":[117]},{"type":"CHAR","value":[108]},{"type":"CHAR","value":[108]},{"type":"END"}],[{"type":"RULE_REF","value":31},{"type":"END"}],[{"type":"RULE_REF","value":4},{"type":"CHAR","value":[58]},{"type":"RULE_REF","value":7},{"type":"RULE_REF","value":2},{"type":"RULE_REF","value":10},{"type":"END"}],[{"type":"CHAR","value":[44]},{"type":"RULE_REF","value":7},{"type":"RULE_REF","value":4},{"type":"CHAR","value":[58]},{"type":"RULE_REF","value":7},{"type":"RULE_REF","value":2},{"type":"END"}],[{"type":"RULE_REF","value":9},{"type":"RULE_REF","value":10},{"type":"ALT"},{"type":"END"}],[{"type":"RULE_REF","value":8},{"type":"ALT"},{"type":"END"}],[{"type":"RULE_REF","value":2},{"type":"RULE_REF","value":14},{"type":"END"}],[{"type":"CHAR","value":[44]},{"type":"RULE_REF","value":7},{"type":"RULE_REF","value":2},{"type":"END"}],[{"type":"RULE_REF","value":13},{"type":"RULE_REF","value":14},{"type":"ALT"},{"type":"END"}],[{"type":"RULE_REF","value":12},{"type":"ALT"},{"type":"END"}],[{"type":"CHAR_NOT","value":[34]},{"type":"CHAR_ALT","value":92},{"type":"CHAR_ALT","value":127},{"type":"CHAR_ALT","value":0},{"type":"CHAR_RNG_UPPER","value":31},{"type":"ALT"},{"type":"CHAR","value":[92]},{"type":"RULE_REF","value":17},{"type":"END"}],[{"type":"CHAR","value":[34]},{"type":"CHAR_ALT","value":92},{"type":"CHAR_ALT","value":47},{"type":"CHAR_ALT","value":98},{"type":"CHAR_ALT","value":102},{"type":"CHAR_ALT","value":110},{"type":"CHAR_ALT","value":114},{"type":"CHAR_ALT","value":116},{"type":"ALT"},{"type":"CHAR","value":[117]},{"type":"CHAR","value":[48]},{"type":"CHAR_RNG_UPPER","value":57},{"type":"CHAR_ALT","value":97},{"type":"CHAR_RNG_UPPER","value":102},{"type":"CHAR_ALT","value":65},{"type":"CHAR_RNG_UPPER","value":70},{"type":"CHAR","value":[48]},{"type":"CHAR_RNG_UPPER","value":57},{"type":"CHAR_ALT","value":97},{"type":"CHAR_RNG_UPPER","value":102},{"type":"CHAR_ALT","value":65},{"type":"CHAR_RNG_UPPER","value":70},{"type":"CHAR","value":[48]},{"type":"CHAR_RNG_UPPER","value":57},{"type":"CHAR_ALT","value":97},{"type":"CHAR_RNG_UPPER","value":102},{"type":"CHAR_ALT","value":65},{"type":"CHAR_RNG_UPPER","value":70},{"type":"CHAR","value":[48]},{"type":"CHAR_RNG_UPPER","value":57},{"type":"CHAR_ALT","value":97},{"type":"CHAR_RNG_UPPER","value":102},{"type":"CHAR_ALT","value":65},{"type":"CHAR_RNG_UPPER","value":70},{"type":"END"}],[{"type":"RULE_REF","value":16},{"type":"RULE_REF","value":18},{"type":"ALT"},{"type":"END"}],[{"type":"RULE_REF","value":20},{"type":"RULE_REF","value":21},{"type":"END"}],[{"type":"CHAR","value":[45]},{"type":"ALT"},{"type":"END"}],[{"type":"CHAR","value":[48]},{"type":"CHAR_RNG_UPPER","value":57},{"type":"ALT"},{"type":"CHAR","value":[49]},{"type":"CHAR_RNG_UPPER","value":57},{"type":"RULE_REF","value":22},{"type":"END"}],[{"type":"CHAR","value":[48]},{"type":"CHAR_RNG_UPPER","value":57},{"type":"RULE_REF","value":22},{"type":"ALT"},{"type":"END"}],[{"type":"CHAR","value":[46]},{"type":"RULE_REF","value":24},{"type":"END"}],[{"type":"CHAR","value":[48]},{"type":"CHAR_RNG_UPPER","value":57},{"type":"RULE_REF","value":24},{"type":"ALT"},{"type":"CHAR","value":[48]},{"type":"CHAR_RNG_UPPER","value":57},{"type":"END"}],[{"type":"RULE_REF","value":23},{"type":"ALT"},{"type":"END"}],[{"type":"CHAR","value":[101]},{"type":"CHAR_ALT","value":69},{"type":"RULE_REF","value":27},{"type":"RULE_REF","value":28},{"type":"END"}],[{"type":"CHAR","value":[45]},{"type":"CHAR_ALT","value":43},{"type":"ALT"},{"type":"END"}],[{"type":"CHAR","value":[48]},{"type":"CHAR_RNG_UPPER","value":57},{"type":"RULE_REF","value":28},{"type":"ALT"},{"type":"CHAR","value":[48]},{"type":"CHAR_RNG_UPPER","value":57},{"type":"END"}],[{"type":"RULE_REF","value":26},{"type":"ALT"},{"type":"END"}],[{"type":"CHAR","value":[32]},{"type":"CHAR_ALT","value":9},{"type":"CHAR_ALT","value":10},{"type":"RULE_REF","value":7},{"type":"END"}],[{"type":"RULE_REF","value":30},{"type":"ALT"},{"type":"END"}]]}],["japanese","\\n# A probably incorrect grammar for Japanese\\nroot ::= jp-char+ ([ \\t\\n] jp-char+)*\\njp-char ::= hiragana | katakana | punctuation | cjk\\nhiragana ::= [ぁ-ゟ]\\nkatakana ::= [ァ-ヿ]\\npunctuation ::= [、-〾]\\ncjk ::= [一-鿿]\\n\\n",{"symbolIds":[["root",0],["jp-char",1],["root_2",2],["root_3",3],["root_4",4],["root_5",5],["hiragana",6],["katakana",7],["punctuation",8],["cjk",9]],"rules":[[{"type":"RULE_REF","value":2},{"type":"RULE_REF","value":5},{"type":"END"}],[{"type":"RULE_REF","value":6},{"type":"ALT"},{"type":"RULE_REF","value":7},{"type":"ALT"},{"type":"RULE_REF","value":8},{"type":"ALT"},{"type":"RULE_REF","value":9},{"type":"END"}],[{"type":"RULE_REF","value":1},{"type":"RULE_REF","value":2},{"type":"ALT"},{"type":"RULE_REF","value":1},{"type":"END"}],[{"type":"CHAR","value":[32]},{"type":"CHAR_ALT","value":9},{"type":"CHAR_ALT","value":10},{"type":"RULE_REF","value":4},{"type":"END"}],[{"type":"RULE_REF","value":1},{"type":"RULE_REF","value":4},{"type":"ALT"},{"type":"RULE_REF","value":1},{"type":"END"}],[{"type":"RULE_REF","value":3},{"type":"RULE_REF","value":5},{"type":"ALT"},{"type":"END"}],[{"type":"CHAR","value":[12353]},{"type":"CHAR_RNG_UPPER","value":12447},{"type":"END"}],[{"type":"CHAR","value":[12449]},{"type":"CHAR_RNG_UPPER","value":12543},{"type":"END"}],[{"type":"CHAR","value":[12289]},{"type":"CHAR_RNG_UPPER","value":12350},{"type":"END"}],[{"type":"CHAR","value":[19968]},{"type":"CHAR_RNG_UPPER","value":40959},{"type":"END"}]]}]]
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/data/validate_grammar_err.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
[["",0,"No rules were found"],["root = \"foo\"",5,"Expecting ::= at 5"],["root ::= foo\nfoo := \"foo\"\n ",17,"Expecting ::= at 17"],["root ::= foo",9,"Undefined rule identifier \"foo\""],["root ::= foo\nbar ::= \"bar\"\n ",9,"Undefined rule identifier \"foo\""],["root ::= foo\nfoo ::= baz\nbar ::= \"bar\"\n ",21,"Undefined rule identifier \"baz\""],["root ::= foo ::= bar",13,"Expecting newline or end at 13"],["root ::= ([a-z]\nfoo ::= \"foo\"\n ",20,"Expecting ')' at 20"]]
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/data/validate_grammar_ok.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
["root ::= \"foo\"","root ::= \"foo\" | \"bar\"","root ::= (\"foo\" | \"bar\")","root ::= (\"foo\" | \"bar\")?","root ::= (\"foo\" | \"bar\")*","root ::= (\"foo\" | \"bar\")+","root ::= [a-z]","root ::= [a-zA-Z]","root ::= [a-zA-Z0-9]","root ::= [a-zA-Z0-9]*","root ::= [a-zA-Z0-9]?","root ::= [a-z]+","root ::= [a-zA-Z0-9]+","root ::= ([a-zA-Z0-9])*","root ::= ([a-zA-Z0-9])?","root ::= ([a-zA-Z0-9])+","\n root ::= foo\n foo ::= \"foo\"","\n root ::= foo\n foo ::= \"foo\" | \"bar\" | ([a-z])?\n "]
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/data/validate_input_err.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
[["root ::= \"foo\"","2",0],["root ::= \"foo\"","b",0],["root ::= \"foo\"","f1",1],["root ::= \"foo\"","fo1",2],["root ::= \"foo\"","fooo",3],["root ::= \"foo\"","fooooooo",3],["root ::= \"foo\" | \"bar\"","1",0],["root ::= \"foo\" | \"bar\"","z",0],["root ::= \"foo\" | \"bar\"","f1",1],["root ::= \"foo\" | \"bar\"","b1",1],["root ::= \"foo\" | \"bar\"","fo1",2],["root ::= \"foo\" | \"bar\"","ba1",2],["root ::= \"foo\" | \"bar\"","fooo",3],["root ::= \"foo\" | \"bar\"","barrr",3],["root ::= \"foo\" | \"bar\" | \"baz\"","1",0],["root ::= \"foo\" | \"bar\" | \"baz\"","z",0],["root ::= \"foo\" | \"bar\" | \"baz\"","f1",1],["root ::= \"foo\" | \"bar\" | \"baz\"","b1",1],["root ::= \"foo\" | \"bar\" | \"baz\"","fo1",2],["root ::= \"foo\" | \"bar\" | \"baz\"","bal",2],["root ::= \"foo\" | \"bar\" | \"baz\"","fooo",3],["root ::= \"foo\" | \"bar\" | \"baz\"","bazrr",3],["root ::= [^a]","a",0],["root ::= [^abc]","b",0],["root ::= [^a-z]","z",0],["root ::= [^a-zA-Z]","X",0],["root ::= [^a-zA-Z0-9]","8",0],["\n root ::= foo\n foo ::= \"foo\"","1",0],["\n root ::= foo\n foo ::= \"foo\"","b",0],["\n root ::= foo\n foo ::= \"foo\"","f1",1],["\n root ::= foo\n foo ::= \"foo\"","fo1",2],["\n root ::= foo\n foo ::= \"foo\"","fooo",3],["\n root::=foo|\"bar\"\n foo::=\"foo\"","1",0],["\n root::=foo|\"bar\"\n foo::=\"foo\"","z",0],["\n root::=foo|\"bar\"\n foo::=\"foo\"","f1",1],["\n root::=foo|\"bar\"\n foo::=\"foo\"","b1",1],["\n root::=foo|\"bar\"\n foo::=\"foo\"","fo1",2],["\n root::=foo|\"bar\"\n foo::=\"foo\"","fooo",3],["\n root::=foo|\"bar\"\n foo::=\"foo\"","barr",3],["\n root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"","1",0],["\n root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"","z",0],["\n root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"","f1",1],["\n root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"","b1",1],["\n root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"","fo1",2],["\n root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"","fooo",3],["\n root::= foo | bar\n foo::=\"foo\"\n bar::=\"bar\"","barr",3],["\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],["\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],["\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],["\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],["\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],["\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],["\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],["\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],["root ::= [a-z]","A",0],["root ::= [a-z]","0",0],["root ::= [a-z]","az",1],["root ::= [a-z]?","A",0],["root ::= [a-z]?","0",0],["root ::= [a-z]?","az",1],["root ::= [a-z]+","A",0],["root ::= [a-z]+","0",0],["root ::= [a-z]+","az0",2],["root ::= [a-z]*","A",0],["root ::= [a-z]*","0",0],["root ::= [a-z]*","az0",2],["root ::= ( [^abcdefgh] | [b-z])*","a",0]]
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/data/validate_input_ok.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
[["root ::= \"foo\"","f"]]
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/helpers.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
DATA_DIR = Path(__file__).parent / 'data'
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def load(name: str):
|
| 8 |
+
"""Load a table of test cases extracted from the reference implementation's test suite."""
|
| 9 |
+
with open(DATA_DIR / f'{name}.json', encoding='utf-8') as f:
|
| 10 |
+
return json.load(f)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def unescape(string: str) -> str:
|
| 14 |
+
return string.replace('\\n', '\n').replace('\\t', '\t')
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/integration/__init__.py
ADDED
|
File without changes
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/integration/test_edge_cases.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from gbnf import GBNF, GrammarParseError, InputParseError, RuleType, isRange, is_range
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def test_accepts_code_points_as_input():
|
| 7 |
+
state = GBNF('root ::= "abc"')
|
| 8 |
+
assert list(state.add(97)) == [{'type': 'char', 'value': [98]}]
|
| 9 |
+
assert list(state.add([97, 98])) == [{'type': 'char', 'value': [99]}]
|
| 10 |
+
assert list(GBNF('root ::= "abc"', [97, 98, 99])) == [{'type': 'end'}]
|
| 11 |
+
assert list(GBNF('root ::= "abc"', initialString='ab')) == [{'type': 'char', 'value': [99]}]
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def test_rule_types_compare_equal_to_their_values():
|
| 15 |
+
assert RuleType.CHAR == 'char'
|
| 16 |
+
assert RuleType.CHAR_EXCLUDE == 'char_exclude'
|
| 17 |
+
assert RuleType.END == 'end'
|
| 18 |
+
assert isRange is is_range
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def test_rules_serialize_as_json():
|
| 22 |
+
import json
|
| 23 |
+
assert json.dumps(list(GBNF('root ::= [a-z]*')), sort_keys=True) == (
|
| 24 |
+
'[{"type": "char", "value": [[97, 122]]}, {"type": "end"}]'
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def test_characters_outside_the_basic_multilingual_plane_are_single_characters():
|
| 29 |
+
state = GBNF('root ::= "\\U0001F4A9" | "😀x"')
|
| 30 |
+
assert list(state) == [{'type': 'char', 'value': [0x1F4A9]}, {'type': 'char', 'value': [0x1F600]}]
|
| 31 |
+
assert list(state.add('😀')) == [{'type': 'char', 'value': [ord('x')]}]
|
| 32 |
+
assert list(state.add('\U0001F4A9')) == [{'type': 'end'}]
|
| 33 |
+
with pytest.raises(InputParseError) as excinfo:
|
| 34 |
+
GBNF('root ::= "😀x"').add('😀y')
|
| 35 |
+
assert str(excinfo.value).endswith('😀y\n ^')
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def test_long_repetitions_do_not_hit_the_recursion_limit():
|
| 39 |
+
state = GBNF('root ::= ([a-z] ws)*\nws ::= " "*')
|
| 40 |
+
for i in range(1500):
|
| 41 |
+
state = state.add('a' if i % 2 == 0 else ' ')
|
| 42 |
+
assert {'type': 'char', 'value': [[97, 122]]} in list(state)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
@pytest.mark.parametrize('grammar', [
|
| 46 |
+
'root ::= root "a" | "a"',
|
| 47 |
+
'root ::= foo\nfoo ::= root "a" | "b"',
|
| 48 |
+
'root ::= a root\na ::= "x"?',
|
| 49 |
+
'root ::= ("a"*)*',
|
| 50 |
+
])
|
| 51 |
+
def test_grammars_that_recurse_without_consuming_input_raise(grammar):
|
| 52 |
+
with pytest.raises(RecursionError):
|
| 53 |
+
GBNF(grammar)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def test_ambiguous_grammars_keep_every_alternative():
|
| 57 |
+
state = GBNF('root ::= "a" "b" | "a" "c"')
|
| 58 |
+
state = state.add('a')
|
| 59 |
+
assert list(state) == [{'type': 'char', 'value': [ord('b')]}, {'type': 'char', 'value': [ord('c')]}]
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def test_failed_additions_leave_the_state_usable():
|
| 63 |
+
state = GBNF('root ::= "ab"').add('a')
|
| 64 |
+
with pytest.raises(InputParseError):
|
| 65 |
+
state.add('x')
|
| 66 |
+
assert list(state.add('b')) == [{'type': 'end'}]
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def test_missing_and_undefined_rules_are_grammar_errors():
|
| 70 |
+
with pytest.raises(GrammarParseError, match='does not contain a root symbol'):
|
| 71 |
+
GBNF('foo ::= "a"')
|
| 72 |
+
with pytest.raises(GrammarParseError, match='Undefined rule identifier "bar"'):
|
| 73 |
+
GBNF('root ::= "a"\nfoo ::= bar*')
|
| 74 |
+
with pytest.raises(GrammarParseError, match=r'Invalid hex digits'):
|
| 75 |
+
GBNF('root ::= "\\xZZ"')
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def test_the_error_hierarchy():
|
| 79 |
+
assert issubclass(GrammarParseError, ValueError)
|
| 80 |
+
assert issubclass(InputParseError, ValueError)
|
| 81 |
+
assert InputParseError('a', 0).name == 'InputParseError'
|
| 82 |
+
assert GrammarParseError('', 0, 'r').name == 'GrammarParseError'
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/integration/test_iteration.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from gbnf import GBNF, InputParseError
|
| 4 |
+
from tests.helpers import load, unescape
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
@pytest.mark.parametrize('grammar,expected', load('iteration'))
|
| 8 |
+
def test_it_returns_parse_state_for_a_grammar(grammar, expected):
|
| 9 |
+
assert list(GBNF(grammar)) == expected
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@pytest.mark.parametrize('grammar,input_,expected', load('iteration_initial'))
|
| 13 |
+
def test_it_returns_parse_state_for_a_grammar_and_initial_string(grammar, input_, expected):
|
| 14 |
+
state = GBNF(grammar)
|
| 15 |
+
state = state.add(input_)
|
| 16 |
+
assert list(state) == expected
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@pytest.mark.parametrize('grammar,input_,expected', load('iteration_initial'))
|
| 20 |
+
def test_it_accepts_the_initial_string_as_an_argument(grammar, input_, expected):
|
| 21 |
+
assert list(GBNF(grammar, input_)) == expected
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
@pytest.mark.parametrize('grammar,starting,additional', load('iteration_additional_errors'))
|
| 25 |
+
def test_it_throws_if_encountering_additional_invalid_input(grammar, starting, additional):
|
| 26 |
+
graph = GBNF(grammar, starting)
|
| 27 |
+
with pytest.raises(InputParseError):
|
| 28 |
+
graph.add(additional)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
@pytest.mark.parametrize('grammar,starting,additional,expected', load('iteration_additional'))
|
| 32 |
+
def test_it_parses_a_grammar_with_starting_and_additional_strings(grammar, starting, additional, expected):
|
| 33 |
+
state = GBNF(grammar, starting)
|
| 34 |
+
state = state.add(additional)
|
| 35 |
+
assert list(state) == expected
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
@pytest.mark.parametrize('error_for_most_recent_input', [False, True])
|
| 39 |
+
def test_it_throws_a_particular_error(error_for_most_recent_input):
|
| 40 |
+
grammar = 'root ::= "bar"'
|
| 41 |
+
state = GBNF(grammar)
|
| 42 |
+
state = state.add('b')
|
| 43 |
+
state = state.add('a')
|
| 44 |
+
with pytest.raises(InputParseError) as excinfo:
|
| 45 |
+
state.add('z')
|
| 46 |
+
err = excinfo.value
|
| 47 |
+
expected_err = InputParseError('z', 0, 'ba')
|
| 48 |
+
if error_for_most_recent_input:
|
| 49 |
+
assert err.error_for_most_recent_input == expected_err.error_for_most_recent_input
|
| 50 |
+
else:
|
| 51 |
+
assert str(err) == str(expected_err)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
@pytest.mark.parametrize('name,test_case,grammar', load('grammars'))
|
| 55 |
+
def test_it_parses_a_known_valid_grammar(name, test_case, grammar):
|
| 56 |
+
state = GBNF(unescape(grammar))
|
| 57 |
+
for char in unescape(test_case):
|
| 58 |
+
state = state.add(char)
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/integration/test_validation.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from gbnf import GBNF, GrammarParseError, InputParseError
|
| 4 |
+
from tests.helpers import load
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
@pytest.mark.parametrize('grammar', load('validate_grammar_ok'))
|
| 8 |
+
def test_it_parses_a_grammar(grammar):
|
| 9 |
+
assert GBNF(grammar)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@pytest.mark.parametrize('grammar,error_pos,error_reason', load('validate_grammar_err'))
|
| 13 |
+
def test_it_reports_an_error_for_an_invalid_grammar(grammar, error_pos, error_reason):
|
| 14 |
+
expected = GrammarParseError(grammar, error_pos, error_reason)
|
| 15 |
+
with pytest.raises(GrammarParseError) as excinfo:
|
| 16 |
+
GBNF(grammar)
|
| 17 |
+
assert str(excinfo.value) == str(expected)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@pytest.mark.parametrize('grammar,input_', load('validate_input_ok'))
|
| 21 |
+
def test_it_parses_a_grammar_and_input(grammar, input_):
|
| 22 |
+
assert GBNF(grammar, input_)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@pytest.mark.parametrize('grammar,input_,error_pos', load('validate_input_err'))
|
| 26 |
+
def test_it_reports_an_error_for_an_invalid_input(grammar, input_, error_pos):
|
| 27 |
+
expected = InputParseError(input_, error_pos, '')
|
| 28 |
+
with pytest.raises(InputParseError) as excinfo:
|
| 29 |
+
graph = GBNF(grammar)
|
| 30 |
+
graph.add(input_)
|
| 31 |
+
assert str(excinfo.value) == str(expected)
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/unit/__init__.py
ADDED
|
File without changes
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/unit/test_build_rule_stack.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from gbnf.grammar_graph.rule_ref import RuleRef
|
| 4 |
+
from gbnf.grammar_graph.types import RuleType
|
| 5 |
+
from gbnf.grammar_parser.build_rule_stack import build_rule_stack
|
| 6 |
+
from gbnf.rules_builder.types import InternalRuleType
|
| 7 |
+
from tests.helpers import load
|
| 8 |
+
|
| 9 |
+
CHAR = InternalRuleType.CHAR
|
| 10 |
+
CHAR_NOT = InternalRuleType.CHAR_NOT
|
| 11 |
+
CHAR_ALT = InternalRuleType.CHAR_ALT
|
| 12 |
+
RNG = InternalRuleType.CHAR_RNG_UPPER
|
| 13 |
+
REF = InternalRuleType.RULE_REF
|
| 14 |
+
ALT = InternalRuleType.ALT
|
| 15 |
+
END = InternalRuleType.END
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def r(type_, value=None):
|
| 19 |
+
return {'type': type_} if value is None else {'type': type_, 'value': value}
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def refs(stack):
|
| 23 |
+
"""Rule refs in the reference tables are bare `{value}` objects."""
|
| 24 |
+
return [[RuleRef(rule['value']) if set(rule) == {'value'} else rule for rule in path] for path in stack]
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def test_single_path():
|
| 28 |
+
assert build_rule_stack([r(CHAR, [120])]) == [[r(RuleType.CHAR, [120]), r(RuleType.END)]]
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def test_two_alternate_paths():
|
| 32 |
+
assert build_rule_stack([r(CHAR, [120]), r(ALT), r(CHAR, [121])]) == [
|
| 33 |
+
[r(RuleType.CHAR, [120]), r(RuleType.END)],
|
| 34 |
+
[r(RuleType.CHAR, [121]), r(RuleType.END)],
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def test_three_alternate_paths():
|
| 39 |
+
assert build_rule_stack([r(CHAR, [120]), r(ALT), r(CHAR, [121]), r(ALT), r(CHAR, [122])]) == [
|
| 40 |
+
[r(RuleType.CHAR, [120]), r(RuleType.END)],
|
| 41 |
+
[r(RuleType.CHAR, [121]), r(RuleType.END)],
|
| 42 |
+
[r(RuleType.CHAR, [122]), r(RuleType.END)],
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def test_char_not():
|
| 47 |
+
assert build_rule_stack([r(CHAR_NOT, [120]), r(ALT), r(CHAR_NOT, [121]), r(ALT), r(CHAR_NOT, [122])]) == [
|
| 48 |
+
[r(RuleType.CHAR_EXCLUDE, [120]), r(RuleType.END)],
|
| 49 |
+
[r(RuleType.CHAR_EXCLUDE, [121]), r(RuleType.END)],
|
| 50 |
+
[r(RuleType.CHAR_EXCLUDE, [122]), r(RuleType.END)],
|
| 51 |
+
]
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def test_mixed_char_and_char_not():
|
| 55 |
+
assert build_rule_stack([r(CHAR_NOT, [120]), r(ALT), r(CHAR, [121]), r(ALT), r(CHAR_NOT, [122])]) == [
|
| 56 |
+
[r(RuleType.CHAR_EXCLUDE, [120]), r(RuleType.END)],
|
| 57 |
+
[r(RuleType.CHAR, [121]), r(RuleType.END)],
|
| 58 |
+
[r(RuleType.CHAR_EXCLUDE, [122]), r(RuleType.END)],
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def test_char_not_with_two_characters_and_a_range():
|
| 63 |
+
assert build_rule_stack([r(CHAR_NOT, [120]), r(CHAR_ALT, 121), r(CHAR_ALT, 122), r(RNG, 124)]) == [
|
| 64 |
+
[r(RuleType.CHAR_EXCLUDE, [120, 121, [122, 124]]), r(RuleType.END)],
|
| 65 |
+
]
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
@pytest.mark.parametrize('table', range(4))
|
| 69 |
+
def test_ranges_and_modifiers(table):
|
| 70 |
+
for _grammar, input_, expected in load(f'build_rule_stack_{table}'):
|
| 71 |
+
assert build_rule_stack(input_) == refs(expected), _grammar
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def test_char_with_range():
|
| 75 |
+
assert build_rule_stack([
|
| 76 |
+
r(CHAR, [97]), r(RNG, 122), r(CHAR_ALT, 65), r(RNG, 90), r(CHAR_ALT, 95), r(END),
|
| 77 |
+
]) == [[r(RuleType.CHAR, [[97, 122], [65, 90], 95]), r(RuleType.END)]]
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def test_rule_ref_stack_for_a_repeated_whitespace_rule():
|
| 81 |
+
assert build_rule_stack([
|
| 82 |
+
r(CHAR, [32]), r(CHAR_ALT, 92), r(CHAR_ALT, 110), r(REF, 4), r(ALT), r(END),
|
| 83 |
+
]) == [
|
| 84 |
+
[r(RuleType.CHAR, [32, 92, 110]), RuleRef(4), r(RuleType.END)],
|
| 85 |
+
[r(RuleType.END)],
|
| 86 |
+
]
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def test_rule_ref_stack_for_a_repeated_range():
|
| 90 |
+
assert build_rule_stack([
|
| 91 |
+
r(CHAR, [97]), r(RNG, 122), r(CHAR_ALT, 65), r(RNG, 90), r(REF, 1), r(ALT),
|
| 92 |
+
r(CHAR, [97]), r(RNG, 122), r(CHAR_ALT, 65), r(RNG, 90), r(END),
|
| 93 |
+
]) == [
|
| 94 |
+
[r(RuleType.CHAR, [[97, 122], [65, 90]]), RuleRef(1), r(RuleType.END)],
|
| 95 |
+
[r(RuleType.CHAR, [[97, 122], [65, 90]]), r(RuleType.END)],
|
| 96 |
+
]
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def test_does_not_mutate_its_input():
|
| 100 |
+
rules = [r(CHAR, [97]), r(RNG, 122), r(END)]
|
| 101 |
+
build_rule_stack(rules)
|
| 102 |
+
assert rules == [r(CHAR, [97]), r(RNG, 122), r(END)]
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
@pytest.mark.parametrize('rules,message', [
|
| 106 |
+
([r(ALT)], 'Encountered alt without anything before it'),
|
| 107 |
+
([r(CHAR_ALT, 1)], 'Encountered char alt, should be handled by above block: CHAR_ALT'),
|
| 108 |
+
([r(RNG, 1)], 'Unsupported rule type: CHAR_RNG_UPPER'),
|
| 109 |
+
])
|
| 110 |
+
def test_throws_on_malformed_input(rules, message):
|
| 111 |
+
with pytest.raises(Exception, match=message):
|
| 112 |
+
build_rule_stack(rules)
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/unit/test_grammar_graph_helpers.py
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
from gbnf.grammar_graph.colorize import Color, colorize
|
| 6 |
+
from gbnf.grammar_graph.generic_set import GenericSet
|
| 7 |
+
from gbnf.grammar_graph.get_input_as_code_points import get_input_as_code_points
|
| 8 |
+
from gbnf.grammar_graph.get_parent_stack_id import get_parent_stack_id
|
| 9 |
+
from gbnf.grammar_graph.get_serialized_rule_key import KEY_TRANSLATION, get_serialized_rule_key
|
| 10 |
+
from gbnf.grammar_graph.rule_ref import RuleRef
|
| 11 |
+
from gbnf.grammar_graph.types import RuleType
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@pytest.mark.parametrize('value,color,expected', [
|
| 15 |
+
('hello', Color.BLUE, '\x1b[34mhello'),
|
| 16 |
+
('test', Color.CYAN, '\x1b[36mtest'),
|
| 17 |
+
('example', Color.GREEN, '\x1b[32mexample'),
|
| 18 |
+
(123, Color.RED, '\x1b[31m123'),
|
| 19 |
+
(456, Color.GRAY, '\x1b[90m456'),
|
| 20 |
+
(789, Color.YELLOW, '\x1b[33m789'),
|
| 21 |
+
])
|
| 22 |
+
def test_colorize(value, color, expected):
|
| 23 |
+
assert colorize(value, color) == expected
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class TestGenericSet:
|
| 27 |
+
def test_it_instantiates(self):
|
| 28 |
+
assert isinstance(GenericSet(lambda _: 'foo'), GenericSet)
|
| 29 |
+
|
| 30 |
+
def test_it_iterates(self):
|
| 31 |
+
s = GenericSet(lambda el: el)
|
| 32 |
+
s.add(1)
|
| 33 |
+
s.add(2)
|
| 34 |
+
s.add(3)
|
| 35 |
+
assert list(s) == [1, 2, 3]
|
| 36 |
+
assert s.size == 3
|
| 37 |
+
assert len(s) == 3
|
| 38 |
+
|
| 39 |
+
def test_it_skips_duplicates(self):
|
| 40 |
+
s = GenericSet(lambda el: el)
|
| 41 |
+
s.add(1)
|
| 42 |
+
s.add(1)
|
| 43 |
+
s.add(1)
|
| 44 |
+
assert list(s) == [1]
|
| 45 |
+
|
| 46 |
+
def test_it_skips_duplicates_based_on_a_given_key(self):
|
| 47 |
+
s = GenericSet(lambda el: el['id'])
|
| 48 |
+
s.add({'id': 1})
|
| 49 |
+
s.add({'id': 1, 'foo': 'foo'})
|
| 50 |
+
s.add({'id': 1, 'bar': 'bar'})
|
| 51 |
+
assert list(s) == [{'id': 1}]
|
| 52 |
+
|
| 53 |
+
def test_can_delete(self):
|
| 54 |
+
s = GenericSet(lambda el: el['id'])
|
| 55 |
+
s.add({'id': 1})
|
| 56 |
+
s.delete({'id': 1})
|
| 57 |
+
s.add({'id': 2, 'foo': 'foo'})
|
| 58 |
+
s.add({'id': 2, 'bar': 'bar'})
|
| 59 |
+
assert list(s) == [{'id': 2, 'foo': 'foo'}]
|
| 60 |
+
|
| 61 |
+
def test_delete_throws_for_an_unknown_element(self):
|
| 62 |
+
with pytest.raises(Exception, match='Could not get ref'):
|
| 63 |
+
GenericSet(lambda el: el).delete(1)
|
| 64 |
+
|
| 65 |
+
def test_has_and_get(self):
|
| 66 |
+
s = GenericSet(lambda el: el['id'])
|
| 67 |
+
first = {'id': 1}
|
| 68 |
+
s.add(first)
|
| 69 |
+
assert s.has(first)
|
| 70 |
+
assert not s.has({'id': 1}) # same key, different object
|
| 71 |
+
assert s.get({'id': 1}) is first
|
| 72 |
+
assert s.get({'id': 2}) is None
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
@pytest.mark.parametrize('input_,expected', [('abc', [97, 98, 99]), (99, [99]), ([99, 100, 101], [99, 100, 101])])
|
| 76 |
+
def test_get_input_as_code_points(input_, expected):
|
| 77 |
+
assert get_input_as_code_points(input_) == expected
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def _mock_colorize(text, color):
|
| 81 |
+
return f'[{json.dumps(color)}]:{text}'
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def _mock_pointer(stack_id, path_id, step_id, parent=None):
|
| 85 |
+
class Node:
|
| 86 |
+
meta = {'stackId': stack_id, 'pathId': path_id, 'stepId': step_id}
|
| 87 |
+
|
| 88 |
+
class Pointer:
|
| 89 |
+
node = Node
|
| 90 |
+
pointer = Pointer()
|
| 91 |
+
pointer.parent = parent
|
| 92 |
+
return pointer
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
class TestGetParentStackId:
|
| 96 |
+
red = json.dumps(Color.RED)
|
| 97 |
+
gray = json.dumps(Color.GRAY)
|
| 98 |
+
|
| 99 |
+
def test_returns_an_empty_string_if_no_parents(self):
|
| 100 |
+
assert get_parent_stack_id(_mock_pointer(1, 1, 1), _mock_colorize) == ''
|
| 101 |
+
|
| 102 |
+
def test_returns_a_single_parent_id_colored_correctly(self):
|
| 103 |
+
pointer = _mock_pointer(2, 2, 2, _mock_pointer(1, 1, 1))
|
| 104 |
+
assert get_parent_stack_id(pointer, _mock_colorize) == f'[{self.red}]:1,1,1'
|
| 105 |
+
|
| 106 |
+
def test_returns_multiple_parent_ids_separated_by_colored_arrows(self):
|
| 107 |
+
pointer = _mock_pointer(2, 2, 2, _mock_pointer(1, 1, 1, _mock_pointer(0, 0, 0)))
|
| 108 |
+
assert get_parent_stack_id(pointer, _mock_colorize) == (
|
| 109 |
+
f'[{self.red}]:1,1,1[{self.gray}]:<-[{self.red}]:0,0,0'
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
def test_handles_deep_nesting_of_pointers(self):
|
| 113 |
+
pointer = _mock_pointer(3, 3, 3, _mock_pointer(2, 2, 2, _mock_pointer(1, 1, 1, _mock_pointer(0, 0, 0))))
|
| 114 |
+
assert get_parent_stack_id(pointer, _mock_colorize) == (
|
| 115 |
+
f'[{self.red}]:2,2,2[{self.gray}]:<-[{self.red}]:1,1,1[{self.gray}]:<-[{self.red}]:0,0,0'
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
def test_accepts_snake_case_meta(self):
|
| 119 |
+
class Node:
|
| 120 |
+
meta = {'stack_id': 1, 'path_id': 2, 'step_id': 3}
|
| 121 |
+
|
| 122 |
+
class Parent:
|
| 123 |
+
node = Node
|
| 124 |
+
parent = None
|
| 125 |
+
|
| 126 |
+
class Pointer:
|
| 127 |
+
parent = Parent()
|
| 128 |
+
assert get_parent_stack_id(Pointer(), _mock_colorize) == f'[{self.red}]:1,2,3'
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
class TestGetSerializedRuleKey:
|
| 132 |
+
def test_returns_type_for_end_rules(self):
|
| 133 |
+
assert get_serialized_rule_key({'type': RuleType.END}) == f'{KEY_TRANSLATION[RuleType.END]}'
|
| 134 |
+
|
| 135 |
+
def test_returns_type_and_value_for_character_rules(self):
|
| 136 |
+
assert get_serialized_rule_key({'type': RuleType.CHAR, 'value': [97]}) == f'{KEY_TRANSLATION[RuleType.CHAR]}-[97]'
|
| 137 |
+
|
| 138 |
+
def test_serializes_ranges(self):
|
| 139 |
+
assert get_serialized_rule_key({'type': RuleType.CHAR, 'value': [[97, 122], 95]}) == '1-[[97, 122], 95]'
|
| 140 |
+
|
| 141 |
+
def test_returns_type_and_value_for_character_exclude_rules(self):
|
| 142 |
+
rule = {'type': RuleType.CHAR_EXCLUDE, 'value': [97]}
|
| 143 |
+
assert get_serialized_rule_key(rule) == f'{KEY_TRANSLATION[RuleType.CHAR_EXCLUDE]}-[97]'
|
| 144 |
+
|
| 145 |
+
def test_returns_ref_type_with_value_for_reference_rules(self):
|
| 146 |
+
assert get_serialized_rule_key(RuleRef(99)) == '3-99'
|
| 147 |
+
|
| 148 |
+
def test_accepts_plain_string_types(self):
|
| 149 |
+
assert get_serialized_rule_key({'type': 'char', 'value': [97]}) == '1-[97]'
|
| 150 |
+
|
| 151 |
+
def test_throws_an_error_for_unknown_rule_types(self):
|
| 152 |
+
with pytest.raises(Exception, match='Unknown rule type: {"type": "UNKNOWN", "value": "something"}'):
|
| 153 |
+
get_serialized_rule_key({'type': 'UNKNOWN', 'value': 'something'})
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
class TestRuleRef:
|
| 157 |
+
def test_initializes_with_a_given_value(self):
|
| 158 |
+
assert RuleRef(123).value == 123
|
| 159 |
+
|
| 160 |
+
def test_allows_setting_and_getting_nodes(self):
|
| 161 |
+
mock_nodes = {object()}
|
| 162 |
+
rule_ref = RuleRef(123)
|
| 163 |
+
rule_ref.nodes = mock_nodes
|
| 164 |
+
assert rule_ref.nodes is mock_nodes
|
| 165 |
+
|
| 166 |
+
def test_throws_an_error_if_trying_to_get_nodes_before_setting(self):
|
| 167 |
+
with pytest.raises(Exception, match='Nodes are not set'):
|
| 168 |
+
RuleRef(123).nodes
|
| 169 |
+
|
| 170 |
+
def test_compares_by_value(self):
|
| 171 |
+
assert RuleRef(1) == RuleRef(1)
|
| 172 |
+
assert RuleRef(1) != RuleRef(2)
|
| 173 |
+
assert RuleRef(1) != {'value': 1}
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/unit/test_graph.py
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from gbnf.grammar_graph.colorize import Color
|
| 4 |
+
from gbnf.grammar_graph.generic_set import GenericSet
|
| 5 |
+
from gbnf.grammar_graph.graph import Graph
|
| 6 |
+
from gbnf.grammar_graph.graph_node import GraphNode
|
| 7 |
+
from gbnf.grammar_graph.graph_pointer import GraphPointer
|
| 8 |
+
from gbnf.grammar_graph.rule_ref import RuleRef
|
| 9 |
+
from gbnf.grammar_graph.types import RuleType
|
| 10 |
+
from gbnf.utils.errors.input_parse_error import InputParseError
|
| 11 |
+
|
| 12 |
+
META = {'stackId': 1, 'pathId': 2, 'stepId': 3}
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class TestGraphNode:
|
| 16 |
+
rule = RuleRef(42)
|
| 17 |
+
|
| 18 |
+
def test_constructs_with_a_rule_and_meta(self):
|
| 19 |
+
node = GraphNode(self.rule, META)
|
| 20 |
+
assert node.rule is self.rule
|
| 21 |
+
assert node.meta == META
|
| 22 |
+
|
| 23 |
+
def test_throws_if_meta_is_undefined(self):
|
| 24 |
+
with pytest.raises(Exception, match='Meta is undefined'):
|
| 25 |
+
GraphNode(self.rule, None)
|
| 26 |
+
|
| 27 |
+
def test_calculates_and_caches_its_id(self):
|
| 28 |
+
node = GraphNode(self.rule, dict(META))
|
| 29 |
+
assert node.id == '1,2,3'
|
| 30 |
+
node.meta = {'stackId': 4, 'pathId': 5, 'stepId': 6}
|
| 31 |
+
assert node.id == '1,2,3'
|
| 32 |
+
|
| 33 |
+
def test_delegates_print_to_print_graph_node(self, monkeypatch):
|
| 34 |
+
calls = []
|
| 35 |
+
monkeypatch.setattr(
|
| 36 |
+
'gbnf.grammar_graph.graph_node.print_graph_node',
|
| 37 |
+
lambda node: calls.append(node) or (lambda *args, **kwargs: 'printed'),
|
| 38 |
+
)
|
| 39 |
+
node = GraphNode(self.rule, META)
|
| 40 |
+
assert calls == [node]
|
| 41 |
+
assert node.print(colorize=lambda s, c: s, show_position=False) == 'printed'
|
| 42 |
+
|
| 43 |
+
def test_links_the_next_node(self):
|
| 44 |
+
next_node = GraphNode(RuleRef(43), META)
|
| 45 |
+
assert GraphNode(self.rule, META, next_node).next is next_node
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class TestGraphPointer:
|
| 49 |
+
def test_initializes_with_just_a_node(self):
|
| 50 |
+
node = GraphNode({'type': RuleType.END}, META)
|
| 51 |
+
pointer = GraphPointer(node)
|
| 52 |
+
assert pointer.node is node
|
| 53 |
+
assert pointer.id == '1,2,3'
|
| 54 |
+
assert pointer.parent is None
|
| 55 |
+
|
| 56 |
+
def test_throws_if_node_is_undefined(self):
|
| 57 |
+
with pytest.raises(Exception, match='Node is undefined'):
|
| 58 |
+
GraphPointer(None)
|
| 59 |
+
|
| 60 |
+
def test_initializes_with_node_and_parent(self):
|
| 61 |
+
parent_node = GraphNode({'type': RuleType.CHAR, 'value': [97]}, META)
|
| 62 |
+
child_node = GraphNode({'type': RuleType.CHAR, 'value': [98]}, {'stackId': 4, 'pathId': 5, 'stepId': 6})
|
| 63 |
+
parent = GraphPointer(parent_node)
|
| 64 |
+
child = GraphPointer(child_node, parent)
|
| 65 |
+
assert child.parent is parent
|
| 66 |
+
assert child.id == '1,2,3-4,5,6'
|
| 67 |
+
|
| 68 |
+
def test_resolve_throws_on_unknown_rule_types(self):
|
| 69 |
+
pointer = GraphPointer(GraphNode({'type': 'UNKNOWN_RULE_TYPE'}, META))
|
| 70 |
+
with pytest.raises(Exception, match='Unknown rule'):
|
| 71 |
+
list(pointer.resolve())
|
| 72 |
+
|
| 73 |
+
@pytest.mark.parametrize('rule', [
|
| 74 |
+
{'type': RuleType.CHAR, 'value': [97]},
|
| 75 |
+
{'type': RuleType.CHAR_EXCLUDE, 'value': [97]},
|
| 76 |
+
{'type': RuleType.END},
|
| 77 |
+
])
|
| 78 |
+
def test_resolve_yields_leaf_rules(self, rule):
|
| 79 |
+
pointer = GraphPointer(GraphNode(rule, META))
|
| 80 |
+
assert list(pointer.resolve()) == [pointer]
|
| 81 |
+
|
| 82 |
+
def test_resolve_yields_the_parent_of_an_end_rule(self):
|
| 83 |
+
end = {'type': RuleType.END}
|
| 84 |
+
parent = GraphPointer(GraphNode(end, {'stackId': 2, 'pathId': 1, 'stepId': 1}))
|
| 85 |
+
child = GraphPointer(GraphNode(end, {'stackId': 1, 'pathId': 1, 'stepId': 1}), parent)
|
| 86 |
+
assert list(child.resolve()) == [parent]
|
| 87 |
+
|
| 88 |
+
def test_resolve_yields_the_grandparent_of_an_end_rule(self):
|
| 89 |
+
end = {'type': RuleType.END}
|
| 90 |
+
grandparent = GraphPointer(GraphNode(end, {'stackId': 2, 'pathId': 1, 'stepId': 1}))
|
| 91 |
+
parent = GraphPointer(GraphNode(end, {'stackId': 2, 'pathId': 1, 'stepId': 1}), grandparent)
|
| 92 |
+
child = GraphPointer(GraphNode(end, {'stackId': 1, 'pathId': 1, 'stepId': 1}), parent)
|
| 93 |
+
assert list(child.resolve()) == [grandparent]
|
| 94 |
+
|
| 95 |
+
def test_resolve_yields_a_parent_that_is_not_an_end(self):
|
| 96 |
+
end = {'type': RuleType.END}
|
| 97 |
+
grandparent = GraphPointer(GraphNode(end, {'stackId': 2, 'pathId': 1, 'stepId': 1}))
|
| 98 |
+
parent = GraphPointer(
|
| 99 |
+
GraphNode({'type': RuleType.CHAR, 'value': [97]}, {'stackId': 2, 'pathId': 1, 'stepId': 1}), grandparent
|
| 100 |
+
)
|
| 101 |
+
child = GraphPointer(GraphNode(end, {'stackId': 1, 'pathId': 1, 'stepId': 1}), parent)
|
| 102 |
+
assert list(child.resolve()) == [parent]
|
| 103 |
+
|
| 104 |
+
def test_resolve_expands_references_in_order(self):
|
| 105 |
+
first = GraphNode({'type': RuleType.CHAR, 'value': [97]}, {'stackId': 1, 'pathId': 0, 'stepId': 0})
|
| 106 |
+
second = GraphNode({'type': RuleType.CHAR, 'value': [98]}, {'stackId': 1, 'pathId': 1, 'stepId': 0})
|
| 107 |
+
ref = RuleRef(1)
|
| 108 |
+
ref.nodes = [first, second]
|
| 109 |
+
pointer = GraphPointer(GraphNode(ref, META))
|
| 110 |
+
resolved = list(pointer.resolve())
|
| 111 |
+
assert [p.node for p in resolved] == [first, second]
|
| 112 |
+
assert all(p.parent is pointer for p in resolved)
|
| 113 |
+
|
| 114 |
+
def test_fetch_next_stops_at_invalid_pointers(self):
|
| 115 |
+
end = {'type': RuleType.END}
|
| 116 |
+
node = GraphNode({'type': RuleType.CHAR, 'value': [97]}, META, GraphNode(end, META))
|
| 117 |
+
pointer = GraphPointer(node)
|
| 118 |
+
pointer.valid = False
|
| 119 |
+
assert list(pointer.fetch_next()) == []
|
| 120 |
+
pointer.valid = True
|
| 121 |
+
assert [p.node for p in pointer.fetch_next()] == [node.next]
|
| 122 |
+
|
| 123 |
+
def test_fetch_next_throws_without_a_next_node(self):
|
| 124 |
+
pointer = GraphPointer(GraphNode({'type': RuleType.CHAR, 'value': [97]}, META))
|
| 125 |
+
with pytest.raises(Exception, match='No next node'):
|
| 126 |
+
list(pointer.fetch_next())
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
STACKED_RULES = [
|
| 130 |
+
[
|
| 131 |
+
[{'type': RuleType.CHAR, 'value': [65, 66, 67]}],
|
| 132 |
+
[{'type': RuleType.CHAR, 'value': [68, 69, 70]}],
|
| 133 |
+
],
|
| 134 |
+
[
|
| 135 |
+
[{'type': RuleType.CHAR, 'value': [71, 72, 73]}],
|
| 136 |
+
[{'type': RuleType.CHAR, 'value': [74, 75, 76]}],
|
| 137 |
+
],
|
| 138 |
+
]
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
class TestGraph:
|
| 142 |
+
grammar = 'example-grammar'
|
| 143 |
+
root_id = 0
|
| 144 |
+
|
| 145 |
+
def test_creates_a_graph(self):
|
| 146 |
+
graph = Graph(self.grammar, STACKED_RULES, self.root_id)
|
| 147 |
+
assert isinstance(graph, Graph)
|
| 148 |
+
assert graph.grammar == self.grammar
|
| 149 |
+
|
| 150 |
+
def test_gets_the_root_node(self):
|
| 151 |
+
graph = Graph(self.grammar, STACKED_RULES, self.root_id)
|
| 152 |
+
root_node = graph.get_root_node(self.root_id)
|
| 153 |
+
assert isinstance(root_node, dict)
|
| 154 |
+
assert len(root_node) == 2
|
| 155 |
+
|
| 156 |
+
def test_throws_for_an_unknown_root(self):
|
| 157 |
+
with pytest.raises(Exception, match='Root node not found for value: 5'):
|
| 158 |
+
Graph(self.grammar, STACKED_RULES, 5)
|
| 159 |
+
|
| 160 |
+
def test_gets_the_initial_pointers(self):
|
| 161 |
+
graph = Graph(self.grammar, STACKED_RULES, self.root_id)
|
| 162 |
+
pointers = graph.get_initial_pointers()
|
| 163 |
+
assert isinstance(pointers, GenericSet)
|
| 164 |
+
assert pointers.size == 2
|
| 165 |
+
|
| 166 |
+
def test_prints_the_graph(self):
|
| 167 |
+
graph = Graph(self.grammar, STACKED_RULES, self.root_id)
|
| 168 |
+
printed = graph.print(colors=True)
|
| 169 |
+
assert isinstance(printed, str)
|
| 170 |
+
assert 'ABC' in printed
|
| 171 |
+
assert 'DEF' in printed
|
| 172 |
+
assert 'GHI' in printed
|
| 173 |
+
|
| 174 |
+
def test_prints_the_graph_without_colors(self):
|
| 175 |
+
graph = Graph(self.grammar, STACKED_RULES, self.root_id)
|
| 176 |
+
assert graph.print() == '\n{0,0,0}[ABC]\n{0,1,0}[DEF]\n{1,0,0}[GHI]\n{1,1,0}[JKL]'
|
| 177 |
+
assert graph.print({'colors': False}) == graph.print()
|
| 178 |
+
assert Color.YELLOW in graph.print(colors=True)
|
| 179 |
+
|
| 180 |
+
def test_adds_input_and_tracks_previous_code_points(self):
|
| 181 |
+
graph = Graph('root ::= "ab"', [[[
|
| 182 |
+
{'type': RuleType.CHAR, 'value': [97]},
|
| 183 |
+
{'type': RuleType.CHAR, 'value': [98]},
|
| 184 |
+
{'type': RuleType.END},
|
| 185 |
+
]]], 0)
|
| 186 |
+
pointers = graph.add('a')
|
| 187 |
+
pointers = graph.add([98], pointers)
|
| 188 |
+
assert [p.rule for p in pointers] == [{'type': RuleType.END}]
|
| 189 |
+
assert graph.previous_code_points == [97, 98]
|
| 190 |
+
with pytest.raises(InputParseError):
|
| 191 |
+
graph.add('c', pointers)
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/unit/test_parse_state_and_gbnf.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from unittest import mock
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
from gbnf import GBNF as GBNFFromPackage
|
| 6 |
+
from gbnf.gbnf import GBNF
|
| 7 |
+
from gbnf.grammar_graph.parse_state import ParseState
|
| 8 |
+
from gbnf.rules_builder.types import InternalRuleType
|
| 9 |
+
from gbnf.utils.errors.grammar_parse_error import GrammarParseError
|
| 10 |
+
|
| 11 |
+
MOCK_RULE = {'type': 'char', 'value': [65]}
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class MockPointer:
|
| 15 |
+
def __init__(self, rule):
|
| 16 |
+
self.rule = rule
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@pytest.fixture
|
| 20 |
+
def mock_graph():
|
| 21 |
+
graph = mock.MagicMock()
|
| 22 |
+
graph.grammar = 'sample-grammar'
|
| 23 |
+
return graph
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@pytest.fixture
|
| 27 |
+
def mock_pointers():
|
| 28 |
+
return [MockPointer(MOCK_RULE)]
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class TestParseState:
|
| 32 |
+
def test_constructs_with_given_graph_and_pointers(self, mock_graph, mock_pointers):
|
| 33 |
+
assert isinstance(ParseState(mock_graph, mock_pointers), ParseState)
|
| 34 |
+
|
| 35 |
+
def test_returns_unique_rules_from_pointers(self, mock_graph):
|
| 36 |
+
state = ParseState(mock_graph, [MockPointer(MOCK_RULE), MockPointer(dict(MOCK_RULE))])
|
| 37 |
+
rules = list(state.rules())
|
| 38 |
+
assert len(rules) == 1
|
| 39 |
+
assert rules[0]['value'] == [65]
|
| 40 |
+
|
| 41 |
+
def test_iterates_over_unique_rules(self, mock_graph, mock_pointers):
|
| 42 |
+
rules = [*ParseState(mock_graph, mock_pointers)]
|
| 43 |
+
assert len(rules) == 1
|
| 44 |
+
assert rules[0]['type'] == 'char'
|
| 45 |
+
|
| 46 |
+
def test_adds_new_input_and_returns_a_new_parse_state(self, mock_graph, mock_pointers):
|
| 47 |
+
new_pointers = [MockPointer({'type': 'char', 'value': [66]})]
|
| 48 |
+
mock_graph.add.return_value = new_pointers
|
| 49 |
+
state = ParseState(mock_graph, mock_pointers)
|
| 50 |
+
new_state = state.add('B')
|
| 51 |
+
assert isinstance(new_state, ParseState)
|
| 52 |
+
assert new_state is not state
|
| 53 |
+
assert list(new_state) == [{'type': 'char', 'value': [66]}]
|
| 54 |
+
mock_graph.add.assert_called_once_with('B', mock_pointers)
|
| 55 |
+
|
| 56 |
+
def test_calculates_the_size_of_unique_rules(self, mock_graph, mock_pointers):
|
| 57 |
+
state = ParseState(mock_graph, mock_pointers)
|
| 58 |
+
assert state.size == 1
|
| 59 |
+
assert len(state) == 1
|
| 60 |
+
|
| 61 |
+
def test_provides_access_to_the_underlying_grammar(self, mock_graph, mock_pointers):
|
| 62 |
+
assert ParseState(mock_graph, mock_pointers).grammar == 'sample-grammar'
|
| 63 |
+
|
| 64 |
+
def test_is_callable(self, mock_graph, mock_pointers):
|
| 65 |
+
mock_graph.add.return_value = mock_pointers
|
| 66 |
+
assert isinstance(ParseState(mock_graph, mock_pointers)('B'), ParseState)
|
| 67 |
+
|
| 68 |
+
def test_states_are_immutable(self):
|
| 69 |
+
state = GBNF('root ::= "ab"')
|
| 70 |
+
advanced = state.add('a')
|
| 71 |
+
assert list(state) == [{'type': 'char', 'value': [97]}]
|
| 72 |
+
assert list(advanced) == [{'type': 'char', 'value': [98]}]
|
| 73 |
+
assert list(state.add('a')) == list(advanced)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
class TestGBNF:
|
| 77 |
+
def test_is_exported_from_the_package(self):
|
| 78 |
+
assert GBNFFromPackage is GBNF
|
| 79 |
+
|
| 80 |
+
def test_throws_if_rules_are_empty(self):
|
| 81 |
+
with pytest.raises(GrammarParseError) as excinfo:
|
| 82 |
+
GBNF('')
|
| 83 |
+
assert str(excinfo.value) == str(GrammarParseError('', 0, 'No rules were found'))
|
| 84 |
+
|
| 85 |
+
def test_throws_if_there_is_no_root_symbol(self):
|
| 86 |
+
grammar = 'foo ::= "foo"'
|
| 87 |
+
with pytest.raises(GrammarParseError) as excinfo:
|
| 88 |
+
GBNF(grammar)
|
| 89 |
+
assert excinfo.value.reason == 'Grammar does not contain a root symbol. Available symbols are: ["foo"]'
|
| 90 |
+
|
| 91 |
+
def test_throws_if_symbol_ids_does_not_have_root(self):
|
| 92 |
+
builder = mock.MagicMock()
|
| 93 |
+
builder.rules = [[{'type': InternalRuleType.END}]]
|
| 94 |
+
builder.symbol_ids = {'root1': 1, 'root2': 1}
|
| 95 |
+
with mock.patch('gbnf.gbnf.RulesBuilder', return_value=builder):
|
| 96 |
+
with pytest.raises(GrammarParseError) as excinfo:
|
| 97 |
+
GBNF('')
|
| 98 |
+
assert excinfo.value.reason == 'Grammar does not contain a root symbol. Available symbols are: ["root1", "root2"]'
|
| 99 |
+
|
| 100 |
+
def test_builds_parse_state(self):
|
| 101 |
+
grammar = 'grammar'
|
| 102 |
+
initial_string = 'foo'
|
| 103 |
+
rules = [
|
| 104 |
+
[{'type': InternalRuleType.END}],
|
| 105 |
+
[{'type': InternalRuleType.CHAR, 'value': [97]}],
|
| 106 |
+
]
|
| 107 |
+
builder = mock.MagicMock()
|
| 108 |
+
builder.rules = rules
|
| 109 |
+
builder.symbol_ids = {'root': 1}
|
| 110 |
+
graph = mock.MagicMock()
|
| 111 |
+
graph.add.return_value = 'pointers'
|
| 112 |
+
with mock.patch('gbnf.gbnf.RulesBuilder', return_value=builder), \
|
| 113 |
+
mock.patch('gbnf.gbnf.build_rule_stack', side_effect=lambda rule: rule) as build_rule_stack, \
|
| 114 |
+
mock.patch('gbnf.gbnf.Graph', return_value=graph) as graph_class, \
|
| 115 |
+
mock.patch('gbnf.gbnf.ParseState', return_value='parse state') as parse_state_class:
|
| 116 |
+
assert GBNF(grammar, initial_string) == 'parse state'
|
| 117 |
+
build_rule_stack.assert_any_call(rules[0])
|
| 118 |
+
build_rule_stack.assert_any_call(rules[1])
|
| 119 |
+
graph_class.assert_called_once_with(grammar, rules, 1)
|
| 120 |
+
graph.add.assert_called_once_with(initial_string)
|
| 121 |
+
parse_state_class.assert_called_once_with(graph, 'pointers')
|
| 122 |
+
|
| 123 |
+
def test_accepts_objects_that_render_to_a_grammar(self):
|
| 124 |
+
class Rule:
|
| 125 |
+
def __str__(self):
|
| 126 |
+
return 'root ::= "a"'
|
| 127 |
+
assert list(GBNF(Rule())) == [{'type': 'char', 'value': [97]}]
|
| 128 |
+
|
| 129 |
+
def test_readme_example(self):
|
| 130 |
+
state = GBNF('root ::= "yes" | "no"')
|
| 131 |
+
assert [*state] == [{'type': 'char', 'value': [ord('y')]}, {'type': 'char', 'value': [ord('n')]}]
|
| 132 |
+
state = GBNF('root ::= "I like green eggs and ham"')
|
| 133 |
+
assert [*state] == [{'type': 'char', 'value': [ord('I')]}]
|
| 134 |
+
state = state.add('I li')
|
| 135 |
+
assert [*state] == [{'type': 'char', 'value': [ord('k')]}]
|
| 136 |
+
state = state.add('ke gree')
|
| 137 |
+
assert [*state] == [{'type': 'char', 'value': [ord('n')]}]
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/unit/test_print.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from gbnf.grammar_graph.colorize import Color
|
| 2 |
+
from gbnf.grammar_graph.graph_node import GraphNode
|
| 3 |
+
from gbnf.grammar_graph.graph_pointer import GraphPointer
|
| 4 |
+
from gbnf.grammar_graph.print import print_graph_node, print_graph_pointer
|
| 5 |
+
from gbnf.grammar_graph.rule_ref import RuleRef
|
| 6 |
+
from gbnf.grammar_graph.types import RuleType
|
| 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 |
+
class MockNode:
|
| 23 |
+
def __init__(self, id_, rule, next_=None):
|
| 24 |
+
self.id = id_
|
| 25 |
+
self.rule = rule
|
| 26 |
+
self.next = next_
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class MockPointer:
|
| 30 |
+
def __init__(self, node):
|
| 31 |
+
self.node = node
|
| 32 |
+
self.parent = None
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def test_print_graph_pointer(monkeypatch):
|
| 36 |
+
monkeypatch.setattr('gbnf.grammar_graph.print.get_parent_stack_id', lambda pointer, col: 'foo')
|
| 37 |
+
pointer = MockPointer(MockNode(1, RuleRef(100)))
|
| 38 |
+
assert print_graph_pointer(pointer)(colorize=mock_colorize) == '[RED]:*foo'
|
| 39 |
+
assert print_graph_pointer(pointer)({'colorize': mock_colorize}) == '[RED]:*foo'
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def test_print_graph_node_with_a_character_rule():
|
| 43 |
+
node = MockNode(1, {'type': RuleType.CHAR, 'value': [65]})
|
| 44 |
+
result = print_graph_node(node)(colorize=mock_colorize, show_position=False)
|
| 45 |
+
assert result == '[GRAY]:[[YELLOW]:A[GRAY]:]'
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def test_print_graph_node_with_a_rule_reference():
|
| 49 |
+
node = MockNode(1, RuleRef(200))
|
| 50 |
+
result = print_graph_node(node)(colorize=mock_colorize, show_position=True)
|
| 51 |
+
assert result == '[BLUE]:{[GRAY]:1[BLUE]:}[GRAY]:Ref([GREEN]:200[GRAY]:)'
|
| 52 |
+
assert print_graph_node(node)({'colorize': mock_colorize, 'showPosition': True}) == result
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def test_print_graph_node_with_ranges_and_newlines():
|
| 56 |
+
node = MockNode(1, {'type': RuleType.CHAR, 'value': [[97, 98], 10]})
|
| 57 |
+
result = print_graph_node(node)(colorize=lambda text, color: str(text), show_position=False)
|
| 58 |
+
assert result == '[a,b\\n]'
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def test_print_graph_node_with_other_rules_prints_the_type():
|
| 62 |
+
for rule in ({'type': RuleType.END}, {'type': RuleType.CHAR_EXCLUDE, 'value': [97]}):
|
| 63 |
+
node = MockNode(1, rule)
|
| 64 |
+
assert print_graph_node(node)(colorize=lambda text, color: str(text)) == rule['type']
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def test_print_graph_node_follows_the_chain_and_marks_pointers():
|
| 68 |
+
meta = {'stackId': 0, 'pathId': 0, 'stepId': 0}
|
| 69 |
+
end = GraphNode({'type': RuleType.END}, {**meta, 'stepId': 1})
|
| 70 |
+
node = GraphNode({'type': RuleType.CHAR, 'value': [97]}, meta, end)
|
| 71 |
+
pointer = GraphPointer(end, GraphPointer(node))
|
| 72 |
+
plain = lambda text, color: str(text) # noqa: E731
|
| 73 |
+
assert node.print(colorize=plain, show_position=True) == '{0,0,0}[a]-> {0,0,1}end'
|
| 74 |
+
assert node.print(colorize=plain, show_position=True, pointers=[pointer]) == '{0,0,0}[a]-> {0,0,1}end[*0,0,0]'
|
| 75 |
+
assert repr(node) == node.print(colorize=lambda text, color: f'{color}{text}', show_position=False)
|
| 76 |
+
assert Color.RED in repr(pointer)
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/unit/test_rules_builder.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from gbnf.rules_builder import RulesBuilder
|
| 4 |
+
from gbnf.rules_builder.symbol_ids import SymbolIds
|
| 5 |
+
from gbnf.rules_builder.types import InternalRuleType
|
| 6 |
+
from gbnf.utils.errors.grammar_parse_error import GrammarParseError
|
| 7 |
+
from tests.helpers import load
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def _prepare(grammar: str) -> str:
|
| 11 |
+
# the tables hold the grammar with its lines trimmed and joined with a literal `\n`
|
| 12 |
+
return '\\n'.join(line.strip() for line in grammar.split('\n')).replace('\\n', '\n')
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@pytest.mark.parametrize('key,grammar,expectation', load('rules_builder'), ids=lambda v: v if isinstance(v, str) and len(v) < 40 else None)
|
| 16 |
+
def test_parses_grammar(key, grammar, expectation):
|
| 17 |
+
parsed = RulesBuilder(_prepare(grammar))
|
| 18 |
+
assert parsed.rules == expectation['rules']
|
| 19 |
+
assert list(parsed.symbol_ids) == expectation['symbolIds']
|
| 20 |
+
assert list(parsed.symbol_ids) == [tuple(entry) for entry in expectation['symbolIds']]
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def test_rule_types_are_plain_strings_too():
|
| 24 |
+
assert RulesBuilder('root ::= "a"').rules == [[{'type': 'CHAR', 'value': [97]}, {'type': 'END'}]]
|
| 25 |
+
assert RulesBuilder('root ::= "a"').rules[0][0]['type'] is InternalRuleType.CHAR
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class TestSymbolIds:
|
| 29 |
+
def test_tracks_keys_values_and_positions(self):
|
| 30 |
+
ids = SymbolIds()
|
| 31 |
+
ids.set('root', 0, 5)
|
| 32 |
+
ids.set('foo', 1, 9)
|
| 33 |
+
assert ids.size == 2
|
| 34 |
+
assert ids.has('root') and not ids.has('bar')
|
| 35 |
+
assert ids.get('foo') == 1
|
| 36 |
+
assert ids.reverse_get(1) == 'foo'
|
| 37 |
+
assert ids.get_pos('foo') == 9
|
| 38 |
+
assert list(ids.keys()) == ['root', 'foo']
|
| 39 |
+
assert list(ids) == [('root', 0), ('foo', 1)]
|
| 40 |
+
assert list(ids) == [['root', 0], ['foo', 1]]
|
| 41 |
+
|
| 42 |
+
@pytest.mark.parametrize('method,arg,message', [
|
| 43 |
+
('get', 'nope', 'SymbolIds does not contain key: nope'),
|
| 44 |
+
('get_pos', 'nope', 'SymbolIds does not contain key: nope'),
|
| 45 |
+
('reverse_get', 3, 'SymbolIds does not contain value: 3'),
|
| 46 |
+
])
|
| 47 |
+
def test_throws_for_missing_entries(self, method, arg, message):
|
| 48 |
+
with pytest.raises(Exception, match=message):
|
| 49 |
+
getattr(SymbolIds(), method)(arg)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
class TestRulesBuilderErrors:
|
| 53 |
+
@pytest.mark.parametrize('grammar,pos,reason', [
|
| 54 |
+
('root = "foo"', 5, 'Expecting ::= at 5'),
|
| 55 |
+
('root ::= foo', 9, 'Undefined rule identifier "foo"'),
|
| 56 |
+
('root ::= foo ::= bar', 13, 'Expecting newline or end at 13'),
|
| 57 |
+
('root ::= ([a-z]\nfoo ::= "foo"\n ', 20, "Expecting ')' at 20"),
|
| 58 |
+
('root ::= "foo', 13, 'Unexpected end of grammar input, failed to complete parse'),
|
| 59 |
+
('root ::= *', 9, 'Expecting preceding item to */+/? at 9'),
|
| 60 |
+
('root ::= foo*', 9, 'Undefined rule identifier "foo"'),
|
| 61 |
+
('root ::= foo\nbar ::= baz*', 9, 'Undefined rule identifier "foo"'),
|
| 62 |
+
])
|
| 63 |
+
def test_reports_an_error(self, grammar, pos, reason):
|
| 64 |
+
with pytest.raises(GrammarParseError) as excinfo:
|
| 65 |
+
RulesBuilder(grammar)
|
| 66 |
+
assert str(excinfo.value) == str(GrammarParseError(grammar, pos, reason))
|
| 67 |
+
|
| 68 |
+
def test_times_out(self):
|
| 69 |
+
with pytest.raises(GrammarParseError, match='duration of -1 exceeded:'):
|
| 70 |
+
RulesBuilder('root ::= "foo"', -1)
|
| 71 |
+
|
| 72 |
+
def test_a_rule_can_be_referenced_before_it_is_defined(self):
|
| 73 |
+
builder = RulesBuilder('root ::= foo bar\nbar ::= "b"\nfoo ::= "f"')
|
| 74 |
+
assert [name for name, _ in builder.symbol_ids] == ['root', 'foo', 'bar']
|
| 75 |
+
assert len(builder.rules) == 3
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/unit/test_rules_builder_helpers.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from gbnf.rules_builder.is_word_char import is_word_char
|
| 4 |
+
from gbnf.rules_builder.parse_char import parse_char
|
| 5 |
+
from gbnf.rules_builder.parse_name import GET_INVALID_CHAR_ERROR, PARSE_NAME_ERROR, parse_name
|
| 6 |
+
from gbnf.rules_builder.parse_space import parse_space
|
| 7 |
+
from gbnf.utils.errors.grammar_parse_error import GrammarParseError
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@pytest.mark.parametrize('char,expected', [
|
| 11 |
+
('a', True), ('z', True), ('A', True), ('Z', True),
|
| 12 |
+
('0', False), ('9', False),
|
| 13 |
+
('-', False), ('@', False), ('_', False), ('?', False), (' ', False),
|
| 14 |
+
])
|
| 15 |
+
def test_is_word_char(char, expected):
|
| 16 |
+
assert is_word_char(char) is expected
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@pytest.mark.parametrize('char', ['a', '9'])
|
| 20 |
+
def test_parse_char_plain(char):
|
| 21 |
+
grammar = f'root ::= "{char}" "foo"'
|
| 22 |
+
assert parse_char(grammar, len('root ::= "')) == [ord(char), 1]
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@pytest.mark.parametrize('escaped_char,code_point,inc_pos', [
|
| 26 |
+
('\\x2A', 0x2A, 4),
|
| 27 |
+
('\\u006F', 0x6F, 6),
|
| 28 |
+
('\\U0001F4A9', 128169, 10),
|
| 29 |
+
('\\t', ord('\t'), 2),
|
| 30 |
+
('\\n', ord('\n'), 2),
|
| 31 |
+
('\\r', ord('\r'), 2),
|
| 32 |
+
('\\"', ord('"'), 2),
|
| 33 |
+
('\\[', ord('['), 2),
|
| 34 |
+
('\\]', ord(']'), 2),
|
| 35 |
+
('\\\\', ord('\\'), 2),
|
| 36 |
+
])
|
| 37 |
+
def test_parse_char_escaped(escaped_char, code_point, inc_pos):
|
| 38 |
+
grammar = f'root ::= "{escaped_char}" "foo"'
|
| 39 |
+
result = parse_char(grammar, len('root ::= "'))
|
| 40 |
+
assert result == [code_point, inc_pos]
|
| 41 |
+
assert result == (code_point, inc_pos)
|
| 42 |
+
value, inc = result
|
| 43 |
+
assert (value, inc) == (code_point, inc_pos)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def test_parse_char_throws_on_invalid_input():
|
| 47 |
+
with pytest.raises(GrammarParseError):
|
| 48 |
+
parse_char('', 0)
|
| 49 |
+
with pytest.raises(GrammarParseError):
|
| 50 |
+
parse_char('a', 1)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
@pytest.mark.parametrize('src', ['\\q', '\\', '\\xZZ'])
|
| 54 |
+
def test_parse_char_throws_on_invalid_escape(src):
|
| 55 |
+
with pytest.raises(GrammarParseError):
|
| 56 |
+
parse_char(src, 0)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
@pytest.mark.parametrize('src', ['v', 'valid', 'valid-name', 'valid-name-foo', 'validName', 'validName-foo'])
|
| 60 |
+
def test_parse_name_valid(src):
|
| 61 |
+
assert parse_name(src, 0) == src
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
@pytest.mark.parametrize('src,position,expectation', [
|
| 65 |
+
('123valid', 3, 'valid'),
|
| 66 |
+
('123valid-name', 3, 'valid-name'),
|
| 67 |
+
('123valid _Name', 3, 'valid'),
|
| 68 |
+
('123valid\n_Name', 3, 'valid'),
|
| 69 |
+
('123valid\t_Name', 3, 'valid'),
|
| 70 |
+
('123valid\r_Name', 3, 'valid'),
|
| 71 |
+
])
|
| 72 |
+
def test_parse_name_from_a_non_zero_position(src, position, expectation):
|
| 73 |
+
assert parse_name(src, position) == expectation
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
@pytest.mark.parametrize('src,error', [
|
| 77 |
+
('123', GrammarParseError('123', 0, PARSE_NAME_ERROR)),
|
| 78 |
+
('valid_name', GrammarParseError('valid_name', 5, GET_INVALID_CHAR_ERROR('_'))),
|
| 79 |
+
('valid123', GrammarParseError('valid123', 5, GET_INVALID_CHAR_ERROR('1'))),
|
| 80 |
+
])
|
| 81 |
+
def test_parse_name_invalid(src, error):
|
| 82 |
+
with pytest.raises(GrammarParseError) as excinfo:
|
| 83 |
+
parse_name(src, 0)
|
| 84 |
+
assert str(excinfo.value) == str(error)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
@pytest.mark.parametrize('input_,newline_ok,expected', [
|
| 88 |
+
('abcdefghijk', True, 'abcdefghijk'),
|
| 89 |
+
(' \t abcdefghijk', True, 'abcdefghijk'),
|
| 90 |
+
('\n\n\r\n\r\nabcdefghijk', True, 'abcdefghijk'),
|
| 91 |
+
('\n\n\r\n\r\nabcdefghijk', False, '\n\n\r\n\r\nabcdefghijk'),
|
| 92 |
+
(' # This is a comment\n\t abcdefghijk', True, 'abcdefghijk'),
|
| 93 |
+
('\n\n # This is a comment\n\r\n\r\nabcdefghijk', True, 'abcdefghijk'),
|
| 94 |
+
('\n\n # This is a comment\n\r\n\r\nabcdefghijk', False, '\n\n # This is a comment\n\r\n\r\nabcdefghijk'),
|
| 95 |
+
(' \t# Comment\n# Another comment\n\n', True, ''),
|
| 96 |
+
('', True, ''),
|
| 97 |
+
])
|
| 98 |
+
def test_parse_space(input_, newline_ok, expected):
|
| 99 |
+
pos = parse_space(input_, 0, newline_ok)
|
| 100 |
+
assert input_[pos:] == expected
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def test_parse_space_stops_at_a_comment_without_a_trailing_newline():
|
| 104 |
+
assert parse_space('# trailing comment', 0, False) == len('# trailing comment')
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/unit/test_type_guards.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from gbnf.grammar_graph.graph_node import GraphNode
|
| 4 |
+
from gbnf.grammar_graph.graph_pointer import GraphPointer
|
| 5 |
+
from gbnf.grammar_graph.rule_ref import RuleRef
|
| 6 |
+
from gbnf.grammar_graph.type_guards import (
|
| 7 |
+
is_graph_pointer_rule_char,
|
| 8 |
+
is_graph_pointer_rule_char_exclude,
|
| 9 |
+
is_graph_pointer_rule_end,
|
| 10 |
+
is_graph_pointer_rule_ref,
|
| 11 |
+
is_range,
|
| 12 |
+
is_rule,
|
| 13 |
+
is_rule_char,
|
| 14 |
+
is_rule_char_excluded,
|
| 15 |
+
is_rule_end,
|
| 16 |
+
is_rule_ref,
|
| 17 |
+
is_rule_type,
|
| 18 |
+
)
|
| 19 |
+
from gbnf.grammar_graph.types import RuleType
|
| 20 |
+
|
| 21 |
+
META = {'stackId': 1, 'pathId': 2, 'stepId': 3}
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def pointer_for(rule):
|
| 25 |
+
return GraphPointer(GraphNode(rule, META))
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
@pytest.mark.parametrize('rule,ref,end,char,char_exclude', [
|
| 29 |
+
(RuleRef(1), True, False, False, False),
|
| 30 |
+
({'type': RuleType.END}, False, True, False, False),
|
| 31 |
+
({'type': RuleType.CHAR, 'value': [97]}, False, False, True, False),
|
| 32 |
+
({'type': RuleType.CHAR_EXCLUDE, 'value': [97]}, False, False, False, True),
|
| 33 |
+
])
|
| 34 |
+
def test_graph_pointer_guards(rule, ref, end, char, char_exclude):
|
| 35 |
+
pointer = pointer_for(rule)
|
| 36 |
+
assert is_graph_pointer_rule_ref(pointer) is ref
|
| 37 |
+
assert is_graph_pointer_rule_end(pointer) is end
|
| 38 |
+
assert is_graph_pointer_rule_char(pointer) is char
|
| 39 |
+
assert is_graph_pointer_rule_char_exclude(pointer) is char_exclude
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def test_is_rule():
|
| 43 |
+
assert is_rule({'type': RuleType.CHAR, 'value': [65, [66, 67]]}) is True
|
| 44 |
+
assert is_rule({'type': 'invalid', 'value': []}) is False
|
| 45 |
+
assert is_rule(None) is False
|
| 46 |
+
assert is_rule(RuleRef(1)) is False
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def test_is_rule_type():
|
| 50 |
+
assert is_rule_type(RuleType.CHAR) is True
|
| 51 |
+
assert is_rule_type(RuleType.END) is True
|
| 52 |
+
assert is_rule_type('char_exclude') is True
|
| 53 |
+
assert is_rule_type('not_a_rule_type') is False
|
| 54 |
+
assert is_rule_type(None) is False
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def test_is_rule_ref():
|
| 58 |
+
assert is_rule_ref(RuleRef(1)) is True
|
| 59 |
+
assert is_rule_ref({'type': RuleType.CHAR, 'value': [65]}) is False
|
| 60 |
+
assert is_rule_ref(None) is False
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def test_is_rule_end():
|
| 64 |
+
assert is_rule_end({'type': RuleType.END}) is True
|
| 65 |
+
assert is_rule_end({'type': RuleType.CHAR, 'value': [65]}) is False
|
| 66 |
+
assert is_rule_end(RuleRef(1)) is False
|
| 67 |
+
assert is_rule_end(None) is False
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def test_is_rule_char_and_excluded():
|
| 71 |
+
char = {'type': RuleType.CHAR, 'value': [65]}
|
| 72 |
+
exclude = {'type': RuleType.CHAR_EXCLUDE, 'value': [65]}
|
| 73 |
+
assert is_rule_char(char) is True
|
| 74 |
+
assert is_rule_char_excluded(char) is False
|
| 75 |
+
assert is_rule_char_excluded(exclude) is True
|
| 76 |
+
assert is_rule_char(exclude) is False
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def test_is_range():
|
| 80 |
+
assert is_range([1, 10]) is True
|
| 81 |
+
assert is_range((1, 10)) is True
|
| 82 |
+
assert is_range([1, '10']) is False
|
| 83 |
+
assert is_range([1]) is False
|
| 84 |
+
assert is_range([1, 2, 3]) is False
|
| 85 |
+
assert is_range(5) is False
|
| 86 |
+
assert is_range('ab') is False
|
| 87 |
+
assert is_range([True, False]) is False
|
v2-models-effort/runs/20261017T184432Z_db5d720b/ported_implementation/tests/unit/test_utils.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
|
| 3 |
+
from gbnf.utils.errors.build_error_position import build_error_position
|
| 4 |
+
from gbnf.utils.errors.get_input_as_string import get_input_as_string
|
| 5 |
+
from gbnf.utils.errors.grammar_parse_error import GRAMMAR_PARSER_ERROR_HEADER_MESSAGE, GrammarParseError
|
| 6 |
+
from gbnf.utils.errors.input_parse_error import INPUT_PARSER_ERROR_HEADER_MESSAGE, InputParseError
|
| 7 |
+
from gbnf.utils.is_point_in_range import is_point_in_range
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@pytest.mark.parametrize('point,expectation,range_', [
|
| 11 |
+
(96, False, [97, 122]),
|
| 12 |
+
(97, True, [97, 122]),
|
| 13 |
+
(98, True, [97, 122]),
|
| 14 |
+
(122, True, [97, 122]),
|
| 15 |
+
(123, False, [97, 122]),
|
| 16 |
+
])
|
| 17 |
+
def test_is_point_in_range(point, expectation, range_):
|
| 18 |
+
assert is_point_in_range(point, range_) == expectation
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@pytest.mark.parametrize('grammar,pos,expected', [
|
| 22 |
+
('root ::= "foo"', 1, ['root ::= "foo"', ' ^']),
|
| 23 |
+
('root ::= "foo"', 5, ['root ::= "foo"', ' ^']),
|
| 24 |
+
# multi line grammars with pos on first line
|
| 25 |
+
('aa\nbb', 1, ['aa', ' ^']),
|
| 26 |
+
# multi line grammars with pos on second line, first character
|
| 27 |
+
('aa\nbb', 2, ['aa', 'bb', '^']),
|
| 28 |
+
# multi line grammars with pos on second line, second character
|
| 29 |
+
('aa\nbb', 2 + 1, ['aa', 'bb', ' ^']),
|
| 30 |
+
# multi line grammars beyond error with pos on second line
|
| 31 |
+
('aa\nbb\ncc', 2 + 1, ['aa', 'bb', ' ^']),
|
| 32 |
+
# multi line grammars beyond error with pos on third line, first char
|
| 33 |
+
('aa\nbb\ncc', 2 + 2 + 0, ['aa', 'bb', 'cc', '^']),
|
| 34 |
+
# multi line grammars beyond error with pos on third line, second char
|
| 35 |
+
('aa\nbb\ncc', 2 + 2 + 1, ['aa', 'bb', 'cc', ' ^']),
|
| 36 |
+
# multi line grammars beyond error with pos on fourth line, first char
|
| 37 |
+
('aa\nbb\ncc\ndd', 2 + 2 + 2 + 0, ['bb', 'cc', 'dd', '^']),
|
| 38 |
+
# multi line grammars beyond error with pos on fifth line, second char
|
| 39 |
+
('aa\nbb\ncc\ndd\nee', 2 + 2 + 2 + 2 + 1, ['cc', 'dd', 'ee', ' ^']),
|
| 40 |
+
])
|
| 41 |
+
def test_build_error_position(grammar, pos, expected):
|
| 42 |
+
assert build_error_position(grammar, pos) == expected
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def test_build_error_position_for_empty_input():
|
| 46 |
+
assert build_error_position('', 0) == ['No input provided']
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def test_build_error_position_past_the_end_of_the_input():
|
| 50 |
+
assert build_error_position('abc', 3) == ['abc', '', '^']
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
@pytest.mark.parametrize('input_,expected', [
|
| 54 |
+
('hello', 'hello'),
|
| 55 |
+
([104, 101, 108, 108, 111], 'hello'),
|
| 56 |
+
(104, 'h'),
|
| 57 |
+
(0x1F600, '😀'),
|
| 58 |
+
([0x1F600, 0x1F601], '😀😁'),
|
| 59 |
+
])
|
| 60 |
+
def test_get_input_as_string(input_, expected):
|
| 61 |
+
assert get_input_as_string(input_) == expected
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def test_grammar_parse_error_renders_a_message():
|
| 65 |
+
err = GrammarParseError('aa\nbb\ncc\ndd\nee', 5, 'reason')
|
| 66 |
+
assert str(err) == '\n'.join([
|
| 67 |
+
GRAMMAR_PARSER_ERROR_HEADER_MESSAGE('reason'),
|
| 68 |
+
'',
|
| 69 |
+
'aa',
|
| 70 |
+
'bb',
|
| 71 |
+
'cc',
|
| 72 |
+
' ^',
|
| 73 |
+
])
|
| 74 |
+
assert (err.grammar, err.pos, err.reason, err.name) == ('aa\nbb\ncc\ndd\nee', 5, 'reason', 'GrammarParseError')
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
@pytest.mark.parametrize('input_,pos,lines', [
|
| 78 |
+
('some input', 1, ['some input', ' ^']),
|
| 79 |
+
('a', 0, ['a', '^']),
|
| 80 |
+
('abcd', 2, ['abcd', ' ^']),
|
| 81 |
+
])
|
| 82 |
+
def test_input_parse_error_renders_a_message(input_, pos, lines):
|
| 83 |
+
err = InputParseError(input_, pos)
|
| 84 |
+
assert str(err) == '\n'.join([INPUT_PARSER_ERROR_HEADER_MESSAGE, '', *lines])
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def test_input_parse_error_includes_previous_input():
|
| 88 |
+
err = InputParseError('z', 0, 'ba')
|
| 89 |
+
assert str(err) == '\n'.join([INPUT_PARSER_ERROR_HEADER_MESSAGE, '', 'baz', ' ^'])
|
| 90 |
+
assert err.src == 'baz'
|
| 91 |
+
assert err.error_for_most_recent_input == '\n'.join([INPUT_PARSER_ERROR_HEADER_MESSAGE, '', 'z', '^'])
|
| 92 |
+
assert err.errorForMostRecentInput == err.error_for_most_recent_input
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def test_input_parse_error_accepts_code_points():
|
| 96 |
+
err = InputParseError([97, 98], 1, [99])
|
| 97 |
+
assert err.src == 'cab'
|
| 98 |
+
assert str(err) == '\n'.join([INPUT_PARSER_ERROR_HEADER_MESSAGE, '', 'cab', ' ^'])
|
v2-models-effort/runs/20261017T184432Z_db5d720b/proxy.log
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
NOTICE Oct 17 18:44:43.401 [1]: Initializing tinyproxy ...
|
| 2 |
+
NOTICE Oct 17 18:44:43.402 [1]: Reloading config file (/etc/tinyproxy/tinyproxy.conf)
|
| 3 |
+
INFO Oct 17 18:44:43.403 [1]: Added address [0.0.0.0] to listen addresses.
|
| 4 |
+
INFO Oct 17 18:44:43.404 [1]: Adding Port [443] to the list allowed by CONNECT
|
| 5 |
+
NOTICE Oct 17 18:44:43.406 [1]: Reloading config file finished
|
| 6 |
+
INFO Oct 17 18:44:43.407 [1]: listen_sock called with addr = '0.0.0.0'
|
| 7 |
+
INFO Oct 17 18:44:43.409 [1]: trying to listen on host[0.0.0.0], family[2], socktype[1], proto[6]
|
| 8 |
+
INFO Oct 17 18:44:43.410 [1]: listening on fd [3]
|
| 9 |
+
INFO Oct 17 18:44:43.411 [1]: Not running as root, so not changing UID/GID.
|
| 10 |
+
INFO Oct 17 18:44:43.412 [1]: Setting the various signals.
|
| 11 |
+
INFO Oct 17 18:44:43.414 [1]: Starting main loop. Accepting connections.
|
| 12 |
+
CONNECT Oct 17 18:44:43.684 [1]: Connect (file descriptor 5): 192.168.32.3
|
| 13 |
+
CONNECT Oct 17 18:44:43.685 [1]: Request (file descriptor 5): CONNECT api.anthropic.com:443 HTTP/1.1
|
| 14 |
+
INFO Oct 17 18:44:43.688 [1]: No upstream proxy for api.anthropic.com
|
| 15 |
+
INFO Oct 17 18:44:43.689 [1]: opensock: opening connection to api.anthropic.com:443
|
| 16 |
+
INFO Oct 17 18:44:43.692 [1]: opensock: getaddrinfo returned for api.anthropic.com:443
|
| 17 |
+
CONNECT Oct 17 18:44:43.698 [1]: Established connection to host "api.anthropic.com" using file descriptor 6.
|
| 18 |
+
INFO Oct 17 18:44:43.699 [1]: Not sending client headers to remote machine
|
| 19 |
+
CONNECT Oct 17 18:44:43.802 [1]: Connect (file descriptor 7): 192.168.32.3
|
| 20 |
+
CONNECT Oct 17 18:44:43.804 [1]: Request (file descriptor 7): CONNECT api.anthropic.com:443 HTTP/1.1
|
| 21 |
+
INFO Oct 17 18:44:43.805 [1]: No upstream proxy for api.anthropic.com
|
| 22 |
+
INFO Oct 17 18:44:43.807 [1]: opensock: opening connection to api.anthropic.com:443
|
| 23 |
+
INFO Oct 17 18:44:43.808 [1]: opensock: getaddrinfo returned for api.anthropic.com:443
|
| 24 |
+
CONNECT Oct 17 18:44:43.815 [1]: Established connection to host "api.anthropic.com" using file descriptor 8.
|
| 25 |
+
INFO Oct 17 18:44:43.816 [1]: Not sending client headers to remote machine
|
| 26 |
+
CONNECT Oct 17 18:44:43.843 [1]: Connect (file descriptor 9): 192.168.32.3
|
| 27 |
+
CONNECT Oct 17 18:44:43.844 [1]: Request (file descriptor 9): CONNECT api.anthropic.com:443 HTTP/1.1
|
| 28 |
+
INFO Oct 17 18:44:43.846 [1]: No upstream proxy for api.anthropic.com
|
| 29 |
+
INFO Oct 17 18:44:43.847 [1]: opensock: opening connection to api.anthropic.com:443
|
| 30 |
+
INFO Oct 17 18:44:43.849 [1]: opensock: getaddrinfo returned for api.anthropic.com:443
|
| 31 |
+
CONNECT Oct 17 18:44:43.854 [1]: Established connection to host "api.anthropic.com" using file descriptor 10.
|
| 32 |
+
INFO Oct 17 18:44:43.855 [1]: Not sending client headers to remote machine
|
| 33 |
+
INFO Oct 17 18:44:44.036 [1]: Closed connection between local client (fd:7) and remote client (fd:8)
|
| 34 |
+
INFO Oct 17 18:44:44.127 [1]: Closed connection between local client (fd:5) and remote client (fd:6)
|
| 35 |
+
INFO Oct 17 19:26:55.545 [1]: Closed connection between local client (fd:9) and remote client (fd:10)
|
v2-models-effort/runs/20261017T184432Z_db5d720b/result.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"duration_api_ms":1191009,"stop_reason":"end_turn","session_id":"b24bf51f-6b2c-460b-ad53-87df3e5a5965","total_cost_usd":4.721225699999998,"usage":{"input_tokens":186,"cache_creation_input_tokens":276891,"cache_read_input_tokens":18723597,"output_tokens":174093,"output_tokens_details":{"thinking_tokens":94995},"server_tool_use":{"web_search_requests":0,"web_fetch_requests":0},"service_tier":"standard","cache_creation":{"ephemeral_1h_input_tokens":276891,"ephemeral_5m_input_tokens":0},"inference_geo":"global","iterations":[],"speed":"standard","fallback_credit":null},"modelUsage":{"claude-sonnet-5-5":{"inputTokens":186,"outputTokens":174093,"cacheReadInputTokens":18723597,"cacheCreationInputTokens":276891,"webSearchRequests":0,"costUSD":4.721225699999998,"contextWindow":1000000,"maxOutputTokens":128000,"thinkingTokens":94995,"canonicalModel":"claude-sonnet-5-5","provider":"firstParty","costBasis":"list"}},"permission_denials":[],"terminal_reason":"completed","fast_mode_state":"off","fast_mode_disabled_reason":"sdk_opt_in_required","subagent_stats":{"spawned":0,"requested":{"background":0,"foreground":0,"unset":0},"started_in_background":0,"max_depth":0,"spawned_by_subagents":0,"completed":0,"failed":0,"killed":{"parent":0,"user":0,"system":0},"refused":{"depth_limit":0,"concurrency_limit":0,"budget":0},"by_type":{}},"safety_stops":0,"is_error":false,"num_turns":95,"subtype":"success","api_error_status":null,"result":"The port is in `/target` as a `gbnf` Python package. It passes its own 1,119 pytest tests, and a fuzz comparison against the original JavaScript showed no unexpected differences.\n\n**Layout and API**\n- The package mirrors the TypeScript layout in snake_case, for example `gbnf/rules_builder/parse_name.py` and `gbnf/grammar_graph/graph_pointer.py`.\n- `from gbnf import GBNF, GrammarParseError, InputParseError, RuleType, ParseState` works.\n- Rules are plain dicts such as `{'type': 'char', 'value': [102]}`, so they compare equal to the literals in the reference tests.\n- States are immutable, callable (`state('x')`), iterable, and have `.rules()`, `.add()`, `.size` and `.grammar`.\n- I couldn't see the Python tests that will be run, so I guessed at naming. Mock targets such as `gbnf.gbnf.RulesBuilder` exist, and camelCase names like `errorForMostRecentInput` and `getRootNode` still resolve.\n\n**Testing**\n- I ran the JS suite in a scratch copy of the reference first: 203 unit and 773 integration tests all pass.\n- The Python tests are the tables from those suites (extracted programmatically) plus hand-translated unit tests and a few edge cases. They run in about 2 seconds.\n- I fuzzed about 2,350 random and mutated grammars, with stepwise, whole-string and random-walk inputs, through both implementations on two seeds. Every result matched except the intentional differences below.\n- Some pathological grammars time out under Python, so those cases were skipped in the comparison.\n- Python is much slower than node on heavy cases, roughly 10 to 30 times.\n\n**Where the Python behaves differently**\n- A grammar with no `root`, or one that references a rule that is never defined, raises `GrammarParseError`. In JS these hit a bare `Error` or a `TypeError`.\n- A `\\x`, `\\u` or `\\U` escape with no hex digits raises `GrammarParseError`. JS silently produces a character that never matches.\n- Strings are code points, not UTF-16 units, so an emoji is one character and `\"\\U0001F4A9\"` matches it.\n- Left-recursive or nullable-cycle grammars raise `RecursionError`, where JS throws `RangeError`. The pointer walk is iterative, so a long repetition like `[a-z]*` can take about 5,000 characters in Python versus about 2,100 in JS.\n\n**Judgement calls**\n- Error text built from `JSON.stringify` uses Python's default `json.dumps` spacing, for example `[\"root1\", \"root2\"]`.\n- `GBNF` calls `build_rule_stack(rule)` with one argument. The JS `map` passes three, which only matters to a test that asserts the call signature.\n\n**Not done**\n- `pip install .` is unverified because `setuptools` isn't available offline.","ttft_ms":1129,"type":"result","duration_ms":2531719,"uuid":"13630fb0-c8d6-421a-b731-b161388ac737","ttft_stream_ms":1042,"time_to_request_ms":47,"first_content_frame_ms":1042,"queued_turn_count":0,"result_index":0}
|
v2-models-effort/runs/20261017T184432Z_db5d720b/run.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"params": {
|
| 3 |
+
"source-language": "javascript",
|
| 4 |
+
"model": "claude-sonnet-5-5",
|
| 5 |
+
"effort": "xhigh"
|
| 6 |
+
},
|
| 7 |
+
"created": "2026-10-17T18:44:32Z",
|
| 8 |
+
"ended": "2026-10-17T19:26:56Z",
|
| 9 |
+
"exit_code": 0
|
| 10 |
+
}
|
v2-models-effort/runs/20261017T184432Z_db5d720b/stderr.txt
ADDED
|
File without changes
|
v2-models-effort/runs/20261017T184432Z_db5d720b/transcript/.claude/projects/-target/b24bf51f-6b2c-460b-ad53-87df3e5a5965.jsonl
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
v2-models-effort/runs/20261017T185204Z_a9c4712f/manifest.json
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"timestamp": "2026-10-17T18:52:04Z",
|
| 3 |
+
"completed_at": "2026-10-17T18:54:45Z",
|
| 4 |
+
"exit_code": 0,
|
| 5 |
+
"study": "v2-model-effort",
|
| 6 |
+
"condition": {
|
| 7 |
+
"name": "source-python_unit-tests_source-integration-tests_effort-low_model-claude-sonnet-5-5",
|
| 8 |
+
"source_language": "python",
|
| 9 |
+
"target_language": "javascript",
|
| 10 |
+
"include_unit_tests": true,
|
| 11 |
+
"include_source_integration_tests": true,
|
| 12 |
+
"include_target_integration_tests": false,
|
| 13 |
+
"effort": "low",
|
| 14 |
+
"model": "claude-sonnet-5-5"
|
| 15 |
+
},
|
| 16 |
+
"derivation": {
|
| 17 |
+
"gbnf_commit": "13f1aca495d11e160fffd68c4ba299a2415909d8"
|
| 18 |
+
},
|
| 19 |
+
"reference_implementation": {
|
| 20 |
+
"included": [
|
| 21 |
+
"python/.gitignore",
|
| 22 |
+
"python/MANIFEST.in",
|
| 23 |
+
"python/Makefile",
|
| 24 |
+
"python/README.md",
|
| 25 |
+
"python/gbnf/GBNF.py",
|
| 26 |
+
"python/gbnf/__init__.py",
|
| 27 |
+
"python/gbnf/grammar_graph/__init__.py",
|
| 28 |
+
"python/gbnf/grammar_graph/colorize.py",
|
| 29 |
+
"python/gbnf/grammar_graph/colorize_test.py",
|
| 30 |
+
"python/gbnf/grammar_graph/get_input_as_code_points.py",
|
| 31 |
+
"python/gbnf/grammar_graph/get_input_as_code_points_test.py",
|
| 32 |
+
"python/gbnf/grammar_graph/get_parent_stack_id.py",
|
| 33 |
+
"python/gbnf/grammar_graph/get_parent_stack_id_test.py",
|
| 34 |
+
"python/gbnf/grammar_graph/get_serialized_rule_key.py",
|
| 35 |
+
"python/gbnf/grammar_graph/get_serialized_rule_key_test.py",
|
| 36 |
+
"python/gbnf/grammar_graph/grammar_graph_types.py",
|
| 37 |
+
"python/gbnf/grammar_graph/graph.py",
|
| 38 |
+
"python/gbnf/grammar_graph/graph_node.py",
|
| 39 |
+
"python/gbnf/grammar_graph/graph_node_test.py",
|
| 40 |
+
"python/gbnf/grammar_graph/graph_pointer.py",
|
| 41 |
+
"python/gbnf/grammar_graph/graph_pointer_test.py",
|
| 42 |
+
"python/gbnf/grammar_graph/graph_test.py",
|
| 43 |
+
"python/gbnf/grammar_graph/parse_state.py",
|
| 44 |
+
"python/gbnf/grammar_graph/parse_state_test.py",
|
| 45 |
+
"python/gbnf/grammar_graph/pointers.py",
|
| 46 |
+
"python/gbnf/grammar_graph/print.py",
|
| 47 |
+
"python/gbnf/grammar_graph/print_test.py",
|
| 48 |
+
"python/gbnf/grammar_graph/rule_ref.py",
|
| 49 |
+
"python/gbnf/grammar_graph/rule_ref_test.py",
|
| 50 |
+
"python/gbnf/grammar_graph/type_guards.py",
|
| 51 |
+
"python/gbnf/grammar_graph/type_guards_test.py",
|
| 52 |
+
"python/gbnf/grammar_parser/__init__.py",
|
| 53 |
+
"python/gbnf/grammar_parser/build_rule_stack.py",
|
| 54 |
+
"python/gbnf/grammar_parser/build_rule_stack_test.py",
|
| 55 |
+
"python/gbnf/rules_builder/__init__.py",
|
| 56 |
+
"python/gbnf/rules_builder/is_word_char.py",
|
| 57 |
+
"python/gbnf/rules_builder/is_word_char_test.py",
|
| 58 |
+
"python/gbnf/rules_builder/parse_char.py",
|
| 59 |
+
"python/gbnf/rules_builder/parse_char_test.py",
|
| 60 |
+
"python/gbnf/rules_builder/parse_name.py",
|
| 61 |
+
"python/gbnf/rules_builder/parse_name_test.py",
|
| 62 |
+
"python/gbnf/rules_builder/parse_space.py",
|
| 63 |
+
"python/gbnf/rules_builder/parse_space_test.py",
|
| 64 |
+
"python/gbnf/rules_builder/rules_builder.py",
|
| 65 |
+
"python/gbnf/rules_builder/rules_builder_test.py",
|
| 66 |
+
"python/gbnf/rules_builder/rules_builder_types.py",
|
| 67 |
+
"python/gbnf/rules_builder/symbol_ids.py",
|
| 68 |
+
"python/gbnf/utils/__init__.py",
|
| 69 |
+
"python/gbnf/utils/errors/__init__.py",
|
| 70 |
+
"python/gbnf/utils/errors/build_error_position.py",
|
| 71 |
+
"python/gbnf/utils/errors/build_error_position_test.py",
|
| 72 |
+
"python/gbnf/utils/errors/errors_types.py",
|
| 73 |
+
"python/gbnf/utils/errors/get_input_as_string.py",
|
| 74 |
+
"python/gbnf/utils/errors/get_input_as_string_test.py",
|
| 75 |
+
"python/gbnf/utils/errors/grammar_parse_error.py",
|
| 76 |
+
"python/gbnf/utils/errors/grammar_parse_error_test.py",
|
| 77 |
+
"python/gbnf/utils/errors/input_parse_error.py",
|
| 78 |
+
"python/gbnf/utils/errors/input_parse_error_test.py",
|
| 79 |
+
"python/gbnf/utils/is_point_in_range.py",
|
| 80 |
+
"python/gbnf/utils/is_point_in_range_test.py",
|
| 81 |
+
"python/gbnf/utils/validate_non_empty.py",
|
| 82 |
+
"python/pyproject.toml",
|
| 83 |
+
"python/requirements.txt",
|
| 84 |
+
"python/tests/generated/iteration/iteration_test.py",
|
| 85 |
+
"python/tests/generated/iteration/iteration_with_additional_strings_test.py",
|
| 86 |
+
"python/tests/generated/iteration/iteration_with_an_initial_string_test.py",
|
| 87 |
+
"python/tests/generated/validation/validate_grammar_test.py",
|
| 88 |
+
"python/tests/generated/validation/validate_input_test.py",
|
| 89 |
+
"python/uv.lock"
|
| 90 |
+
]
|
| 91 |
+
},
|
| 92 |
+
"sandbox": {
|
| 93 |
+
"image_id": "sha256:f61aa8c75392851989411fcb00aebc88ac9f37ff07d9e78bd342d8b74a508378"
|
| 94 |
+
},
|
| 95 |
+
"harness": {
|
| 96 |
+
"commit": "9e24e21259430f5e40bca73c392757016eda7cda",
|
| 97 |
+
"dirty": true
|
| 98 |
+
}
|
| 99 |
+
}
|
v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/GBNF.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Graph } from "./grammar_graph/graph";
|
| 2 |
+
import { ParseState } from "./grammar_graph/parse_state";
|
| 3 |
+
import { build_rule_stack } from "./grammar_parser/build_rule_stack";
|
| 4 |
+
import { GrammarParseError, RulesBuilder } from "./rules_builder";
|
| 5 |
+
|
| 6 |
+
export function GBNF(grammar: string, initial_string: string = ""): ParseState {
|
| 7 |
+
if (typeof grammar !== "string") throw new Error("grammar must be a string");
|
| 8 |
+
if (typeof initial_string !== "string") throw new Error("input must be a string");
|
| 9 |
+
|
| 10 |
+
const rules_builder = new RulesBuilder(grammar);
|
| 11 |
+
const { rules, symbol_ids } = rules_builder;
|
| 12 |
+
if (rules.length === 0) throw new GrammarParseError(grammar, 0, "No rules were found");
|
| 13 |
+
const root_id = symbol_ids.get("root");
|
| 14 |
+
if (root_id === undefined) throw new GrammarParseError(grammar, 0, "Grammar does not contain a 'root' symbol");
|
| 15 |
+
|
| 16 |
+
const stacked_rules = rules.map((rule) => build_rule_stack(rule));
|
| 17 |
+
const graph = new Graph(grammar, stacked_rules, root_id);
|
| 18 |
+
return new ParseState(graph, graph.add(initial_string));
|
| 19 |
+
}
|
v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/colorize.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export const Color = {
|
| 2 |
+
BLUE: "\x1b[34m",
|
| 3 |
+
CYAN: "\x1b[36m",
|
| 4 |
+
GREEN: "\x1b[32m",
|
| 5 |
+
RED: "\x1b[31m",
|
| 6 |
+
GRAY: "\x1b[90m",
|
| 7 |
+
YELLOW: "\x1b[33m",
|
| 8 |
+
} as const;
|
| 9 |
+
|
| 10 |
+
export function colorize(text: string | number, color: string): string {
|
| 11 |
+
return `${color}${text}`;
|
| 12 |
+
}
|
v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/get_input_as_code_points.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { ValidInput } from "./grammar_graph_types";
|
| 2 |
+
|
| 3 |
+
export function get_code_point(char: string): number {
|
| 4 |
+
const cp = char.codePointAt(0);
|
| 5 |
+
if (cp === undefined) throw new Error(`Could not get code point for character: ${char}`);
|
| 6 |
+
return cp;
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
export function get_input_as_code_points(src: ValidInput): number[] {
|
| 10 |
+
if (typeof src === "number") return [src];
|
| 11 |
+
if (Array.isArray(src)) {
|
| 12 |
+
for (const c of src) {
|
| 13 |
+
if (!Number.isInteger(c)) {
|
| 14 |
+
throw new Error(`code_point must be an integer for ${c} if src is a list`);
|
| 15 |
+
}
|
| 16 |
+
}
|
| 17 |
+
return src;
|
| 18 |
+
}
|
| 19 |
+
if (typeof src === "string") return Array.from(src, get_code_point);
|
| 20 |
+
throw new Error(`Invalid input type: ${typeof src}`);
|
| 21 |
+
}
|
v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/get_parent_stack_id.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Color } from "./colorize";
|
| 2 |
+
import type { GraphPointer } from "./graph_pointer";
|
| 3 |
+
|
| 4 |
+
export function get_parent_stack_id(pointer: GraphPointer<any>, col: (text: string | number, color: string) => string): string {
|
| 5 |
+
const stack_ids: string[] = [];
|
| 6 |
+
let parent: GraphPointer<any> | null = pointer.parent;
|
| 7 |
+
while (parent) {
|
| 8 |
+
stack_ids.push(`${parent.node.meta.stackId},${parent.node.meta.pathId},${parent.node.meta.stepId}`);
|
| 9 |
+
parent = parent.parent;
|
| 10 |
+
}
|
| 11 |
+
const arrow = col("<-", Color.GRAY);
|
| 12 |
+
return stack_ids.map((id) => col(id, Color.RED)).join(arrow);
|
| 13 |
+
}
|
v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/get_serialized_rule_key.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { RuleChar, RuleCharExclude, RuleEnd, RuleRef, UnresolvedRule } from "./grammar_graph_types";
|
| 2 |
+
import { is_rule_char, is_rule_char_exclude, is_rule_end, is_rule_ref } from "./type_guards";
|
| 3 |
+
|
| 4 |
+
export const KEY_TRANSLATION = { RuleEnd: 0, RuleChar: 1, RuleCharExclude: 2 } as const;
|
| 5 |
+
|
| 6 |
+
// mimics python's json.dumps formatting
|
| 7 |
+
function dumps(v: unknown): string {
|
| 8 |
+
if (Array.isArray(v)) return `[${v.map(dumps).join(", ")}]`;
|
| 9 |
+
return JSON.stringify(v);
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
export function get_serialized_rule_key(rule: UnresolvedRule): string {
|
| 13 |
+
if (is_rule_end(rule)) return `${KEY_TRANSLATION.RuleEnd}`;
|
| 14 |
+
if (is_rule_char(rule)) return `${KEY_TRANSLATION.RuleChar}-${dumps(rule.value)}`;
|
| 15 |
+
if (is_rule_char_exclude(rule)) return `${KEY_TRANSLATION.RuleCharExclude}-${dumps(rule.value)}`;
|
| 16 |
+
if (is_rule_ref(rule)) return `3-${rule.value}`;
|
| 17 |
+
throw new Error(`Unknown rule type: ${rule}`);
|
| 18 |
+
}
|
v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/grammar_graph_types.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { RuleRef } from "./rule_ref";
|
| 2 |
+
import type { Pointers } from "./pointers";
|
| 3 |
+
|
| 4 |
+
export { RuleRef };
|
| 5 |
+
|
| 6 |
+
export interface PrintOpts {
|
| 7 |
+
pointers: Pointers;
|
| 8 |
+
colorize: (text: string | number, color: string) => string;
|
| 9 |
+
show_position: boolean;
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
export type Range = [number, number];
|
| 13 |
+
|
| 14 |
+
export class Rule {
|
| 15 |
+
get type(): string {
|
| 16 |
+
return this.constructor.name;
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
export class RuleWithListOfIntsOrRanges extends Rule {
|
| 21 |
+
value: (number | Range)[];
|
| 22 |
+
constructor(value: (number | Range)[] = []) {
|
| 23 |
+
super();
|
| 24 |
+
this.value = [...value];
|
| 25 |
+
}
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
export class RuleChar extends RuleWithListOfIntsOrRanges {}
|
| 29 |
+
export class RuleCharExclude extends RuleWithListOfIntsOrRanges {}
|
| 30 |
+
export class RuleEnd extends Rule {}
|
| 31 |
+
|
| 32 |
+
export type UnresolvedRule = RuleChar | RuleCharExclude | RuleRef | RuleEnd;
|
| 33 |
+
|
| 34 |
+
// ValidInput can either be a string, or a number indicating a code point.
|
| 35 |
+
export type ValidInput = string | number | number[];
|
| 36 |
+
|
| 37 |
+
export type ResolvedRule = RuleCharExclude | RuleChar | RuleEnd;
|
v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/graph.ts
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { InputParseError } from "../utils/errors";
|
| 2 |
+
import { is_point_in_range } from "../utils/is_point_in_range";
|
| 3 |
+
import { colorize } from "./colorize";
|
| 4 |
+
import { get_input_as_code_points } from "./get_input_as_code_points";
|
| 5 |
+
import { get_serialized_rule_key } from "./get_serialized_rule_key";
|
| 6 |
+
import type { UnresolvedRule, ValidInput } from "./grammar_graph_types";
|
| 7 |
+
import { GraphNode } from "./graph_node";
|
| 8 |
+
import { GraphPointer } from "./graph_pointer";
|
| 9 |
+
import { Pointers } from "./pointers";
|
| 10 |
+
import { RuleRef } from "./rule_ref";
|
| 11 |
+
import { is_range, is_rule_char, is_rule_char_exclude, is_rule_end, is_rule_ref } from "./type_guards";
|
| 12 |
+
|
| 13 |
+
export type RootNode = Map<number, GraphNode>;
|
| 14 |
+
|
| 15 |
+
export class Graph {
|
| 16 |
+
__roots__: Map<number, RootNode> = new Map();
|
| 17 |
+
grammar: string;
|
| 18 |
+
__rootNode__: RootNode | null = null;
|
| 19 |
+
previous_code_points: number[] = [];
|
| 20 |
+
|
| 21 |
+
constructor(grammar: string, stacked_rules: UnresolvedRule[][][], root_id: number) {
|
| 22 |
+
this.grammar = grammar;
|
| 23 |
+
const rule_refs: RuleRef[] = [];
|
| 24 |
+
const unique_rules = new Map<string, UnresolvedRule>();
|
| 25 |
+
|
| 26 |
+
for (let stack_id = 0; stack_id < stacked_rules.length; stack_id++) {
|
| 27 |
+
const stack = stacked_rules[stack_id];
|
| 28 |
+
const nodes: RootNode = new Map();
|
| 29 |
+
for (let path_id = 0; path_id < stack.length; path_id++) {
|
| 30 |
+
const path = stack[path_id];
|
| 31 |
+
let node: GraphNode | null = null;
|
| 32 |
+
for (let step_id = path.length - 1; step_id >= 0; step_id--) {
|
| 33 |
+
const next_node: GraphNode | null = node;
|
| 34 |
+
const rule = stack[path_id][step_id];
|
| 35 |
+
unique_rules.set(get_serialized_rule_key(rule), rule);
|
| 36 |
+
if (is_rule_ref(rule)) rule_refs.push(rule);
|
| 37 |
+
const unique_rule = unique_rules.get(get_serialized_rule_key(rule));
|
| 38 |
+
if (unique_rule === undefined) throw new Error("Could not get unique rule");
|
| 39 |
+
node = new GraphNode(unique_rule, { stackId: stack_id, pathId: path_id, stepId: step_id }, next_node);
|
| 40 |
+
}
|
| 41 |
+
if (node === null) throw new Error("Could not get node");
|
| 42 |
+
nodes.set(path_id, node);
|
| 43 |
+
}
|
| 44 |
+
this.__roots__.set(stack_id, nodes);
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
const root_node = this.__roots__.get(root_id);
|
| 48 |
+
if (root_node === undefined) throw new Error(`Root node not found for value: ${root_id}`);
|
| 49 |
+
this.__rootNode__ = root_node;
|
| 50 |
+
|
| 51 |
+
for (const rule_ref of rule_refs) {
|
| 52 |
+
rule_ref.nodes = new Set(this.__get_root_node__(rule_ref.value).values());
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
__get_root_node__(value: number): RootNode {
|
| 57 |
+
const root_node = this.__roots__.get(value);
|
| 58 |
+
if (root_node === undefined) throw new Error(`Root node not found for value: ${value}`);
|
| 59 |
+
return root_node;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
__get_initial_pointers__(): Pointers {
|
| 63 |
+
const pointers = new Pointers();
|
| 64 |
+
const root_node = this.__rootNode__;
|
| 65 |
+
if (root_node === null) throw new Error("Root node is not defined");
|
| 66 |
+
|
| 67 |
+
for (const [node, parent] of this.__fetch_nodes_for_root_node__(root_node)) {
|
| 68 |
+
const pointer = new GraphPointer(node, parent);
|
| 69 |
+
for (const resolved of this.__resolve_pointer__(pointer)) pointers.add(resolved);
|
| 70 |
+
}
|
| 71 |
+
return pointers;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
__set_valid__(pointers: GraphPointer<any>[], valid: boolean): void {
|
| 75 |
+
for (const pointer of pointers) pointer.valid = valid;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
__parse__(current_pointers: Pointers, code_point: number): Pointers {
|
| 79 |
+
for (const [rule, graph_pointers] of this.__iterate_over_pointers__(current_pointers)) {
|
| 80 |
+
if (is_rule_char(rule)) {
|
| 81 |
+
let valid = false;
|
| 82 |
+
for (const possible of rule.value) {
|
| 83 |
+
if (valid) continue;
|
| 84 |
+
if (is_range(possible)) {
|
| 85 |
+
if (is_point_in_range(code_point, possible)) valid = true;
|
| 86 |
+
} else if (code_point === possible) {
|
| 87 |
+
valid = true;
|
| 88 |
+
}
|
| 89 |
+
}
|
| 90 |
+
this.__set_valid__(graph_pointers, valid);
|
| 91 |
+
} else if (is_rule_char_exclude(rule)) {
|
| 92 |
+
let valid = true;
|
| 93 |
+
for (const possible of rule.value) {
|
| 94 |
+
if (!valid) continue;
|
| 95 |
+
if (is_range(possible)) {
|
| 96 |
+
if (is_point_in_range(code_point, possible)) valid = false;
|
| 97 |
+
} else if (code_point === possible) {
|
| 98 |
+
valid = false;
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
this.__set_valid__(graph_pointers, valid);
|
| 102 |
+
} else if (!is_rule_end(rule)) {
|
| 103 |
+
throw new Error(`Unsupported rule: ${rule}`);
|
| 104 |
+
}
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
const next_pointers = new Pointers();
|
| 108 |
+
for (const current_pointer of current_pointers) {
|
| 109 |
+
for (const unresolved of current_pointer.fetch_next()) {
|
| 110 |
+
for (const resolved of this.__resolve_pointer__(unresolved)) next_pointers.add(resolved);
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
return next_pointers;
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
*__resolve_pointer__(unresolved_pointer: GraphPointer<any>): Generator<GraphPointer<any>> {
|
| 117 |
+
for (const resolved of unresolved_pointer.resolve()) {
|
| 118 |
+
if (is_rule_ref(resolved.node.rule)) {
|
| 119 |
+
throw new Error("Encountered a reference rule when building pointers to the graph");
|
| 120 |
+
}
|
| 121 |
+
if (is_rule_end(resolved.node.rule) && resolved.parent !== null) {
|
| 122 |
+
throw new Error("Encountered an ending rule with a parent when building pointers to the graph");
|
| 123 |
+
}
|
| 124 |
+
yield resolved;
|
| 125 |
+
}
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
add(src: ValidInput, pointers?: Pointers | null): Pointers {
|
| 129 |
+
if (typeof src !== "string") throw new Error("src must be a string in graph.add");
|
| 130 |
+
if (!pointers || pointers.length === 0) pointers = this.__get_initial_pointers__();
|
| 131 |
+
|
| 132 |
+
const code_points = get_input_as_code_points(src);
|
| 133 |
+
for (let pos = 0; pos < code_points.length; pos++) {
|
| 134 |
+
pointers = this.__parse__(pointers, code_points[pos]);
|
| 135 |
+
if (pointers.length === 0) {
|
| 136 |
+
throw new InputParseError(code_points, pos, this.previous_code_points);
|
| 137 |
+
}
|
| 138 |
+
}
|
| 139 |
+
this.previous_code_points.push(...code_points);
|
| 140 |
+
return pointers;
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
*__fetch_nodes_for_root_node__(
|
| 144 |
+
root_nodes: RootNode,
|
| 145 |
+
parent: GraphPointer<any> | null = null,
|
| 146 |
+
): Generator<[GraphNode, GraphPointer<any> | null]> {
|
| 147 |
+
for (const node of root_nodes.values()) {
|
| 148 |
+
if (is_rule_ref(node.rule)) {
|
| 149 |
+
yield* this.__fetch_nodes_for_root_node__(
|
| 150 |
+
this.__get_root_node__(node.rule.value),
|
| 151 |
+
new GraphPointer(node, parent),
|
| 152 |
+
);
|
| 153 |
+
} else {
|
| 154 |
+
yield [node, parent];
|
| 155 |
+
}
|
| 156 |
+
}
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
print(pointers?: Pointers | null, colors: boolean = false): string {
|
| 160 |
+
const graph_view: string[] = [];
|
| 161 |
+
const ptrs = pointers ?? new Pointers();
|
| 162 |
+
for (const root_node of this.__roots__.values()) {
|
| 163 |
+
for (const node of root_node.values()) {
|
| 164 |
+
graph_view.push(
|
| 165 |
+
node.print({
|
| 166 |
+
pointers: ptrs,
|
| 167 |
+
show_position: true,
|
| 168 |
+
colorize: colors ? colorize : (s) => String(s),
|
| 169 |
+
}),
|
| 170 |
+
);
|
| 171 |
+
}
|
| 172 |
+
}
|
| 173 |
+
return graph_view.join("\n");
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
__iterate_over_pointers__(pointers: Pointers): Iterable<[UnresolvedRule, GraphPointer<any>[]]> {
|
| 177 |
+
const seen_rules = new Map<UnresolvedRule, GraphPointer<any>[]>();
|
| 178 |
+
for (const pointer of pointers) {
|
| 179 |
+
const rule = pointer.rule;
|
| 180 |
+
if (is_rule_ref(rule)) throw new Error("Encountered a reference rule in the graph");
|
| 181 |
+
let seen = seen_rules.get(rule);
|
| 182 |
+
if (seen === undefined) {
|
| 183 |
+
seen = [];
|
| 184 |
+
seen_rules.set(rule, seen);
|
| 185 |
+
}
|
| 186 |
+
seen.push(pointer);
|
| 187 |
+
}
|
| 188 |
+
return seen_rules.entries();
|
| 189 |
+
}
|
| 190 |
+
}
|
v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/graph_node.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { PrintOpts, UnresolvedRule } from "./grammar_graph_types";
|
| 2 |
+
import { print_graph_node } from "./print";
|
| 3 |
+
|
| 4 |
+
export interface GraphNodeMeta {
|
| 5 |
+
stackId: number;
|
| 6 |
+
pathId: number;
|
| 7 |
+
stepId: number;
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
export class GraphNode<T extends UnresolvedRule = UnresolvedRule> {
|
| 11 |
+
rule: T;
|
| 12 |
+
next: GraphNode | null;
|
| 13 |
+
meta: GraphNodeMeta;
|
| 14 |
+
private __id__: string | null = null;
|
| 15 |
+
|
| 16 |
+
constructor(rule: T, meta: GraphNodeMeta | null | undefined, next_node: GraphNode | null = null) {
|
| 17 |
+
this.rule = rule;
|
| 18 |
+
if (!meta) throw new Error("Meta is undefined");
|
| 19 |
+
this.meta = meta;
|
| 20 |
+
this.next = next_node;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
get id(): string {
|
| 24 |
+
if (this.__id__ === null) {
|
| 25 |
+
this.__id__ = `${this.meta.stackId},${this.meta.pathId},${this.meta.stepId}`;
|
| 26 |
+
}
|
| 27 |
+
return this.__id__;
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
print(opts: PrintOpts): string {
|
| 31 |
+
return print_graph_node(this)(opts);
|
| 32 |
+
}
|
| 33 |
+
}
|
v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/graph_pointer.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { PrintOpts, UnresolvedRule } from "./grammar_graph_types";
|
| 2 |
+
import type { GraphNode } from "./graph_node";
|
| 3 |
+
import { print_graph_pointer } from "./print";
|
| 4 |
+
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_rule_end,
|
| 10 |
+
} from "./type_guards";
|
| 11 |
+
|
| 12 |
+
export class GraphPointer<T extends UnresolvedRule = UnresolvedRule> {
|
| 13 |
+
node: GraphNode<T>;
|
| 14 |
+
parent: GraphPointer<any> | null;
|
| 15 |
+
id: string;
|
| 16 |
+
private __valid__: boolean | null = null;
|
| 17 |
+
|
| 18 |
+
constructor(node: GraphNode<T>, parent: GraphPointer<any> | null = null) {
|
| 19 |
+
if (!node) throw new Error("Node is undefined");
|
| 20 |
+
this.node = node;
|
| 21 |
+
this.parent = parent;
|
| 22 |
+
this.id = parent ? `${parent.id}-${node.id}` : node.id;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
get rule(): T {
|
| 26 |
+
return this.node.rule;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
get valid(): boolean | null {
|
| 30 |
+
return this.__valid__;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
set valid(valid: boolean | null) {
|
| 34 |
+
this.__valid__ = valid;
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
print(opts: PrintOpts): string {
|
| 38 |
+
return print_graph_pointer(this)(opts);
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
*resolve(resolved: boolean = false): Generator<GraphPointer<any>> {
|
| 42 |
+
if (is_graph_pointer_rule_ref(this)) {
|
| 43 |
+
if (resolved) {
|
| 44 |
+
if (!this.node.next) throw new Error(`No next node: ${this.node}`);
|
| 45 |
+
yield* new GraphPointer(this.node.next, this.parent).resolve();
|
| 46 |
+
} else {
|
| 47 |
+
for (const node of (this.node.rule as any).nodes) {
|
| 48 |
+
yield* new GraphPointer(node, this).resolve();
|
| 49 |
+
}
|
| 50 |
+
}
|
| 51 |
+
} else if (is_graph_pointer_rule_end(this)) {
|
| 52 |
+
if (!this.parent) {
|
| 53 |
+
yield this;
|
| 54 |
+
} else {
|
| 55 |
+
yield* this.parent.resolve(true);
|
| 56 |
+
}
|
| 57 |
+
} else if (is_graph_pointer_rule_char(this) || is_graph_pointer_rule_char_exclude(this)) {
|
| 58 |
+
yield this;
|
| 59 |
+
} else {
|
| 60 |
+
throw new Error(`Unknown rule: ${this.node.rule}`);
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
*fetch_next(): Generator<GraphPointer<any>> {
|
| 65 |
+
if (!this.valid) return;
|
| 66 |
+
|
| 67 |
+
if (is_rule_end(this.node.rule)) {
|
| 68 |
+
if (this.parent) yield* this.parent.fetch_next();
|
| 69 |
+
} else {
|
| 70 |
+
if (!this.node.next) throw new Error(`No next node: ${this.node}`);
|
| 71 |
+
yield* new GraphPointer(this.node.next, this.parent).resolve();
|
| 72 |
+
}
|
| 73 |
+
}
|
| 74 |
+
}
|
v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/parse_state.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { ResolvedRule } from "./grammar_graph_types";
|
| 2 |
+
import type { Graph } from "./graph";
|
| 3 |
+
import type { Pointers } from "./pointers";
|
| 4 |
+
|
| 5 |
+
export class ParseState {
|
| 6 |
+
private __graph__: Graph;
|
| 7 |
+
private __pointers__: Pointers;
|
| 8 |
+
|
| 9 |
+
constructor(graph: Graph, pointers: Pointers) {
|
| 10 |
+
this.__graph__ = graph;
|
| 11 |
+
this.__pointers__ = pointers;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
[Symbol.iterator](): Iterator<ResolvedRule> {
|
| 15 |
+
return this.rules();
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
*rules(): Generator<ResolvedRule> {
|
| 19 |
+
const seen = new Set<string>();
|
| 20 |
+
for (const pointer of this.__pointers__) {
|
| 21 |
+
const rule = pointer.rule as ResolvedRule & { value?: unknown };
|
| 22 |
+
const key = JSON.stringify({ type: rule.type, value: rule.value });
|
| 23 |
+
if (!seen.has(key)) {
|
| 24 |
+
seen.add(key);
|
| 25 |
+
yield rule;
|
| 26 |
+
}
|
| 27 |
+
}
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
add(text: string): ParseState {
|
| 31 |
+
if (typeof text !== "string") throw new Error("input text must be of type string");
|
| 32 |
+
const pointers = this.__graph__.add(text, this.__pointers__);
|
| 33 |
+
return new ParseState(this.__graph__, pointers);
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
get size(): number {
|
| 37 |
+
return Array.from(this.rules()).length;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
get grammar(): string {
|
| 41 |
+
return this.__graph__.grammar;
|
| 42 |
+
}
|
| 43 |
+
}
|
v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/pointers.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { GraphPointer } from "./graph_pointer";
|
| 2 |
+
|
| 3 |
+
export class Pointers {
|
| 4 |
+
private __pointers__ = new Map<string, GraphPointer<any>>();
|
| 5 |
+
|
| 6 |
+
constructor(...pointers: GraphPointer<any>[]) {
|
| 7 |
+
for (const p of pointers) this.add(p);
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
add(pointer: GraphPointer<any>): void {
|
| 11 |
+
this.__pointers__.set(pointer.id, pointer);
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
*[Symbol.iterator](): IterableIterator<GraphPointer<any>> {
|
| 15 |
+
yield* this.__pointers__.values();
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
get length(): number {
|
| 19 |
+
return this.__pointers__.size;
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
get size(): number {
|
| 23 |
+
return this.__pointers__.size;
|
| 24 |
+
}
|
| 25 |
+
}
|
v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/print.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Color } from "./colorize";
|
| 2 |
+
import { get_parent_stack_id } from "./get_parent_stack_id";
|
| 3 |
+
import type { PrintOpts } from "./grammar_graph_types";
|
| 4 |
+
import type { GraphNode } from "./graph_node";
|
| 5 |
+
import type { GraphPointer } from "./graph_pointer";
|
| 6 |
+
import { is_range, is_rule_char, is_rule_ref } from "./type_guards";
|
| 7 |
+
|
| 8 |
+
export function get_char(char_code: number): string {
|
| 9 |
+
const char = String.fromCodePoint(char_code);
|
| 10 |
+
if (char === "\n") return "\\n";
|
| 11 |
+
return char;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
export function print_graph_pointer(pointer: GraphPointer<any>): (opts: PrintOpts) => string {
|
| 15 |
+
return (opts) => {
|
| 16 |
+
const col = opts.colorize;
|
| 17 |
+
return col(`*${get_parent_stack_id(pointer, col)}`, Color.RED);
|
| 18 |
+
};
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
export function print_graph_node(node: GraphNode<any>): (opts: PrintOpts) => string {
|
| 22 |
+
return (opts) => {
|
| 23 |
+
const pointers = opts.pointers;
|
| 24 |
+
const col = opts.colorize;
|
| 25 |
+
const show_position = opts.show_position ?? false;
|
| 26 |
+
const rule = node.rule;
|
| 27 |
+
const parts: string[] = [];
|
| 28 |
+
if (show_position) {
|
| 29 |
+
parts.push(col("{", Color.BLUE), col(node.id, Color.GRAY), col("}", Color.BLUE));
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
if (is_rule_char(rule)) {
|
| 33 |
+
parts.push(
|
| 34 |
+
col("[", Color.GRAY),
|
| 35 |
+
col(
|
| 36 |
+
rule.value
|
| 37 |
+
.map((v) => (is_range(v) ? v.map((val) => col(get_char(val), Color.YELLOW)).join("") : get_char(v as number)))
|
| 38 |
+
.join(""),
|
| 39 |
+
Color.YELLOW,
|
| 40 |
+
),
|
| 41 |
+
col("]", Color.GRAY),
|
| 42 |
+
);
|
| 43 |
+
} else if (is_rule_ref(rule)) {
|
| 44 |
+
parts.push(col("Ref(", Color.GRAY), col(rule.value, Color.GREEN), col(")", Color.GRAY));
|
| 45 |
+
} else {
|
| 46 |
+
parts.push(col(rule.type, Color.YELLOW));
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
if (pointers) {
|
| 50 |
+
for (const pointer of pointers) {
|
| 51 |
+
const pointer_parts: string[] = [];
|
| 52 |
+
if (pointer.node === node) pointer_parts.push(pointer.print(opts));
|
| 53 |
+
if (pointer_parts.length > 0) {
|
| 54 |
+
parts.push(col("[", Color.GRAY), col(pointer_parts.join(""), Color.YELLOW), col("]", Color.GRAY));
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
const parts_to_return = [parts.join("")];
|
| 60 |
+
if (node.next) parts_to_return.push(node.next.print(opts));
|
| 61 |
+
return parts_to_return.join(col("-> ", Color.GRAY));
|
| 62 |
+
};
|
| 63 |
+
}
|
v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/rule_ref.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export type GraphNode = object;
|
| 2 |
+
|
| 3 |
+
export class RuleRef {
|
| 4 |
+
private __nodes__: Set<GraphNode> | null = null;
|
| 5 |
+
value: number;
|
| 6 |
+
|
| 7 |
+
constructor(value: number) {
|
| 8 |
+
this.value = value;
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
get nodes(): Set<GraphNode> {
|
| 12 |
+
if (this.__nodes__ === null) throw new Error("Nodes are not set");
|
| 13 |
+
return this.__nodes__;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
set nodes(nodes: Set<GraphNode>) {
|
| 17 |
+
this.__nodes__ = nodes;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
get type(): string {
|
| 21 |
+
return "RuleRef";
|
| 22 |
+
}
|
| 23 |
+
}
|
v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_graph/type_guards.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { RuleChar, RuleCharExclude, RuleEnd, UnresolvedRule } from "./grammar_graph_types";
|
| 2 |
+
import { RuleRef } from "./rule_ref";
|
| 3 |
+
import type { GraphPointer } from "./graph_pointer";
|
| 4 |
+
|
| 5 |
+
export const is_rule = (rule: unknown): boolean =>
|
| 6 |
+
rule instanceof RuleChar || rule instanceof RuleCharExclude || rule instanceof RuleEnd || rule instanceof RuleRef;
|
| 7 |
+
export const is_rule_ref = (rule: unknown): rule is RuleRef => rule instanceof RuleRef;
|
| 8 |
+
export const is_rule_end = (rule: unknown): rule is RuleEnd => rule instanceof RuleEnd;
|
| 9 |
+
export const is_rule_char = (rule: unknown): rule is RuleChar => rule instanceof RuleChar;
|
| 10 |
+
export const is_rule_char_exclude = (rule: unknown): rule is RuleCharExclude => rule instanceof RuleCharExclude;
|
| 11 |
+
export const is_range = (inpt: unknown): inpt is [number, number] =>
|
| 12 |
+
Array.isArray(inpt) && inpt.length === 2 && inpt.every((n) => Number.isInteger(n));
|
| 13 |
+
|
| 14 |
+
export const is_graph_pointer_rule_ref = (p: GraphPointer<UnresolvedRule>): boolean => is_rule_ref(p.rule);
|
| 15 |
+
export const is_graph_pointer_rule_end = (p: GraphPointer<UnresolvedRule>): boolean => is_rule_end(p.rule);
|
| 16 |
+
export const is_graph_pointer_rule_char = (p: GraphPointer<UnresolvedRule>): boolean => is_rule_char(p.rule);
|
| 17 |
+
export const is_graph_pointer_rule_char_exclude = (p: GraphPointer<UnresolvedRule>): boolean =>
|
| 18 |
+
is_rule_char_exclude(p.rule);
|
v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/grammar_parser/build_rule_stack.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Range, RuleChar, RuleCharExclude, RuleEnd, UnresolvedRule } from "../grammar_graph/grammar_graph_types";
|
| 2 |
+
import { RuleRef } from "../grammar_graph/rule_ref";
|
| 3 |
+
import { is_range, is_rule_end } from "../grammar_graph/type_guards";
|
| 4 |
+
import {
|
| 5 |
+
InternalRuleDef,
|
| 6 |
+
InternalRuleDefChar,
|
| 7 |
+
InternalRuleDefCharNot,
|
| 8 |
+
is_rule_def_alt,
|
| 9 |
+
is_rule_def_char,
|
| 10 |
+
is_rule_def_char_alt,
|
| 11 |
+
is_rule_def_char_not,
|
| 12 |
+
is_rule_def_char_rng_upper,
|
| 13 |
+
is_rule_def_end,
|
| 14 |
+
is_rule_def_ref,
|
| 15 |
+
} from "../rules_builder/rules_builder_types";
|
| 16 |
+
|
| 17 |
+
export function make_char_rule(rule_def: InternalRuleDefChar | InternalRuleDefCharNot): RuleChar | RuleCharExclude {
|
| 18 |
+
const value = rule_def.value as (number | Range)[];
|
| 19 |
+
if (is_rule_def_char_not(rule_def)) return new RuleCharExclude(value);
|
| 20 |
+
if (is_rule_def_char(rule_def)) return new RuleChar(value);
|
| 21 |
+
throw new Error(`Unsupported rule type for make_char_rule: ${JSON.stringify(rule_def)}`);
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
export function build_rule_stack(linear_rules: InternalRuleDef[]): UnresolvedRule[][] {
|
| 25 |
+
let paths: UnresolvedRule[] = [];
|
| 26 |
+
const stack: UnresolvedRule[][] = [];
|
| 27 |
+
let idx = 0;
|
| 28 |
+
|
| 29 |
+
while (idx < linear_rules.length) {
|
| 30 |
+
let rule_def = linear_rules[idx];
|
| 31 |
+
if (is_rule_def_char(rule_def) || is_rule_def_char_not(rule_def)) {
|
| 32 |
+
const char_rule = make_char_rule(rule_def);
|
| 33 |
+
idx += 1;
|
| 34 |
+
let rule: InternalRuleDef | undefined = idx < linear_rules.length ? linear_rules[idx] : undefined;
|
| 35 |
+
while (idx < linear_rules.length && (is_rule_def_char_rng_upper(rule) || is_rule_def_char_alt(rule))) {
|
| 36 |
+
if (is_rule_def_char_rng_upper(rule)) {
|
| 37 |
+
const prev_value = char_rule.value.pop();
|
| 38 |
+
if (is_range(prev_value)) {
|
| 39 |
+
throw new Error(`Unexpected range, expected a number but got an array: ${JSON.stringify(prev_value)}`);
|
| 40 |
+
}
|
| 41 |
+
if (prev_value === undefined) {
|
| 42 |
+
throw new Error("Unexpected undefined value");
|
| 43 |
+
}
|
| 44 |
+
char_rule.value.push([prev_value as number, rule.value] as Range);
|
| 45 |
+
}
|
| 46 |
+
if (is_rule_def_char_alt(rule)) {
|
| 47 |
+
char_rule.value.push(rule.value);
|
| 48 |
+
}
|
| 49 |
+
idx += 1;
|
| 50 |
+
rule = idx < linear_rules.length ? linear_rules[idx] : undefined;
|
| 51 |
+
}
|
| 52 |
+
paths.push(char_rule);
|
| 53 |
+
} else {
|
| 54 |
+
if (is_rule_def_alt(rule_def)) {
|
| 55 |
+
if (paths.length === 0) throw new Error("Encountered alt without anything before it");
|
| 56 |
+
paths.push(new RuleEnd());
|
| 57 |
+
stack.push(paths);
|
| 58 |
+
paths = [];
|
| 59 |
+
} else if (is_rule_def_end(rule_def)) {
|
| 60 |
+
paths.push(new RuleEnd());
|
| 61 |
+
} else if (is_rule_def_ref(rule_def)) {
|
| 62 |
+
paths.push(new RuleRef(rule_def.value));
|
| 63 |
+
} else if (is_rule_def_char_alt(rule_def)) {
|
| 64 |
+
throw new Error(`Encountered char alt, should be handled by above block: ${JSON.stringify(rule_def)}`);
|
| 65 |
+
} else {
|
| 66 |
+
throw new Error(`Unsupported rule type: ${JSON.stringify(rule_def)}`);
|
| 67 |
+
}
|
| 68 |
+
idx += 1;
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
if (!is_rule_end(paths[paths.length - 1])) {
|
| 73 |
+
paths.push(new RuleEnd());
|
| 74 |
+
}
|
| 75 |
+
stack.push(paths);
|
| 76 |
+
return stack;
|
| 77 |
+
}
|
v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/index.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export { GBNF } from "./GBNF";
|
| 2 |
+
export { RuleChar, RuleCharExclude, RuleEnd } from "./grammar_graph/grammar_graph_types";
|
| 3 |
+
export { GrammarParseError, InputParseError } from "./utils/errors";
|
v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/rules_builder/index.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export { GrammarParseError, InputParseError } from "../utils/errors";
|
| 2 |
+
export { RulesBuilder } from "./rules_builder";
|
v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/rules_builder/is_word_char.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export function is_word_char(c: string): boolean {
|
| 2 |
+
return /[a-zA-Z]/.test(c);
|
| 3 |
+
}
|
v2-models-effort/runs/20261017T185204Z_a9c4712f/ported_implementation/gbnf/rules_builder/parse_char.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { GrammarParseError } from "../utils/errors";
|
| 2 |
+
|
| 3 |
+
function parse_hex(s: string): number {
|
| 4 |
+
if (!/^[0-9a-fA-F]+$/.test(s)) {
|
| 5 |
+
throw new Error(`invalid literal for int() with base 16: '${s}'`);
|
| 6 |
+
}
|
| 7 |
+
return parseInt(s, 16);
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
export function parse_char(src: string, pos: number): [number, number] {
|
| 11 |
+
if (pos >= src.length) {
|
| 12 |
+
throw new GrammarParseError(src, pos, "Unexpected end of grammar input, failed to complete parse");
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
if (src[pos] === "\\") {
|
| 16 |
+
const next = src[pos + 1];
|
| 17 |
+
if (next === "x") return [parse_hex(src.slice(pos + 2, pos + 4)), 4];
|
| 18 |
+
if (next === "u") return [parse_hex(src.slice(pos + 2, pos + 6)), 6];
|
| 19 |
+
if (next === "U") return [parse_hex(src.slice(pos + 2, pos + 10)), 10];
|
| 20 |
+
if (next === "t") return [9, 2];
|
| 21 |
+
if (next === "r") return [13, 2];
|
| 22 |
+
if (next === "n") return [10, 2];
|
| 23 |
+
if (next === '"' || next === "[" || next === "]" || next === "\\") return [next.charCodeAt(0), 2];
|
| 24 |
+
throw new GrammarParseError(src, pos, `Unknown escape at ${src[pos]}`);
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
const cp = src.codePointAt(pos)!;
|
| 28 |
+
return [cp, cp > 0xffff ? 2 : 1];
|
| 29 |
+
}
|