From f7e7e308edb354d0860adc10bbea98c4a6638003 Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 18 Feb 2015 19:41:18 -0500 Subject: [PATCH] Changed to allow 'mutable' lambda function as semantic action. Closed #2. --- peglib.h | 30 ++++++++++++++++++++++++++++++ test/test.cc | 12 ++++++++++++ 2 files changed, 42 insertions(+) diff --git a/peglib.h b/peglib.h index c37e35f..823e9b8 100644 --- a/peglib.h +++ b/peglib.h @@ -284,6 +284,11 @@ private: return TypeAdaptor(fn); } + template + Fty make_adaptor(F fn, R (F::*mf)(const char*, size_t, const std::vector& v, any& c)) { + return TypeAdaptor(fn); + } + template Fty make_adaptor(F fn, R(*mf)(const char*, size_t, const std::vector& v, any& c)) { return TypeAdaptor(fn); @@ -294,6 +299,11 @@ private: return TypeAdaptor_s_l_v(fn); } + template + Fty make_adaptor(F fn, R (F::*mf)(const char*, size_t, const std::vector& v)) { + return TypeAdaptor_s_l_v(fn); + } + template Fty make_adaptor(F fn, R(*mf)(const char*, size_t, const std::vector& v)) { return TypeAdaptor_s_l_v(fn); @@ -304,6 +314,11 @@ private: return TypeAdaptor_s_l(fn); } + template + Fty make_adaptor(F fn, R (F::*mf)(const char*, size_t)) { + return TypeAdaptor_s_l(fn); + } + template Fty make_adaptor(F fn, R (*mf)(const char*, size_t)) { return TypeAdaptor_s_l(fn); @@ -314,6 +329,11 @@ private: return TypeAdaptor_v_n(fn); } + template + Fty make_adaptor(F fn, R (F::*mf)(const std::vector& v, any& c)) { + return TypeAdaptor_v_n(fn); + } + template Fty make_adaptor(F fn, R (*mf)(const std::vector& v, any& c)) { return TypeAdaptor_v_n(fn); @@ -324,6 +344,11 @@ private: return TypeAdaptor_v(fn); } + template + Fty make_adaptor(F fn, R (F::*mf)(const std::vector& v)) { + return TypeAdaptor_v(fn); + } + template Fty make_adaptor(F fn, R (*mf)(const std::vector& v)) { return TypeAdaptor_v(fn); @@ -334,6 +359,11 @@ private: return TypeAdaptor_empty(fn); } + template + Fty make_adaptor(F fn, R (F::*mf)()) { + return TypeAdaptor_empty(fn); + } + template Fty make_adaptor(F fn, R (*mf)()) { return TypeAdaptor_empty(fn); diff --git a/test/test.cc b/test/test.cc index 9133814..598d536 100644 --- a/test/test.cc +++ b/test/test.cc @@ -183,6 +183,18 @@ TEST_CASE("Backtracking test", "[general]") REQUIRE(count == 2); } +TEST_CASE("mutable lambda test", "[general]") +{ + vector vec; + + peg pg("ROOT <- 'mutable lambda test'"); + + // This test makes sure if the following code can be compiled. + pg["TOKEN"] = [=](const char* s, size_t l) mutable { + vec.push_back(string(s, l)); + }; +} + TEST_CASE("Simple calculator test", "[general]") { auto syntax =