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