spirit_qi.hpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file support/spirit_qi.hpp
  9. * \author Andrey Semashev
  10. * \date 19.07.2009
  11. *
  12. * This header enables Boost.Spirit.Qi support for Boost.Log.
  13. */
  14. #ifndef BOOST_LOG_SUPPORT_SPIRIT_QI_HPP_INCLUDED_
  15. #define BOOST_LOG_SUPPORT_SPIRIT_QI_HPP_INCLUDED_
  16. #include <boost/core/enable_if.hpp>
  17. #include <boost/spirit/include/qi_parse.hpp>
  18. #include <boost/spirit/include/qi_domain.hpp>
  19. #include <boost/spirit/include/support_unused.hpp>
  20. #include <boost/spirit/home/support/meta_compiler.hpp> // spirit::compile()
  21. #include <boost/spirit/home/qi/nonterminal/nonterminal_fwd.hpp> // rule forward declaration
  22. #include <boost/log/detail/config.hpp>
  23. #include <boost/log/utility/functional/matches.hpp>
  24. #include <boost/log/detail/header.hpp>
  25. #ifdef BOOST_HAS_PRAGMA_ONCE
  26. #pragma once
  27. #endif
  28. namespace boost {
  29. BOOST_LOG_OPEN_NAMESPACE
  30. namespace aux {
  31. //! This tag type is used if an expression is recognized as a Boost.Spirit.Qi expression
  32. struct boost_spirit_qi_expression_tag;
  33. //! The metafunction detects the matching expression kind and returns a tag that is used to specialize \c match_traits
  34. template< typename ExpressionT >
  35. struct matching_expression_kind< ExpressionT, typename boost::enable_if_c< spirit::traits::matches< spirit::qi::domain, ExpressionT >::value >::type >
  36. {
  37. typedef boost_spirit_qi_expression_tag type;
  38. };
  39. //! The matching function implementation
  40. template< typename ExpressionT >
  41. struct match_traits< ExpressionT, boost_spirit_qi_expression_tag >
  42. {
  43. typedef typename spirit::result_of::compile< spirit::qi::domain, ExpressionT, spirit::unused_type >::type compiled_type;
  44. static compiled_type compile(ExpressionT const& expr)
  45. {
  46. return spirit::compile< spirit::qi::domain >(expr);
  47. }
  48. template< typename StringT >
  49. static bool matches(StringT const& str, ExpressionT const& expr)
  50. {
  51. typedef typename StringT::const_iterator const_iterator;
  52. const_iterator it = str.begin(), end = str.end();
  53. return (spirit::qi::parse(it, end, expr) && it == end);
  54. }
  55. };
  56. //! The matching function implementation
  57. template< typename IteratorT, typename T1, typename T2, typename T3, typename T4 >
  58. struct match_traits< spirit::qi::rule< IteratorT, T1, T2, T3, T4 >, boost_spirit_qi_expression_tag >
  59. {
  60. typedef spirit::qi::rule< IteratorT, T1, T2, T3, T4 > compiled_type;
  61. static compiled_type compile(compiled_type const& expr) { return expr; }
  62. template< typename StringT >
  63. static bool matches(StringT const& str, compiled_type const& expr)
  64. {
  65. typedef typename StringT::const_iterator const_iterator;
  66. const_iterator it = str.begin(), end = str.end();
  67. return (spirit::qi::parse(it, end, expr) && it == end);
  68. }
  69. };
  70. } // namespace aux
  71. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  72. } // namespace boost
  73. #include <boost/log/detail/footer.hpp>
  74. #endif // BOOST_LOG_SUPPORT_SPIRIT_QI_HPP_INCLUDED_