1 /*=============================================================================
2 Boost.Wave: A Standard compliant C++ preprocessor library
3
4 http://www.boost.org/
5
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 =============================================================================*/
11
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
14
15 #include <boost/wave/wave_config.hpp>
16
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>
22
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>
26
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)
30
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
34 #endif
35
36 ///////////////////////////////////////////////////////////////////////////////
37 namespace boost {
38 namespace wave {
39 namespace grammars {
40
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) \
45 /**/
46
47 template <typename ContainerT>
48 struct has_include_grammar :
49 public boost::spirit::classic::grammar<has_include_grammar<ContainerT> >
50 {
has_include_grammarboost::wave::grammars::has_include_grammar51 has_include_grammar(ContainerT &tokens_seq_,
52 bool &is_quoted_filename_,
53 bool &is_system_)
54 : tokens_seq(tokens_seq_), is_quoted_filename(is_quoted_filename_),
55 is_system(is_system_), true_(true)
56 {
57 BOOST_SPIRIT_DEBUG_TRACE_GRAMMAR_NAME(*this, "has_include_grammar",
58 TRACE_CPP_HAS_INCLUDE_GRAMMAR);
59 is_quoted_filename = false;
60 is_system = false;
61 }
62
63 template <typename ScannerT>
64 struct definition
65 {
66 typedef boost::spirit::classic::rule<ScannerT> rule_t;
67
68 rule_t has_include_op;
69 rule_t system_include;
70 rule_t nonsystem_include;
71 rule_t computed_include;
72
definitionboost::wave::grammars::has_include_grammar::definition73 definition(has_include_grammar const & self)
74 {
75 using namespace boost::spirit::classic;
76 using namespace boost::wave;
77 using namespace boost::wave::util;
78
79 has_include_op
80 = ch_p(T_IDENTIFIER) // token contains '__has_include'
81 >> (
82 ch_p(T_LEFTPAREN) >>
83 (system_include | nonsystem_include | computed_include)
84 // final right paren removed by caller
85 )
86 >> end_p
87 ;
88
89 system_include
90 = ch_p(T_LESS)
91 [
92 spirit_append_actor(self.tokens_seq)
93 ]
94 >> * (~ch_p(T_GREATER))
95 [
96 spirit_append_actor(self.tokens_seq)
97 ]
98 >> ch_p(T_GREATER)
99 [
100 spirit_append_actor(self.tokens_seq)
101 ][
102 assign_a(self.is_quoted_filename, self.true_)
103 ][
104 assign_a(self.is_system, self.true_)
105 ]
106 ;
107
108 nonsystem_include = ch_p(T_STRINGLIT)
109 [
110 spirit_append_actor(self.tokens_seq)
111 ][
112 assign_a(self.is_quoted_filename, self.true_)
113 ]
114 ;
115
116 // if neither of the above match we take everything between
117 // the parentheses and evaluate it ("computed include")
118 computed_include = * anychar_p
119 [
120 spirit_append_actor(self.tokens_seq)
121 ]
122 ;
123
124
125 BOOST_SPIRIT_DEBUG_TRACE_RULE(has_include_op, TRACE_CPP_HAS_INCLUDE_GRAMMAR);
126 BOOST_SPIRIT_DEBUG_TRACE_RULE(system_include, TRACE_CPP_HAS_INCLUDE_GRAMMAR);
127 BOOST_SPIRIT_DEBUG_TRACE_RULE(nonsystem_include, TRACE_CPP_HAS_INCLUDE_GRAMMAR);
128 BOOST_SPIRIT_DEBUG_TRACE_RULE(computed_include, TRACE_CPP_HAS_INCLUDE_GRAMMAR);
129 }
130
131 // start rule of this grammar
startboost::wave::grammars::has_include_grammar::definition132 rule_t const& start() const
133 { return has_include_op; }
134 };
135
136 ContainerT &tokens_seq;
137 bool &is_quoted_filename;
138 bool &is_system;
139 const bool true_; // Spirit Classic actors operate on references, not values
140 };
141
142 ///////////////////////////////////////////////////////////////////////////////
143 #undef TRACE_CPP_HAS_INCLUDE_GRAMMAR
144
145 ///////////////////////////////////////////////////////////////////////////////
146 //
147 // The following parse function is has_include here, to allow the separation of
148 // the compilation of the has_include_grammar from the function
149 // using it.
150 //
151 ///////////////////////////////////////////////////////////////////////////////
152
153 #if BOOST_WAVE_SEPARATE_GRAMMAR_INSTANTIATION != 0
154 #define BOOST_WAVE_HAS_INCLUDE_GRAMMAR_GEN_INLINE
155 #else
156 #define BOOST_WAVE_HAS_INCLUDE_GRAMMAR_GEN_INLINE inline
157 #endif
158
159 // The parse_operator_define function is instantiated manually twice to
160 // simplify the explicit specialization of this template. This way the user
161 // has only to specify one template parameter (the lexer type) to correctly
162 // formulate the required explicit specialization.
163 // This results in no code overhead, because otherwise the function would be
164 // generated by the compiler twice anyway.
165
166 template <typename LexIteratorT>
167 BOOST_WAVE_HAS_INCLUDE_GRAMMAR_GEN_INLINE
168 boost::spirit::classic::parse_info<
169 typename has_include_grammar_gen<LexIteratorT>::iterator1_type
170 >
parse_operator_has_include(iterator1_type const & first,iterator1_type const & last,token_sequence_type & tokens,bool & is_quoted_filename,bool & is_system)171 has_include_grammar_gen<LexIteratorT>::parse_operator_has_include (
172 iterator1_type const &first, iterator1_type const &last,
173 token_sequence_type &tokens,
174 bool &is_quoted_filename, bool &is_system)
175 {
176 using namespace boost::spirit::classic;
177 using namespace boost::wave;
178
179 has_include_grammar<token_sequence_type>
180 g(tokens, is_quoted_filename, is_system);
181 return boost::spirit::classic::parse (
182 first, last, g, ch_p(T_SPACE) | ch_p(T_CCOMMENT));
183 }
184
185 template <typename LexIteratorT>
186 BOOST_WAVE_HAS_INCLUDE_GRAMMAR_GEN_INLINE
187 boost::spirit::classic::parse_info<
188 typename has_include_grammar_gen<LexIteratorT>::iterator2_type
189 >
parse_operator_has_include(iterator2_type const & first,iterator2_type const & last,token_sequence_type & found_qualified_name,bool & is_quoted_filename,bool & is_system)190 has_include_grammar_gen<LexIteratorT>::parse_operator_has_include (
191 iterator2_type const &first, iterator2_type const &last,
192 token_sequence_type &found_qualified_name,
193 bool &is_quoted_filename, bool &is_system)
194 {
195 using namespace boost::spirit::classic;
196 using namespace boost::wave;
197
198 has_include_grammar<token_sequence_type>
199 g(found_qualified_name, is_quoted_filename, is_system);
200 return boost::spirit::classic::parse (
201 first, last, g, ch_p(T_SPACE) | ch_p(T_CCOMMENT));
202 }
203
204 #undef BOOST_WAVE_HAS_INCLUDE_GRAMMAR_GEN_INLINE
205
206 ///////////////////////////////////////////////////////////////////////////////
207 } // namespace grammars
208 } // namespace wave
209 } // namespace boost
210
211 // the suffix header occurs after all of the code
212 #ifdef BOOST_HAS_ABI_HEADERS
213 #include BOOST_ABI_SUFFIX
214 #endif
215
216 #endif // !defined(BOOST_CPP_HAS_INCLUDE_GRAMMAR_HPP_F48287B2_DC67_40A8_B4A1_800EFBD67869_INCLUDED)
217