reference.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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_REFERENCE_OCTOBER_31_2008_1218AM)
  7. #define BOOST_SPIRIT_REFERENCE_OCTOBER_31_2008_1218AM
  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/parser.hpp>
  13. #include <boost/spirit/home/support/info.hpp>
  14. #include <boost/spirit/home/support/handles_container.hpp>
  15. #include <boost/type_traits/remove_const.hpp>
  16. #include <boost/ref.hpp>
  17. namespace boost { namespace spirit { namespace qi
  18. {
  19. ///////////////////////////////////////////////////////////////////////////
  20. // reference is a parser that references another parser (its Subject)
  21. ///////////////////////////////////////////////////////////////////////////
  22. template <typename Subject>
  23. struct reference : parser<reference<Subject> >
  24. {
  25. typedef Subject subject_type;
  26. reference(Subject& subject)
  27. : ref(subject) {}
  28. template <typename Context, typename Iterator>
  29. struct attribute : Subject::template attribute<Context, Iterator> {};
  30. template <typename Iterator, typename Context
  31. , typename Skipper, typename Attribute>
  32. bool parse(Iterator& first, Iterator const& last
  33. , Context& context, Skipper const& skipper
  34. , Attribute& attr_) const
  35. {
  36. return ref.get().parse(first, last, context, skipper, attr_);
  37. }
  38. template <typename Context>
  39. info what(Context& context) const
  40. {
  41. // the reference is transparent (does not add any info)
  42. return ref.get().what(context);
  43. }
  44. boost::reference_wrapper<Subject> ref;
  45. };
  46. }}}
  47. namespace boost { namespace spirit { namespace traits
  48. {
  49. ///////////////////////////////////////////////////////////////////////////
  50. template <typename Subject, typename Attribute, typename Context
  51. , typename Iterator>
  52. struct handles_container<qi::reference<Subject>, Attribute, Context
  53. , Iterator>
  54. : handles_container<typename remove_const<Subject>::type
  55. , Attribute, Context, Iterator>
  56. {};
  57. }}}
  58. #endif