mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-11-13 22:55:29 +00:00
Handle an invalid escape sequence char
This commit is contained in:
parent
35d2d201e1
commit
3dc0205ffa
3
peglib.h
3
peglib.h
@ -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;
|
||||
|
31
test/test.cc
31
test/test.cc
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user