Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:01:56

0001 /*=============================================================================
0002     Copyright (c) 1998-2003 Joel de Guzman
0003     http://spirit.sourceforge.net/
0004 
0005   Distributed under the Boost Software License, Version 1.0. (See accompanying
0006   file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 =============================================================================*/
0008 #if !defined(BOOST_SPIRIT_MATCH_HPP)
0009 #define BOOST_SPIRIT_MATCH_HPP
0010 
0011 #include <boost/spirit/home/classic/namespace.hpp>
0012 #include <boost/spirit/home/classic/core/config.hpp>
0013 #include <boost/spirit/home/classic/core/nil.hpp>
0014 #include <boost/call_traits.hpp>
0015 #include <boost/optional.hpp>
0016 #include <boost/spirit/home/classic/core/assert.hpp>
0017 #include <boost/spirit/home/classic/core/safe_bool.hpp>
0018 #include <boost/spirit/home/classic/core/impl/match_attr_traits.ipp>
0019 #include <boost/type_traits/add_const.hpp>
0020 #include <boost/type_traits/add_reference.hpp>
0021 #include <boost/type_traits/conditional.hpp>
0022 #include <boost/type_traits/is_reference.hpp>
0023 
0024 namespace boost { namespace spirit {
0025 
0026 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
0027 
0028     ///////////////////////////////////////////////////////////////////////////
0029     //
0030     //  match class
0031     //
0032     //      The match holds the result of a parser. A match object evaluates
0033     //      to true when a successful match is found, otherwise false. The
0034     //      length of the match is the number of characters (or tokens) that
0035     //      is successfully matched. This can be queried through its length()
0036     //      member function. A negative value means that the match is
0037     //      unsuccessful.
0038     //
0039     //      Each parser may have an associated attribute. This attribute is
0040     //      also returned back to the client on a successful parse through
0041     //      the match object. The match's value() member function returns the
0042     //      match's attribute.
0043     //
0044     //      A match attribute is valid:
0045     //
0046     //          * on a successful match
0047     //          * when its value is set through the value(val) member function
0048     //          * if it is assigned or copied from a compatible match object
0049     //            (e.g. match<double> from match<int>) with a valid attribute.
0050     //
0051     //      The match attribute is undefined:
0052     //
0053     //          * on an unsuccessful match
0054     //          * when an attempt to copy or assign from another match object
0055     //            with an incompatible attribute type (e.g. match<std::string>
0056     //            from match<int>).
0057     //
0058     //      The member function has_valid_attribute() can be queried to know if
0059     //      it is safe to get the match's attribute. The attribute may be set
0060     //      through the member function value(v) where v is the new attribute
0061     //      value.
0062     //
0063     ///////////////////////////////////////////////////////////////////////////
0064     template <typename T = nil_t>
0065     class match : public safe_bool<match<T> >
0066     {
0067         typedef typename
0068             conditional<
0069                 is_reference<T>::value
0070               , T
0071               , typename add_reference<
0072                     typename add_const<T>::type
0073                 >::type
0074             >::type attr_ref_t;
0075 
0076     public:
0077 
0078         typedef typename boost::optional<T> optional_type;
0079         typedef attr_ref_t ctor_param_t;
0080         typedef attr_ref_t return_t;
0081         typedef T attr_t;
0082 
0083                                 match();
0084         explicit                match(std::size_t length);
0085                                 match(std::size_t length, ctor_param_t val);
0086 
0087         bool                    operator!() const;
0088         std::ptrdiff_t          length() const;
0089         bool                    has_valid_attribute() const;
0090         return_t                value() const;
0091         void                    swap(match& other);
0092 
0093         template <typename T2>
0094         match(match<T2> const& other)
0095         : len(other.length()), val()
0096         {
0097             impl::match_attr_traits<T>::copy(val, other);
0098         }
0099 
0100         template <typename T2>
0101         match&
0102         operator=(match<T2> const& other)
0103         {
0104             impl::match_attr_traits<T>::assign(val, other);
0105             len = other.length();
0106             return *this;
0107         }
0108 
0109         template <typename MatchT>
0110         void
0111         concat(MatchT const& other)
0112         {
0113             BOOST_SPIRIT_ASSERT(*this && other);
0114             len += other.length();
0115         }
0116 
0117         template <typename ValueT>
0118         void
0119         value(ValueT const& val_)
0120         {
0121             impl::match_attr_traits<T>::set_value(val, val_, is_reference<T>());
0122         }
0123 
0124         bool operator_bool() const
0125         {
0126             return len >= 0;
0127         }
0128 
0129     private:
0130 
0131         std::ptrdiff_t len;
0132         optional_type val;
0133     };
0134 
0135     ///////////////////////////////////////////////////////////////////////////
0136     //
0137     //  match class specialization for nil_t values
0138     //
0139     ///////////////////////////////////////////////////////////////////////////
0140     template <>
0141     class match<nil_t> : public safe_bool<match<nil_t> >
0142     {
0143     public:
0144 
0145         typedef nil_t attr_t;
0146         typedef nil_t return_t;
0147 
0148                                 match();
0149         explicit                match(std::size_t length);
0150                                 match(std::size_t length, nil_t);
0151 
0152         bool                    operator!() const;
0153         bool                    has_valid_attribute() const;
0154         std::ptrdiff_t          length() const;
0155         nil_t                   value() const;
0156         void                    value(nil_t);
0157         void                    swap(match& other);
0158 
0159         template <typename T>
0160         match(match<T> const& other)
0161         : len(other.length()) {}
0162 
0163         template <typename T>
0164         match<>&
0165         operator=(match<T> const& other)
0166         {
0167             len = other.length();
0168             return *this;
0169         }
0170 
0171         template <typename T>
0172         void
0173         concat(match<T> const& other)
0174         {
0175             BOOST_SPIRIT_ASSERT(*this && other);
0176             len += other.length();
0177         }
0178 
0179         bool operator_bool() const
0180         {
0181             return len >= 0;
0182         }
0183 
0184     private:
0185 
0186         std::ptrdiff_t len;
0187     };
0188 
0189 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
0190 
0191 }} // namespace BOOST_SPIRIT_CLASSIC_NS
0192 
0193 #endif
0194 #include <boost/spirit/home/classic/core/impl/match.ipp>
0195