From d25636685ba44f5d98a498983105fd5a4aca8e58 Mon Sep 17 00:00:00 2001 From: dellaert Date: Mon, 1 Dec 2014 11:32:33 +0100 Subject: [PATCH] TypeListGrammar --- wrap/tests/testType.cpp | 85 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/wrap/tests/testType.cpp b/wrap/tests/testType.cpp index abf3cf65f..7f165b109 100644 --- a/wrap/tests/testType.cpp +++ b/wrap/tests/testType.cpp @@ -83,6 +83,91 @@ TEST( Type, grammar ) { actual.clear(); } +/* ************************************************************************* */ +// http://boost-spirit.com/distrib/spirit_1_8_2/libs/spirit/doc/grammar.html +struct TypeListGrammar: public classic::grammar { + + typedef std::vector TypeList; + TypeList& result_; ///< successful parse will be placed in here + + mutable wrap::Qualified type; // temporary type for use during parsing + TypeGrammar type_g; + + /// Construct type grammar and specify where result is placed + TypeListGrammar(TypeList& result) : + result_(result), type_g(type) { + } + + /// Definition of type grammar + template + struct definition: basic_rules { + + typedef classic::rule Rule; + + Rule type_p, typeList_p; + + definition(TypeListGrammar const& self) { + using namespace classic; + type_p = self.type_g // + [classic::push_back_a(self.result_, self.type)] // + [clear_a(self.type)]; + typeList_p = '{' >> !type_p >> *(',' >> type_p) >> '}'; + } + + Rule const& start() const { + return typeList_p; + } + + }; +}; +// TypeListGrammar + +//****************************************************************************** +TEST( TypeList, grammar ) { + + using classic::space_p; + + // Create type grammar that will place result in actual + vector actual; + TypeListGrammar g(actual); + + EXPECT(parse("{gtsam::Point2}", g, space_p).full); + EXPECT_LONGS_EQUAL(1, actual.size()); + actual.clear(); + + EXPECT(parse("{}", g, space_p).full); + EXPECT_LONGS_EQUAL(0, actual.size()); + actual.clear(); + + EXPECT(parse("{char}", g, space_p).full); + EXPECT_LONGS_EQUAL(1, actual.size()); + actual.clear(); + + EXPECT(parse("{unsigned char}", g, space_p).full); + EXPECT_LONGS_EQUAL(1, actual.size()); + actual.clear(); + + EXPECT(parse("{Vector, Matrix}", g, space_p).full); + EXPECT_LONGS_EQUAL(2, actual.size()); + EXPECT(actual[0]==Qualified("Vector",Qualified::EIGEN)); + EXPECT(actual[1]==Qualified("Matrix",Qualified::EIGEN)); + actual.clear(); + + EXPECT(parse("{Point2}", g, space_p).full); + EXPECT_LONGS_EQUAL(1, actual.size()); + actual.clear(); + + EXPECT(parse("{Point2, Point3}", g, space_p).full); + EXPECT_LONGS_EQUAL(2, actual.size()); + EXPECT(actual[0]==Qualified("Point2")); + EXPECT(actual[1]==Qualified("Point3")); + actual.clear(); + + EXPECT(parse("{gtsam::Point2}", g, space_p).full); + EXPECT_LONGS_EQUAL(1, actual.size()); + actual.clear(); +} + //****************************************************************************** int main() { TestResult tr;