Handle an invalid escape sequence char

This commit is contained in:
yhirose 2020-01-26 23:00:37 -05:00
parent 35d2d201e1
commit 3dc0205ffa
2 changed files with 30 additions and 4 deletions

View File

@ -383,6 +383,9 @@ inline std::string resolve_escape_sequence(const char* s, size_t n) {
auto ch = s[i];
if (ch == '\\') {
i++;
if (i == n) {
throw std::runtime_error("Invalid escape sequence...");
}
switch (s[i]) {
case 'n': r += '\n'; i++; break;
case 'r': r += '\r'; i++; break;

View File

@ -20,10 +20,10 @@ TEST_CASE("Simple syntax test (with unicode)", "[general]")
TEST_CASE("Simple syntax test", "[general]")
{
peg::parser parser(
" ROOT <- _ "
" _ <- ' ' "
);
peg::parser parser(R"(
ROOT <- _
_ <- ' '
)");
bool ret = parser;
REQUIRE(ret == true);
@ -36,6 +36,28 @@ TEST_CASE("Empty syntax test", "[general]")
REQUIRE(ret == false);
}
TEST_CASE("Backslash escape sequence test", "[general]")
{
peg::parser parser(R"(
ROOT <- _
_ <- '\\'
)");
bool ret = parser;
REQUIRE(ret == true);
}
TEST_CASE("Invalid escape sequence test", "[general]")
{
peg::parser parser(R"(
ROOT <- _
_ <- '\'
)");
bool ret = parser;
REQUIRE(ret == false);
}
TEST_CASE("Infinite loop 1", "[infinite loop]")
{
peg::parser pg(R"(
@ -1727,6 +1749,7 @@ TEST_CASE("PEG Literal", "[peg]")
REQUIRE(exact(g, "Literal", "\"'\"abc\"'\" ") == false);
REQUIRE(exact(g, "Literal", "abc") == false);
REQUIRE(exact(g, "Literal", "") == false);
REQUIRE(exact(g, "Literal", "\\") == false);
REQUIRE(exact(g, "Literal", u8"'日本語'") == true);
REQUIRE(exact(g, "Literal", u8"\"日本語\"") == true);
REQUIRE(exact(g, "Literal", u8"日本語") == false);