diff --git a/peglib.h b/peglib.h index b49e000..41c2588 100644 --- a/peglib.h +++ b/peglib.h @@ -3132,7 +3132,10 @@ private: tok(oom(seq(npd(chr(']')), g["Range"]))), chr(']'), g["Spacing"]); - g["Range"] <= cho(seq(g["Char"], chr('-'), g["Char"]), g["Char"]); + // NOTE: This is different from The original Brian Ford's paper, and this + // modification allows us to specify `[+-]` as a valid char class. + g["Range"] <= cho(seq(g["Char"], chr('-'), npd(chr(']')), g["Char"]), g["Char"]); + g["Char"] <= cho(seq(chr('\\'), cls("nrt'\"[]\\^")), seq(chr('\\'), cls("0-3"), cls("0-7"), cls("0-7")), diff --git a/test/test3.cc b/test/test3.cc index 1d42559..9cd2340 100644 --- a/test/test3.cc +++ b/test/test3.cc @@ -159,7 +159,7 @@ TEST(LeftRecursiveTest, PEG_Class) { EXPECT_TRUE(exact(g, "Class", "[az]")); EXPECT_TRUE(exact(g, "Class", "[a-zA-Z-]")); EXPECT_TRUE(exact(g, "Class", "[a-zA-Z-0-9]")); - EXPECT_FALSE(exact(g, "Class", "[a-]")); + EXPECT_TRUE(exact(g, "Class", "[a-]")); EXPECT_TRUE(exact(g, "Class", "[-a]")); EXPECT_FALSE(exact(g, "Class", "[")); EXPECT_FALSE(exact(g, "Class", "[a")); @@ -168,7 +168,7 @@ TEST(LeftRecursiveTest, PEG_Class) { EXPECT_TRUE(exact(g, "Class", u8"[あ-ん]")); EXPECT_FALSE(exact(g, "Class", u8"あ-ん")); EXPECT_TRUE(exact(g, "Class", "[-+]")); - EXPECT_FALSE(exact(g, "Class", "[+-]")); + EXPECT_TRUE(exact(g, "Class", "[+-]")); EXPECT_TRUE(exact(g, "Class", "[\\^]")); } @@ -180,7 +180,7 @@ TEST(LeftRecursiveTest, PEG_Negated_Class) { EXPECT_TRUE(exact(g, "NegatedClass", "[^az]")); EXPECT_TRUE(exact(g, "NegatedClass", "[^a-zA-Z-]")); EXPECT_TRUE(exact(g, "NegatedClass", "[^a-zA-Z-0-9]")); - EXPECT_FALSE(exact(g, "NegatedClass", "[^a-]")); + EXPECT_TRUE(exact(g, "NegatedClass", "[^a-]")); EXPECT_TRUE(exact(g, "NegatedClass", "[^-a]")); EXPECT_FALSE(exact(g, "NegatedClass", "[^")); EXPECT_FALSE(exact(g, "NegatedClass", "[^a")); @@ -189,7 +189,7 @@ TEST(LeftRecursiveTest, PEG_Negated_Class) { EXPECT_TRUE(exact(g, "NegatedClass", u8"[^あ-ん]")); EXPECT_FALSE(exact(g, "NegatedClass", u8"^あ-ん")); EXPECT_TRUE(exact(g, "NegatedClass", "[^-+]")); - EXPECT_FALSE(exact(g, "NegatedClass", "[^+-]")); + EXPECT_TRUE(exact(g, "NegatedClass", "[^+-]")); EXPECT_TRUE(exact(g, "NegatedClass", "[^^]")); }