From e9635121644d208735e77ffade7b5cb26ffc1aea Mon Sep 17 00:00:00 2001 From: dellaert Date: Mon, 1 Dec 2014 20:03:26 +0100 Subject: [PATCH] Tightened up individual Grammars --- wrap/Argument.h | 26 +++++++++++------------ wrap/Module.cpp | 2 +- wrap/Qualified.h | 43 +++++++++++++++++---------------------- wrap/ReturnValue.h | 6 ++---- wrap/Template.h | 13 +++++------- wrap/spirit.h | 18 ++++++++-------- wrap/tests/testMethod.cpp | 2 -- 7 files changed, 48 insertions(+), 62 deletions(-) diff --git a/wrap/Argument.h b/wrap/Argument.h index 98a8ab7b1..3b7a13ee3 100644 --- a/wrap/Argument.h +++ b/wrap/Argument.h @@ -129,7 +129,7 @@ struct ArgumentList: public std::vector { struct ArgumentGrammar: public classic::grammar { wrap::Argument& result_; ///< successful parse will be placed in here - TypeGrammar argument_type_g; + TypeGrammar argument_type_g; ///< Type parser for Argument::type /// Construct type grammar and specify where result is placed ArgumentGrammar(wrap::Argument& result) : @@ -138,15 +138,13 @@ struct ArgumentGrammar: public classic::grammar { /// Definition of type grammar template - struct definition: basic_rules { + struct definition: BasicRules { typedef classic::rule Rule; - Rule argument_p, argumentList_p; + Rule argument_p; definition(ArgumentGrammar const& self) { - - using namespace wrap; using namespace classic; // NOTE: allows for pointers to all types @@ -156,7 +154,7 @@ struct ArgumentGrammar: public classic::grammar { >> self.argument_type_g // >> !ch_p('*')[assign_a(self.result_.is_ptr, T)] >> !ch_p('&')[assign_a(self.result_.is_ref, T)] - >> basic_rules::name_p[assign_a(self.result_.name)]; + >> BasicRules::name_p[assign_a(self.result_.name)]; } Rule const& start() const { @@ -173,9 +171,9 @@ struct ArgumentListGrammar: public classic::grammar { wrap::ArgumentList& result_; ///< successful parse will be placed in here - const Argument arg0; // used to reset arg - mutable Argument arg; // temporary argument for use during parsing - ArgumentGrammar argument_g; + const Argument arg0; ///< used to reset arg + mutable Argument arg; ///< temporary argument for use during parsing + ArgumentGrammar argument_g; ///< single Argument parser /// Construct type grammar and specify where result is placed ArgumentListGrammar(wrap::ArgumentList& result) : @@ -184,21 +182,21 @@ struct ArgumentListGrammar: public classic::grammar { /// Definition of type grammar template - struct definition: basic_rules { + struct definition { - typedef classic::rule Rule; - - Rule argument_p, argumentList_p; + classic::rule argument_p, argumentList_p; definition(ArgumentListGrammar const& self) { using namespace classic; + argument_p = self.argument_g // [classic::push_back_a(self.result_, self.arg)] // [assign_a(self.arg, self.arg0)]; + argumentList_p = '(' >> !argument_p >> *(',' >> argument_p) >> ')'; } - Rule const& start() const { + classic::rule const& start() const { return argumentList_p; } diff --git a/wrap/Module.cpp b/wrap/Module.cpp index d7059d316..decc390d6 100644 --- a/wrap/Module.cpp +++ b/wrap/Module.cpp @@ -109,7 +109,7 @@ void Module::parseMarkup(const std::string& data) { // Define Rule and instantiate basic rules typedef rule Rule; - basic_rules basic; + BasicRules basic; // TODO, do we really need cls here? Non-local Class cls0(verbose),cls(verbose); diff --git a/wrap/Qualified.h b/wrap/Qualified.h index 29b961518..000d749ec 100644 --- a/wrap/Qualified.h +++ b/wrap/Qualified.h @@ -35,7 +35,7 @@ public: std::vector namespaces_; ///< Stack of namespaces std::string name_; ///< type name - friend class TypeGrammar; + friend struct TypeGrammar; friend class TemplateSubstitution; public: @@ -81,7 +81,7 @@ public: // Qualified is 'abused' as template argument name as well // this function checks whether *this matches with templateArg bool match(const std::string& templateArg) const { - return (name_ == templateArg && namespaces_.empty());//TODO && category == CLASS); + return (name_ == templateArg && namespaces_.empty()); //TODO && category == CLASS); } void rename(const Qualified& q) { @@ -128,7 +128,6 @@ public: return Qualified("void", VOID); } - /// Return a qualified string using given delimiter std::string qualifiedName(const std::string& delimiter = "") const { std::string result; @@ -156,12 +155,10 @@ public: /* ************************************************************************* */ // http://boost-spirit.com/distrib/spirit_1_8_2/libs/spirit/doc/grammar.html -class TypeGrammar: public classic::grammar { +struct TypeGrammar: classic::grammar { wrap::Qualified& result_; ///< successful parse will be placed in here -public: - /// Construct type grammar and specify where result is placed TypeGrammar(wrap::Qualified& result) : result_(result) { @@ -169,17 +166,17 @@ public: /// Definition of type grammar template - struct definition: basic_rules { + struct definition: BasicRules { typedef classic::rule Rule; - Rule void_p, my_basisType_p, my_eigenType_p, namespace_del_p, class_p, - type_p; + Rule void_p, basisType_p, eigenType_p, namespace_del_p, class_p, type_p; definition(TypeGrammar const& self) { using namespace wrap; using namespace classic; + typedef BasicRules Basic; // HACK: use const values instead of using enums themselves - somehow this doesn't result in values getting assigned to gibberish static const Qualified::Category EIGEN = Qualified::EIGEN; @@ -187,25 +184,26 @@ public: static const Qualified::Category CLASS = Qualified::CLASS; static const Qualified::Category VOID = Qualified::VOID; - void_p = str_p("void")[assign_a(self.result_.name_)] // + void_p = str_p("void") // + [assign_a(self.result_.name_)] // [assign_a(self.result_.category, VOID)]; - my_basisType_p = basic_rules::basisType_p // + basisType_p = Basic::basisType_p // [assign_a(self.result_.name_)] // [assign_a(self.result_.category, BASIS)]; - my_eigenType_p = basic_rules::eigenType_p // + eigenType_p = Basic::eigenType_p // [assign_a(self.result_.name_)] // [assign_a(self.result_.category, EIGEN)]; - namespace_del_p = basic_rules::namespace_p // + namespace_del_p = Basic::namespace_p // [push_back_a(self.result_.namespaces_)] >> str_p("::"); - class_p = *namespace_del_p >> basic_rules::className_p // + class_p = *namespace_del_p >> Basic::className_p // [assign_a(self.result_.name_)] // [assign_a(self.result_.category, CLASS)]; - type_p = void_p | my_basisType_p | class_p | my_eigenType_p; + type_p = void_p | basisType_p | class_p | eigenType_p; } Rule const& start() const { @@ -218,8 +216,8 @@ public: /* ************************************************************************* */ // http://boost-spirit.com/distrib/spirit_1_8_2/libs/spirit/doc/grammar.html -template -struct TypeListGrammar: public classic::grammar > { +template +struct TypeListGrammar: public classic::grammar > { typedef std::vector TypeList; TypeList& result_; ///< successful parse will be placed in here @@ -234,11 +232,9 @@ struct TypeListGrammar: public classic::grammar > { /// Definition of type grammar template - struct definition: basic_rules { + struct definition { - typedef classic::rule Rule; - - Rule type_p, typeList_p; + classic::rule type_p, typeList_p; definition(TypeListGrammar const& self) { using namespace classic; @@ -248,7 +244,7 @@ struct TypeListGrammar: public classic::grammar > { typeList_p = OPEN >> !type_p >> *(',' >> type_p) >> CLOSE; } - Rule const& start() const { + classic::rule const& start() const { return typeList_p; } @@ -260,6 +256,5 @@ struct TypeListGrammar: public classic::grammar > { // Needed for other parsers in Argument.h and ReturnType.h static const bool T = true; - -}// \namespace wrap +} // \namespace wrap diff --git a/wrap/ReturnValue.h b/wrap/ReturnValue.h index b23cbf681..629684a34 100644 --- a/wrap/ReturnValue.h +++ b/wrap/ReturnValue.h @@ -82,8 +82,7 @@ struct ReturnValue { struct ReturnValueGrammar: public classic::grammar { wrap::ReturnValue& result_; ///< successful parse will be placed in here - - ReturnTypeGrammar returnType1_g, returnType2_g; + ReturnTypeGrammar returnType1_g, returnType2_g; ///< Type parsers /// Construct type grammar and specify where result is placed ReturnValueGrammar(wrap::ReturnValue& result) : @@ -97,7 +96,6 @@ struct ReturnValueGrammar: public classic::grammar { classic::rule pair_p, returnValue_p; definition(ReturnValueGrammar const& self) { - using namespace classic; pair_p = (str_p("pair") >> '<' >> self.returnType1_g >> ',' @@ -114,4 +112,4 @@ struct ReturnValueGrammar: public classic::grammar { }; // ReturnValueGrammar -} // \namespace wrap +}// \namespace wrap diff --git a/wrap/Template.h b/wrap/Template.h index c9edcaf2b..cfa0db008 100644 --- a/wrap/Template.h +++ b/wrap/Template.h @@ -37,8 +37,7 @@ struct Template { struct TemplateGrammar: public classic::grammar { Template& result_; ///< successful parse will be placed in here - - TypeListGrammar<'{', '}'> argValues_g; + TypeListGrammar<'{', '}'> argValues_g; ///< TypeList parser /// Construct type grammar and specify where result is placed TemplateGrammar(Template& result) : @@ -47,20 +46,18 @@ struct TemplateGrammar: public classic::grammar { /// Definition of type grammar template - struct definition: basic_rules { + struct definition: BasicRules { - typedef classic::rule Rule; - - Rule templateArgValues_p; + classic::rule templateArgValues_p; definition(TemplateGrammar const& self) { using namespace classic; templateArgValues_p = (str_p("template") >> '<' - >> (basic_rules::name_p)[assign_a(self.result_.argName)] + >> (BasicRules::name_p)[assign_a(self.result_.argName)] >> '=' >> self.argValues_g >> '>'); } - Rule const& start() const { + classic::rule const& start() const { return templateArgValues_p; } diff --git a/wrap/spirit.h b/wrap/spirit.h index 4a18b53e8..92abe93ef 100644 --- a/wrap/spirit.h +++ b/wrap/spirit.h @@ -43,7 +43,7 @@ BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN /////////////////////////////////////////////////////////////////////////// struct pop_action { - template< typename T> + template< typename T> void act(T& ref_) const { if (!ref_.empty()) @@ -86,7 +86,7 @@ inline ref_actor pop_a(T& ref_) struct append_action { - template< typename T, typename ValueT > + template< typename T, typename ValueT > void act(T& ref_, ValueT const& value_) const { ref_.insert(ref_.begin(), value_.begin(), value_.end()); @@ -125,18 +125,18 @@ BOOST_SPIRIT_CLASSIC_NAMESPACE_END } } +namespace wrap { + namespace classic = BOOST_SPIRIT_CLASSIC_NS; /// Some basic rules used by all parsers template -struct basic_rules { +struct BasicRules { - typedef BOOST_SPIRIT_CLASSIC_NS::rule Rule; + classic::rule comments_p, basisType_p, eigenType_p, keywords_p, + stlType_p, name_p, className_p, namespace_p; - Rule comments_p, basisType_p, eigenType_p, keywords_p, stlType_p, name_p, - className_p, namespace_p; - - basic_rules() { + BasicRules() { using namespace BOOST_SPIRIT_CLASSIC_NS; @@ -160,5 +160,5 @@ struct basic_rules { namespace_p = lexeme_d[lower_p >> *(alnum_p | '_')] - keywords_p; } }; - +} diff --git a/wrap/tests/testMethod.cpp b/wrap/tests/testMethod.cpp index bc21b332c..5b58fa31e 100644 --- a/wrap/tests/testMethod.cpp +++ b/wrap/tests/testMethod.cpp @@ -65,8 +65,6 @@ TEST( Method, addOverload ) { // template // struct definition: basic_rules { // -// typedef classic::rule Rule; -// // Rule templateArgValue_p, templateArgValues_p, argument_p, argumentList_p, // returnType1_p, returnType2_p, pair_p, returnValue_p, methodName_p, // method_p;