Gaudi Framework, version v21r4

Home   Generated: 7 Sep 2009

Grammars.h

Go to the documentation of this file.
00001 // $Id: Grammars.h,v 1.10 2008/10/27 16:41:33 marcocle Exp $
00002 // ============================================================================
00003 #ifndef GAUDIKERNEL_GRAMMARS_H
00004 #define GAUDIKERNEL_GRAMMARS_H 1
00005 // ============================================================================
00006 // Include files
00007 // ============================================================================
00008 // STD & STL
00009 // ============================================================================
00010 #include <cctype>
00011 // ============================================================================
00012 // Boost.Spirit
00013 // ============================================================================
00014 #include <boost/version.hpp>
00015 #if BOOST_VERSION >= 103800
00016 // FIXME: Move to the new boost::spirit::classic namespace
00017 #if !defined(BOOST_SPIRIT_USE_OLD_NAMESPACE)
00018 #define BOOST_SPIRIT_USE_OLD_NAMESPACE
00019 #endif
00020 #include <boost/spirit/include/classic.hpp>
00021 #include <boost/spirit/include/phoenix1.hpp>
00022 #else
00023 #include <boost/spirit.hpp>
00024 #include <boost/spirit/phoenix.hpp>
00025 #endif
00026 // ============================================================================
00037 // ============================================================================
00038 namespace Gaudi
00039 {
00040   namespace Parsers
00041   {
00042     // ========================================================================
00043     using namespace boost::spirit ;
00044     // ========================================================================
00045     using namespace phoenix ;
00046     // ========================================================================
00055    template <typename T>
00056     struct ClosureGrammar : public boost::spirit::closure < ClosureGrammar<T>,T >
00057     {
00058       typedef  boost::spirit::closure<ClosureGrammar, T> closure;
00059       typename closure::member1 val;
00060     };
00061     // ========================================================================
00072     template <typename T1,typename T2>
00073     struct AttributesClosureGrammar
00074       : public boost::spirit::closure<AttributesClosureGrammar<T1,T2>,T1,T2>
00075     {
00076       typedef boost::spirit::closure<AttributesClosureGrammar, T1,T2> closure;
00077       typename closure::member1 val;
00078       typename closure::member2 attrs;
00079     };
00080     // ========================================================================
00092     class BoolGrammar : public grammar
00093     <
00094       BoolGrammar,
00095       ClosureGrammar<bool>::context_t
00096     >
00097     {
00098     public:
00099       typedef bool ResultT;
00100     public:
00101       template <typename ScannerT>
00102       struct definition
00103       {
00104         definition( BoolGrammar const &self)
00105         {
00106           boolean_literal
00107             = true_literal[self.val = true] | false_literal[self.val = false];
00108           true_literal
00109             = str_p("true" ) | str_p("True" ) | str_p("TRUE" ) | str_p("1");
00110           false_literal
00111             = str_p("false") | str_p("False") | str_p("FALSE") | str_p("0");
00112         }
00113         rule<ScannerT> const& start() const
00114         { return boolean_literal;}
00115         rule<ScannerT> boolean_literal,true_literal,false_literal;
00116       };
00117     };
00118     // ========================================================================
00129     template<typename RT=char>
00130     class CharGrammar : public grammar
00131     <
00132       CharGrammar<RT> , typename ClosureGrammar<RT>::context_t
00133     >
00134     {
00135     public:
00136       typedef RT ResultT;
00137     public:
00138       template <typename ScannerT>
00139       struct definition
00140       {
00141         definition( CharGrammar<RT> const &self)
00142         {
00143           char_literal
00144             = int_parser<RT>()[self.val=arg1]
00145             | ('\''
00146                >> ( str_p("\\'")[self.val='\'']
00147                     | (anychar_p[self.val=arg1]-'\'') )>>'\'');
00148         }
00149         rule<ScannerT> const& start() const
00150         { return char_literal; }
00151         rule<ScannerT> char_literal;
00152       };
00153     };
00154     // ========================================================================
00166     template<typename RT=int>
00167     class IntGrammar : public grammar
00168     <
00169       IntGrammar<RT>,
00170       typename ClosureGrammar<RT>::context_t
00171     >
00172     {
00173     public:
00174       typedef RT ResultT;
00175     public:
00176       template <typename ScannerT>
00177       struct definition
00178       {
00179         definition( IntGrammar<RT> const &self)
00180         {
00181           int_literal = lexeme_d[int_parser<RT>()[self.val=arg1]
00182             >> !(ch_p('u') | ch_p('U') | ch_p('l') | ch_p('L'))];
00183         }
00184         rule<ScannerT> const& start() const { return int_literal; }
00185         rule<ScannerT> int_literal;
00186       };
00187     };
00188     // ========================================================================
00200     template<typename RT=double>
00201     class RealGrammar : public grammar
00202     <
00203       RealGrammar<RT>,typename ClosureGrammar<RT>::context_t
00204     >
00205     {
00206     public:
00207       typedef RT ResultT;
00208     public:
00209       template <typename ScannerT>
00210       struct definition
00211       {
00212         definition( RealGrammar const &self)
00213         {
00214           real_literal
00215             = lexeme_d[real_parser<RT,
00216             real_parser_policies<RT> >()[self.val = arg1]
00217             >> !(ch_p('f') | ch_p('F') | ch_p('l') | ch_p('L'))];
00218         }
00219         rule<ScannerT> const& start() const
00220         { return real_literal; }
00221         rule<ScannerT> real_literal;
00222       };
00223     };
00224     // ========================================================================
00239     class StringGrammar : public grammar
00240     <
00241       StringGrammar, ClosureGrammar<std::string>::context_t
00242     >
00243     {
00244     public:
00245       typedef std::string ResultT;
00254       void matchString() const
00255       {
00256         for ( std::string::iterator cur=this->val().begin();
00257               cur!=this->val().end();cur++)
00258         { if(std::isspace(*cur) ) { *cur = ' '; } }
00259       }
00260     public:
00261         template <typename ScannerT>
00262         struct definition
00263         {
00264           definition( StringGrammar const &self )
00265           {
00266             string_literal = (lexeme_d
00267               [
00268                ('"' >> (*( str_p("\\\"")
00269                            |
00270                            (anychar_p-'"') ))
00271                 [self.val = construct_<std::string>
00272                  (arg1,arg2)] >>
00273                 '"')
00274                |
00275                ('\'' >> (*( str_p("\\'")
00276                             |
00277                             (anychar_p-'\'') ))
00278                 [self.val = construct_<std::string>
00279                  (arg1,arg2)]>>
00280                 '\'')])[boost::bind(&StringGrammar::matchString,&self)];
00281           }
00282           rule<ScannerT> const& start() const { return string_literal; }
00283           rule<ScannerT> string_literal;
00284         };
00285       };
00286     // ========================================================================
00299     class SkipperGrammar : public grammar<SkipperGrammar>
00300     {
00301     public:
00305       SkipperGrammar ( const bool skipnewline = true )
00306         : m_skipnewline(skipnewline){}
00307     public:
00309       bool skipnewline() const{return m_skipnewline;}
00310     public:
00311       template <typename ScannerT>
00312       struct definition
00313       {
00314         definition( SkipperGrammar const& self)
00315         {
00316           if ( self.skipnewline() )
00317           {
00318             skip
00319               =   space_p
00320               |   comment_p("//")     // C++ comment
00321               |   comment_p("/*", "*/")     // C comment
00322               ;
00323           }
00324           else
00325           {
00326             skip
00327               =   (space_p-eol_p)
00328               |   comment_p("//")     // C++ comment
00329               |   comment_p("/*", "*/")     // C comment
00330               ;
00331           }
00332         }
00333         rule<ScannerT>  skip;
00334         rule<ScannerT> const& start() const { return skip; }
00335       };
00336     private:
00337       bool m_skipnewline;
00338     };
00339     // ========================================================================
00350     template <typename KeyGrammarT, typename ValueGrammarT>
00351     class PairGrammar : public grammar
00352     <
00353       PairGrammar<KeyGrammarT,ValueGrammarT>,
00354       typename ClosureGrammar<
00355       std::pair<typename KeyGrammarT::ResultT,
00356                 typename ValueGrammarT::ResultT> >::context_t
00357     >
00358     {
00359     public:
00360       typedef typename KeyGrammarT::ResultT KeyT;
00361       typedef typename ValueGrammarT::ResultT ValueT;
00362       typedef std::pair<KeyT,ValueT> ResultT;
00363     public:
00367       PairGrammar ( const std::string&  delim = "," )
00368         : m_delim(delim) {}
00369     public:
00371       void matchFirst  ( const KeyT&   first  ) const { this->val().first = first; }
00373       void matchSecond ( const ValueT& second ) const { this->val().second = second; }
00374     public:
00375       template <typename ScannerT>
00376       struct definition
00377       {
00378         definition( PairGrammar const &self)
00379         {
00380           para
00381             = (
00382                str_p("(")
00383                >> (grkey[boost::bind(&PairGrammar::matchFirst,&self,_1)])
00384                >> self.delim().c_str()
00385                >> (grvalue[boost::bind(&PairGrammar::matchSecond,&self,_1)])
00386                >> str_p(")")
00387                ) ;
00388         }
00389         rule<ScannerT> const& start() const { return para; }
00390         rule<ScannerT> para;
00391         KeyGrammarT grkey;
00392         ValueGrammarT grvalue;
00393       };
00394     public:
00396       const std::string& delim() const { return m_delim ; }
00400       void setDelim ( const std::string& delim ) { m_delim = delim;}
00401     private:
00402       std::string m_delim;
00403     };
00404     // ========================================================================
00416     template <typename GrammarT>
00417     class VectorGrammar : public grammar
00418     <
00419       VectorGrammar<GrammarT> ,
00420       typename ClosureGrammar<std::vector<typename GrammarT::ResultT> >::context_t
00421     >
00422     {
00423     public:
00424       typedef typename GrammarT::ResultT ValueT;
00425       typedef std::vector<ValueT> ResultT;
00426       typedef VectorGrammar<GrammarT> SelfT;
00427     public:
00429       void matchItem(const ValueT& value) const { this->val().push_back(value); }
00430     public:
00431       template <typename ScannerT>
00432       struct definition
00433       {
00434         definition(SelfT const &self)
00435         {
00436           inner =
00437             !(gr[boost::bind(&VectorGrammar::matchItem,&self,_1)]
00438               >> *(','>>gr[boost::bind(&VectorGrammar::matchItem,&self,_1)]));
00439           vec =
00440             "{">>inner>>"}" | "[">>inner>>"]";
00441         }
00442         rule<ScannerT> const& start() const { return vec; }
00443         rule<ScannerT> vec,inner;
00444         GrammarT gr;
00445       };
00446     };
00447     // ========================================================================
00462     template <typename KeyGrammarT, typename ValueGrammarT>
00463     class MapGrammar : public grammar
00464     <
00465       MapGrammar<KeyGrammarT,ValueGrammarT>,
00466       typename AttributesClosureGrammar
00467       < std::map<typename KeyGrammarT::ResultT,
00468                  typename ValueGrammarT::ResultT>,
00469         std::pair<typename KeyGrammarT::ResultT,
00470                   typename ValueGrammarT::ResultT> >::context_t
00471     >
00472     {
00473     public:
00474       typedef typename KeyGrammarT::ResultT KeyT;
00475       typedef typename ValueGrammarT::ResultT ValueT;
00476       typedef std::map<KeyT,ValueT> ResultT;
00477     public:
00479       void matchItem  () const
00480       {
00481         //this->val().insert(this->attrs());
00482         this->val()[this->attrs().first] = this->attrs().second ;
00483       }
00485       void matchFirst ( const KeyT&   value ) const {  this->attrs().first = value ; }
00487       void matchSecond( const ValueT& value ) const { this->attrs().second = value ; }
00488     public:
00489       template <typename ScannerT>
00490       struct definition
00491       {
00492         definition( MapGrammar const &self)
00493         {
00494           vec
00495             = ('{'>> inner_list >> '}') | ('['>>inner_list>>']');
00496           inner_list
00497             =
00498             !( inner[boost::bind(&MapGrammar::matchItem,&self)]
00499                >> *( ch_p(',') >>
00500                      inner[boost::bind(&MapGrammar::matchItem,&self)] )
00501                );
00502           inner
00503             =
00504             grKey[boost ::bind(&MapGrammar::matchFirst,&self,_1)]
00505               >> ( ch_p('=') | ch_p(':'))
00506               >> grValue[boost::bind(&MapGrammar::matchSecond,&self,_1)] ;
00507         }
00508         KeyGrammarT grKey;
00509         ValueGrammarT grValue;
00510         rule<ScannerT> const& start() const { return vec; }
00511         rule<ScannerT> vec,inner, inner_list ;
00512       };
00513     };
00514     // ========================================================================
00515   } // end of namespace Gaudi::Parsers
00516 } // end of namespace Gaudi
00517 // ============================================================================
00518 // The END
00519 // ============================================================================
00520 #endif // GAUDIKERNEL_GRAMMARS_H
00521 // ============================================================================

Generated at Mon Sep 7 18:05:37 2009 for Gaudi Framework, version v21r4 by Doxygen version 1.5.6 written by Dimitri van Heesch, © 1997-2004