Added '\f' and '\v'

This commit is contained in:
yhirose 2022-05-06 15:23:29 -04:00
parent 04b737a515
commit 187ac4849a
2 changed files with 13 additions and 1 deletions

View File

@ -208,9 +208,11 @@ inline std::string escape_characters(const char *s, size_t n) {
for (size_t i = 0; i < n; i++) { for (size_t i = 0; i < n; i++) {
auto c = s[i]; auto c = s[i];
switch (c) { switch (c) {
case '\f': str += "\\f"; break;
case '\n': str += "\\n"; break; case '\n': str += "\\n"; break;
case '\r': str += "\\r"; break; case '\r': str += "\\r"; break;
case '\t': str += "\\t"; break; case '\t': str += "\\t"; break;
case '\v': str += "\\v"; break;
default: str += c; break; default: str += c; break;
} }
} }
@ -280,6 +282,10 @@ inline std::string resolve_escape_sequence(const char *s, size_t n) {
i++; i++;
if (i == n) { throw std::runtime_error("Invalid escape sequence..."); } if (i == n) { throw std::runtime_error("Invalid escape sequence..."); }
switch (s[i]) { switch (s[i]) {
case 'f':
r += '\f';
i++;
break;
case 'n': case 'n':
r += '\n'; r += '\n';
i++; i++;
@ -292,6 +298,10 @@ inline std::string resolve_escape_sequence(const char *s, size_t n) {
r += '\t'; r += '\t';
i++; i++;
break; break;
case 'v':
r += '\v';
i++;
break;
case '\'': case '\'':
r += '\''; r += '\'';
i++; i++;
@ -3137,7 +3147,7 @@ private:
g["Range"] <= cho(seq(g["Char"], chr('-'), npd(chr(']')), g["Char"]), g["Char"]); g["Range"] <= cho(seq(g["Char"], chr('-'), npd(chr(']')), g["Char"]), g["Char"]);
g["Char"] <= g["Char"] <=
cho(seq(chr('\\'), cls("nrt'\"[]\\^")), cho(seq(chr('\\'), cls("fnrtv'\"[]\\^")),
seq(chr('\\'), cls("0-3"), cls("0-7"), cls("0-7")), seq(chr('\\'), cls("0-3"), cls("0-7"), cls("0-7")),
seq(chr('\\'), cls("0-7"), opt(cls("0-7"))), seq(chr('\\'), cls("0-7"), opt(cls("0-7"))),
seq(lit("\\x"), cls("0-9a-fA-F"), opt(cls("0-9a-fA-F"))), seq(lit("\\x"), cls("0-9a-fA-F"), opt(cls("0-9a-fA-F"))),

View File

@ -205,9 +205,11 @@ TEST(LeftRecursiveTest, PEG_Range) {
TEST(LeftRecursiveTest, PEG_Char) { TEST(LeftRecursiveTest, PEG_Char) {
auto g = ParserGenerator::grammar(); auto g = ParserGenerator::grammar();
EXPECT_TRUE(exact(g, "Char", "\\f"));
EXPECT_TRUE(exact(g, "Char", "\\n")); EXPECT_TRUE(exact(g, "Char", "\\n"));
EXPECT_TRUE(exact(g, "Char", "\\r")); EXPECT_TRUE(exact(g, "Char", "\\r"));
EXPECT_TRUE(exact(g, "Char", "\\t")); EXPECT_TRUE(exact(g, "Char", "\\t"));
EXPECT_TRUE(exact(g, "Char", "\\v"));
EXPECT_TRUE(exact(g, "Char", "\\'")); EXPECT_TRUE(exact(g, "Char", "\\'"));
EXPECT_TRUE(exact(g, "Char", "\\\"")); EXPECT_TRUE(exact(g, "Char", "\\\""));
EXPECT_TRUE(exact(g, "Char", "\\[")); EXPECT_TRUE(exact(g, "Char", "\\["));