From c661329ac1909156702d35f73b5ad8c9f4088be5 Mon Sep 17 00:00:00 2001 From: dellaert Date: Sun, 30 Nov 2014 23:24:24 +0100 Subject: [PATCH] ReturnType grammar --- wrap/ReturnType.h | 2 + wrap/ReturnValue.h | 2 + wrap/tests/testReturnValue.cpp | 73 ++++++++++++++++++++++++++++++++-- 3 files changed, 74 insertions(+), 3 deletions(-) diff --git a/wrap/ReturnType.h b/wrap/ReturnType.h index 64ef26991..f9d8b318a 100644 --- a/wrap/ReturnType.h +++ b/wrap/ReturnType.h @@ -22,6 +22,8 @@ struct ReturnType: public Qualified { bool isPtr; + friend struct ReturnValueGrammar; + /// Makes a void type ReturnType() : isPtr(false) { diff --git a/wrap/ReturnValue.h b/wrap/ReturnValue.h index 1ab844043..7fab5faca 100644 --- a/wrap/ReturnValue.h +++ b/wrap/ReturnValue.h @@ -25,6 +25,8 @@ struct ReturnValue { bool isPair; ReturnType type1, type2; + friend struct ReturnValueGrammar; + /// Constructor ReturnValue() : isPair(false) { diff --git a/wrap/tests/testReturnValue.cpp b/wrap/tests/testReturnValue.cpp index 3cd7e63ca..8b9774fe4 100644 --- a/wrap/tests/testReturnValue.cpp +++ b/wrap/tests/testReturnValue.cpp @@ -34,13 +34,71 @@ TEST( ReturnType, Constructor1 ) { //****************************************************************************** TEST( ReturnType, Constructor2 ) { - ReturnType actual("Point3",Qualified::CLASS, true); + ReturnType actual("Point3", Qualified::CLASS, true); EXPECT(actual.namespaces().empty()); EXPECT(actual.name()=="Point3"); EXPECT(actual.category==Qualified::CLASS); EXPECT(actual.isPtr); } +//****************************************************************************** +// http://boost-spirit.com/distrib/spirit_1_8_2/libs/spirit/doc/grammar.html +struct ReturnTypeGrammar: public classic::grammar { + + wrap::ReturnType& result_; ///< successful parse will be placed in here + + TypeGrammar type_g; + + /// Construct ReturnType grammar and specify where result is placed + ReturnTypeGrammar(wrap::ReturnType& result) : + result_(result), type_g(result_) { + } + + /// Definition of type grammar + template + struct definition { + + classic::rule type_p; + + definition(ReturnTypeGrammar const& self) { + using namespace classic; + type_p = self.type_g >> !ch_p('*')[assign_a(self.result_.isPtr, T)]; + } + + classic::rule const& start() const { + return type_p; + } + + }; +}; +// ReturnTypeGrammar + +//****************************************************************************** +TEST( ReturnType, grammar ) { + + using classic::space_p; + + // Create type grammar that will place result in actual + ReturnType actual; + ReturnTypeGrammar g(actual); + + EXPECT(parse("Point3", g, space_p).full); + EXPECT( actual==ReturnType("Point3")); + actual.clear(); + + EXPECT(parse("Test*", g, space_p).full); + EXPECT( actual==ReturnType("Test",Qualified::CLASS,true)); + actual.clear(); + + EXPECT(parse("VectorNotEigen", g, space_p).full); + EXPECT(actual==ReturnType("VectorNotEigen")); + actual.clear(); + + EXPECT(parse("double", g, space_p).full); + EXPECT(actual==ReturnType("double",Qualified::BASIS)); + actual.clear(); +} + //****************************************************************************** TEST( ReturnValue, Constructor ) { ReturnValue actual(ReturnType("Point2"), ReturnType("Point3")); @@ -55,7 +113,7 @@ struct ReturnValueGrammar: public classic::grammar { wrap::ReturnValue& result_; ///< successful parse will be placed in here - TypeGrammar returnType1_g, returnType2_g; + ReturnTypeGrammar returnType1_g, returnType2_g; /// Construct type grammar and specify where result is placed ReturnValueGrammar(wrap::ReturnValue& result) : @@ -97,7 +155,16 @@ TEST( ReturnValue, grammar ) { EXPECT(parse("pair", g, space_p).full); EXPECT( actual==ReturnValue(ReturnType("Point2"),ReturnType("Point3"))); - cout << actual << endl; + actual.clear(); + + EXPECT(parse("pair", g, space_p).full); + EXPECT( actual==ReturnValue( // + ReturnType("Test",Qualified::CLASS,true),ReturnType("Test"))); + actual.clear(); + + EXPECT(parse("pair", g, space_p).full); + EXPECT( actual==ReturnValue(ReturnType("Test"), // + ReturnType("Test",Qualified::CLASS,true))); actual.clear(); EXPECT(parse("VectorNotEigen", g, space_p).full);