From 0061f393de54cf0326621c079dc2988336d1ebb3 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 7 Aug 2020 10:21:47 -0400 Subject: [PATCH] Fix #121 --- peglib.h | 21 +++++++++++++++++---- test/test1.cc | 11 +++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/peglib.h b/peglib.h index b285339..fc949de 100644 --- a/peglib.h +++ b/peglib.h @@ -1239,12 +1239,10 @@ class LiteralString : public Ope, public std::enable_shared_from_this { public: LiteralString(std::string &&s, bool ignore_case) - : lit_(s), ignore_case_(ignore_case), - is_word_(false) {} + : lit_(s), ignore_case_(ignore_case), is_word_(false) {} LiteralString(const std::string &s, bool ignore_case) - : lit_(s), ignore_case_(ignore_case), - is_word_(false) {} + : lit_(s), ignore_case_(ignore_case), is_word_(false) {} size_t parse_core(const char *s, size_t n, SemanticValues &sv, Context &c, any &dt) const override; @@ -3438,6 +3436,21 @@ private: } } + // Check if the start rule has ignore operator + { + auto &rule = grammar[data.start]; + if (rule.ignoreSemanticValue) { + if (log) { + auto line = line_info(s, rule.s_); + log(line.first, line.second, + "Ignore operator cannot be applied to '" + rule.name + "'."); + } + ret = false; + } + } + + if (!ret) { return nullptr; } + // Check missing definitions for (auto &x : grammar) { auto &rule = x.second; diff --git a/test/test1.cc b/test/test1.cc index 4f8f586..32e6706 100644 --- a/test/test1.cc +++ b/test/test1.cc @@ -34,6 +34,17 @@ TEST_CASE("Empty syntax test", "[general]") REQUIRE(ret == false); } +TEST_CASE("Start rule with ignore operator test", "[general]") +{ + parser parser(R"( + ~ROOT <- _ + _ <- ' ' + )"); + + bool ret = parser; + REQUIRE(ret == false); +} + TEST_CASE("Backslash escape sequence test", "[general]") { parser parser(R"(