attr_cast.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright (c) 2001-2011 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/qi/detail/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_param) 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, domain>
  79. transform;
  80. typename transform::type attr_ = transform::pre(attr_param);
  81. if (!compile<qi::domain>(subject).
  82. parse(first, last, context, skipper, attr_))
  83. {
  84. transform::fail(attr_param);
  85. return false;
  86. }
  87. // do up-stream transformation, this mainly integrates the results
  88. // back into the original attribute value, if appropriate
  89. transform::post(attr_param, attr_);
  90. return true;
  91. }
  92. template <typename Context>
  93. info what(Context& context) const
  94. {
  95. return info("attr_cast"
  96. , compile<qi::domain>(subject).what(context));
  97. }
  98. Subject subject;
  99. // silence MSVC warning C4512: assignment operator could not be generated
  100. BOOST_DELETED_FUNCTION(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