eps.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  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(BOOST_SPIRIT_EPS_MARCH_23_2007_0454PM)
  7. #define BOOST_SPIRIT_EPS_MARCH_23_2007_0454PM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/spirit/home/qi/domain.hpp>
  12. #include <boost/spirit/home/qi/skip_over.hpp>
  13. #include <boost/spirit/home/qi/meta_compiler.hpp>
  14. #include <boost/spirit/home/support/unused.hpp>
  15. #include <boost/spirit/home/support/info.hpp>
  16. #include <boost/spirit/home/support/common_terminals.hpp>
  17. #include <boost/fusion/include/at.hpp>
  18. #include <boost/type_traits/is_convertible.hpp>
  19. namespace boost { namespace spirit
  20. {
  21. ///////////////////////////////////////////////////////////////////////////
  22. // Enablers
  23. ///////////////////////////////////////////////////////////////////////////
  24. template <>
  25. struct use_terminal<qi::domain, tag::eps> // enables eps
  26. : mpl::true_ {};
  27. template <typename A0>
  28. struct use_terminal<qi::domain
  29. , terminal_ex<tag::eps, fusion::vector1<A0> > // enables eps(bool-condition)
  30. > : is_convertible<A0, bool> {};
  31. template <> // enables eps(f)
  32. struct use_lazy_terminal<
  33. qi::domain, tag::eps, 1 /*arity*/
  34. > : mpl::true_ {};
  35. }}
  36. namespace boost { namespace spirit { namespace qi
  37. {
  38. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  39. using spirit::eps;
  40. #endif
  41. using spirit::eps_type;
  42. struct eps_parser : primitive_parser<eps_parser>
  43. {
  44. template <typename Context, typename Iterator>
  45. struct attribute
  46. {
  47. typedef unused_type type;
  48. };
  49. template <typename Iterator, typename Context
  50. , typename Skipper, typename Attribute>
  51. bool parse(Iterator& first, Iterator const& last
  52. , Context& /*context*/, Skipper const& skipper
  53. , Attribute& /*attr*/) const
  54. {
  55. qi::skip_over(first, last, skipper);
  56. return true;
  57. }
  58. template <typename Context>
  59. info what(Context& /*context*/) const
  60. {
  61. return info("eps");
  62. }
  63. };
  64. struct semantic_predicate : primitive_parser<semantic_predicate>
  65. {
  66. template <typename Context, typename Iterator>
  67. struct attribute
  68. {
  69. typedef unused_type type;
  70. };
  71. semantic_predicate(bool predicate_)
  72. : predicate(predicate_) {}
  73. template <typename Iterator, typename Context
  74. , typename Skipper, typename Attribute>
  75. bool parse(Iterator& first, Iterator const& last
  76. , Context& /*context*/, Skipper const& skipper
  77. , Attribute& /*attr*/) const
  78. {
  79. qi::skip_over(first, last, skipper);
  80. return predicate;
  81. }
  82. template <typename Context>
  83. info what(Context& /*context*/) const
  84. {
  85. return info("semantic-predicate");
  86. }
  87. bool predicate;
  88. };
  89. ///////////////////////////////////////////////////////////////////////////
  90. // Parser generators: make_xxx function (objects)
  91. ///////////////////////////////////////////////////////////////////////////
  92. template <typename Modifiers>
  93. struct make_primitive<tag::eps, Modifiers>
  94. {
  95. typedef eps_parser result_type;
  96. result_type operator()(unused_type, unused_type) const
  97. {
  98. return result_type();
  99. }
  100. };
  101. template <typename Modifiers, typename A0>
  102. struct make_primitive<
  103. terminal_ex<tag::eps, fusion::vector1<A0> >
  104. , Modifiers>
  105. {
  106. typedef semantic_predicate result_type;
  107. template <typename Terminal>
  108. result_type operator()(Terminal const& term, unused_type) const
  109. {
  110. return result_type(fusion::at_c<0>(term.args) ? true : false);
  111. }
  112. };
  113. }}}
  114. #endif