1 /*=============================================================================
2 Boost.Wave: A Standard compliant C++ preprocessor library
6 Copyright (c) 2020 Jeff Trull
7 Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
8 Software License, Version 1.0. (See accompanying file
9 LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 =============================================================================*/
12 #if !defined(BOOST_CPP_HAS_INCLUDE_GRAMMAR_HPP_F48287B2_DC67_40A8_B4A1_800EFBD67869_INCLUDED)
13 #define BOOST_CPP_HAS_INCLUDE_GRAMMAR_HPP_F48287B2_DC67_40A8_B4A1_800EFBD67869_INCLUDED
15 #include <boost/wave/wave_config.hpp>
17 #include <boost/assert.hpp>
18 #include <boost/spirit/include/classic_core.hpp>
19 #include <boost/spirit/include/classic_closure.hpp>
20 #include <boost/spirit/include/classic_assign_actor.hpp>
21 #include <boost/spirit/include/classic_push_back_actor.hpp>
23 #include <boost/wave/token_ids.hpp>
24 #include <boost/wave/util/pattern_parser.hpp>
25 #include <boost/wave/grammars/cpp_has_include_grammar_gen.hpp>
27 #if !defined(spirit_append_actor)
28 #define spirit_append_actor(actor) boost::spirit::classic::push_back_a(actor)
29 #endif // !has_include(spirit_append_actor)
31 // this must occur after all of the includes and before any code appears
32 #ifdef BOOST_HAS_ABI_HEADERS
33 #include BOOST_ABI_PREFIX
36 ///////////////////////////////////////////////////////////////////////////////
41 ///////////////////////////////////////////////////////////////////////////////
42 // define, whether the rule's should generate some debug output
43 #define TRACE_CPP_HAS_INCLUDE_GRAMMAR \
44 bool(BOOST_SPIRIT_DEBUG_FLAGS_CPP & BOOST_SPIRIT_DEBUG_FLAGS_HAS_INCLUDE_GRAMMAR) \
47 template <typename ContainerT>
48 struct has_include_grammar :
49 public boost::spirit::classic::grammar<has_include_grammar<ContainerT> >
51 has_include_grammar(ContainerT &tokens_seq_,
52 bool &is_quoted_filename_,
54 : tokens_seq(tokens_seq_), is_quoted_filename(is_quoted_filename_),
55 is_system(is_system_), true_(true)
57 BOOST_SPIRIT_DEBUG_TRACE_GRAMMAR_NAME(*this, "has_include_grammar",
58 TRACE_CPP_HAS_INCLUDE_GRAMMAR);
59 is_quoted_filename = false;
63 template <typename ScannerT>
66 typedef boost::spirit::classic::rule<ScannerT> rule_t;
68 rule_t has_include_op;
69 rule_t system_include;
70 rule_t nonsystem_include;
73 rule_t parenthesized_exp;
74 rule_t computed_include;
76 definition(has_include_grammar const & self)
78 using namespace boost::spirit::classic;
79 using namespace boost::wave;
80 using namespace boost::wave::util;
83 = ch_p(T_IDENTIFIER) >> // token contains '__has_include'
85 (system_include | nonsystem_include | computed_include) >>
92 spirit_append_actor(self.tokens_seq)
94 >> * (~ch_p(T_GREATER))
96 spirit_append_actor(self.tokens_seq)
100 spirit_append_actor(self.tokens_seq)
102 assign_a(self.is_quoted_filename, self.true_)
104 assign_a(self.is_system, self.true_)
108 nonsystem_include = ch_p(T_STRINGLIT)
110 spirit_append_actor(self.tokens_seq)
112 assign_a(self.is_quoted_filename, self.true_)
116 // an action to store a sequence of parsed tokens
117 auto append_seq = [&](typename ScannerT::iterator_t first,
118 typename ScannerT::iterator_t last) {
119 for (;first != last;++first) {
120 self.tokens_seq.push_back(*first);
124 // if neither of the above match we take everything between
125 // the parentheses and evaluate it ("computed include")
126 // supported expressions are "implementation defined" per the gcc manual
127 // we've tried to be fairly generous in Wave
128 // here we accept any set of non-whitespace characters with
129 // properly nested parentheses:
130 nonparen = (anychar_p - ch_p(T_LEFTPAREN) - ch_p(T_RIGHTPAREN)) [ append_seq ] ;
133 ch_p(T_LEFTPAREN)[ spirit_append_actor(self.tokens_seq) ] >>
135 ch_p(T_RIGHTPAREN)[ spirit_append_actor(self.tokens_seq) ] ;
136 computed_include = * (nonparen | parenthesized_exp) ;
139 BOOST_SPIRIT_DEBUG_TRACE_RULE(has_include_op, TRACE_CPP_HAS_INCLUDE_GRAMMAR);
140 BOOST_SPIRIT_DEBUG_TRACE_RULE(system_include, TRACE_CPP_HAS_INCLUDE_GRAMMAR);
141 BOOST_SPIRIT_DEBUG_TRACE_RULE(nonsystem_include, TRACE_CPP_HAS_INCLUDE_GRAMMAR);
142 BOOST_SPIRIT_DEBUG_TRACE_RULE(computed_include, TRACE_CPP_HAS_INCLUDE_GRAMMAR);
143 BOOST_SPIRIT_DEBUG_TRACE_RULE(parenthesized_exp, TRACE_CPP_HAS_INCLUDE_GRAMMAR);
144 BOOST_SPIRIT_DEBUG_TRACE_RULE(nonparen, TRACE_CPP_HAS_INCLUDE_GRAMMAR);
147 // start rule of this grammar
148 rule_t const& start() const
149 { return has_include_op; }
152 ContainerT &tokens_seq;
153 bool &is_quoted_filename;
155 const bool true_; // Spirit Classic actors operate on references, not values
158 ///////////////////////////////////////////////////////////////////////////////
159 #undef TRACE_CPP_HAS_INCLUDE_GRAMMAR
161 ///////////////////////////////////////////////////////////////////////////////
163 // The following parse function is has_include here, to allow the separation of
164 // the compilation of the has_include_grammar from the function
167 ///////////////////////////////////////////////////////////////////////////////
169 #if BOOST_WAVE_SEPARATE_GRAMMAR_INSTANTIATION != 0
170 #define BOOST_WAVE_HAS_INCLUDE_GRAMMAR_GEN_INLINE
172 #define BOOST_WAVE_HAS_INCLUDE_GRAMMAR_GEN_INLINE inline
175 // The parse_operator_define function is instantiated manually twice to
176 // simplify the explicit specialization of this template. This way the user
177 // has only to specify one template parameter (the lexer type) to correctly
178 // formulate the required explicit specialization.
179 // This results in no code overhead, because otherwise the function would be
180 // generated by the compiler twice anyway.
182 template <typename LexIteratorT>
183 BOOST_WAVE_HAS_INCLUDE_GRAMMAR_GEN_INLINE
184 boost::spirit::classic::parse_info<
185 typename has_include_grammar_gen<LexIteratorT>::iterator1_type
187 has_include_grammar_gen<LexIteratorT>::parse_operator_has_include (
188 iterator1_type const &first, iterator1_type const &last,
189 token_sequence_type &tokens,
190 bool &is_quoted_filename, bool &is_system)
192 using namespace boost::spirit::classic;
193 using namespace boost::wave;
195 has_include_grammar<token_sequence_type>
196 g(tokens, is_quoted_filename, is_system);
197 return boost::spirit::classic::parse (
198 first, last, g, ch_p(T_SPACE) | ch_p(T_CCOMMENT));
201 template <typename LexIteratorT>
202 BOOST_WAVE_HAS_INCLUDE_GRAMMAR_GEN_INLINE
203 boost::spirit::classic::parse_info<
204 typename has_include_grammar_gen<LexIteratorT>::iterator2_type
206 has_include_grammar_gen<LexIteratorT>::parse_operator_has_include (
207 iterator2_type const &first, iterator2_type const &last,
208 token_sequence_type &found_qualified_name,
209 bool &is_quoted_filename, bool &is_system)
211 using namespace boost::spirit::classic;
212 using namespace boost::wave;
214 has_include_grammar<token_sequence_type>
215 g(found_qualified_name, is_quoted_filename, is_system);
216 return boost::spirit::classic::parse (
217 first, last, g, ch_p(T_SPACE) | ch_p(T_CCOMMENT));
220 #undef BOOST_WAVE_HAS_INCLUDE_GRAMMAR_GEN_INLINE
222 ///////////////////////////////////////////////////////////////////////////////
223 } // namespace grammars
227 // the suffix header occurs after all of the code
228 #ifdef BOOST_HAS_ABI_HEADERS
229 #include BOOST_ABI_SUFFIX
232 #endif // !defined(BOOST_CPP_HAS_INCLUDE_GRAMMAR_HPP_F48287B2_DC67_40A8_B4A1_800EFBD67869_INCLUDED)