lexeme.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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(SPIRIT_LEXEME_MARCH_24_2007_0802AM)
  7. #define SPIRIT_LEXEME_MARCH_24_2007_0802AM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/spirit/home/qi/meta_compiler.hpp>
  12. #include <boost/spirit/home/qi/skip_over.hpp>
  13. #include <boost/spirit/home/qi/parser.hpp>
  14. #include <boost/spirit/home/qi/detail/unused_skipper.hpp>
  15. #include <boost/spirit/home/support/unused.hpp>
  16. #include <boost/spirit/home/support/common_terminals.hpp>
  17. #include <boost/spirit/home/qi/detail/attributes.hpp>
  18. #include <boost/spirit/home/support/info.hpp>
  19. #include <boost/spirit/home/support/handles_container.hpp>
  20. #include <boost/utility/enable_if.hpp>
  21. namespace boost { namespace spirit
  22. {
  23. ///////////////////////////////////////////////////////////////////////////
  24. // Enablers
  25. ///////////////////////////////////////////////////////////////////////////
  26. template <>
  27. struct use_directive<qi::domain, tag::lexeme> // enables lexeme
  28. : mpl::true_ {};
  29. }}
  30. namespace boost { namespace spirit { namespace qi
  31. {
  32. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  33. using spirit::lexeme;
  34. #endif
  35. using spirit::lexeme_type;
  36. template <typename Subject>
  37. struct lexeme_directive : unary_parser<lexeme_directive<Subject> >
  38. {
  39. typedef Subject subject_type;
  40. lexeme_directive(Subject const& subject_)
  41. : subject(subject_) {}
  42. template <typename Context, typename Iterator>
  43. struct attribute
  44. {
  45. typedef typename
  46. traits::attribute_of<subject_type, Context, Iterator>::type
  47. type;
  48. };
  49. template <typename Iterator, typename Context
  50. , typename Skipper, typename Attribute>
  51. typename disable_if<detail::is_unused_skipper<Skipper>, bool>::type
  52. parse(Iterator& first, Iterator const& last
  53. , Context& context, Skipper const& skipper
  54. , Attribute& attr_) const
  55. {
  56. qi::skip_over(first, last, skipper);
  57. return subject.parse(first, last, context
  58. , detail::unused_skipper<Skipper>(skipper), attr_);
  59. }
  60. template <typename Iterator, typename Context
  61. , typename Skipper, typename Attribute>
  62. typename enable_if<detail::is_unused_skipper<Skipper>, bool>::type
  63. parse(Iterator& first, Iterator const& last
  64. , Context& context, Skipper const& skipper
  65. , Attribute& attr_) const
  66. {
  67. // no need to pre-skip if skipper is unused
  68. //- qi::skip_over(first, last, skipper);
  69. return subject.parse(first, last, context
  70. , skipper, attr_);
  71. }
  72. template <typename Context>
  73. info what(Context& context) const
  74. {
  75. return info("lexeme", subject.what(context));
  76. }
  77. Subject subject;
  78. };
  79. ///////////////////////////////////////////////////////////////////////////
  80. // Parser generators: make_xxx function (objects)
  81. ///////////////////////////////////////////////////////////////////////////
  82. template <typename Subject, typename Modifiers>
  83. struct make_directive<tag::lexeme, Subject, Modifiers>
  84. {
  85. typedef lexeme_directive<Subject> result_type;
  86. result_type operator()(unused_type, Subject const& subject, unused_type) const
  87. {
  88. return result_type(subject);
  89. }
  90. };
  91. }}}
  92. namespace boost { namespace spirit { namespace traits
  93. {
  94. ///////////////////////////////////////////////////////////////////////////
  95. template <typename Subject>
  96. struct has_semantic_action<qi::lexeme_directive<Subject> >
  97. : unary_has_semantic_action<Subject> {};
  98. ///////////////////////////////////////////////////////////////////////////
  99. template <typename Subject, typename Attribute, typename Context
  100. , typename Iterator>
  101. struct handles_container<qi::lexeme_directive<Subject>, Attribute
  102. , Context, Iterator>
  103. : unary_handles_container<Subject, Attribute, Context, Iterator> {};
  104. }}}
  105. #endif