custom_directives.hpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*=============================================================================
  2. Boost.Wave: A Standard compliant C++ preprocessor library
  3. http://www.boost.org/
  4. Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
  5. Software License, Version 1.0. (See accompanying file
  6. LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #if !defined(BOOST_WAVE_CUSTOM_DIRECTIVES_HOOKS_INCLUDED)
  9. #define BOOST_WAVE_CUSTOM_DIRECTIVES_HOOKS_INCLUDED
  10. #include <cstdio>
  11. #include <ostream>
  12. #include <string>
  13. #include <algorithm>
  14. #include <boost/assert.hpp>
  15. #include <boost/config.hpp>
  16. #include <boost/wave/token_ids.hpp>
  17. #include <boost/wave/util/macro_helpers.hpp>
  18. #include <boost/wave/preprocessing_hooks.hpp>
  19. ///////////////////////////////////////////////////////////////////////////////
  20. //
  21. // The custom_directives_hooks policy class is used to register some
  22. // of the more advanced (and probably more rarely used hooks with the Wave
  23. // library.
  24. //
  25. // This policy type is used as a template parameter to the boost::wave::context<>
  26. // object.
  27. //
  28. ///////////////////////////////////////////////////////////////////////////////
  29. class custom_directives_hooks
  30. : public boost::wave::context_policies::default_preprocessing_hooks
  31. {
  32. public:
  33. ///////////////////////////////////////////////////////////////////////////
  34. //
  35. // The function 'found_unknown_directive' is called, whenever an unknown
  36. // preprocessor directive was encountered.
  37. //
  38. // The parameter 'ctx' is a reference to the context object used for
  39. // instantiating the preprocessing iterators by the user.
  40. //
  41. // The parameter 'line' holds the tokens of the entire source line
  42. // containing the unknown directive.
  43. //
  44. // The parameter 'pending' may be used to push tokens back into the input
  45. // stream, which are to be used as the replacement text for the whole
  46. // line containing the unknown directive.
  47. //
  48. // The return value defines, whether the given expression has been
  49. // properly interpreted by the hook function or not. If this function
  50. // returns 'false', the library will raise an 'ill_formed_directive'
  51. // preprocess_exception. Otherwise the tokens pushed back into 'pending'
  52. // are passed on to the user program.
  53. //
  54. ///////////////////////////////////////////////////////////////////////////
  55. template <typename ContextT, typename ContainerT>
  56. bool
  57. found_unknown_directive(ContextT const& ctx, ContainerT const& line,
  58. ContainerT& pending)
  59. {
  60. namespace wave = boost::wave;
  61. typedef typename ContainerT::const_iterator iterator_type;
  62. iterator_type it = line.begin();
  63. wave::token_id id = wave::util::impl::skip_whitespace(it, line.end());
  64. if (id != wave::T_IDENTIFIER)
  65. return false; // nothing we could do
  66. if ((*it).get_value() == "version" || (*it).get_value() == "extension")
  67. {
  68. // handle #version and #extension directives
  69. std::copy(line.begin(), line.end(), std::back_inserter(pending));
  70. return true;
  71. }
  72. return false; // unknown directive
  73. }
  74. };
  75. #endif // !defined(BOOST_WAVE_ADVANCED_PREPROCESSING_HOOKS_INCLUDED)