From 9ad70096c828452c714e10083d3421cee0ea66d7 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 9 Jan 2021 15:10:42 -0500 Subject: [PATCH] Another infinite loop grammar detection. Fix #133 --- docs/native.wasm | Bin 349329 -> 349485 bytes peglib.h | 4 +++- test/test2.cc | 8 ++++++++ test/test3.cc | 5 +++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/docs/native.wasm b/docs/native.wasm index f6d0f321b1839152719e1cc10a6854eb038c54c9..9377c0c544d21390cb2ee107b9beedaff30ab493 100644 GIT binary patch delta 478 zcmXX?ze^lJ6rOqS?d{>t!Gd1A;x3sT19u3vYkL!dhmA1=n-EB0q29Kp5>&9T3Q;Vk z2q8^4NP=MHwF)T$Ig&rYG%qVr4va<- ziiZOu&tK^4;lJ1Ni}lap_jTFP*^i2@?9C}}3&)QYb*=7@a#Vu2Xi2)^ok&|p;bO#( z!qH!jLte0*600nE)6Xefe$&%o<)zeP>Ovs0;cipBWppU0c#S48oLKfMr-tPxYLQb8 zvL&v{6GJ)iZ?grkxkEEfm(W-iq_L8d0^q?wW4QQ>sy?fH$X_x_gM6ORIu;{);O>e(z;rpugTlAkV>!=kZ$LI%s+Hvm{tEkOVP diff --git a/peglib.h b/peglib.h index 38d6276..b7910e0 100644 --- a/peglib.h +++ b/peglib.h @@ -2872,7 +2872,9 @@ private: }; g["Sequence"] = [&](const SemanticValues &vs) { - if (vs.size() == 1) { + if (vs.empty()) { + return npd(dot()); + } else if (vs.size() == 1) { return std::any_cast>(vs[0]); } else { std::vector> opes; diff --git a/test/test2.cc b/test/test2.cc index 4c60112..24a7eff 100644 --- a/test/test2.cc +++ b/test/test2.cc @@ -159,6 +159,14 @@ TEST_CASE("Infinite loop 7", "[infinite loop]") { REQUIRE(!pg); } +TEST_CASE("Infinite loop 8", "[infinite loop]") { + parser pg(R"( + ROOT <- ('A' /)* + )"); + + REQUIRE(!pg); +} + TEST_CASE("Not infinite 1", "[infinite loop]") { parser pg(R"( Numbers <- Number* EOF diff --git a/test/test3.cc b/test/test3.cc index 377864e..c9e191d 100644 --- a/test/test3.cc +++ b/test/test3.cc @@ -42,6 +42,11 @@ TEST_CASE("PEG Expression", "[peg]") REQUIRE(exact(g, "Expression", "") == true); REQUIRE(exact(g, "Expression", " ") == false); REQUIRE(exact(g, "Expression", " a b ") == false); + + // NOTE: The followings are actually allowed in the original Ford's paper... + REQUIRE(exact(g, "Expression", "a//b ") == true); + REQUIRE(exact(g, "Expression", "a // b ") == true); + REQUIRE(exact(g, "Expression", "a / / b ") == true); } TEST_CASE("PEG Sequence", "[peg]")