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