mirror of
https://github.com/ikawrakow/ik_llama.cpp.git
synced 2026-03-10 14:00:08 +00:00
--------- Co-authored-by: Piotr Wilkin <piotr.wilkin@syndatis.com> common : add nemotron 3 parsing (#18077) common : add parser for ministral/mistral large 3/devstral 2 (#17713) common : default content to an empty string (#18485) chat: make tool description and parameters optional per OpenAI spec (#18478) Per the OpenAI API specification, both 'description' and 'parameters' fields in tool function definitions are optional. Previously, the parser would throw an exception if these fields were missing. Attempts to fix #17667 common : implement new jinja template engine (#18462) --------- Co-authored-by: Alde Rojas <hello@alde.dev> Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> Co-authored-by: Georgi Gerganov <ggerganov@gmail.com> jinja: correct member access rule (#18905) jinja : fix lexing of float literals with sign (#18901) jinja : add missing tojson filter for bool (#18900) jinja : attribute support for join, map and sort (#18883) jinja : fix object item order (and properly implement dictsort) (#18904) tests : add test-jinja -py option for cross-checking (#18906) Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> --------- Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> ci : run test-jinja -py on high perf [no ci] (#18916) jinja : fix undefined keys and attributes and int/float as bool (#18924) jinja: support none|string (#18995) Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> --------- Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> jinja : implement mixed type object keys (#18955) --------- Co-authored-by: Xuan Son Nguyen <son@huggingface.co> jinja : undefined should be treated as sequence/iterable (return string/array) by filters/tests (#19147) `tojson` is not a supported `undefined` filter keep it DRY and fix some types jinja : do not pass empty tools and add some none filters (#19176) jinja : add unordered_map include to value.h [no ci] (#19205) jinja : add missing 'in' test to template engine (#19004) (#19239) The jinja template parser was missing the 'in' test from global_builtins(), causing templates using reject("in", ...), select("in", ...), or 'x is in(y)' to fail with "selectattr: unknown test 'in'". This broke tool-calling for Qwen3-Coder and any other model whose chat template uses the 'in' test. Added test_is_in supporting array, string, and object containment checks, mirroring the existing 'in' operator logic in runtime.cpp. Includes test cases for all three containment types plus reject/select filter usage. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Sid Mohan <sidmohan0@users.noreply.github.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: Xuan Son Nguyen <son@huggingface.co> Add Jinja support for "indent" string filter (#19529) Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> --------- Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com> add vendor refactor chat server : support preserving reasoning_content in assistant message (#18994) chat : fix translategemma crash on common_chat_format_example (#19019) chat: fix language input for translategemma (#19052) Co-authored-by: Aldehir Rojas <hello@alde.dev> --------- Co-authored-by: Aldehir Rojas <hello@alde.dev> chat: fix case where template accepts type content only (#19419) mtmd : chat : Fix extra \n between text and media marker (#19595) Thanks to @tugot17 for detecting and reporting the issue. For vision models (e.g. LFM2.5-VL-1.6B and Qwen/Qwen3-VL-4B-Instruct) `llama-mtmd-cli` produces identical output to HF implementation. However `llama-server` doesn't. I traced it down to extra newline inserted after `<__media__>`. This happens in `to_json_oaicompat`, that treats media markers as text and joins all parts with `\n` separator. PR introduces new type `media_marker` and uses it for media markers. Extra logic is added to prevent insertion of newlines before and after media markers. With this change number of input tokens is identical to HF implementation and as a result the output is also identical. I explored other ways to address the issue * remove completely `\n` between text parts in `to_json_oaicompat` * merge text messages in server-common.cpp before sending them to `to_json_oaicompat` Please propose alternative ways of fixing this issue. Co-authored-by: Piotr Wilkin (ilintar) <piotr.wilkin@syndatis.com> --------- Co-authored-by: Piotr Wilkin (ilintar) <piotr.wilkin@syndatis.com> common : merge qwen3-coder and nemotron nano 3 parsers (#19765) common : fix improper trimming in XML parser on complete message (#19805) Co-authored-by: Jules LEIDELINGER <11395311+julio75012@users.noreply.github.com> jinja: correct stats for tojson and string filters (#19785) jinja : correct default size for string slices (#19913) common : handle unicode during partial json parsing (#16526) common : fix json schema with '\' in literals (#17307) add back qwen_coder_xml and mirothinker Co-authored-by: Aldehir Rojas <hello@alde.dev>
450 lines
20 KiB
C++
450 lines
20 KiB
C++
#include "tests.h"
|
|
|
|
#include "peg-parser.h"
|
|
|
|
#include <string>
|
|
#include <sstream>
|
|
#include <iomanip>
|
|
#include <cctype>
|
|
|
|
static void assert_result_equal(testing & t, common_peg_parse_result_type expected, common_peg_parse_result_type actual) {
|
|
t.assert_equal(common_peg_parse_result_type_name(expected), common_peg_parse_result_type_name(actual));
|
|
}
|
|
|
|
static std::string hex_dump(const std::string& str) {
|
|
std::ostringstream oss;
|
|
for (unsigned char c : str) {
|
|
if (std::isprint(c)) {
|
|
oss << c;
|
|
} else {
|
|
oss << "\\x" << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(c);
|
|
}
|
|
}
|
|
return oss.str();
|
|
}
|
|
|
|
void test_unicode(testing &t) {
|
|
struct test_case {
|
|
std::string input;
|
|
std::string expected_text;
|
|
common_peg_parse_result_type expected_result;
|
|
};
|
|
|
|
t.test("any", [](testing &t) {
|
|
std::vector<test_case> test_cases {
|
|
// Valid UTF-8 sequences
|
|
{"Hello", "Hello", COMMON_PEG_PARSE_RESULT_SUCCESS},
|
|
{std::string("Caf\xC3\xA9"), std::string("Caf\xC3\xA9"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
|
{std::string("\xE4\xBD\xA0\xE5\xA5\xBD"), std::string("\xE4\xBD\xA0\xE5\xA5\xBD"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
|
{std::string("\xF0\x9F\x9A\x80"), std::string("\xF0\x9F\x9A\x80"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
|
|
|
// Incomplete UTF-8 sequences (partial bytes at end)
|
|
{std::string("Caf\xC3"), "Caf", COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
|
{std::string("\xE4\xBD"), "", COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
|
{std::string("\xF0\x9F\x9A"), "", COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
|
|
|
// Invalid/malformed UTF-8 sequences
|
|
{std::string("\xFF\xFE"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
|
{std::string("Hello\x80World"), "Hello", COMMON_PEG_PARSE_RESULT_FAIL},
|
|
{std::string("\xC3\x28"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
|
};
|
|
|
|
auto parser = build_peg_parser([](common_peg_parser_builder& p) {
|
|
return p.sequence({p.one_or_more(p.any()), p.end()});
|
|
});
|
|
|
|
for (size_t i = 0; i < test_cases.size(); i++) {
|
|
const auto & tc = test_cases[i];
|
|
std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input);
|
|
|
|
t.test(test_name, [&](testing &t) {
|
|
common_peg_parse_context ctx(tc.input, true);
|
|
auto result = parser.parse(ctx);
|
|
|
|
// Assert result type matches
|
|
assert_result_equal(t, tc.expected_result, result.type);
|
|
|
|
// Assert matched text if success or need_more_input
|
|
if (result.success() || result.need_more_input()) {
|
|
std::string matched = tc.input.substr(result.start, result.end - result.start);
|
|
t.assert_equal(tc.expected_text, matched);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
t.test("char classes", [](testing &t) {
|
|
t.test("unicode range U+4E00-U+9FFF (CJK)", [](testing &t) {
|
|
std::vector<test_case> test_cases {
|
|
// Within range - CJK Unified Ideographs
|
|
{std::string("\xE4\xB8\x80"), std::string("\xE4\xB8\x80"), COMMON_PEG_PARSE_RESULT_SUCCESS}, // U+4E00
|
|
{std::string("\xE4\xBD\xA0"), std::string("\xE4\xBD\xA0"), COMMON_PEG_PARSE_RESULT_SUCCESS}, // U+4F60
|
|
{std::string("\xE5\xA5\xBD"), std::string("\xE5\xA5\xBD"), COMMON_PEG_PARSE_RESULT_SUCCESS}, // U+597D
|
|
{std::string("\xE9\xBF\xBF"), std::string("\xE9\xBF\xBF"), COMMON_PEG_PARSE_RESULT_SUCCESS}, // U+9FFF
|
|
|
|
// Outside range - should fail
|
|
{"a", "", COMMON_PEG_PARSE_RESULT_FAIL}, // ASCII
|
|
{std::string("\xE4\xB7\xBF"), "", COMMON_PEG_PARSE_RESULT_FAIL}, // U+4DFF (before range)
|
|
{std::string("\xEA\x80\x80"), "", COMMON_PEG_PARSE_RESULT_FAIL}, // U+A000 (after range)
|
|
|
|
// Incomplete sequences in range
|
|
{std::string("\xE4\xB8"), "", COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT}, // Incomplete U+4E00
|
|
{std::string("\xE5\xA5"), "", COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT}, // Incomplete U+597D
|
|
};
|
|
|
|
auto parser = build_peg_parser([](common_peg_parser_builder& p) {
|
|
return p.sequence({p.chars(R"([\u4E00-\u9FFF])"), p.end()});
|
|
});
|
|
|
|
for (size_t i = 0; i < test_cases.size(); i++) {
|
|
const auto & tc = test_cases[i];
|
|
std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input);
|
|
|
|
t.test(test_name, [&](testing &t) {
|
|
common_peg_parse_context ctx(tc.input, true);
|
|
auto result = parser.parse(ctx);
|
|
|
|
// Assert result type matches
|
|
assert_result_equal(t, tc.expected_result, result.type);
|
|
|
|
// Assert matched text if success or need_more_input
|
|
if (result.success() || result.need_more_input()) {
|
|
std::string matched = tc.input.substr(result.start, result.end - result.start);
|
|
t.assert_equal(tc.expected_text, matched);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
t.test("unicode range U+1F600-U+1F64F (emoticons)", [](testing &t) {
|
|
std::vector<test_case> test_cases {
|
|
// Within range - Emoticons (all 4-byte UTF-8)
|
|
{std::string("\xF0\x9F\x98\x80"), std::string("\xF0\x9F\x98\x80"), COMMON_PEG_PARSE_RESULT_SUCCESS}, // U+1F600
|
|
{std::string("\xF0\x9F\x98\x81"), std::string("\xF0\x9F\x98\x81"), COMMON_PEG_PARSE_RESULT_SUCCESS}, // U+1F601
|
|
{std::string("\xF0\x9F\x99\x8F"), std::string("\xF0\x9F\x99\x8F"), COMMON_PEG_PARSE_RESULT_SUCCESS}, // U+1F64F
|
|
|
|
// Outside range
|
|
{std::string("\xF0\x9F\x97\xBF"), "", COMMON_PEG_PARSE_RESULT_FAIL}, // U+1F5FF (before range)
|
|
{std::string("\xF0\x9F\x99\x90"), "", COMMON_PEG_PARSE_RESULT_FAIL}, // U+1F650 (after range)
|
|
{std::string("\xF0\x9F\x9A\x80"), "", COMMON_PEG_PARSE_RESULT_FAIL}, // U+1F680 (outside range)
|
|
|
|
// Incomplete sequences
|
|
{std::string("\xF0\x9F\x98"), "", COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT}, // Incomplete emoji
|
|
{std::string("\xF0\x9F"), "", COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT}, // Very incomplete
|
|
};
|
|
|
|
auto parser = build_peg_parser([](common_peg_parser_builder& p) {
|
|
return p.sequence({p.chars(R"([\U0001F600-\U0001F64F])"), p.end()});
|
|
});
|
|
|
|
for (size_t i = 0; i < test_cases.size(); i++) {
|
|
const auto & tc = test_cases[i];
|
|
std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input);
|
|
|
|
t.test(test_name, [&](testing &t) {
|
|
common_peg_parse_context ctx(tc.input, true);
|
|
auto result = parser.parse(ctx);
|
|
|
|
// Assert result type matches
|
|
assert_result_equal(t, tc.expected_result, result.type);
|
|
|
|
// Assert matched text if success or need_more_input
|
|
if (result.success() || result.need_more_input()) {
|
|
std::string matched = tc.input.substr(result.start, result.end - result.start);
|
|
t.assert_equal(tc.expected_text, matched);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
t.test("mixed unicode ranges", [](testing &t) {
|
|
std::vector<test_case> test_cases {
|
|
// Match CJK
|
|
{std::string("\xE4\xB8\x80"), std::string("\xE4\xB8\x80"), COMMON_PEG_PARSE_RESULT_SUCCESS}, // U+4E00
|
|
{std::string("\xE4\xBD\xA0"), std::string("\xE4\xBD\xA0"), COMMON_PEG_PARSE_RESULT_SUCCESS}, // U+4F60
|
|
|
|
// Match emoticons
|
|
{std::string("\xF0\x9F\x98\x80"), std::string("\xF0\x9F\x98\x80"), COMMON_PEG_PARSE_RESULT_SUCCESS}, // U+1F600
|
|
|
|
// Match ASCII digits
|
|
{"5", "5", COMMON_PEG_PARSE_RESULT_SUCCESS},
|
|
|
|
// Don't match outside any range
|
|
{"a", "", COMMON_PEG_PARSE_RESULT_FAIL},
|
|
{std::string("\xF0\x9F\x9A\x80"), "", COMMON_PEG_PARSE_RESULT_FAIL}, // U+1F680
|
|
|
|
// Incomplete
|
|
{std::string("\xE4\xB8"), "", COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
|
{std::string("\xF0\x9F\x98"), "", COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
|
};
|
|
|
|
auto parser = build_peg_parser([](common_peg_parser_builder& p) {
|
|
return p.sequence({p.chars(R"([\u4E00-\u9FFF\U0001F600-\U0001F64F0-9])"), p.end()});
|
|
});
|
|
|
|
for (size_t i = 0; i < test_cases.size(); i++) {
|
|
const auto & tc = test_cases[i];
|
|
std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input);
|
|
|
|
t.test(test_name, [&](testing &t) {
|
|
common_peg_parse_context ctx(tc.input, true);
|
|
auto result = parser.parse(ctx);
|
|
|
|
// Assert result type matches
|
|
assert_result_equal(t, tc.expected_result, result.type);
|
|
|
|
// Assert matched text if success or need_more_input
|
|
if (result.success() || result.need_more_input()) {
|
|
std::string matched = tc.input.substr(result.start, result.end - result.start);
|
|
t.assert_equal(tc.expected_text, matched);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
t.test("until parser", [](testing &t) {
|
|
t.test("ASCII delimiter with Unicode content", [](testing &t) {
|
|
std::vector<test_case> test_cases {
|
|
// CJK characters before delimiter
|
|
{std::string("\xE4\xBD\xA0\xE5\xA5\xBD</tag>"), std::string("\xE4\xBD\xA0\xE5\xA5\xBD"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
|
|
|
// Emoji before delimiter
|
|
{std::string("\xF0\x9F\x98\x80</tag>"), std::string("\xF0\x9F\x98\x80"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
|
|
|
// Mixed content
|
|
{std::string("Hello \xE4\xB8\x96\xE7\x95\x8C!</tag>"), std::string("Hello \xE4\xB8\x96\xE7\x95\x8C!"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
|
};
|
|
|
|
auto parser = build_peg_parser([](common_peg_parser_builder& p) {
|
|
return p.until("</tag>");
|
|
});
|
|
|
|
for (size_t i = 0; i < test_cases.size(); i++) {
|
|
const auto & tc = test_cases[i];
|
|
std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input);
|
|
|
|
t.test(test_name, [&](testing &t) {
|
|
common_peg_parse_context ctx(tc.input, false);
|
|
auto result = parser.parse(ctx);
|
|
|
|
assert_result_equal(t, tc.expected_result, result.type);
|
|
|
|
if (result.success()) {
|
|
std::string matched = tc.input.substr(result.start, result.end - result.start);
|
|
t.assert_equal(tc.expected_text, matched);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
t.test("incomplete UTF-8 at end", [](testing &t) {
|
|
std::vector<test_case> test_cases {
|
|
// Incomplete emoji at end, no delimiter
|
|
{std::string("content\xF0\x9F\x98"), std::string("content"), COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
|
|
|
// Incomplete CJK at end, no delimiter
|
|
{std::string("hello\xE4\xB8"), std::string("hello"), COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
|
|
|
// Complete content, no delimiter (should consume all valid UTF-8)
|
|
{std::string("\xE4\xBD\xA0\xE5\xA5\xBD"), std::string("\xE4\xBD\xA0\xE5\xA5\xBD"), COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
|
};
|
|
|
|
auto parser = build_peg_parser([](common_peg_parser_builder& p) {
|
|
return p.until("</tag>");
|
|
});
|
|
|
|
for (size_t i = 0; i < test_cases.size(); i++) {
|
|
const auto & tc = test_cases[i];
|
|
std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input);
|
|
|
|
t.test(test_name, [&](testing &t) {
|
|
common_peg_parse_context ctx(tc.input, true);
|
|
auto result = parser.parse(ctx);
|
|
|
|
assert_result_equal(t, tc.expected_result, result.type);
|
|
|
|
if (result.success() || result.need_more_input()) {
|
|
std::string matched = tc.input.substr(result.start, result.end - result.start);
|
|
t.assert_equal(tc.expected_text, matched);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
t.test("malformed UTF-8", [](testing &t) {
|
|
std::vector<test_case> test_cases {
|
|
// Invalid UTF-8 bytes
|
|
{std::string("Hello\xFF\xFE"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
|
|
|
// Continuation byte without lead byte
|
|
{std::string("Hello\x80World"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
|
|
|
// Invalid continuation byte
|
|
{std::string("\xC3\x28"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
|
};
|
|
|
|
auto parser = build_peg_parser([](common_peg_parser_builder& p) {
|
|
return p.until("</tag>");
|
|
});
|
|
|
|
for (size_t i = 0; i < test_cases.size(); i++) {
|
|
const auto & tc = test_cases[i];
|
|
std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input);
|
|
|
|
t.test(test_name, [&](testing &t) {
|
|
common_peg_parse_context ctx(tc.input, false);
|
|
auto result = parser.parse(ctx);
|
|
|
|
assert_result_equal(t, tc.expected_result, result.type);
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
t.test("json_string parser", [](testing &t) {
|
|
t.test("valid UTF-8 characters", [](testing &t) {
|
|
std::vector<test_case> test_cases {
|
|
// ASCII only
|
|
{"Hello World\"", "Hello World", COMMON_PEG_PARSE_RESULT_SUCCESS},
|
|
|
|
// 2-byte UTF-8 (accented characters)
|
|
{std::string("Caf\xC3\xA9\""), std::string("Caf\xC3\xA9"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
|
|
|
// 3-byte UTF-8 (CJK)
|
|
{std::string("\xE4\xBD\xA0\xE5\xA5\xBD\""), std::string("\xE4\xBD\xA0\xE5\xA5\xBD"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
|
|
|
// 4-byte UTF-8 (emoji)
|
|
{std::string("\xF0\x9F\x98\x80\""), std::string("\xF0\x9F\x98\x80"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
|
|
|
// Mixed content
|
|
{std::string("Hello \xE4\xB8\x96\xE7\x95\x8C!\""), std::string("Hello \xE4\xB8\x96\xE7\x95\x8C!"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
|
};
|
|
|
|
for (size_t i = 0; i < test_cases.size(); i++) {
|
|
const auto & tc = test_cases[i];
|
|
std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input);
|
|
|
|
t.test(test_name, [&](testing &t) {
|
|
auto parser = build_peg_parser([](common_peg_parser_builder& p) {
|
|
return p.sequence({p.json_string_content(), p.literal("\"")});
|
|
});
|
|
|
|
common_peg_parse_context ctx(tc.input, false);
|
|
auto result = parser.parse(ctx);
|
|
|
|
assert_result_equal(t, tc.expected_result, result.type);
|
|
|
|
if (result.success()) {
|
|
std::string matched = tc.input.substr(result.start, result.end - result.start - 1); // -1 to exclude closing quote
|
|
t.assert_equal(tc.expected_text, matched);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
t.test("incomplete UTF-8", [](testing &t) {
|
|
std::vector<test_case> test_cases {
|
|
// Incomplete 2-byte sequence
|
|
{std::string("Caf\xC3"), std::string("Caf"), COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
|
|
|
// Incomplete 3-byte sequence
|
|
{std::string("Hello\xE4\xB8"), std::string("Hello"), COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
|
|
|
// Incomplete 4-byte sequence
|
|
{std::string("Text\xF0\x9F\x98"), std::string("Text"), COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
|
|
|
// Incomplete at very start
|
|
{std::string("\xE4\xBD"), std::string(""), COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
|
};
|
|
|
|
for (size_t i = 0; i < test_cases.size(); i++) {
|
|
const auto & tc = test_cases[i];
|
|
std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input);
|
|
|
|
t.test(test_name, [&](testing &t) {
|
|
auto parser = build_peg_parser([](common_peg_parser_builder& p) {
|
|
return p.json_string_content();
|
|
});
|
|
|
|
common_peg_parse_context ctx(tc.input, true);
|
|
auto result = parser.parse(ctx);
|
|
|
|
assert_result_equal(t, tc.expected_result, result.type);
|
|
|
|
if (result.need_more_input()) {
|
|
std::string matched = tc.input.substr(result.start, result.end - result.start);
|
|
t.assert_equal(tc.expected_text, matched);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
t.test("malformed UTF-8", [](testing &t) {
|
|
std::vector<test_case> test_cases {
|
|
// Invalid UTF-8 bytes
|
|
{std::string("Hello\xFF\xFE"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
|
|
|
// Continuation byte without lead byte
|
|
{std::string("Hello\x80World"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
|
|
|
// Invalid continuation byte
|
|
{std::string("\xC3\x28"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
|
|
|
// Overlong encoding (security issue)
|
|
{std::string("\xC0\x80"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
|
};
|
|
|
|
for (size_t i = 0; i < test_cases.size(); i++) {
|
|
const auto & tc = test_cases[i];
|
|
std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input);
|
|
|
|
t.test(test_name, [&](testing &t) {
|
|
auto parser = build_peg_parser([](common_peg_parser_builder& p) {
|
|
return p.json_string_content();
|
|
});
|
|
|
|
common_peg_parse_context ctx(tc.input, false);
|
|
auto result = parser.parse(ctx);
|
|
|
|
assert_result_equal(t, tc.expected_result, result.type);
|
|
});
|
|
}
|
|
});
|
|
|
|
t.test("escape sequences with UTF-8", [](testing &t) {
|
|
std::vector<test_case> test_cases {
|
|
// Unicode escape sequence
|
|
{"Hello\\u0041\"", "Hello\\u0041", COMMON_PEG_PARSE_RESULT_SUCCESS},
|
|
|
|
// Mix of UTF-8 and escape sequences
|
|
{std::string("\xE4\xBD\xA0\\n\xE5\xA5\xBD\""), std::string("\xE4\xBD\xA0\\n\xE5\xA5\xBD"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
|
|
|
// Escaped quote in UTF-8 string
|
|
{std::string("\xE4\xBD\xA0\\\"\xE5\xA5\xBD\""), std::string("\xE4\xBD\xA0\\\"\xE5\xA5\xBD"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
|
};
|
|
|
|
for (size_t i = 0; i < test_cases.size(); i++) {
|
|
const auto & tc = test_cases[i];
|
|
std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input);
|
|
|
|
t.test(test_name, [&](testing &t) {
|
|
auto parser = build_peg_parser([](common_peg_parser_builder& p) {
|
|
return p.sequence({p.json_string_content(), p.literal("\"")});
|
|
});
|
|
|
|
common_peg_parse_context ctx(tc.input, false);
|
|
auto result = parser.parse(ctx);
|
|
|
|
assert_result_equal(t, tc.expected_result, result.type);
|
|
|
|
if (result.success()) {
|
|
std::string matched = tc.input.substr(result.start, result.end - result.start - 1); // -1 to exclude closing quote
|
|
t.assert_equal(tc.expected_text, matched);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
});
|
|
}
|