diff --git a/wrap/Module.cpp b/wrap/Module.cpp index 2b6c6ffe2..f1b6c4acb 100644 --- a/wrap/Module.cpp +++ b/wrap/Module.cpp @@ -16,6 +16,7 @@ #include "Module.h" #include "utilities.h" +#include "pop_actor.h" //#define BOOST_SPIRIT_DEBUG #include @@ -51,7 +52,7 @@ Module::Module(const string& interfacePath, Method method0(enable_verbose), method(enable_verbose); StaticMethod static_method0(enable_verbose), static_method(enable_verbose); Class cls0(enable_verbose),cls(enable_verbose); - vector namespaces, namespaces_parent; + vector namespaces, namespaces_parent, namespaces_temp; //---------------------------------------------------------------------------- // Grammar with actions that build the Class object. Actions are @@ -161,11 +162,11 @@ Module::Module(const string& interfacePath, Rule namespace_name_p = lexeme_d[(upper_p | lower_p) >> *(alnum_p | '_')]; Rule namespace_p = str_p("namespace") >> - namespace_name_p[assign_a(namespaces_parent, namespaces)][push_back_a(namespaces)] // save previous state + namespace_name_p[push_back_a(namespaces)] >> ch_p('{') >> *(class_p | namespace_p | comments_p) >> str_p("}///\\namespace") // end namespace, avoid confusion with classes - [assign_a(namespaces, namespaces_parent)]; // switch back to parent namespace + [pop_a(namespaces)]; Rule module_content_p = comments_p | class_p | namespace_p ; @@ -204,16 +205,6 @@ Module::Module(const string& interfacePath, printf("parsing stopped at \n%.20s\n",info.stop); throw ParseFailed(info.length); } - -// if (!namespaces.empty()) { -// cout << "Namespaces not closed, remaining: "; -// BOOST_FOREACH(const string& ns, namespaces) -// cout << ns << " "; -// cout << endl; -// } -// -// if (!cls.name.empty()) -// cout << "\nClass name: " << cls.name << endl; } /* ************************************************************************* */ diff --git a/wrap/pop_actor.h b/wrap/pop_actor.h new file mode 100644 index 000000000..97bfdcca6 --- /dev/null +++ b/wrap/pop_actor.h @@ -0,0 +1,59 @@ +/** + * @file pop_actor.h + * + * @brief An actor to pop a vector/container, based off of the clear_actor + * + * @date Dec 8, 2011 + * @author Alex Cunningham + */ + +#pragma once + +#include + +namespace boost { namespace spirit { + +BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN + + /////////////////////////////////////////////////////////////////////////// + // Summary: + // A semantic action policy that calls pop_back method. + // (This doc uses convention available in actors.hpp) + // + // Actions (what it does): + // ref.pop_back(); + // + // Policy name: + // clear_action + // + // Policy holder, corresponding helper method: + // ref_actor, clear_a( ref ); + // + // () operators: both. + // + // See also ref_actor for more details. + /////////////////////////////////////////////////////////////////////////// + struct pop_action + { + template< + typename T + > + void act(T& ref_) const + { + ref_.pop_back(); + } + }; + + /////////////////////////////////////////////////////////////////////////// + // helper method that creates a and_assign_actor. + /////////////////////////////////////////////////////////////////////////// + template + inline ref_actor pop_a(T& ref_) + { + return ref_actor(ref_); + } + +BOOST_SPIRIT_CLASSIC_NAMESPACE_END + +}} + diff --git a/wrap/tests/testNamespaces.h b/wrap/tests/testNamespaces.h index 59c80201c..f95e08a74 100644 --- a/wrap/tests/testNamespaces.h +++ b/wrap/tests/testNamespaces.h @@ -26,6 +26,12 @@ class ClassB { }///\namespace +class ClassC { +}; + }///\namespace +class ClassD { +}; + diff --git a/wrap/tests/testWrap.cpp b/wrap/tests/testWrap.cpp index 9241a8ec6..7b60e9ad2 100644 --- a/wrap/tests/testWrap.cpp +++ b/wrap/tests/testWrap.cpp @@ -123,7 +123,7 @@ TEST( wrap, parse ) { TEST( wrap, parse_namespaces ) { string header_path = topdir + "/wrap/tests"; Module module(header_path.c_str(), "testNamespaces",enable_verbose); - EXPECT_LONGS_EQUAL(4, module.classes.size()); + EXPECT_LONGS_EQUAL(6, module.classes.size()); Class cls1 = module.classes.at(0); EXPECT(assert_equal("ClassA", cls1.name)); @@ -145,6 +145,17 @@ TEST( wrap, parse_namespaces ) { EXPECT_LONGS_EQUAL(2, cls4.namespaces.size()); EXPECT(assert_equal("ns2", cls4.namespaces.front())); EXPECT(assert_equal("ns3", cls4.namespaces.back())); + + Class cls5 = module.classes.at(4); + EXPECT(assert_equal("ClassC", cls5.name)); + EXPECT_LONGS_EQUAL(1, cls5.namespaces.size()); + EXPECT(assert_equal("ns2", cls5.namespaces.front())); + + Class cls6 = module.classes.at(5); + EXPECT(assert_equal("ClassD", cls6.name)); + EXPECT_LONGS_EQUAL(0, cls6.namespaces.size()); + if (!cls6.namespaces.empty()) + cout << "Extraneous namespace: " << cls6.namespaces.front() << endl; } /* ************************************************************************* */