chat : implement token aware parsing (#29307)

This commit is contained in:
Aldehir Rojas authored and GitHub committed 2026-11-22 11:36:03 -06:00
1 parent 0b51bb2fed
commit 1014775c8a
30 files changed
+1988 -502

No files matched your search

+294 -68
View File
@@ -129,14 +129,14 @@ void test_gbnf_generation(testing &t) {
});
assert_gbnf_equal(t, R"""(
root ::= until-0
root ::= until-1
space ::= | " " | "\n"{1,2} [ \t]{0,20}
until-0 ::= | [<] until-0-01 | [^<] until-0
until-0-01 ::= | [<] until-0-01 | [/] until-0-02 | [^/<] until-0
until-0-02 ::= | [<] until-0-01 | [t] until-0-03 | [^<t] until-0
until-0-03 ::= | [<] until-0-01 | [a] until-0-04 | [^<a] until-0
until-0-04 ::= | [<] until-0-01 | [g] until-0-05 | [^<g] until-0
until-0-05 ::= | [<] until-0-01 | [^<>] until-0
until-1 ::= | [<] until-1-01 | [^<] until-1
until-1-01 ::= | [<] until-1-01 | [/] until-1-02 | [^/<] until-1
until-1-02 ::= | [<] until-1-01 | [t] until-1-03 | [^<t] until-1
until-1-03 ::= | [<] until-1-01 | [a] until-1-04 | [^<a] until-1
until-1-04 ::= | [<] until-1-01 | [g] until-1-05 | [^<g] until-1
until-1-05 ::= | [<] until-1-01 | [^<>] until-1
)""", gbnf);
});
@@ -150,22 +150,22 @@ void test_gbnf_generation(testing &t) {
});
assert_gbnf_equal(t, R"""(
root ::= until-0
root ::= until-1
space ::= | " " | "\n"{1,2} [ \t]{0,20}
until-0 ::= | [\n] until-0-01 | [^\n] until-0
until-0-01 ::= | [\n] until-0-01 | [<] until-0-02 | [^\n<] until-0
until-0-02 ::= | [\n] until-0-01 | [/] until-0-03 | [^\n/] until-0
until-0-03 ::= | [\n] until-0-01 | [p] until-0-04 | [^\np] until-0
until-0-04 ::= | [\n] until-0-01 | [a] until-0-05 | [^\na] until-0
until-0-05 ::= | [\n] until-0-01 | [r] until-0-06 | [^\nr] until-0
until-0-06 ::= | [\n] until-0-01 | [a] until-0-07 | [^\na] until-0
until-0-07 ::= | [\n] until-0-01 | [m] until-0-08 | [^\nm] until-0
until-0-08 ::= | [\n] until-0-01 | [e] until-0-09 | [^\ne] until-0
until-0-09 ::= | [\n] until-0-01 | [t] until-0-10 | [^\nt] until-0
until-0-10 ::= | [\n] until-0-01 | [e] until-0-11 | [^\ne] until-0
until-0-11 ::= | [\n] until-0-01 | [r] until-0-12 | [^\nr] until-0
until-0-12 ::= | [\n] until-0-01 | [>] until-0-13 | [^\n>] until-0
until-0-13 ::= | [^\n] until-0
until-1 ::= | [\n] until-1-01 | [^\n] until-1
until-1-01 ::= | [\n] until-1-01 | [<] until-1-02 | [^\n<] until-1
until-1-02 ::= | [\n] until-1-01 | [/] until-1-03 | [^\n/] until-1
until-1-03 ::= | [\n] until-1-01 | [p] until-1-04 | [^\np] until-1
until-1-04 ::= | [\n] until-1-01 | [a] until-1-05 | [^\na] until-1
until-1-05 ::= | [\n] until-1-01 | [r] until-1-06 | [^\nr] until-1
until-1-06 ::= | [\n] until-1-01 | [a] until-1-07 | [^\na] until-1
until-1-07 ::= | [\n] until-1-01 | [m] until-1-08 | [^\nm] until-1
until-1-08 ::= | [\n] until-1-01 | [e] until-1-09 | [^\ne] until-1
until-1-09 ::= | [\n] until-1-01 | [t] until-1-10 | [^\nt] until-1
until-1-10 ::= | [\n] until-1-01 | [e] until-1-11 | [^\ne] until-1
until-1-11 ::= | [\n] until-1-01 | [r] until-1-12 | [^\nr] until-1
until-1-12 ::= | [\n] until-1-01 | [>] until-1-13 | [^\n>] until-1
until-1-13 ::= | [^\n] until-1
)""", gbnf);
});
@@ -181,15 +181,15 @@ void test_gbnf_generation(testing &t) {
});
assert_gbnf_equal(t, R"""(
root ::= until-0
root ::= until-1
space ::= | " " | "\n"{1,2} [ \t]{0,20}
until-0 ::= | [<] until-0-01 | [^<] until-0
until-0-01 ::= | [<] until-0-01 | [\uFF5C] until-0-02 | [^<\uFF5C] until-0
until-0-02 ::= | [<] until-0-01 | [D] until-0-03 | [^<D] until-0
until-0-03 ::= | [<] until-0-01 | [S] until-0-04 | [^<S] until-0
until-0-04 ::= | [<] until-0-01 | [M] until-0-05 | [^<M] until-0
until-0-05 ::= | [<] until-0-01 | [L] until-0-06 | [^<L] until-0
until-0-06 ::= | [<] until-0-01 | [^<\uFF5C] until-0
until-1 ::= | [<] until-1-01 | [^<] until-1
until-1-01 ::= | [<] until-1-01 | [\uFF5C] until-1-02 | [^<\uFF5C] until-1
until-1-02 ::= | [<] until-1-01 | [D] until-1-03 | [^<D] until-1
until-1-03 ::= | [<] until-1-01 | [S] until-1-04 | [^<S] until-1
until-1-04 ::= | [<] until-1-01 | [M] until-1-05 | [^<M] until-1
until-1-05 ::= | [<] until-1-01 | [L] until-1-06 | [^<L] until-1
until-1-06 ::= | [<] until-1-01 | [^<\uFF5C] until-1
)""", gbnf);
});
@@ -203,18 +203,18 @@ void test_gbnf_generation(testing &t) {
});
assert_gbnf_equal(t, R"""(
root ::= until-0
root ::= until-3
space ::= | " " | "\n"{1,2} [ \t]{0,20}
until-0 ::= | [a] until-0-01 | [c] until-0-03 | [e] until-0-05 | [^ace] until-0
until-0-01 ::= | [a] until-0-01 | [c] until-0-03 | [e] until-0-05 | [^abce] until-0
until-0-03 ::= | [a] until-0-01 | [c] until-0-03 | [e] until-0-05 | [^acde] until-0
until-0-05 ::= | [a] until-0-01 | [c] until-0-03 | [e] until-0-05 | [^acef] until-0
until-3 ::= | [a] until-3-01 | [c] until-3-03 | [e] until-3-05 | [^ace] until-3
until-3-01 ::= | [a] until-3-01 | [c] until-3-03 | [e] until-3-05 | [^abce] until-3
until-3-03 ::= | [a] until-3-01 | [c] until-3-03 | [e] until-3-05 | [^acde] until-3
until-3-05 ::= | [a] until-3-01 | [c] until-3-03 | [e] until-3-05 | [^acef] until-3
)""", gbnf);
});
t.test("ac grammar", [](testing &t) {
t.test("through grammar", [](testing &t) {
auto parser = build_peg_parser([](common_peg_parser_builder & p) {
return p.ac(p.until("</tag>") + p.literal("</tag>"), "</tag>");
return p.through(p.until("</tag>"), "</tag>");
});
auto gbnf = build_grammar([&](const common_grammar_builder & builder) {
@@ -222,20 +222,20 @@ void test_gbnf_generation(testing &t) {
});
assert_gbnf_equal(t, R"""(
ac-3 ::= [<] ac-3-01 | [^<] ac-3
ac-3-01 ::= [<] ac-3-01 | [/] ac-3-02 | [^/<] ac-3
ac-3-02 ::= [<] ac-3-01 | [t] ac-3-03 | [^<t] ac-3
ac-3-03 ::= [<] ac-3-01 | [a] ac-3-04 | [^<a] ac-3
ac-3-04 ::= [<] ac-3-01 | [g] ac-3-05 | [^<g] ac-3
ac-3-05 ::= [>] | [<] ac-3-01 | [^<>] ac-3
root ::= ac-3
root ::= until-3
space ::= | " " | "\n"{1,2} [ \t]{0,20}
until-3 ::= [<] until-3-01 | [^<] until-3
until-3-01 ::= [<] until-3-01 | [/] until-3-02 | [^/<] until-3
until-3-02 ::= [<] until-3-01 | [t] until-3-03 | [^<t] until-3
until-3-03 ::= [<] until-3-01 | [a] until-3-04 | [^<a] until-3
until-3-04 ::= [<] until-3-01 | [g] until-3-05 | [^<g] until-3
until-3-05 ::= [>] | [<] until-3-01 | [^<>] until-3
)""", gbnf);
});
t.test("ac grammar terminates at first delimiter", [](testing &t) {
t.test("through grammar terminates at first delimiter", [](testing &t) {
auto parser = build_peg_parser([](common_peg_parser_builder & p) {
return p.ac(p.until("\n</parameter>\n") + p.literal("\n</parameter>\n"), "\n</parameter>\n");
return p.through(p.until("\n</parameter>\n"), "\n</parameter>\n");
});
auto gbnf = build_grammar([&](const common_grammar_builder & builder) {
@@ -243,28 +243,28 @@ void test_gbnf_generation(testing &t) {
});
assert_gbnf_equal(t, R"""(
ac-3 ::= [\n] ac-3-01 | [^\n] ac-3
ac-3-01 ::= [\n] ac-3-01 | [<] ac-3-02 | [^\n<] ac-3
ac-3-02 ::= [\n] ac-3-01 | [/] ac-3-03 | [^\n/] ac-3
ac-3-03 ::= [\n] ac-3-01 | [p] ac-3-04 | [^\np] ac-3
ac-3-04 ::= [\n] ac-3-01 | [a] ac-3-05 | [^\na] ac-3
ac-3-05 ::= [\n] ac-3-01 | [r] ac-3-06 | [^\nr] ac-3
ac-3-06 ::= [\n] ac-3-01 | [a] ac-3-07 | [^\na] ac-3
ac-3-07 ::= [\n] ac-3-01 | [m] ac-3-08 | [^\nm] ac-3
ac-3-08 ::= [\n] ac-3-01 | [e] ac-3-09 | [^\ne] ac-3
ac-3-09 ::= [\n] ac-3-01 | [t] ac-3-10 | [^\nt] ac-3
ac-3-10 ::= [\n] ac-3-01 | [e] ac-3-11 | [^\ne] ac-3
ac-3-11 ::= [\n] ac-3-01 | [r] ac-3-12 | [^\nr] ac-3
ac-3-12 ::= [\n] ac-3-01 | [>] ac-3-13 | [^\n>] ac-3
ac-3-13 ::= [\n] | [^\n] ac-3
root ::= ac-3
root ::= until-3
space ::= | " " | "\n"{1,2} [ \t]{0,20}
until-3 ::= [\n] until-3-01 | [^\n] until-3
until-3-01 ::= [\n] until-3-01 | [<] until-3-02 | [^\n<] until-3
until-3-02 ::= [\n] until-3-01 | [/] until-3-03 | [^\n/] until-3
until-3-03 ::= [\n] until-3-01 | [p] until-3-04 | [^\np] until-3
until-3-04 ::= [\n] until-3-01 | [a] until-3-05 | [^\na] until-3
until-3-05 ::= [\n] until-3-01 | [r] until-3-06 | [^\nr] until-3
until-3-06 ::= [\n] until-3-01 | [a] until-3-07 | [^\na] until-3
until-3-07 ::= [\n] until-3-01 | [m] until-3-08 | [^\nm] until-3
until-3-08 ::= [\n] until-3-01 | [e] until-3-09 | [^\ne] until-3
until-3-09 ::= [\n] until-3-01 | [t] until-3-10 | [^\nt] until-3
until-3-10 ::= [\n] until-3-01 | [e] until-3-11 | [^\ne] until-3
until-3-11 ::= [\n] until-3-01 | [r] until-3-12 | [^\nr] until-3
until-3-12 ::= [\n] until-3-01 | [>] until-3-13 | [^\n>] until-3
until-3-13 ::= [\n] | [^\n] until-3
)""", gbnf);
});
t.test("ac grammar multiple delimiters", [](testing &t) {
t.test("through grammar multiple delimiters", [](testing &t) {
auto parser = build_peg_parser([](common_peg_parser_builder & p) {
return p.ac(p.eps(), std::vector<std::string>{"ab", "cd", "ef"});
return p.through(p.eps(), { { p.literal("ab") }, { p.literal("cd") }, { p.literal("ef") } });
});
auto gbnf = build_grammar([&](const common_grammar_builder & builder) {
@@ -272,12 +272,238 @@ void test_gbnf_generation(testing &t) {
});
assert_gbnf_equal(t, R"""(
ac-1 ::= [a] ac-1-01 | [c] ac-1-03 | [e] ac-1-05 | [^ace] ac-1
ac-1-01 ::= [b] | [a] ac-1-01 | [c] ac-1-03 | [e] ac-1-05 | [^abce] ac-1
ac-1-03 ::= [d] | [a] ac-1-01 | [c] ac-1-03 | [e] ac-1-05 | [^acde] ac-1
ac-1-05 ::= [f] | [a] ac-1-01 | [c] ac-1-03 | [e] ac-1-05 | [^acef] ac-1
root ::= ac-1
root ::= until-4
space ::= | " " | "\n"{1,2} [ \t]{0,20}
until-4 ::= [a] until-4-01 | [c] until-4-03 | [e] until-4-05 | [^ace] until-4
until-4-01 ::= [b] | [a] until-4-01 | [c] until-4-03 | [e] until-4-05 | [^abce] until-4
until-4-03 ::= [d] | [a] until-4-01 | [c] until-4-03 | [e] until-4-05 | [^acde] until-4
until-4-05 ::= [f] | [a] until-4-01 | [c] until-4-03 | [e] until-4-05 | [^acef] until-4
)""", gbnf);
});
t.test("until grammar with parser delimiters", [](testing &t) {
auto parser = build_peg_parser([](common_peg_parser_builder & p) {
return p.until((p.literal("a") + p.literal("b")) | p.literal("cd"));
});
auto gbnf = build_grammar([&](const common_grammar_builder & builder) {
parser.build_grammar(builder);
});
assert_gbnf_equal(t, R"""(
root ::= until-5
space ::= | " " | "\n"{1,2} [ \t]{0,20}
until-5 ::= | [a] until-5-01 | [c] until-5-03 | [^ac] until-5
until-5-01 ::= | [a] until-5-01 | [c] until-5-03 | [^abc] until-5
until-5-03 ::= | [a] until-5-01 | [c] until-5-03 | [^acd] until-5
)""", gbnf);
});
t.test("delimiters see through tags and atomics", [](testing &t) {
auto parser = build_peg_parser([](common_peg_parser_builder & p) {
return p.until(p.tag("open", p.literal("a") + p.atomic(p.literal("b"))));
});
auto gbnf = build_grammar([&](const common_grammar_builder & builder) {
parser.build_grammar(builder);
});
assert_gbnf_equal(t, R"""(
root ::= until-5
space ::= | " " | "\n"{1,2} [ \t]{0,20}
until-5 ::= | [a] until-5-01 | [^a] until-5
until-5-01 ::= | [a] until-5-01 | [^ab] until-5
)""", gbnf);
});
t.test("delimiters expand a choice in a sequence", [](testing &t) {
auto parser = build_peg_parser([](common_peg_parser_builder & p) {
return p.until(p.literal("a") + (p.literal("b") | p.literal("d")));
});
auto gbnf = build_grammar([&](const common_grammar_builder & builder) {
parser.build_grammar(builder);
});
assert_gbnf_equal(t, R"""(
root ::= until-5
space ::= | " " | "\n"{1,2} [ \t]{0,20}
until-5 ::= | [a] until-5-01 | [^a] until-5
until-5-01 ::= | [a] until-5-01 | [^abd] until-5
)""", gbnf);
});
t.test("delimiters reject anything but sequences, choices, literals, and tokens", [](testing &t) {
bool threw = false;
try {
build_peg_parser([](common_peg_parser_builder & p) {
return p.until(p.literal("a") + p.space());
});
} catch (const std::invalid_argument &) {
threw = true;
}
t.assert_equal("space_rejected", true, threw);
});
t.test("through branches reject a shared delimiter", [](testing &t) {
bool threw = false;
try {
build_peg_parser([](common_peg_parser_builder & p) {
return p.through(p.eps(), { { p.literal("ab"), p.literal("x") }, { p.literal("ab"), p.literal("y") } });
});
} catch (const std::invalid_argument &) {
threw = true;
}
t.assert_equal("duplicate_rejected", true, threw);
});
t.test("through grammar branches continue with their rest", [](testing &t) {
auto parser = build_peg_parser([](common_peg_parser_builder & p) {
return p.through(p.eps(), { { p.literal("ab"), p.literal("x") }, { p.literal("cd") } });
});
auto gbnf = build_grammar([&](const common_grammar_builder & builder) {
parser.build_grammar(builder);
});
assert_gbnf_equal(t, R"""(
root ::= until-4
space ::= | " " | "\n"{1,2} [ \t]{0,20}
until-4 ::= [a] until-4-01 | [c] until-4-03 | [^ac] until-4
until-4-01 ::= [b] until-4-rest-0 | [a] until-4-01 | [c] until-4-03 | [^abc] until-4
until-4-03 ::= [d] | [a] until-4-01 | [c] until-4-03 | [^acd] until-4
until-4-rest-0 ::= "x"
)""", gbnf);
});
t.test("through grammar optional may end before a delimiter", [](testing &t) {
auto parser = build_peg_parser([](common_peg_parser_builder & p) {
return p.through(p.eps(), { { p.literal("ab"), p.literal("x") } }, true);
});
auto gbnf = build_grammar([&](const common_grammar_builder & builder) {
parser.build_grammar(builder);
});
assert_gbnf_equal(t, R"""(
root ::= until-3
space ::= | " " | "\n"{1,2} [ \t]{0,20}
until-3 ::= | [a] until-3-01 | [^a] until-3
until-3-01 ::= | [b] until-3-rest-0 | [a] until-3-01 | [^ab] until-3
until-3-rest-0 ::= "x"
)""", gbnf);
});
t.test("trigger rules emit their start and rest when not lazy", [](testing &t) {
auto parser = build_peg_parser([](common_peg_parser_builder & p) {
auto ab = p.literal("ab");
auto cd = p.literal("cd");
auto scan = p.until(ab | cd);
return scan + (p.trigger_rule("one", ab, p.literal("x")) | p.trigger_rule("two", cd, p.literal("y")));
});
auto gbnf = build_grammar([&](const common_grammar_builder & builder) {
parser.build_grammar(builder, false);
});
assert_gbnf_equal(t, R"""(
one ::= "ab" "x"
root ::= until-3 (one | two)
space ::= | " " | "\n"{1,2} [ \t]{0,20}
two ::= "cd" "y"
until-3 ::= | [a] until-3-01 | [c] until-3-03 | [^ac] until-3
until-3-01 ::= | [a] until-3-01 | [c] until-3-03 | [^abc] until-3
until-3-03 ::= | [a] until-3-01 | [c] until-3-03 | [^acd] until-3
)""", gbnf);
});
t.test("trigger rules make the lazy grammar scan for their starts", [](testing &t) {
auto parser = build_peg_parser([](common_peg_parser_builder & p) {
auto ab = p.literal("ab");
auto cd = p.literal("cd");
auto scan = p.until(ab | cd);
return scan + (p.trigger_rule("one", ab, p.literal("x")) | p.trigger_rule("two", cd, p.literal("y")));
});
auto gbnf = build_grammar([&](const common_grammar_builder & builder) {
parser.build_grammar(builder, true);
});
assert_gbnf_equal(t, R"""(
one-rest ::= "x"
root ::= trigger
space ::= | " " | "\n"{1,2} [ \t]{0,20}
trigger ::= | [a] trigger-01 | [c] trigger-03 | [^ac] trigger
trigger-01 ::= | [b] one-rest | [a] trigger-01 | [c] trigger-03 | [^abc] trigger
trigger-03 ::= | [d] two-rest | [a] trigger-01 | [c] trigger-03 | [^acd] trigger
two-rest ::= "y"
)""", gbnf);
});
t.test("scanning grammar lets other named tokens through", [](testing &t) {
common_peg_tokens tokens;
tokens.ids.emplace("<a>", 1);
tokens.ids.emplace("<b>", 2);
tokens.tokens.emplace(1, common_peg_token{ "<a>", LLAMA_TOKEN_ATTR_CONTROL });
tokens.tokens.emplace(2, common_peg_token{ "<b>", LLAMA_TOKEN_ATTR_CONTROL });
common_peg_parser_builder p(tokens);
auto start = p.token("<a>") + p.literal("x");
p.set_root(p.until(start) + p.trigger_rule("tool", start, p.token("<b>")));
auto parser = p.build();
auto gbnf = build_grammar([&](const common_grammar_builder & builder) {
parser.build_grammar(builder, true);
});
assert_gbnf_equal(t, R"""(
root ::= trigger
space ::= | " " | "\n"{1,2} [ \t]{0,20}
tool-rest ::= <[2]>
trigger ::= | <[1]> trigger-01 | (. | <[2]>) trigger
trigger-01 ::= | [x] tool-rest | <[1]> trigger-01 | ([^x] | <[2]>) trigger
)""", gbnf);
});
t.test("token sets", [](testing &t) {
common_peg_tokens tokens;
tokens.ids.emplace("<a>", 1);
tokens.ids.emplace("<b>", 2);
tokens.tokens.emplace(1, common_peg_token{ "<a>", LLAMA_TOKEN_ATTR_CONTROL });
tokens.tokens.emplace(2, common_peg_token{ "<b>", LLAMA_TOKEN_ATTR_CONTROL });
common_peg_parser_builder p(tokens);
p.set_root(p.token({ "<a>", "<b>" }) + p.token({ "<a>", "<b>" }, true));
auto parser = p.build();
auto gbnf = build_grammar([&](const common_grammar_builder & builder) {
parser.build_grammar(builder);
});
assert_gbnf_equal(t, R"""(
root ::= (<[1]> | <[2]>) !<[1-2]>
space ::= | " " | "\n"{1,2} [ \t]{0,20}
)""", gbnf);
});
t.test("through grammar suffix delimiter offers both rests", [](testing &t) {
auto parser = build_peg_parser([](common_peg_parser_builder & p) {
return p.through(p.eps(), { { p.literal("xab"), p.literal("1") }, { p.literal("ab"), p.literal("2") } });
});
auto gbnf = build_grammar([&](const common_grammar_builder & builder) {
parser.build_grammar(builder);
});
assert_gbnf_equal(t, R"""(
root ::= until-5
space ::= | " " | "\n"{1,2} [ \t]{0,20}
until-5 ::= [x] until-5-01 | [a] until-5-04 | [^ax] until-5
until-5-01 ::= [x] until-5-01 | [a] until-5-02 | [^ax] until-5
until-5-02 ::= [b] (until-5-rest-0 | until-5-rest-1) | [x] until-5-01 | [a] until-5-04 | [^abx] until-5
until-5-04 ::= [b] until-5-rest-1 | [x] until-5-01 | [a] until-5-04 | [^abx] until-5
until-5-rest-0 ::= "1"
until-5-rest-1 ::= "2"
)""", gbnf);
});