Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/spirit/home/qi/string/symbols.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*=============================================================================
0002     Copyright (c) 2001-2011 Joel de Guzman
0003 
0004     Distributed under the Boost Software License, Version 1.0. (See accompanying
0005     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 ==============================================================================*/
0007 #if !defined(BOOST_SPIRIT_SYMBOLS_MARCH_11_2007_1055AM)
0008 #define BOOST_SPIRIT_SYMBOLS_MARCH_11_2007_1055AM
0009 
0010 #if defined(_MSC_VER)
0011 #pragma once
0012 #endif
0013 
0014 #include <boost/spirit/home/qi/domain.hpp>
0015 #include <boost/spirit/home/qi/skip_over.hpp>
0016 #include <boost/spirit/home/qi/string/tst.hpp>
0017 #include <boost/spirit/home/qi/reference.hpp>
0018 #include <boost/spirit/home/qi/meta_compiler.hpp>
0019 #include <boost/spirit/home/qi/detail/assign_to.hpp>
0020 #include <boost/spirit/home/qi/parser.hpp>
0021 #include <boost/spirit/home/support/detail/get_encoding.hpp>
0022 #include <boost/spirit/home/support/modify.hpp>
0023 #include <boost/spirit/home/support/info.hpp>
0024 #include <boost/spirit/home/support/unused.hpp>
0025 #include <boost/spirit/home/support/string_traits.hpp>
0026 #include <boost/proto/extends.hpp>
0027 #include <boost/proto/traits.hpp>
0028 #include <boost/range/begin.hpp>
0029 #include <boost/range/end.hpp>
0030 #include <boost/shared_ptr.hpp>
0031 
0032 #if defined(BOOST_MSVC)
0033 # pragma warning(push)
0034 # pragma warning(disable: 4355) // 'this' : used in base member initializer list warning
0035 #endif
0036 
0037 namespace boost { namespace spirit { namespace qi
0038 {
0039     template <
0040         typename Char = char
0041       , typename T = unused_type
0042       , typename Lookup = tst<Char, T>
0043       , typename Filter = tst_pass_through>
0044     struct symbols
0045       : proto::extends<
0046             typename proto::terminal<
0047                 reference<symbols<Char, T, Lookup, Filter> >
0048             >::type
0049           , symbols<Char, T, Lookup, Filter>
0050         >
0051       , primitive_parser<symbols<Char, T, Lookup, Filter> >
0052     {
0053         typedef Char char_type; // the character type
0054         typedef T value_type; // the value associated with each entry
0055         typedef symbols<Char, T, Lookup, Filter> this_type;
0056         typedef reference<this_type> reference_;
0057         typedef typename proto::terminal<reference_>::type terminal;
0058         typedef proto::extends<terminal, this_type> base_type;
0059 
0060         template <typename Context, typename Iterator>
0061         struct attribute
0062         {
0063             typedef value_type type;
0064         };
0065 
0066         symbols(std::string const& name = "symbols")
0067           : base_type(terminal::make(reference_(*this)))
0068           , add(*this)
0069           , remove(*this)
0070           , lookup(new Lookup())
0071           , name_(name)
0072         {
0073         }
0074 
0075         symbols(symbols const& syms)
0076           : base_type(terminal::make(reference_(*this)))
0077           , add(*this)
0078           , remove(*this)
0079           , lookup(syms.lookup)
0080           , name_(syms.name_)
0081         {
0082         }
0083 
0084         template <typename Filter_>
0085         symbols(symbols<Char, T, Lookup, Filter_> const& syms)
0086           : base_type(terminal::make(reference_(*this)))
0087           , add(*this)
0088           , remove(*this)
0089           , lookup(syms.lookup)
0090           , name_(syms.name_)
0091         {
0092         }
0093 
0094         template <typename Symbols>
0095         symbols(Symbols const& syms, std::string const& name = "symbols")
0096           : base_type(terminal::make(reference_(*this)))
0097           , add(*this)
0098           , remove(*this)
0099           , lookup(new Lookup())
0100           , name_(name)
0101         {
0102             typename range_const_iterator<Symbols>::type si = boost::begin(syms);
0103             while (si != boost::end(syms))
0104                 add(*si++);
0105         }
0106 
0107         template <typename Symbols, typename Data>
0108         symbols(Symbols const& syms, Data const& data
0109               , std::string const& name = "symbols")
0110           : base_type(terminal::make(reference_(*this)))
0111           , add(*this)
0112           , remove(*this)
0113           , lookup(new Lookup())
0114           , name_(name)
0115         {
0116             typename range_const_iterator<Symbols>::type si = boost::begin(syms);
0117             typename range_const_iterator<Data>::type di = boost::begin(data);
0118             while (si != boost::end(syms))
0119                 add(*si++, *di++);
0120         }
0121 
0122         symbols&
0123         operator=(symbols const& rhs)
0124         {
0125             name_ = rhs.name_;
0126             *lookup = *rhs.lookup;
0127             return *this;
0128         }
0129 
0130         template <typename Filter_>
0131         symbols&
0132         operator=(symbols<Char, T, Lookup, Filter_> const& rhs)
0133         {
0134             name_ = rhs.name_;
0135             *lookup = *rhs.lookup;
0136             return *this;
0137         }
0138 
0139         void clear()
0140         {
0141             lookup->clear();
0142         }
0143 
0144         struct adder;
0145         struct remover;
0146 
0147         template <typename Str>
0148         adder const&
0149         operator=(Str const& str)
0150         {
0151             lookup->clear();
0152             return add(str);
0153         }
0154 
0155         template <typename Str>
0156         friend adder const&
0157         operator+=(symbols& sym, Str const& str)
0158         {
0159             return sym.add(str);
0160         }
0161 
0162         template <typename Str>
0163         friend remover const&
0164         operator-=(symbols& sym, Str const& str)
0165         {
0166             return sym.remove(str);
0167         }
0168 
0169 #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
0170         // non-const version needed to suppress proto's += kicking in
0171         template <typename Str>
0172         friend adder const&
0173         operator+=(symbols& sym, Str& str)
0174         {
0175             return sym.add(str);
0176         }
0177 
0178         // non-const version needed to suppress proto's -= kicking in
0179         template <typename Str>
0180         friend remover const&
0181         operator-=(symbols& sym, Str& str)
0182         {
0183             return sym.remove(str);
0184         }
0185 #else
0186         // for rvalue references
0187         template <typename Str>
0188         friend adder const&
0189         operator+=(symbols& sym, Str&& str)
0190         {
0191             return sym.add(str);
0192         }
0193 
0194         // for rvalue references
0195         template <typename Str>
0196         friend remover const&
0197         operator-=(symbols& sym, Str&& str)
0198         {
0199             return sym.remove(str);
0200         }
0201 #endif
0202         template <typename F>
0203         void for_each(F f) const
0204         {
0205             lookup->for_each(f);
0206         }
0207 
0208         template <typename Str>
0209         value_type& at(Str const& str)
0210         {
0211             return *lookup->add(traits::get_begin<Char>(str)
0212                 , traits::get_end<Char>(str), T());
0213         }
0214 
0215         template <typename Iterator>
0216         value_type* prefix_find(Iterator& first, Iterator const& last)
0217         {
0218             return lookup->find(first, last, Filter());
0219         }
0220 
0221         template <typename Iterator>
0222         value_type const* prefix_find(Iterator& first, Iterator const& last) const
0223         {
0224             return lookup->find(first, last, Filter());
0225         }
0226 
0227         template <typename Str>
0228         value_type* find(Str const& str)
0229         {
0230             return find_impl(traits::get_begin<Char>(str)
0231                 , traits::get_end<Char>(str));
0232         }
0233 
0234         template <typename Str>
0235         value_type const* find(Str const& str) const
0236         {
0237             return find_impl(traits::get_begin<Char>(str)
0238                 , traits::get_end<Char>(str));
0239         }
0240 
0241 private:
0242         template <typename Iterator>
0243         value_type* find_impl(Iterator begin, Iterator end)
0244         {
0245             value_type* r = lookup->find(begin, end, Filter());
0246             return begin == end ? r : 0;
0247         }
0248 
0249         template <typename Iterator>
0250         value_type const* find_impl(Iterator begin, Iterator end) const
0251         {
0252             value_type const* r = lookup->find(begin, end, Filter());
0253             return begin == end ? r : 0;
0254         }
0255 
0256 public:
0257         template <typename Iterator, typename Context
0258           , typename Skipper, typename Attribute>
0259         bool parse(Iterator& first, Iterator const& last
0260           , Context& /*context*/, Skipper const& skipper, Attribute& attr_) const
0261         {
0262             qi::skip_over(first, last, skipper);
0263 
0264             if (value_type* val_ptr
0265                 = lookup->find(first, last, Filter()))
0266             {
0267                 spirit::traits::assign_to(*val_ptr, attr_);
0268                 return true;
0269             }
0270             return false;
0271         }
0272 
0273         template <typename Context>
0274         info what(Context& /*context*/) const
0275         {
0276             return info(name_);
0277         }
0278 
0279         void name(std::string const &str)
0280         {
0281             name_ = str;
0282         }
0283         std::string const &name() const
0284         {
0285             return name_;
0286         }
0287 
0288 #ifdef _MSC_VER
0289 #  pragma warning(push)
0290 #  pragma warning(disable: 4512) // assignment operator could not be generated.
0291 #endif
0292         struct adder
0293         {
0294             template <typename, typename = unused_type, typename = unused_type>
0295             struct result { typedef adder const& type; };
0296 
0297             adder(symbols& sym_)
0298               : sym(sym_)
0299             {
0300             }
0301 
0302             template <typename Iterator>
0303             adder const&
0304             operator()(Iterator const& first, Iterator const& last, T const& val) const
0305             {
0306                 sym.lookup->add(first, last, val);
0307                 return *this;
0308             }
0309 
0310             template <typename Str>
0311             adder const&
0312             operator()(Str const& s, T const& val = T()) const
0313             {
0314                 sym.lookup->add(traits::get_begin<Char>(s)
0315                   , traits::get_end<Char>(s), val);
0316                 return *this;
0317             }
0318 
0319             template <typename Str>
0320             adder const&
0321             operator,(Str const& s) const
0322             {
0323                 sym.lookup->add(traits::get_begin<Char>(s)
0324                   , traits::get_end<Char>(s), T());
0325                 return *this;
0326             }
0327 
0328             symbols& sym;
0329         };
0330 
0331         struct remover
0332         {
0333             template <typename, typename = unused_type, typename = unused_type>
0334             struct result { typedef remover const& type; };
0335 
0336             remover(symbols& sym_)
0337               : sym(sym_)
0338             {
0339             }
0340 
0341             template <typename Iterator>
0342             remover const&
0343             operator()(Iterator const& first, Iterator const& last) const
0344             {
0345                 sym.lookup->remove(first, last);
0346                 return *this;
0347             }
0348 
0349             template <typename Str>
0350             remover const&
0351             operator()(Str const& s) const
0352             {
0353                 sym.lookup->remove(traits::get_begin<Char>(s)
0354                   , traits::get_end<Char>(s));
0355                 return *this;
0356             }
0357 
0358             template <typename Str>
0359             remover const&
0360             operator,(Str const& s) const
0361             {
0362                 sym.lookup->remove(traits::get_begin<Char>(s)
0363                   , traits::get_end<Char>(s));
0364                 return *this;
0365             }
0366 
0367             symbols& sym;
0368         };
0369 #ifdef _MSC_VER
0370 #  pragma warning(pop)
0371 #endif
0372 
0373         adder add;
0374         remover remove;
0375         shared_ptr<Lookup> lookup;
0376         std::string name_;
0377     };
0378 
0379     ///////////////////////////////////////////////////////////////////////////
0380     // Parser generators: make_xxx function (objects)
0381     ///////////////////////////////////////////////////////////////////////////
0382     template <typename Char, typename T, typename Lookup
0383       , typename Filter, typename Modifiers>
0384     struct make_primitive<reference<symbols<Char, T, Lookup, Filter> >, Modifiers>
0385     {
0386         template <typename CharEncoding>
0387         struct no_case_filter
0388         {
0389             Char operator()(Char ch) const
0390             {
0391                 return static_cast<Char>(CharEncoding::tolower(ch));
0392             }
0393         };
0394 
0395         typedef has_modifier<Modifiers, tag::char_code_base<tag::no_case> > no_case;
0396         typedef reference<symbols<Char, T, Lookup, Filter> > reference_;
0397         typedef no_case_filter<
0398             typename spirit::detail::get_encoding_with_case<
0399                 Modifiers
0400               , char_encoding::standard
0401               , no_case::value>::type>
0402         nc_filter;
0403 
0404         typedef typename mpl::if_<
0405             no_case
0406           , symbols<Char, T, Lookup, nc_filter>
0407           , reference_>::type
0408         result_type;
0409 
0410         result_type operator()(reference_ ref, unused_type) const
0411         {
0412             return result_type(ref.ref.get());
0413         }
0414     };
0415 }}}
0416 
0417 namespace boost { namespace spirit { namespace traits
0418 {
0419     ///////////////////////////////////////////////////////////////////////////
0420     template <typename Char, typename T, typename Lookup, typename Filter
0421       , typename Attr, typename Context, typename Iterator>
0422     struct handles_container<qi::symbols<Char, T, Lookup, Filter>, Attr, Context, Iterator>
0423       : traits::is_container<Attr> {};
0424 }}}
0425 
0426 #if defined(BOOST_MSVC)
0427 # pragma warning(pop)
0428 #endif
0429 
0430 #endif