From e98ec628044d8e48a0df66a35fdceb4ec08de302 Mon Sep 17 00:00:00 2001 From: dellaert Date: Sun, 30 Nov 2014 11:09:34 +0100 Subject: [PATCH] start with grammar prototype --- .cproject | 106 ++++++++++++++++++++-------------------- wrap/tests/testType.cpp | 105 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+), 54 deletions(-) create mode 100644 wrap/tests/testType.cpp diff --git a/.cproject b/.cproject index 1dcc51dfe..c18177bf8 100644 --- a/.cproject +++ b/.cproject @@ -592,7 +592,6 @@ make - tests/testBayesTree.run true false @@ -600,7 +599,6 @@ make - testBinaryBayesNet.run true false @@ -648,7 +646,6 @@ make - testSymbolicBayesNet.run true false @@ -656,7 +653,6 @@ make - tests/testSymbolicFactor.run true false @@ -664,7 +660,6 @@ make - testSymbolicFactorGraph.run true false @@ -680,7 +675,6 @@ make - tests/testBayesTree true false @@ -1128,7 +1122,6 @@ make - testErrors.run true false @@ -1358,46 +1351,6 @@ true true - - make - -j5 - testBTree.run - true - true - true - - - make - -j5 - testDSF.run - true - true - true - - - make - -j5 - testDSFMap.run - true - true - true - - - make - -j5 - testDSFVector.run - true - true - true - - - make - -j5 - testFixedVector.run - true - true - true - make -j2 @@ -1480,6 +1433,7 @@ make + testSimulated2DOriented.run true false @@ -1519,6 +1473,7 @@ make + testSimulated2D.run true false @@ -1526,6 +1481,7 @@ make + testSimulated3D.run true false @@ -1539,6 +1495,46 @@ true true + + make + -j5 + testBTree.run + true + true + true + + + make + -j5 + testDSF.run + true + true + true + + + make + -j5 + testDSFMap.run + true + true + true + + + make + -j5 + testDSFVector.run + true + true + true + + + make + -j5 + testFixedVector.run + true + true + true + make -j5 @@ -1796,7 +1792,6 @@ cpack - -G DEB true false @@ -1804,7 +1799,6 @@ cpack - -G RPM true false @@ -1812,7 +1806,6 @@ cpack - -G TGZ true false @@ -1820,7 +1813,6 @@ cpack - --config CPackSourceConfig.cmake true false @@ -2401,6 +2393,14 @@ true true + + make + -j4 + testType.run + true + true + true + make -j5 @@ -2635,7 +2635,6 @@ make - testGraph.run true false @@ -2643,7 +2642,6 @@ make - testJunctionTree.run true false @@ -2651,7 +2649,6 @@ make - testSymbolicBayesNetB.run true false @@ -3187,6 +3184,7 @@ make + tests/testGaussianISAM2 true false diff --git a/wrap/tests/testType.cpp b/wrap/tests/testType.cpp new file mode 100644 index 000000000..59ccd279d --- /dev/null +++ b/wrap/tests/testType.cpp @@ -0,0 +1,105 @@ +/* ---------------------------------------------------------------------------- + + * GTSAM Copyright 2010, Georgia Tech Research Corporation, + * Atlanta, Georgia 30332-0415 + * All Rights Reserved + * Authors: Frank Dellaert, et al. (see THANKS for the full author list) + + * See LICENSE for the license information + + * -------------------------------------------------------------------------- */ + +/** + * @file testType.cpp + * @brief unit test for parsing a fully qualified type + * @author Frank Dellaert + * @date Nov 30, 2014 + **/ + +#include +#include + +#include +#include +#include + +using namespace std; +using namespace wrap; +using namespace BOOST_SPIRIT_CLASSIC_NS; + +typedef rule Rule; + +//****************************************************************************** +// http://boost-spirit.com/distrib/spirit_1_8_2/libs/spirit/doc/grammar.html +struct type_grammar: public grammar { + + Qualified& result_; ///< successful parse will be placed in here + + /// Construct type grammar and specify where result is placed + type_grammar(Qualified& result) : + result_(result) { + } + +/// Definition of type grammar + template + struct definition { + + typedef rule Rule; + + Rule basisType_p, keywords_p, eigenType_p, stlType_p, className_p, + namepsace_p, namespace_del_p, qualified_p, type_p; + + definition(type_grammar const& self) { + basisType_p = (str_p("string") | "bool" | "size_t" | "int" | "double" + | "char" | "unsigned char")[assign_a(self.result_.name)]; + + eigenType_p = (str_p("Vector") | "Matrix")[assign_a(self.result_.name)]; + + keywords_p = (str_p("const") | "static" | "namespace" | "void" + | basisType_p); + + //Rule for STL Containers (class names are lowercase) + stlType_p = (str_p("vector") | "list"); + + className_p = (lexeme_d[upper_p >> *(alnum_p | '_')] - eigenType_p + - keywords_p) | stlType_p; + + namepsace_p = lexeme_d[lower_p >> *(alnum_p | '_')] - keywords_p; + + namespace_del_p = namepsace_p[push_back_a(self.result_.namespaces)] + >> str_p("::"); + + qualified_p = *namespace_del_p + >> className_p[assign_a(self.result_.name)]; + + type_p = basisType_p | eigenType_p; + } + + Rule const& start() const { + return type_p; + } + + }; +}; + +//****************************************************************************** +TEST( spirit, grammar ) { + // Create grammar that will place result in actual + Qualified actual; + type_grammar type_g(actual); + + // a basic type + EXPECT(parse("double", type_g, space_p).full); + EXPECT(actual.name=="double"); + + // an Eigen type + EXPECT(parse("Vector", type_g, space_p).full); + EXPECT(actual.name=="Vector"); +} + +//****************************************************************************** +int main() { + TestResult tr; + return TestRegistry::runAllTests(tr); +} +//******************************************************************************