json-schema: control-character escapes match their JSON escape; other class escapes are refused

Inside a class, escapes GBNF does not know ([\b], [\f], [\v], [\0], an escaped space, \p{..}, \c,
a backreference) were copied raw and the grammar then failed to load, an internal error on a
schema the converter had accepted.

A JSON string holds a control character only as an escape, so \t \n \r \f \v \0 (and \b in a
class) now match that escape, in a positive class as alternatives beside it and in a negated
class by leaving the escape out. Outside a class the escape is a group, so a quantifier takes all
of it. An escaped space or non-ASCII character is itself; \x and \u in a class need their hex
digits. Any other class escape is refused at conversion with a message naming it.
vendor/patches/0020, vendor/UPSTREAM; gbnf_test cases.
This commit is contained in:
davetha authored and StillDeadcode committed 2026-10-19 10:38:06 +02:00
1 parent 12735a62dc
commit 8015d9a639
4 files changed
+279 -14

No files matched your search

+24 -1
View File
@@ -1728,6 +1728,23 @@ TEST(json_schema_grammars_admit_exactly_their_instances) {
{ R"({"type":"string","pattern":"^[\\--z]$"})", { R"("-")", R"("a")" }, { R"("+")" } },
{ R"({"type":"string","pattern":"^[\\.]$"})", { R"(".")" }, { R"("a")" } },
{ R"({"type":"string","pattern":"a\\-b"})", { R"("xa-by")" }, { R"("ab")" } },
/* control-character escapes (0020): a JSON string holds a tab or a backspace only as an
* escape, so [\t] [\b] [\f] [\v] [\0] and \t outside a class match the escape, and an
* escaped space or non-ASCII character is itself. */
{ R"({"type":"string","pattern":"^[\\b]$"})", { R"("\b")" }, { R"("b")", R"("\\")" } },
{ R"({"type":"string","pattern":"^[a\\t]+$"})", { R"("a\ta")", R"("a")" }, { R"("t")", R"("a b")" } },
{ R"({"type":"string","pattern":"^[\\f\\v]$"})", { R"("\f")", R"("\u000b")", R"("\u000B")" }, { R"("f")", R"("v")" } },
{ R"({"type":"string","pattern":"^[\\0]$"})", { R"("\u0000")" }, { R"("0")" } },
{ R"({"type":"string","pattern":"^a\\tb$"})", { R"("a\tb")" }, { R"("atb")" } },
{ R"({"type":"string","pattern":"^[a\\ ]+$"})", { R"("a a")" }, { R"("ab")" } },
{ R"({"type":"string","pattern":"^a\\ b$"})", { R"("a b")" }, { R"("ab")" } },
{ R"({"type":"string","pattern":"^[\\s]$"})", { R"(" ")", R"("\n")" }, { R"("n")" } },
{ R"({"type":"string","pattern":"^[^\\t]$"})", { R"("a")" }, { R"("")" } },
{ R"({"type":"string","pattern":"^a\\t+b$"})", { R"("a\t\tb")" }, { R"("a\tttb")", R"("ab")" } },
{ R"({"type":"string","pattern":"^x\\v{2}$"})", { R"("x\u000b\u000B")" }, { R"("x\u000b")" } },
{ R"({"type":"string","pattern":"^[\\t]{2}$"})", { R"("\t\t")" }, { R"("\t")" } },
{ R"({"type":"string","pattern":"^[^\\n]+$"})", { R"("ab")", R"("a\tb")" }, { R"("a\nb")" } },
{ R"({"type":"string","pattern":"^[^\\s]+$"})", { R"("ab")" }, { R"("\t")", R"("a b")" } },
{ R"({"type":"string","pattern":"^(?:[A-Za-z0-9_'+\\-]+\\.)*[A-Za-z0-9_'+\\-]*[A-Za-z0-9_+-]@(?:[A-Za-z0-9][A-Za-z0-9\\-]*\\.)+[A-Za-z]{2,}$"})",
{ R"("a.b@c.io")", R"("x-y@d-e.org")" }, { R"("a@b")", R"("a b@c.io")" } },
{ R"({"type":"array","items":{"type":"integer"},"minItems":1,"maxItems":3})",
@@ -1751,7 +1768,13 @@ TEST(json_schema_grammars_admit_exactly_their_instances) {
"on \"number\"\n");
for (const char* schema : { R"({"type":"string","pattern":"^a|b"})",
R"({"type":"string","pattern":"a|b$"})",
R"({"type":"string","pattern":"^[a\\S]$"})" }) {
R"({"type":"string","pattern":"^[a\\S]$"})",
R"({"type":"string","pattern":"^[\\p{L}]$"})",
R"({"type":"string","pattern":"^[\\cA]$"})",
R"({"type":"string","pattern":"^[a\\1]$"})",
R"({"type":"string","pattern":"^[\\01]$"})",
R"({"type":"string","pattern":"^[\\xZ]$"})",
R"({"type":"string","pattern":"^[\\u{41}]$"})" }) {
std::string gbnf, err;
CHECK(grammar_from_json_schema(schema, "root", &gbnf, &err) < 0);
}