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];
|
auto ch = s[i];
|
||||||
if (ch == '\\') {
|
if (ch == '\\') {
|
||||||
i++;
|
i++;
|
||||||
|
if (i == n) {
|
||||||
|
throw std::runtime_error("Invalid escape sequence...");
|
||||||
|
}
|
||||||
switch (s[i]) {
|
switch (s[i]) {
|
||||||
case 'n': r += '\n'; i++; break;
|
case 'n': r += '\n'; i++; break;
|
||||||
case 'r': r += '\r'; 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]")
|
TEST_CASE("Simple syntax test", "[general]")
|
||||||
{
|
{
|
||||||
peg::parser parser(
|
peg::parser parser(R"(
|
||||||
" ROOT <- _ "
|
ROOT <- _
|
||||||
" _ <- ' ' "
|
_ <- ' '
|
||||||
);
|
)");
|
||||||
|
|
||||||
bool ret = parser;
|
bool ret = parser;
|
||||||
REQUIRE(ret == true);
|
REQUIRE(ret == true);
|
||||||
@ -36,6 +36,28 @@ TEST_CASE("Empty syntax test", "[general]")
|
|||||||
REQUIRE(ret == false);
|
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]")
|
TEST_CASE("Infinite loop 1", "[infinite loop]")
|
||||||
{
|
{
|
||||||
peg::parser pg(R"(
|
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", "abc") == false);
|
REQUIRE(exact(g, "Literal", "abc") == false);
|
||||||
REQUIRE(exact(g, "Literal", "") == 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"\"日本語\"") == true);
|
REQUIRE(exact(g, "Literal", u8"\"日本語\"") == true);
|
||||||
REQUIRE(exact(g, "Literal", u8"日本語") == false);
|
REQUIRE(exact(g, "Literal", u8"日本語") == false);
|
||||||
|
Loading…
Reference in New Issue
Block a user