From 6239881746e756810b383323502defba1c8428cb Mon Sep 17 00:00:00 2001 From: dellaert Date: Thu, 22 Jan 2015 03:02:53 +0100 Subject: [PATCH] Parse successful --- wrap/Template.h | 44 +++++++++++++++++++++++++++++++++++-- wrap/tests/testTemplate.cpp | 9 ++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/wrap/Template.h b/wrap/Template.h index 5a64412ed..e04ea1792 100644 --- a/wrap/Template.h +++ b/wrap/Template.h @@ -26,6 +26,7 @@ namespace wrap { class Template { std::string argName_; std::vector argValues_; + std::vector intList_; friend struct TemplateGrammar; public: /// The only way to get values into a Template is via our friendly Grammar @@ -38,6 +39,9 @@ public: const std::string& argName() const { return argName_; } + const std::vector& intList() const { + return intList_; + } const std::vector& argValues() const { return argValues_; } @@ -53,16 +57,52 @@ public: }; +/* ************************************************************************* */ +// http://boost-spirit.com/distrib/spirit_1_8_2/libs/spirit/doc/grammar.html +struct IntListGrammar: public classic::grammar { + + typedef std::vector IntList; + IntList& result_; ///< successful parse will be placed in here + + /// Construct type grammar and specify where result is placed + IntListGrammar(IntList& result) : + result_(result) { + } + + /// Definition of type grammar + template + struct definition { + + classic::rule integer_p, intList_p; + + definition(IntListGrammar const& self) { + using namespace classic; + + integer_p = int_p[push_back_a(self.result_)]; + + intList_p = '{' >> !integer_p >> *(',' >> integer_p) >> '}'; + } + + classic::rule const& start() const { + return intList_p; + } + + }; +}; +// IntListGrammar + /* ************************************************************************* */ // http://boost-spirit.com/distrib/spirit_1_8_2/libs/spirit/doc/grammar.html struct TemplateGrammar: public classic::grammar { Template& result_; ///< successful parse will be placed in here TypeListGrammar<'{', '}'> argValues_g; ///< TypeList parser + IntListGrammar intList_g; ///< TypeList parser /// Construct type grammar and specify where result is placed TemplateGrammar(Template& result) : - result_(result), argValues_g(result.argValues_) { + result_(result), argValues_g(result.argValues_), // + intList_g(result.intList_) { } /// Definition of type grammar @@ -76,7 +116,7 @@ struct TemplateGrammar: public classic::grammar { using classic::assign_a; templateArgValues_p = (str_p("template") >> '<' >> (BasicRules::name_p)[assign_a(self.result_.argName_)] - >> '=' >> self.argValues_g >> '>'); + >> '=' >> (self.argValues_g | self.intList_g) >> '>'); } classic::rule const& start() const { diff --git a/wrap/tests/testTemplate.cpp b/wrap/tests/testTemplate.cpp index 37cb95205..eed144677 100644 --- a/wrap/tests/testTemplate.cpp +++ b/wrap/tests/testTemplate.cpp @@ -47,6 +47,15 @@ TEST( Template, grammar ) { EXPECT(actual[2]==Qualified("Vector",Qualified::EIGEN)); EXPECT(actual[3]==Qualified("Matrix",Qualified::EIGEN)); actual.clear(); + + EXPECT(parse("template", g, space_p).full); + EXPECT_LONGS_EQUAL(4, actual.intList().size()); + EXPECT(actual.argName()=="N"); + EXPECT_LONGS_EQUAL(1,actual.intList()[0]); + EXPECT_LONGS_EQUAL(2,actual.intList()[1]); + EXPECT_LONGS_EQUAL(3,actual.intList()[2]); + EXPECT_LONGS_EQUAL(4,actual.intList()[3]); + actual.clear(); } //******************************************************************************