PageRenderTime 27ms CodeModel.GetById 13ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/src/contrib/boost/spirit/home/qi/auxiliary/attr_cast.hpp

http://pythonocc.googlecode.com/
C++ Header | 144 lines | 92 code | 23 blank | 29 comment | 1 complexity | a683d68febe71c3b3260b0bf2036221b MD5 | raw file
  1//  Copyright (c) 2001-2010 Hartmut Kaiser
  2//
  3//  Distributed under the Boost Software License, Version 1.0. (See accompanying
  4//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5
  6#if !defined(SPIRIT_QI_ATTR_CAST_SEP_26_2009_0735PM)
  7#define SPIRIT_QI_ATTR_CAST_SEP_26_2009_0735PM
  8
  9#if defined(_MSC_VER)
 10#pragma once
 11#endif
 12
 13#include <boost/spirit/home/qi/meta_compiler.hpp>
 14#include <boost/spirit/home/qi/parser.hpp>
 15#include <boost/spirit/home/qi/domain.hpp>
 16#include <boost/spirit/home/support/unused.hpp>
 17#include <boost/spirit/home/support/info.hpp>
 18#include <boost/spirit/home/support/common_terminals.hpp>
 19#include <boost/spirit/home/support/attributes.hpp>
 20#include <boost/spirit/home/support/auxiliary/attr_cast.hpp>
 21
 22namespace boost { namespace spirit
 23{
 24    ///////////////////////////////////////////////////////////////////////////
 25    // Enablers
 26    ///////////////////////////////////////////////////////////////////////////
 27
 28    // enables attr_cast<>() pseudo parser
 29    template <typename Expr, typename Exposed, typename Transformed>
 30    struct use_terminal<qi::domain
 31          , tag::stateful_tag<Expr, tag::attr_cast, Exposed, Transformed> >
 32      : mpl::true_ {};
 33}}
 34
 35namespace boost { namespace spirit { namespace qi
 36{
 37    using spirit::attr_cast;
 38
 39    ///////////////////////////////////////////////////////////////////////////
 40    // attr_cast_parser consumes the attribute of subject generator without
 41    // generating anything
 42    ///////////////////////////////////////////////////////////////////////////
 43    template <typename Exposed, typename Transformed, typename Subject>
 44    struct attr_cast_parser 
 45      : unary_parser<attr_cast_parser<Exposed, Transformed, Subject> >
 46    {
 47        typedef typename result_of::compile<qi::domain, Subject>::type
 48            subject_type;
 49
 50        typedef typename mpl::eval_if<
 51            traits::not_is_unused<Transformed>
 52          , mpl::identity<Transformed>
 53          , traits::attribute_of<subject_type> >::type 
 54        transformed_attribute_type;
 55
 56        attr_cast_parser(Subject const& subject)
 57          : subject(subject) 
 58        {
 59            // If you got an error_invalid_expression error message here,
 60            // then the expression (Subject) is not a valid spirit qi
 61            // expression.
 62            BOOST_SPIRIT_ASSERT_MATCH(qi::domain, Subject);
 63        }
 64
 65        // If Exposed is given, we use the given type, otherwise all we can do
 66        // is to guess, so we expose our inner type as an attribute and
 67        // deal with the passed attribute inside the parse function.
 68        template <typename Context, typename Iterator>
 69        struct attribute
 70          : mpl::if_<traits::not_is_unused<Exposed>, Exposed
 71              , transformed_attribute_type>
 72        {};
 73
 74        template <typename Iterator, typename Context, typename Skipper
 75          , typename Attribute>
 76        bool parse(Iterator& first, Iterator const& last
 77          , Context& context, Skipper const& skipper
 78          , Attribute& attr) const
 79        {
 80            // Find the real exposed attribute. If exposed is given, we use it
 81            // otherwise we assume the exposed attribute type to be the actual
 82            // attribute type as passed by the user.
 83            typedef typename mpl::if_<
 84                traits::not_is_unused<Exposed>, Exposed, Attribute>::type
 85            exposed_attribute_type;
 86
 87            // do down-stream transformation, provides attribute for embedded
 88            // parser
 89            typedef traits::transform_attribute<
 90                exposed_attribute_type, transformed_attribute_type> transform;
 91
 92            typename transform::type attr_ = transform::pre(attr);
 93
 94            if (!compile<qi::domain>(subject).
 95                    parse(first, last, context, skipper, attr_))
 96            {
 97                transform::fail(attr);
 98                return false;
 99            }
100
101            // do up-stream transformation, this mainly integrates the results
102            // back into the original attribute value, if appropriate
103            traits::post_transform(attr, attr_);
104            return true;
105        }
106
107        template <typename Context>
108        info what(Context& context) const
109        {
110            return info("attr_cast"
111              , compile<qi::domain>(subject).what(context));
112        }
113
114        Subject subject;
115
116    private:
117        // silence MSVC warning C4512: assignment operator could not be generated
118        attr_cast_parser& operator= (attr_cast_parser const&);
119    };
120
121    ///////////////////////////////////////////////////////////////////////////
122    // Parser generator: make_xxx function (objects)
123    ///////////////////////////////////////////////////////////////////////////
124    template <typename Expr, typename Exposed, typename Transformed
125      , typename Modifiers>
126    struct make_primitive<
127        tag::stateful_tag<Expr, tag::attr_cast, Exposed, Transformed>, Modifiers>
128    {
129        typedef attr_cast_parser<Exposed, Transformed, Expr> result_type;
130
131        template <typename Terminal>
132        result_type operator()(Terminal const& term, unused_type) const
133        {
134            typedef tag::stateful_tag<
135                Expr, tag::attr_cast, Exposed, Transformed> tag_type;
136            using spirit::detail::get_stateful_data;
137            return result_type(get_stateful_data<tag_type>::call(term));
138        }
139    };
140
141
142}}}
143
144#endif