annotate DEPENDENCIES/generic/include/boost/wave/util/cpp_iterator.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents 2665513ce2d3
children
rev   line source
Chris@16 1 /*=============================================================================
Chris@16 2 Boost.Wave: A Standard compliant C++ preprocessor library
Chris@16 3
Chris@16 4 Definition of the preprocessor iterator
Chris@16 5
Chris@16 6 http://www.boost.org/
Chris@16 7
Chris@16 8 Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
Chris@16 9 Software License, Version 1.0. (See accompanying file
Chris@16 10 LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 11 =============================================================================*/
Chris@16 12
Chris@16 13 #if !defined(CPP_ITERATOR_HPP_175CA88F_7273_43FA_9039_BCF7459E1F29_INCLUDED)
Chris@16 14 #define CPP_ITERATOR_HPP_175CA88F_7273_43FA_9039_BCF7459E1F29_INCLUDED
Chris@16 15
Chris@16 16 #include <string>
Chris@16 17 #include <vector>
Chris@16 18 #include <list>
Chris@16 19 #include <cstdlib>
Chris@16 20 #include <cctype>
Chris@16 21
Chris@16 22 #include <boost/assert.hpp>
Chris@16 23 #include <boost/shared_ptr.hpp>
Chris@16 24 #include <boost/filesystem/path.hpp>
Chris@16 25 #include <boost/filesystem/operations.hpp>
Chris@16 26 #include <boost/spirit/include/classic_multi_pass.hpp>
Chris@16 27 #include <boost/spirit/include/classic_parse_tree_utils.hpp>
Chris@16 28
Chris@16 29 #include <boost/wave/wave_config.hpp>
Chris@16 30 #include <boost/pool/pool_alloc.hpp>
Chris@16 31
Chris@16 32 #include <boost/wave/util/insert_whitespace_detection.hpp>
Chris@16 33 #include <boost/wave/util/macro_helpers.hpp>
Chris@16 34 #include <boost/wave/util/cpp_macromap_utils.hpp>
Chris@16 35 #include <boost/wave/util/interpret_pragma.hpp>
Chris@16 36 #include <boost/wave/util/transform_iterator.hpp>
Chris@16 37 #include <boost/wave/util/functor_input.hpp>
Chris@16 38 #include <boost/wave/util/filesystem_compatibility.hpp>
Chris@16 39
Chris@16 40 #include <boost/wave/grammars/cpp_grammar_gen.hpp>
Chris@16 41 #include <boost/wave/grammars/cpp_expression_grammar_gen.hpp>
Chris@16 42 #if BOOST_WAVE_ENABLE_COMMANDLINE_MACROS != 0
Chris@16 43 #include <boost/wave/grammars/cpp_predef_macros_gen.hpp>
Chris@16 44 #endif
Chris@16 45
Chris@16 46 #include <boost/wave/whitespace_handling.hpp>
Chris@16 47 #include <boost/wave/cpp_iteration_context.hpp>
Chris@16 48 #include <boost/wave/cpp_exceptions.hpp>
Chris@16 49 #include <boost/wave/language_support.hpp>
Chris@16 50
Chris@16 51 // this must occur after all of the includes and before any code appears
Chris@16 52 #ifdef BOOST_HAS_ABI_HEADERS
Chris@16 53 #include BOOST_ABI_PREFIX
Chris@16 54 #endif
Chris@16 55
Chris@16 56 ///////////////////////////////////////////////////////////////////////////////
Chris@16 57 namespace boost {
Chris@16 58 namespace wave {
Chris@16 59 namespace util {
Chris@16 60
Chris@16 61 ///////////////////////////////////////////////////////////////////////////////
Chris@16 62 // retrieve the macro name from the parse tree
Chris@16 63 template <
Chris@16 64 typename ContextT, typename ParseNodeT, typename TokenT,
Chris@16 65 typename PositionT
Chris@16 66 >
Chris@16 67 inline bool
Chris@16 68 retrieve_macroname(ContextT& ctx, ParseNodeT const &node,
Chris@16 69 boost::spirit::classic::parser_id id, TokenT &macroname, PositionT& act_pos,
Chris@16 70 bool update_position)
Chris@16 71 {
Chris@16 72 ParseNodeT const *name_node = 0;
Chris@16 73
Chris@16 74 using boost::spirit::classic::find_node;
Chris@16 75 if (!find_node(node, id, &name_node))
Chris@16 76 {
Chris@16 77 // ill formed define statement (unexpected, should not happen)
Chris@16 78 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, bad_define_statement,
Chris@16 79 "bad parse tree (unexpected)", act_pos);
Chris@16 80 return false;
Chris@16 81 }
Chris@16 82
Chris@16 83 typename ParseNodeT::children_t const &children = name_node->children;
Chris@16 84
Chris@16 85 if (0 == children.size() ||
Chris@16 86 children.front().value.begin() == children.front().value.end())
Chris@16 87 {
Chris@16 88 // ill formed define statement (unexpected, should not happen)
Chris@16 89 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, bad_define_statement,
Chris@16 90 "bad parse tree (unexpected)", act_pos);
Chris@16 91 return false;
Chris@16 92 }
Chris@16 93
Chris@16 94 // retrieve the macro name
Chris@16 95 macroname = *children.front().value.begin();
Chris@16 96 if (update_position) {
Chris@16 97 macroname.set_position(act_pos);
Chris@16 98 act_pos.set_column(act_pos.get_column() + macroname.get_value().size());
Chris@16 99 }
Chris@16 100 return true;
Chris@16 101 }
Chris@16 102
Chris@16 103 ///////////////////////////////////////////////////////////////////////////////
Chris@16 104 // retrieve the macro parameters or the macro definition from the parse tree
Chris@16 105 template <typename ParseNodeT, typename ContainerT, typename PositionT>
Chris@16 106 inline bool
Chris@16 107 retrieve_macrodefinition(
Chris@16 108 ParseNodeT const &node, boost::spirit::classic::parser_id id,
Chris@16 109 ContainerT &macrodefinition, PositionT& act_pos, bool update_position)
Chris@16 110 {
Chris@16 111 using namespace boost::wave;
Chris@16 112 typedef typename ParseNodeT::const_tree_iterator const_tree_iterator;
Chris@16 113
Chris@16 114 // find macro parameters/macro definition inside the parse tree
Chris@16 115 std::pair<const_tree_iterator, const_tree_iterator> nodes;
Chris@16 116
Chris@16 117 using boost::spirit::classic::get_node_range;
Chris@16 118 if (get_node_range(node, id, nodes)) {
Chris@16 119 // copy all parameters to the supplied container
Chris@16 120 typename ContainerT::iterator last_nonwhite = macrodefinition.end();
Chris@16 121 const_tree_iterator end = nodes.second;
Chris@16 122
Chris@16 123 for (const_tree_iterator cit = nodes.first; cit != end; ++cit) {
Chris@16 124 if ((*cit).value.begin() != (*cit).value.end()) {
Chris@16 125 typename ContainerT::iterator inserted = macrodefinition.insert(
Chris@16 126 macrodefinition.end(), *(*cit).value.begin());
Chris@16 127
Chris@16 128 if (!IS_CATEGORY(macrodefinition.back(), WhiteSpaceTokenType) &&
Chris@16 129 T_NEWLINE != token_id(macrodefinition.back()) &&
Chris@16 130 T_EOF != token_id(macrodefinition.back()))
Chris@16 131 {
Chris@16 132 last_nonwhite = inserted;
Chris@16 133 }
Chris@16 134
Chris@16 135 if (update_position) {
Chris@16 136 (*inserted).set_position(act_pos);
Chris@16 137 act_pos.set_column(
Chris@16 138 act_pos.get_column() + (*inserted).get_value().size());
Chris@16 139 }
Chris@16 140 }
Chris@16 141 }
Chris@16 142
Chris@16 143 // trim trailing whitespace (leading whitespace is trimmed by the grammar)
Chris@16 144 if (last_nonwhite != macrodefinition.end()) {
Chris@16 145 if (update_position) {
Chris@16 146 act_pos.set_column((*last_nonwhite).get_position().get_column() +
Chris@16 147 (*last_nonwhite).get_value().size());
Chris@16 148 }
Chris@16 149 macrodefinition.erase(++last_nonwhite, macrodefinition.end());
Chris@16 150 }
Chris@16 151 return true;
Chris@16 152 }
Chris@16 153 return false;
Chris@16 154 }
Chris@16 155
Chris@16 156 #if BOOST_WAVE_ENABLE_COMMANDLINE_MACROS != 0
Chris@16 157 ///////////////////////////////////////////////////////////////////////////////
Chris@16 158 // add an additional predefined macro given by a string (MACRO(x)=definition)
Chris@16 159 template <typename ContextT>
Chris@16 160 bool add_macro_definition(ContextT &ctx, std::string macrostring,
Chris@16 161 bool is_predefined, boost::wave::language_support language)
Chris@16 162 {
Chris@16 163 typedef typename ContextT::token_type token_type;
Chris@16 164 typedef typename ContextT::lexer_type lexer_type;
Chris@16 165 typedef typename token_type::position_type position_type;
Chris@16 166 typedef boost::wave::grammars::predefined_macros_grammar_gen<lexer_type>
Chris@16 167 predef_macros_type;
Chris@16 168
Chris@16 169 using namespace boost::wave;
Chris@16 170 using namespace std; // isspace is in std namespace for some systems
Chris@16 171
Chris@16 172 // skip leading whitespace
Chris@16 173 std::string::iterator begin = macrostring.begin();
Chris@16 174 std::string::iterator end = macrostring.end();
Chris@16 175
Chris@16 176 while(begin != end && isspace(*begin))
Chris@16 177 ++begin;
Chris@16 178
Chris@16 179 // parse the macro definition
Chris@16 180 position_type act_pos("<command line>");
Chris@16 181 boost::spirit::classic::tree_parse_info<lexer_type> hit =
Chris@16 182 predef_macros_type::parse_predefined_macro(
Chris@16 183 lexer_type(begin, end, position_type(), language), lexer_type());
Chris@16 184
Chris@16 185 if (!hit.match || (!hit.full && T_EOF != token_id(*hit.stop))) {
Chris@16 186 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, bad_macro_definition,
Chris@16 187 macrostring.c_str(), act_pos);
Chris@16 188 return false;
Chris@16 189 }
Chris@16 190
Chris@16 191 // retrieve the macro definition from the parse tree
Chris@16 192 token_type macroname;
Chris@16 193 std::vector<token_type> macroparameters;
Chris@16 194 typename ContextT::token_sequence_type macrodefinition;
Chris@16 195 bool has_parameters = false;
Chris@16 196
Chris@16 197 if (!boost::wave::util::retrieve_macroname(ctx, *hit.trees.begin(),
Chris@16 198 BOOST_WAVE_PLAIN_DEFINE_ID, macroname, act_pos, true))
Chris@16 199 return false;
Chris@16 200 has_parameters = boost::wave::util::retrieve_macrodefinition(*hit.trees.begin(),
Chris@16 201 BOOST_WAVE_MACRO_PARAMETERS_ID, macroparameters, act_pos, true);
Chris@16 202 boost::wave::util::retrieve_macrodefinition(*hit.trees.begin(),
Chris@16 203 BOOST_WAVE_MACRO_DEFINITION_ID, macrodefinition, act_pos, true);
Chris@16 204
Chris@16 205 // get rid of trailing T_EOF
Chris@16 206 if (!macrodefinition.empty() && token_id(macrodefinition.back()) == T_EOF)
Chris@16 207 macrodefinition.pop_back();
Chris@16 208
Chris@16 209 // If no macrodefinition is given, and the macro string does not end with a
Chris@16 210 // '=', then the macro should be defined with the value '1'
Chris@16 211 if (macrodefinition.empty() && '=' != macrostring[macrostring.size()-1])
Chris@16 212 macrodefinition.push_back(token_type(T_INTLIT, "1", act_pos));
Chris@16 213
Chris@16 214 // add the new macro to the macromap
Chris@16 215 return ctx.add_macro_definition(macroname, has_parameters, macroparameters,
Chris@16 216 macrodefinition, is_predefined);
Chris@16 217 }
Chris@16 218 #endif // BOOST_WAVE_ENABLE_COMMANDLINE_MACROS != 0
Chris@16 219
Chris@16 220 ///////////////////////////////////////////////////////////////////////////////
Chris@16 221 } // namespace util
Chris@16 222
Chris@16 223 ///////////////////////////////////////////////////////////////////////////////
Chris@16 224 // forward declaration
Chris@16 225 template <typename ContextT> class pp_iterator;
Chris@16 226
Chris@16 227 namespace impl {
Chris@16 228
Chris@16 229 ///////////////////////////////////////////////////////////////////////////////
Chris@16 230 //
Chris@16 231 // pp_iterator_functor
Chris@16 232 //
Chris@16 233 ///////////////////////////////////////////////////////////////////////////////
Chris@16 234 template <typename ContextT>
Chris@16 235 class pp_iterator_functor {
Chris@16 236
Chris@16 237 public:
Chris@16 238 // interface to the boost::spirit::classic::iterator_policies::functor_input policy
Chris@16 239 typedef typename ContextT::token_type result_type;
Chris@16 240
Chris@16 241 // eof token
Chris@16 242 static result_type const eof;
Chris@16 243
Chris@16 244 private:
Chris@16 245 // type of a token sequence
Chris@16 246 typedef typename ContextT::token_sequence_type token_sequence_type;
Chris@16 247
Chris@16 248 typedef typename ContextT::lexer_type lexer_type;
Chris@16 249 typedef typename result_type::string_type string_type;
Chris@16 250 typedef typename result_type::position_type position_type;
Chris@16 251 typedef boost::wave::grammars::cpp_grammar_gen<lexer_type, token_sequence_type>
Chris@16 252 cpp_grammar_type;
Chris@16 253
Chris@16 254 // iteration context related types (an iteration context represents a current
Chris@16 255 // position in an included file)
Chris@16 256 typedef base_iteration_context<ContextT, lexer_type>
Chris@16 257 base_iteration_context_type;
Chris@16 258 typedef iteration_context<ContextT, lexer_type> iteration_context_type;
Chris@16 259
Chris@16 260 // parse tree related types
Chris@16 261 typedef typename cpp_grammar_type::node_factory_type node_factory_type;
Chris@16 262 typedef boost::spirit::classic::tree_parse_info<lexer_type, node_factory_type>
Chris@16 263 tree_parse_info_type;
Chris@16 264 typedef boost::spirit::classic::tree_match<lexer_type, node_factory_type>
Chris@16 265 parse_tree_match_type;
Chris@16 266 typedef typename parse_tree_match_type::node_t parse_node_type; // tree_node<node_val_data<> >
Chris@16 267 typedef typename parse_tree_match_type::parse_node_t parse_node_value_type; // node_val_data<>
Chris@16 268 typedef typename parse_tree_match_type::container_t parse_tree_type; // parse_node_type::children_t
Chris@16 269
Chris@16 270 public:
Chris@16 271 template <typename IteratorT>
Chris@16 272 pp_iterator_functor(ContextT &ctx_, IteratorT const &first_,
Chris@16 273 IteratorT const &last_, typename ContextT::position_type const &pos_)
Chris@16 274 : ctx(ctx_),
Chris@16 275 iter_ctx(new base_iteration_context_type(ctx,
Chris@16 276 lexer_type(first_, last_, pos_,
Chris@16 277 boost::wave::enable_prefer_pp_numbers(ctx.get_language())),
Chris@16 278 lexer_type(),
Chris@16 279 pos_.get_file().c_str()
Chris@16 280 )),
Chris@16 281 seen_newline(true), skipped_newline(false),
Chris@16 282 must_emit_line_directive(false), act_pos(ctx_.get_main_pos()),
Chris@16 283 whitespace(boost::wave::need_insert_whitespace(ctx.get_language()))
Chris@16 284 {
Chris@16 285 act_pos.set_file(pos_.get_file());
Chris@16 286 #if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0
Chris@16 287 ctx_.set_current_filename(pos_.get_file().c_str());
Chris@16 288 #endif
Chris@16 289 iter_ctx->emitted_lines = (unsigned int)(-1); // force #line directive
Chris@16 290 }
Chris@16 291
Chris@16 292 // get the next preprocessed token
Chris@16 293 result_type const &operator()();
Chris@16 294
Chris@16 295 // get the last recognized token (for error processing etc.)
Chris@16 296 result_type const &current_token() const { return act_token; }
Chris@16 297
Chris@16 298 protected:
Chris@16 299 friend class pp_iterator<ContextT>;
Chris@16 300 bool on_include_helper(char const *t, char const *s, bool is_system,
Chris@16 301 bool include_next);
Chris@16 302
Chris@16 303 protected:
Chris@16 304 result_type const &get_next_token();
Chris@16 305 result_type const &pp_token();
Chris@16 306
Chris@16 307 template <typename IteratorT>
Chris@16 308 bool extract_identifier(IteratorT &it);
Chris@16 309 template <typename IteratorT>
Chris@16 310 bool ensure_is_last_on_line(IteratorT& it, bool call_hook = true);
Chris@16 311 template <typename IteratorT>
Chris@16 312 bool skip_to_eol_with_check(IteratorT &it, bool call_hook = true);
Chris@16 313
Chris@16 314 bool pp_directive();
Chris@16 315 template <typename IteratorT>
Chris@16 316 bool handle_pp_directive(IteratorT &it);
Chris@16 317 bool dispatch_directive(tree_parse_info_type const &hit,
Chris@16 318 result_type const& found_directive,
Chris@16 319 token_sequence_type const& found_eoltokens);
Chris@16 320 void replace_undefined_identifiers(token_sequence_type &expanded);
Chris@16 321
Chris@16 322 void on_include(string_type const &s, bool is_system, bool include_next);
Chris@16 323 void on_include(typename parse_tree_type::const_iterator const &begin,
Chris@16 324 typename parse_tree_type::const_iterator const &end, bool include_next);
Chris@16 325
Chris@16 326 void on_define(parse_node_type const &node);
Chris@16 327 void on_undefine(lexer_type const &it);
Chris@16 328
Chris@16 329 void on_ifdef(result_type const& found_directive, lexer_type const &it);
Chris@16 330 // typename parse_tree_type::const_iterator const &end);
Chris@16 331 void on_ifndef(result_type const& found_directive, lexer_type const& it);
Chris@16 332 // typename parse_tree_type::const_iterator const &end);
Chris@16 333 void on_else();
Chris@16 334 void on_endif();
Chris@16 335 void on_illformed(typename result_type::string_type s);
Chris@16 336
Chris@16 337 void on_line(typename parse_tree_type::const_iterator const &begin,
Chris@16 338 typename parse_tree_type::const_iterator const &end);
Chris@16 339 void on_if(result_type const& found_directive,
Chris@16 340 typename parse_tree_type::const_iterator const &begin,
Chris@16 341 typename parse_tree_type::const_iterator const &end);
Chris@16 342 void on_elif(result_type const& found_directive,
Chris@16 343 typename parse_tree_type::const_iterator const &begin,
Chris@16 344 typename parse_tree_type::const_iterator const &end);
Chris@16 345 void on_error(typename parse_tree_type::const_iterator const &begin,
Chris@16 346 typename parse_tree_type::const_iterator const &end);
Chris@16 347 #if BOOST_WAVE_SUPPORT_WARNING_DIRECTIVE != 0
Chris@16 348 void on_warning(typename parse_tree_type::const_iterator const &begin,
Chris@16 349 typename parse_tree_type::const_iterator const &end);
Chris@16 350 #endif
Chris@16 351 bool on_pragma(typename parse_tree_type::const_iterator const &begin,
Chris@16 352 typename parse_tree_type::const_iterator const &end);
Chris@16 353
Chris@16 354 bool emit_line_directive();
Chris@16 355 bool returned_from_include();
Chris@16 356
Chris@16 357 bool interpret_pragma(token_sequence_type const &pragma_body,
Chris@16 358 token_sequence_type &result);
Chris@16 359
Chris@16 360 private:
Chris@16 361 ContextT &ctx; // context, this iterator is associated with
Chris@16 362 boost::shared_ptr<base_iteration_context_type> iter_ctx;
Chris@16 363
Chris@16 364 bool seen_newline; // needed for recognizing begin of line
Chris@16 365 bool skipped_newline; // a newline has been skipped since last one
Chris@16 366 bool must_emit_line_directive; // must emit a line directive
Chris@16 367 result_type act_token; // current token
Chris@16 368 typename result_type::position_type &act_pos; // current fileposition (references the macromap)
Chris@16 369
Chris@16 370 token_sequence_type unput_queue; // tokens to be preprocessed again
Chris@16 371 token_sequence_type pending_queue; // tokens already preprocessed
Chris@16 372
Chris@16 373 // detect whether to insert additional whitespace in between two adjacent
Chris@16 374 // tokens, which otherwise would form a different token type, if
Chris@16 375 // re-tokenized
Chris@16 376 boost::wave::util::insert_whitespace_detection whitespace;
Chris@16 377 };
Chris@16 378
Chris@16 379 ///////////////////////////////////////////////////////////////////////////////
Chris@16 380 // eof token
Chris@16 381 template <typename ContextT>
Chris@16 382 typename pp_iterator_functor<ContextT>::result_type const
Chris@16 383 pp_iterator_functor<ContextT>::eof;
Chris@16 384
Chris@16 385 ///////////////////////////////////////////////////////////////////////////////
Chris@16 386 //
Chris@16 387 // returned_from_include()
Chris@16 388 //
Chris@16 389 // Tests if it is necessary to pop the include file context (eof inside
Chris@16 390 // a file was reached). If yes, it pops this context. Preprocessing will
Chris@16 391 // continue with the next outer file scope.
Chris@16 392 //
Chris@16 393 ///////////////////////////////////////////////////////////////////////////////
Chris@16 394 template <typename ContextT>
Chris@16 395 inline bool
Chris@16 396 pp_iterator_functor<ContextT>::returned_from_include()
Chris@16 397 {
Chris@16 398 if (iter_ctx->first == iter_ctx->last && ctx.get_iteration_depth() > 0) {
Chris@16 399 // call the include policy trace function
Chris@16 400 #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
Chris@16 401 ctx.get_hooks().returning_from_include_file();
Chris@16 402 #else
Chris@16 403 ctx.get_hooks().returning_from_include_file(ctx.derived());
Chris@16 404 #endif
Chris@16 405
Chris@16 406 // restore the previous iteration context after finishing the preprocessing
Chris@16 407 // of the included file
Chris@16 408 BOOST_WAVE_STRINGTYPE oldfile = iter_ctx->real_filename;
Chris@16 409 position_type old_pos (act_pos);
Chris@16 410
Chris@16 411 // if this file has include guards handle it as if it had a #pragma once
Chris@16 412 #if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0
Chris@16 413 if (need_include_guard_detection(ctx.get_language())) {
Chris@16 414 std::string guard_name;
Chris@16 415 if (iter_ctx->first.has_include_guards(guard_name))
Chris@16 416 ctx.add_pragma_once_header(ctx.get_current_filename(), guard_name);
Chris@16 417 }
Chris@16 418 #endif
Chris@16 419 iter_ctx = ctx.pop_iteration_context();
Chris@16 420
Chris@16 421 must_emit_line_directive = true;
Chris@16 422 iter_ctx->emitted_lines = (unsigned int)(-1); // force #line directive
Chris@16 423 seen_newline = true;
Chris@16 424
Chris@16 425 // restore current file position
Chris@16 426 act_pos.set_file(iter_ctx->filename);
Chris@16 427 act_pos.set_line(iter_ctx->line);
Chris@16 428 act_pos.set_column(0);
Chris@16 429
Chris@16 430 // restore the actual current file and directory
Chris@16 431 #if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0
Chris@16 432 namespace fs = boost::filesystem;
Chris@16 433 fs::path rfp(wave::util::create_path(iter_ctx->real_filename.c_str()));
Chris@16 434 std::string real_filename(rfp.string());
Chris@16 435 ctx.set_current_filename(real_filename.c_str());
Chris@16 436 #endif
Chris@16 437 ctx.set_current_directory(iter_ctx->real_filename.c_str());
Chris@16 438 ctx.set_current_relative_filename(iter_ctx->real_relative_filename.c_str());
Chris@16 439
Chris@16 440 // ensure the integrity of the #if/#endif stack
Chris@16 441 // report unbalanced #if/#endif now to make it possible to recover properly
Chris@16 442 if (iter_ctx->if_block_depth != ctx.get_if_block_depth()) {
Chris@16 443 using boost::wave::util::impl::escape_lit;
Chris@16 444 BOOST_WAVE_STRINGTYPE msg(escape_lit(oldfile));
Chris@16 445 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, unbalanced_if_endif,
Chris@16 446 msg.c_str(), old_pos);
Chris@16 447 }
Chris@16 448 return true;
Chris@16 449 }
Chris@16 450 return false;
Chris@16 451 }
Chris@16 452
Chris@16 453 ///////////////////////////////////////////////////////////////////////////////
Chris@16 454 //
Chris@16 455 // operator()(): get the next preprocessed token
Chris@16 456 //
Chris@16 457 // throws a preprocess_exception, if appropriate
Chris@16 458 //
Chris@16 459 ///////////////////////////////////////////////////////////////////////////////
Chris@16 460 namespace impl {
Chris@16 461
Chris@16 462 // It may be necessary to emit a #line directive either
Chris@16 463 // - when comments need to be preserved: if the current token is not a
Chris@16 464 // whitespace, except comments
Chris@16 465 // - when comments are to be skipped: if the current token is not a
Chris@16 466 // whitespace token.
Chris@16 467 template <typename ContextT>
Chris@16 468 bool consider_emitting_line_directive(ContextT const& ctx, token_id id)
Chris@16 469 {
Chris@16 470 if (need_preserve_comments(ctx.get_language()))
Chris@16 471 {
Chris@16 472 if (!IS_CATEGORY(id, EOLTokenType) && !IS_CATEGORY(id, EOFTokenType))
Chris@16 473 {
Chris@16 474 return true;
Chris@16 475 }
Chris@16 476 }
Chris@16 477 if (!IS_CATEGORY(id, WhiteSpaceTokenType) &&
Chris@16 478 !IS_CATEGORY(id, EOLTokenType) && !IS_CATEGORY(id, EOFTokenType))
Chris@16 479 {
Chris@16 480 return true;
Chris@16 481 }
Chris@16 482 return false;
Chris@16 483 }
Chris@16 484 }
Chris@16 485
Chris@16 486 template <typename ContextT>
Chris@16 487 inline typename pp_iterator_functor<ContextT>::result_type const &
Chris@16 488 pp_iterator_functor<ContextT>::operator()()
Chris@16 489 {
Chris@16 490 using namespace boost::wave;
Chris@16 491
Chris@16 492 // make sure the cwd has been initialized
Chris@16 493 ctx.init_context();
Chris@16 494
Chris@16 495 // loop over skip able whitespace until something significant is found
Chris@16 496 bool was_seen_newline = seen_newline;
Chris@16 497 bool was_skipped_newline = skipped_newline;
Chris@16 498 token_id id = T_UNKNOWN;
Chris@16 499
Chris@16 500 try { // catch lexer exceptions
Chris@16 501 do {
Chris@16 502 if (skipped_newline) {
Chris@16 503 was_skipped_newline = true;
Chris@16 504 skipped_newline = false;
Chris@16 505 }
Chris@16 506
Chris@16 507 // get_next_token assigns result to act_token member
Chris@16 508 get_next_token();
Chris@16 509
Chris@16 510 // if comments shouldn't be preserved replace them with newlines
Chris@16 511 id = token_id(act_token);
Chris@16 512 if (!need_preserve_comments(ctx.get_language()) &&
Chris@16 513 (T_CPPCOMMENT == id || context_policies::util::ccomment_has_newline(act_token)))
Chris@16 514 {
Chris@16 515 act_token.set_token_id(id = T_NEWLINE);
Chris@16 516 act_token.set_value("\n");
Chris@16 517 }
Chris@16 518
Chris@16 519 if (IS_CATEGORY(id, EOLTokenType))
Chris@16 520 seen_newline = true;
Chris@16 521
Chris@16 522 } while (ctx.get_hooks().may_skip_whitespace(ctx.derived(), act_token, skipped_newline));
Chris@16 523 }
Chris@16 524 catch (boost::wave::cpplexer::lexing_exception const& e) {
Chris@16 525 // dispatch any lexer exceptions to the context hook function
Chris@16 526 ctx.get_hooks().throw_exception(ctx.derived(), e);
Chris@16 527 return act_token;
Chris@16 528 }
Chris@16 529
Chris@16 530 // restore the accumulated skipped_newline state for next invocation
Chris@16 531 if (was_skipped_newline)
Chris@16 532 skipped_newline = true;
Chris@16 533
Chris@16 534 // if there were skipped any newlines, we must emit a #line directive
Chris@16 535 if ((must_emit_line_directive || (was_seen_newline && skipped_newline)) &&
Chris@16 536 impl::consider_emitting_line_directive(ctx, id))
Chris@16 537 {
Chris@16 538 // must emit a #line directive
Chris@16 539 if (need_emit_line_directives(ctx.get_language()) && emit_line_directive())
Chris@16 540 {
Chris@16 541 skipped_newline = false;
Chris@16 542 ctx.get_hooks().may_skip_whitespace(ctx.derived(), act_token, skipped_newline); // feed ws eater FSM
Chris@16 543 id = token_id(act_token);
Chris@16 544 }
Chris@16 545 }
Chris@16 546
Chris@16 547 // cleanup of certain tokens required
Chris@16 548 seen_newline = false;
Chris@16 549 switch (static_cast<unsigned int>(id)) {
Chris@16 550 case T_NONREPLACABLE_IDENTIFIER:
Chris@16 551 act_token.set_token_id(id = T_IDENTIFIER);
Chris@16 552 break;
Chris@16 553
Chris@16 554 case T_GENERATEDNEWLINE: // was generated by emit_line_directive()
Chris@16 555 act_token.set_token_id(id = T_NEWLINE);
Chris@16 556 ++iter_ctx->emitted_lines;
Chris@16 557 seen_newline = true;
Chris@16 558 break;
Chris@16 559
Chris@16 560 case T_NEWLINE:
Chris@16 561 case T_CPPCOMMENT:
Chris@16 562 seen_newline = true;
Chris@16 563 ++iter_ctx->emitted_lines;
Chris@16 564 break;
Chris@16 565
Chris@16 566 #if BOOST_WAVE_SUPPORT_CPP0X != 0
Chris@16 567 case T_RAWSTRINGLIT:
Chris@16 568 iter_ctx->emitted_lines +=
Chris@16 569 context_policies::util::rawstring_count_newlines(act_token);
Chris@16 570 break;
Chris@16 571 #endif
Chris@16 572
Chris@16 573 case T_CCOMMENT: // will come here only if whitespace is preserved
Chris@16 574 iter_ctx->emitted_lines +=
Chris@16 575 context_policies::util::ccomment_count_newlines(act_token);
Chris@16 576 break;
Chris@16 577
Chris@16 578 case T_PP_NUMBER: // re-tokenize the pp-number
Chris@16 579 {
Chris@16 580 token_sequence_type rescanned;
Chris@16 581
Chris@16 582 std::string pp_number(
Chris@16 583 util::to_string<std::string>(act_token.get_value()));
Chris@16 584
Chris@16 585 lexer_type it = lexer_type(pp_number.begin(),
Chris@16 586 pp_number.end(), act_token.get_position(),
Chris@16 587 ctx.get_language());
Chris@16 588 lexer_type end = lexer_type();
Chris@16 589
Chris@16 590 for (/**/; it != end && T_EOF != token_id(*it); ++it)
Chris@16 591 rescanned.push_back(*it);
Chris@16 592
Chris@16 593 pending_queue.splice(pending_queue.begin(), rescanned);
Chris@16 594 act_token = pending_queue.front();
Chris@16 595 id = token_id(act_token);
Chris@16 596 pending_queue.pop_front();
Chris@16 597 }
Chris@16 598 break;
Chris@16 599
Chris@16 600 case T_EOF:
Chris@16 601 seen_newline = true;
Chris@16 602 break;
Chris@16 603
Chris@16 604 default: // make sure whitespace at line begin keeps seen_newline status
Chris@16 605 if (IS_CATEGORY(id, WhiteSpaceTokenType))
Chris@16 606 seen_newline = was_seen_newline;
Chris@16 607 break;
Chris@16 608 }
Chris@16 609
Chris@16 610 if (whitespace.must_insert(id, act_token.get_value())) {
Chris@16 611 // must insert some whitespace into the output stream to avoid adjacent
Chris@16 612 // tokens, which would form different (and wrong) tokens
Chris@16 613 whitespace.shift_tokens(T_SPACE);
Chris@16 614 pending_queue.push_front(act_token); // push this token back
Chris@16 615 return act_token = result_type(T_SPACE,
Chris@16 616 typename result_type::string_type(" "),
Chris@16 617 act_token.get_position());
Chris@16 618 }
Chris@16 619 whitespace.shift_tokens(id);
Chris@16 620 return ctx.get_hooks().generated_token(ctx.derived(), act_token);
Chris@16 621 }
Chris@16 622
Chris@16 623 ///////////////////////////////////////////////////////////////////////////////
Chris@16 624 template <typename ContextT>
Chris@16 625 inline typename pp_iterator_functor<ContextT>::result_type const &
Chris@16 626 pp_iterator_functor<ContextT>::get_next_token()
Chris@16 627 {
Chris@16 628 using namespace boost::wave;
Chris@16 629
Chris@16 630 // if there is something in the unput_queue, then return the next token from
Chris@16 631 // there (all tokens in the queue are preprocessed already)
Chris@16 632 if (!pending_queue.empty() || !unput_queue.empty())
Chris@16 633 return pp_token(); // return next token
Chris@16 634
Chris@16 635 // test for EOF, if there is a pending input context, pop it back and continue
Chris@16 636 // parsing with it
Chris@16 637 bool returned_from_include_file = returned_from_include();
Chris@16 638
Chris@16 639 // try to generate the next token
Chris@16 640 if (iter_ctx->first != iter_ctx->last) {
Chris@16 641 do {
Chris@16 642 // If there are pending tokens in the queue, we'll have to return
Chris@16 643 // these. This may happen from a #pragma directive, which got replaced
Chris@16 644 // by some token sequence.
Chris@16 645 if (!pending_queue.empty()) {
Chris@16 646 util::on_exit::pop_front<token_sequence_type>
Chris@16 647 pop_front_token(pending_queue);
Chris@16 648
Chris@16 649 return act_token = pending_queue.front();
Chris@16 650 }
Chris@16 651
Chris@16 652 // adjust the current position (line and column)
Chris@16 653 bool was_seen_newline = seen_newline || returned_from_include_file;
Chris@16 654
Chris@16 655 // fetch the current token
Chris@16 656 act_token = *iter_ctx->first;
Chris@16 657 act_pos = act_token.get_position();
Chris@16 658
Chris@16 659 // act accordingly on the current token
Chris@16 660 token_id id = token_id(act_token);
Chris@16 661
Chris@16 662 if (T_EOF == id) {
Chris@16 663 // returned from an include file, continue with the next token
Chris@16 664 whitespace.shift_tokens(T_EOF);
Chris@16 665 ++iter_ctx->first;
Chris@16 666
Chris@16 667 // now make sure this line has a newline
Chris@16 668 if ((!seen_newline || act_pos.get_column() > 1) &&
Chris@16 669 !need_single_line(ctx.get_language()))
Chris@16 670 {
Chris@16 671 // warn, if this file does not end with a newline
Chris@16 672 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
Chris@16 673 last_line_not_terminated, "", act_pos);
Chris@16 674 }
Chris@16 675 continue; // if this is the main file, the while loop breaks
Chris@16 676 }
Chris@16 677 else if (T_NEWLINE == id || T_CPPCOMMENT == id) {
Chris@16 678 // a newline is to be returned ASAP, a C++ comment too
Chris@16 679 // (the C++ comment token includes the trailing newline)
Chris@16 680 seen_newline = true;
Chris@16 681 ++iter_ctx->first;
Chris@16 682
Chris@16 683 if (!ctx.get_if_block_status()) {
Chris@16 684 // skip this token because of the disabled #if block
Chris@16 685 whitespace.shift_tokens(id); // whitespace controller
Chris@16 686 util::impl::call_skipped_token_hook(ctx, act_token);
Chris@16 687 continue;
Chris@16 688 }
Chris@16 689 return act_token;
Chris@16 690 }
Chris@16 691 seen_newline = false;
Chris@16 692
Chris@16 693 if (was_seen_newline && pp_directive()) {
Chris@16 694 // a pp directive was found
Chris@16 695 // pending_queue.push_back(result_type(T_NEWLINE, "\n", act_pos));
Chris@16 696 // seen_newline = true;
Chris@16 697 // must_emit_line_directive = true;
Chris@16 698
Chris@16 699 // loop to the next token to analyze
Chris@16 700 // simply fall through, since the iterator was already adjusted
Chris@16 701 // correctly
Chris@16 702 }
Chris@16 703 else if (ctx.get_if_block_status()) {
Chris@16 704 // preprocess this token, eat up more, if appropriate, return
Chris@16 705 // the next preprocessed token
Chris@16 706 return pp_token();
Chris@16 707 }
Chris@16 708 else {
Chris@16 709 // compilation condition is false: if the current token is a
Chris@16 710 // newline, account for it, otherwise discard the actual token and
Chris@16 711 // try the next one
Chris@16 712 if (T_NEWLINE == token_id(act_token)) {
Chris@16 713 seen_newline = true;
Chris@16 714 must_emit_line_directive = true;
Chris@16 715 }
Chris@16 716
Chris@16 717 // next token
Chris@16 718 util::impl::call_skipped_token_hook(ctx, act_token);
Chris@16 719 ++iter_ctx->first;
Chris@16 720 }
Chris@16 721
Chris@16 722 } while ((iter_ctx->first != iter_ctx->last) ||
Chris@16 723 (returned_from_include_file = returned_from_include()));
Chris@16 724
Chris@16 725 // overall eof reached
Chris@16 726 if (ctx.get_if_block_depth() > 0 && !need_single_line(ctx.get_language()))
Chris@16 727 {
Chris@16 728 // missing endif directive(s)
Chris@16 729 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
Chris@16 730 missing_matching_endif, "", act_pos);
Chris@16 731 }
Chris@16 732 }
Chris@16 733 else {
Chris@16 734 act_token = eof; // this is the last token
Chris@16 735 }
Chris@16 736
Chris@16 737 // whitespace.shift_tokens(T_EOF); // whitespace controller
Chris@16 738 return act_token; // return eof token
Chris@16 739 }
Chris@16 740
Chris@16 741 ///////////////////////////////////////////////////////////////////////////////
Chris@16 742 //
Chris@16 743 // emit_line_directive(): emits a line directive from the act_token data
Chris@16 744 //
Chris@16 745 ///////////////////////////////////////////////////////////////////////////////
Chris@16 746 template <typename ContextT>
Chris@16 747 inline bool
Chris@16 748 pp_iterator_functor<ContextT>::emit_line_directive()
Chris@16 749 {
Chris@16 750 using namespace boost::wave;
Chris@16 751
Chris@16 752 typename ContextT::position_type pos = act_token.get_position();
Chris@16 753
Chris@16 754 // if (must_emit_line_directive &&
Chris@16 755 // iter_ctx->emitted_lines+1 == act_pos.get_line() &&
Chris@16 756 // iter_ctx->filename == act_pos.get_file())
Chris@16 757 // {
Chris@16 758 // must_emit_line_directive = false;
Chris@16 759 // return false;
Chris@16 760 // }
Chris@16 761
Chris@16 762 if (must_emit_line_directive ||
Chris@16 763 iter_ctx->emitted_lines+1 != act_pos.get_line())
Chris@16 764 {
Chris@16 765 // unput the current token
Chris@16 766 pending_queue.push_front(act_token);
Chris@16 767 pos.set_line(act_pos.get_line());
Chris@16 768
Chris@16 769 if (iter_ctx->emitted_lines+2 == act_pos.get_line() && act_pos.get_line() != 1) {
Chris@16 770 // prefer to output a single newline instead of the #line directive
Chris@16 771 // whitespace.shift_tokens(T_NEWLINE);
Chris@16 772 act_token = result_type(T_NEWLINE, "\n", pos);
Chris@16 773 }
Chris@16 774 else {
Chris@16 775 // account for the newline emitted here
Chris@16 776 act_pos.set_line(act_pos.get_line()-1);
Chris@16 777 iter_ctx->emitted_lines = act_pos.get_line()-1;
Chris@16 778
Chris@16 779 token_sequence_type pending;
Chris@16 780
Chris@16 781 if (!ctx.get_hooks().emit_line_directive(ctx, pending, act_token))
Chris@16 782 {
Chris@16 783 unsigned int column = 6;
Chris@16 784
Chris@16 785 // the hook did not generate anything, emit default #line
Chris@16 786 pos.set_column(1);
Chris@16 787 pending.push_back(result_type(T_PP_LINE, "#line", pos));
Chris@16 788
Chris@16 789 pos.set_column(column); // account for '#line'
Chris@16 790 pending.push_back(result_type(T_SPACE, " ", pos));
Chris@16 791
Chris@16 792 // 21 is the max required size for a 64 bit integer represented as a
Chris@16 793 // string
Chris@16 794 char buffer[22];
Chris@16 795
Chris@16 796 using namespace std; // for some systems sprintf is in namespace std
Chris@16 797 sprintf (buffer, "%ld", pos.get_line());
Chris@16 798
Chris@16 799 pos.set_column(++column); // account for ' '
Chris@16 800 pending.push_back(result_type(T_INTLIT, buffer, pos));
Chris@16 801 pos.set_column(column += (unsigned int)strlen(buffer)); // account for <number>
Chris@16 802 pending.push_back(result_type(T_SPACE, " ", pos));
Chris@16 803 pos.set_column(++column); // account for ' '
Chris@16 804
Chris@16 805 std::string file("\"");
Chris@16 806 boost::filesystem::path filename(
Chris@16 807 wave::util::create_path(act_pos.get_file().c_str()));
Chris@16 808
Chris@16 809 using wave::util::impl::escape_lit;
Chris@16 810 file += escape_lit(wave::util::native_file_string(filename)) + "\"";
Chris@16 811
Chris@16 812 pending.push_back(result_type(T_STRINGLIT, file.c_str(), pos));
Chris@16 813 pos.set_column(column += (unsigned int)file.size()); // account for filename
Chris@16 814 pending.push_back(result_type(T_GENERATEDNEWLINE, "\n", pos));
Chris@16 815 }
Chris@16 816
Chris@16 817 // if there is some replacement text, insert it into the pending queue
Chris@16 818 if (!pending.empty()) {
Chris@16 819 pending_queue.splice(pending_queue.begin(), pending);
Chris@16 820 act_token = pending_queue.front();
Chris@16 821 pending_queue.pop_front();
Chris@16 822 }
Chris@16 823 }
Chris@16 824
Chris@16 825 must_emit_line_directive = false; // we are now in sync
Chris@16 826 return true;
Chris@16 827 }
Chris@16 828
Chris@16 829 must_emit_line_directive = false; // we are now in sync
Chris@16 830 return false;
Chris@16 831 }
Chris@16 832
Chris@16 833 ///////////////////////////////////////////////////////////////////////////////
Chris@16 834 //
Chris@16 835 // pptoken(): return the next preprocessed token
Chris@16 836 //
Chris@16 837 ///////////////////////////////////////////////////////////////////////////////
Chris@16 838 template <typename ContextT>
Chris@16 839 inline typename pp_iterator_functor<ContextT>::result_type const &
Chris@16 840 pp_iterator_functor<ContextT>::pp_token()
Chris@16 841 {
Chris@16 842 using namespace boost::wave;
Chris@16 843
Chris@16 844 token_id id = token_id(*iter_ctx->first);
Chris@16 845
Chris@16 846 // eat all T_PLACEHOLDER tokens, eventually slipped through out of the
Chris@16 847 // macro engine
Chris@16 848 do {
Chris@16 849 if (!pending_queue.empty()) {
Chris@16 850 // if there are pending tokens in the queue, return the first one
Chris@16 851 act_token = pending_queue.front();
Chris@16 852 pending_queue.pop_front();
Chris@16 853 act_pos = act_token.get_position();
Chris@16 854 }
Chris@16 855 else if (!unput_queue.empty()
Chris@16 856 || T_IDENTIFIER == id
Chris@16 857 || IS_CATEGORY(id, KeywordTokenType)
Chris@16 858 || IS_EXTCATEGORY(id, OperatorTokenType|AltExtTokenType)
Chris@16 859 || IS_CATEGORY(id, BoolLiteralTokenType))
Chris@16 860 {
Chris@16 861 // call the lexer, preprocess the required number of tokens, put them
Chris@16 862 // into the unput queue
Chris@16 863 act_token = ctx.expand_tokensequence(iter_ctx->first,
Chris@16 864 iter_ctx->last, pending_queue, unput_queue, skipped_newline);
Chris@16 865 }
Chris@16 866 else {
Chris@16 867 // simply return the next token
Chris@16 868 act_token = *iter_ctx->first;
Chris@16 869 ++iter_ctx->first;
Chris@16 870 }
Chris@16 871 id = token_id(act_token);
Chris@16 872
Chris@16 873 } while (T_PLACEHOLDER == id);
Chris@16 874 return act_token;
Chris@16 875 }
Chris@16 876
Chris@16 877 ///////////////////////////////////////////////////////////////////////////////
Chris@16 878 //
Chris@16 879 // pp_directive(): recognize a preprocessor directive
Chris@16 880 //
Chris@16 881 ///////////////////////////////////////////////////////////////////////////////
Chris@16 882 namespace impl {
Chris@16 883
Chris@16 884 // call 'found_directive' preprocessing hook
Chris@16 885 template <typename ContextT>
Chris@16 886 bool call_found_directive_hook(ContextT& ctx,
Chris@16 887 typename ContextT::token_type const& found_directive)
Chris@16 888 {
Chris@16 889 #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
Chris@16 890 ctx.get_hooks().found_directive(found_directive);
Chris@16 891 #else
Chris@16 892 if (ctx.get_hooks().found_directive(ctx.derived(), found_directive))
Chris@16 893 return true; // skip this directive and return newline only
Chris@16 894 #endif
Chris@16 895 return false;
Chris@16 896 }
Chris@16 897
Chris@16 898 // // call 'skipped_token' preprocessing hook
Chris@16 899 // template <typename ContextT>
Chris@16 900 // void call_skipped_token_hook(ContextT& ctx,
Chris@16 901 // typename ContextT::token_type const& skipped)
Chris@16 902 // {
Chris@16 903 // #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
Chris@16 904 // ctx.get_hooks().skipped_token(skipped);
Chris@16 905 // #else
Chris@16 906 // ctx.get_hooks().skipped_token(ctx.derived(), skipped);
Chris@16 907 // #endif
Chris@16 908 // }
Chris@16 909
Chris@16 910 template <typename ContextT, typename IteratorT>
Chris@16 911 bool next_token_is_pp_directive(ContextT &ctx, IteratorT &it, IteratorT const &end)
Chris@16 912 {
Chris@16 913 using namespace boost::wave;
Chris@16 914
Chris@16 915 token_id id = T_UNKNOWN;
Chris@16 916 for (/**/; it != end; ++it) {
Chris@16 917 id = token_id(*it);
Chris@16 918 if (!IS_CATEGORY(id, WhiteSpaceTokenType))
Chris@16 919 break; // skip leading whitespace
Chris@16 920 if (IS_CATEGORY(id, EOLTokenType) || IS_CATEGORY(id, EOFTokenType))
Chris@16 921 break; // do not enter a new line
Chris@16 922 if (T_CPPCOMMENT == id ||
Chris@16 923 context_policies::util::ccomment_has_newline(*it))
Chris@16 924 {
Chris@16 925 break;
Chris@16 926 }
Chris@16 927
Chris@16 928 // this token gets skipped
Chris@16 929 util::impl::call_skipped_token_hook(ctx, *it);
Chris@16 930 }
Chris@16 931 BOOST_ASSERT(it == end || id != T_UNKNOWN);
Chris@16 932 return it != end && IS_CATEGORY(id, PPTokenType);
Chris@16 933 }
Chris@16 934
Chris@16 935 // verify that there isn't anything significant left on the line
Chris@16 936 template <typename ContextT, typename IteratorT>
Chris@16 937 bool pp_is_last_on_line(ContextT &ctx, IteratorT &it, IteratorT const &end,
Chris@16 938 bool call_hook = true)
Chris@16 939 {
Chris@16 940 using namespace boost::wave;
Chris@16 941
Chris@16 942 // this token gets skipped
Chris@16 943 if (call_hook)
Chris@16 944 util::impl::call_skipped_token_hook(ctx, *it);
Chris@16 945
Chris@16 946 for (++it; it != end; ++it) {
Chris@16 947 token_id id = token_id(*it);
Chris@16 948
Chris@16 949 if (T_CPPCOMMENT == id || T_NEWLINE == id ||
Chris@16 950 context_policies::util::ccomment_has_newline(*it))
Chris@16 951 {
Chris@16 952 if (call_hook)
Chris@16 953 util::impl::call_skipped_token_hook(ctx, *it);
Chris@16 954 ++it; // skip eol/C/C++ comment
Chris@16 955 return true; // no more significant tokens on this line
Chris@16 956 }
Chris@16 957
Chris@16 958 if (!IS_CATEGORY(id, WhiteSpaceTokenType))
Chris@16 959 break;
Chris@16 960
Chris@16 961 // this token gets skipped
Chris@16 962 if (call_hook)
Chris@16 963 util::impl::call_skipped_token_hook(ctx, *it);
Chris@16 964 }
Chris@16 965 return false;
Chris@16 966 }
Chris@16 967
Chris@16 968 ///////////////////////////////////////////////////////////////////////////
Chris@16 969 template <typename ContextT, typename IteratorT>
Chris@16 970 bool skip_to_eol(ContextT &ctx, IteratorT &it, IteratorT const &end,
Chris@16 971 bool call_hook = true)
Chris@16 972 {
Chris@16 973 using namespace boost::wave;
Chris@16 974
Chris@16 975 for (/**/; it != end; ++it) {
Chris@16 976 token_id id = token_id(*it);
Chris@16 977
Chris@16 978 if (T_CPPCOMMENT == id || T_NEWLINE == id ||
Chris@16 979 context_policies::util::ccomment_has_newline(*it))
Chris@16 980 {
Chris@16 981 // always call hook for eol
Chris@16 982 util::impl::call_skipped_token_hook(ctx, *it);
Chris@16 983 ++it; // skip eol/C/C++ comment
Chris@16 984 return true; // found eol
Chris@16 985 }
Chris@16 986
Chris@16 987 if (call_hook)
Chris@16 988 util::impl::call_skipped_token_hook(ctx, *it);
Chris@16 989 }
Chris@16 990 return false;
Chris@16 991 }
Chris@16 992
Chris@16 993 ///////////////////////////////////////////////////////////////////////////
Chris@16 994 template <typename ContextT, typename ContainerT>
Chris@16 995 inline void
Chris@16 996 remove_leading_whitespace(ContextT &ctx, ContainerT& c, bool call_hook = true)
Chris@16 997 {
Chris@16 998 typename ContainerT::iterator it = c.begin();
Chris@16 999 while (IS_CATEGORY(*it, WhiteSpaceTokenType)) {
Chris@16 1000 typename ContainerT::iterator save = it++;
Chris@16 1001 if (call_hook)
Chris@16 1002 util::impl::call_skipped_token_hook(ctx, *save);
Chris@16 1003 c.erase(save);
Chris@16 1004 }
Chris@16 1005 }
Chris@16 1006 }
Chris@16 1007
Chris@16 1008 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1009 template <typename ContextT>
Chris@16 1010 template <typename IteratorT>
Chris@16 1011 inline bool
Chris@16 1012 pp_iterator_functor<ContextT>::extract_identifier(IteratorT &it)
Chris@16 1013 {
Chris@16 1014 token_id id = util::impl::skip_whitespace(it, iter_ctx->last);
Chris@16 1015 if (T_IDENTIFIER == id || IS_CATEGORY(id, KeywordTokenType) ||
Chris@16 1016 IS_EXTCATEGORY(id, OperatorTokenType|AltExtTokenType) ||
Chris@16 1017 IS_CATEGORY(id, BoolLiteralTokenType))
Chris@16 1018 {
Chris@16 1019 IteratorT save = it;
Chris@16 1020 if (impl::pp_is_last_on_line(ctx, save, iter_ctx->last, false))
Chris@16 1021 return true;
Chris@16 1022 }
Chris@16 1023
Chris@16 1024 // report the ill formed directive
Chris@16 1025 impl::skip_to_eol(ctx, it, iter_ctx->last);
Chris@16 1026
Chris@16 1027 string_type str(util::impl::as_string<string_type>(iter_ctx->first, it));
Chris@16 1028
Chris@16 1029 seen_newline = true;
Chris@16 1030 iter_ctx->first = it;
Chris@16 1031 on_illformed(str);
Chris@16 1032 return false;
Chris@16 1033 }
Chris@16 1034
Chris@16 1035 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1036 template <typename ContextT>
Chris@16 1037 template <typename IteratorT>
Chris@16 1038 inline bool
Chris@16 1039 pp_iterator_functor<ContextT>::ensure_is_last_on_line(IteratorT& it, bool call_hook)
Chris@16 1040 {
Chris@16 1041 if (!impl::pp_is_last_on_line(ctx, it, iter_ctx->last, call_hook))
Chris@16 1042 {
Chris@16 1043 // enable error recovery (start over with the next line)
Chris@16 1044 impl::skip_to_eol(ctx, it, iter_ctx->last);
Chris@16 1045
Chris@16 1046 string_type str(util::impl::as_string<string_type>(
Chris@16 1047 iter_ctx->first, it));
Chris@16 1048
Chris@16 1049 seen_newline = true;
Chris@16 1050 iter_ctx->first = it;
Chris@16 1051
Chris@16 1052 // report an invalid directive
Chris@16 1053 on_illformed(str);
Chris@16 1054 return false;
Chris@16 1055 }
Chris@16 1056
Chris@16 1057 if (it == iter_ctx->last && !need_single_line(ctx.get_language()))
Chris@16 1058 {
Chris@16 1059 // The line doesn't end with an eol but eof token.
Chris@16 1060 seen_newline = true; // allow to resume after warning
Chris@16 1061 iter_ctx->first = it;
Chris@16 1062
Chris@16 1063 // Trigger a warning that the last line was not terminated with a
Chris@16 1064 // newline.
Chris@16 1065 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
Chris@16 1066 last_line_not_terminated, "", act_pos);
Chris@16 1067
Chris@16 1068 return false;
Chris@16 1069 }
Chris@16 1070 return true;
Chris@16 1071 }
Chris@16 1072
Chris@16 1073 template <typename ContextT>
Chris@16 1074 template <typename IteratorT>
Chris@16 1075 inline bool
Chris@16 1076 pp_iterator_functor<ContextT>::skip_to_eol_with_check(IteratorT &it, bool call_hook)
Chris@16 1077 {
Chris@16 1078 typename ContextT::string_type value ((*it).get_value());
Chris@16 1079 if (!impl::skip_to_eol(ctx, it, iter_ctx->last, call_hook) &&
Chris@16 1080 !need_single_line(ctx.get_language()))
Chris@16 1081 {
Chris@16 1082 // The line doesn't end with an eol but eof token.
Chris@16 1083 seen_newline = true; // allow to resume after warning
Chris@16 1084 iter_ctx->first = it;
Chris@16 1085
Chris@16 1086 // Trigger a warning, that the last line was not terminated with a
Chris@16 1087 // newline.
Chris@16 1088 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
Chris@16 1089 last_line_not_terminated, "", act_pos);
Chris@16 1090 return false;
Chris@16 1091 }
Chris@16 1092
Chris@16 1093 // normal line ending reached, adjust iterator and flag
Chris@16 1094 seen_newline = true;
Chris@16 1095 iter_ctx->first = it;
Chris@16 1096 return true;
Chris@16 1097 }
Chris@16 1098
Chris@16 1099 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1100 // handle_pp_directive: handle certain pp_directives
Chris@16 1101 template <typename ContextT>
Chris@16 1102 template <typename IteratorT>
Chris@16 1103 inline bool
Chris@16 1104 pp_iterator_functor<ContextT>::handle_pp_directive(IteratorT &it)
Chris@16 1105 {
Chris@16 1106 token_id id = token_id(*it);
Chris@16 1107 bool can_exit = true;
Chris@16 1108 bool call_hook_in_skip = true;
Chris@16 1109 if (!ctx.get_if_block_status()) {
Chris@16 1110 if (IS_EXTCATEGORY(*it, PPConditionalTokenType)) {
Chris@16 1111 // simulate the if block hierarchy
Chris@16 1112 switch (static_cast<unsigned int>(id)) {
Chris@16 1113 case T_PP_IFDEF: // #ifdef
Chris@16 1114 case T_PP_IFNDEF: // #ifndef
Chris@16 1115 case T_PP_IF: // #if
Chris@16 1116 ctx.enter_if_block(false);
Chris@16 1117 break;
Chris@16 1118
Chris@16 1119 case T_PP_ELIF: // #elif
Chris@16 1120 if (!ctx.get_enclosing_if_block_status()) {
Chris@16 1121 if (!ctx.enter_elif_block(false)) {
Chris@16 1122 // #else without matching #if
Chris@16 1123 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
Chris@16 1124 missing_matching_if, "#elif", act_pos);
Chris@16 1125 return true; // do not analyze this directive any further
Chris@16 1126 }
Chris@16 1127 }
Chris@16 1128 else {
Chris@16 1129 can_exit = false; // #elif is not always safe to skip
Chris@16 1130 }
Chris@16 1131 break;
Chris@16 1132
Chris@16 1133 case T_PP_ELSE: // #else
Chris@16 1134 case T_PP_ENDIF: // #endif
Chris@16 1135 {
Chris@16 1136 // handle this directive
Chris@16 1137 if (T_PP_ELSE == token_id(*it))
Chris@16 1138 on_else();
Chris@16 1139 else
Chris@16 1140 on_endif();
Chris@16 1141
Chris@16 1142 // make sure, there are no (non-whitespace) tokens left on
Chris@16 1143 // this line
Chris@16 1144 ensure_is_last_on_line(it);
Chris@16 1145
Chris@16 1146 // we skipped to the end of this line already
Chris@16 1147 seen_newline = true;
Chris@16 1148 iter_ctx->first = it;
Chris@16 1149 }
Chris@16 1150 return true;
Chris@16 1151
Chris@16 1152 default: // #something else
Chris@16 1153 on_illformed((*it).get_value());
Chris@16 1154 break;
Chris@16 1155 }
Chris@16 1156 }
Chris@16 1157 else {
Chris@16 1158 util::impl::call_skipped_token_hook(ctx, *it);
Chris@16 1159 ++it;
Chris@16 1160 }
Chris@16 1161 }
Chris@16 1162 else {
Chris@16 1163 // try to handle the simple pp directives without parsing
Chris@16 1164 result_type directive = *it;
Chris@16 1165 bool include_next = false;
Chris@16 1166 switch (static_cast<unsigned int>(id)) {
Chris@16 1167 case T_PP_QHEADER: // #include "..."
Chris@16 1168 #if BOOST_WAVE_SUPPORT_INCLUDE_NEXT != 0
Chris@16 1169 case T_PP_QHEADER_NEXT:
Chris@16 1170 #endif
Chris@16 1171 include_next = (T_PP_QHEADER_NEXT == id) ? true : false;
Chris@16 1172 if (!impl::call_found_directive_hook(ctx, *it))
Chris@16 1173 {
Chris@16 1174 string_type dir((*it).get_value());
Chris@16 1175
Chris@16 1176 // make sure, there are no (non-whitespace) tokens left on
Chris@16 1177 // this line
Chris@16 1178 if (ensure_is_last_on_line(it))
Chris@16 1179 {
Chris@16 1180 seen_newline = true;
Chris@16 1181 iter_ctx->first = it;
Chris@16 1182 on_include (dir, false, include_next);
Chris@16 1183 }
Chris@16 1184 return true;
Chris@16 1185 }
Chris@16 1186 break;
Chris@16 1187
Chris@16 1188 case T_PP_HHEADER: // #include <...>
Chris@16 1189 #if BOOST_WAVE_SUPPORT_INCLUDE_NEXT != 0
Chris@16 1190 case T_PP_HHEADER_NEXT:
Chris@16 1191 #endif
Chris@16 1192 include_next = (T_PP_HHEADER_NEXT == id) ? true : false;
Chris@16 1193 if (!impl::call_found_directive_hook(ctx, *it))
Chris@16 1194 {
Chris@16 1195 string_type dir((*it).get_value());
Chris@16 1196
Chris@16 1197 // make sure, there are no (non-whitespace) tokens left on
Chris@16 1198 // this line
Chris@16 1199 if (ensure_is_last_on_line(it))
Chris@16 1200 {
Chris@16 1201 seen_newline = true;
Chris@16 1202 iter_ctx->first = it;
Chris@16 1203 on_include (dir, true, include_next);
Chris@16 1204 }
Chris@16 1205 return true;
Chris@16 1206 }
Chris@16 1207 break;
Chris@16 1208
Chris@16 1209 case T_PP_ELSE: // #else
Chris@16 1210 case T_PP_ENDIF: // #endif
Chris@16 1211 if (!impl::call_found_directive_hook(ctx, *it))
Chris@16 1212 {
Chris@16 1213 // handle this directive
Chris@16 1214 if (T_PP_ELSE == token_id(*it))
Chris@16 1215 on_else();
Chris@16 1216 else
Chris@16 1217 on_endif();
Chris@16 1218
Chris@16 1219 // make sure, there are no (non-whitespace) tokens left on
Chris@16 1220 // this line
Chris@16 1221 ensure_is_last_on_line(it);
Chris@16 1222
Chris@16 1223 // we skipped to the end of this line already
Chris@16 1224 seen_newline = true;
Chris@16 1225 iter_ctx->first = it;
Chris@16 1226 return true;
Chris@16 1227 }
Chris@16 1228 break;
Chris@16 1229
Chris@16 1230 // extract everything on this line as arguments
Chris@16 1231 // case T_PP_IF: // #if
Chris@16 1232 // case T_PP_ELIF: // #elif
Chris@16 1233 // case T_PP_ERROR: // #error
Chris@16 1234 // case T_PP_WARNING: // #warning
Chris@16 1235 // case T_PP_PRAGMA: // #pragma
Chris@16 1236 // case T_PP_LINE: // #line
Chris@16 1237 // break;
Chris@16 1238
Chris@16 1239 // extract first non-whitespace token as argument
Chris@16 1240 case T_PP_UNDEF: // #undef
Chris@16 1241 if (!impl::call_found_directive_hook(ctx, *it) &&
Chris@16 1242 extract_identifier(it))
Chris@16 1243 {
Chris@16 1244 on_undefine(it);
Chris@16 1245 }
Chris@16 1246 call_hook_in_skip = false;
Chris@16 1247 break;
Chris@16 1248
Chris@16 1249 case T_PP_IFDEF: // #ifdef
Chris@16 1250 if (!impl::call_found_directive_hook(ctx, *it) &&
Chris@16 1251 extract_identifier(it))
Chris@16 1252 {
Chris@16 1253 on_ifdef(directive, it);
Chris@16 1254 }
Chris@16 1255 call_hook_in_skip = false;
Chris@16 1256 break;
Chris@16 1257
Chris@16 1258 case T_PP_IFNDEF: // #ifndef
Chris@16 1259 if (!impl::call_found_directive_hook(ctx, *it) &&
Chris@16 1260 extract_identifier(it))
Chris@16 1261 {
Chris@16 1262 on_ifndef(directive, it);
Chris@16 1263 }
Chris@16 1264 call_hook_in_skip = false;
Chris@16 1265 break;
Chris@16 1266
Chris@16 1267 #if BOOST_WAVE_SUPPORT_MS_EXTENSIONS != 0
Chris@16 1268 // case T_MSEXT_PP_REGION: // #region ...
Chris@16 1269 // break;
Chris@16 1270 //
Chris@16 1271 // case T_MSEXT_PP_ENDREGION: // #endregion
Chris@16 1272 // break;
Chris@16 1273 #endif
Chris@16 1274
Chris@16 1275 default:
Chris@16 1276 can_exit = false;
Chris@16 1277 break;
Chris@16 1278 }
Chris@16 1279 }
Chris@16 1280
Chris@16 1281 // start over with the next line, if only possible
Chris@16 1282 if (can_exit) {
Chris@16 1283 skip_to_eol_with_check(it, call_hook_in_skip);
Chris@16 1284 return true; // may be safely ignored
Chris@16 1285 }
Chris@16 1286 return false; // do not ignore this pp directive
Chris@16 1287 }
Chris@16 1288
Chris@16 1289 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1290 // pp_directive(): recognize a preprocessor directive
Chris@16 1291 template <typename ContextT>
Chris@16 1292 inline bool
Chris@16 1293 pp_iterator_functor<ContextT>::pp_directive()
Chris@16 1294 {
Chris@16 1295 using namespace cpplexer;
Chris@16 1296
Chris@16 1297 // test, if the next non-whitespace token is a pp directive
Chris@16 1298 lexer_type it = iter_ctx->first;
Chris@16 1299
Chris@16 1300 if (!impl::next_token_is_pp_directive(ctx, it, iter_ctx->last)) {
Chris@16 1301 // skip null pp directive (no need to do it via the parser)
Chris@16 1302 if (it != iter_ctx->last && T_POUND == BASE_TOKEN(token_id(*it))) {
Chris@16 1303 if (impl::pp_is_last_on_line(ctx, it, iter_ctx->last)) {
Chris@16 1304 // start over with the next line
Chris@16 1305 seen_newline = true;
Chris@16 1306 iter_ctx->first = it;
Chris@16 1307 return true;
Chris@16 1308 }
Chris@16 1309 else if (ctx.get_if_block_status()) {
Chris@16 1310 // report invalid pp directive
Chris@16 1311 impl::skip_to_eol(ctx, it, iter_ctx->last);
Chris@16 1312 seen_newline = true;
Chris@16 1313
Chris@16 1314 string_type str(boost::wave::util::impl::as_string<string_type>(
Chris@16 1315 iter_ctx->first, it));
Chris@16 1316
Chris@16 1317 token_sequence_type faulty_line;
Chris@16 1318
Chris@16 1319 for (/**/; iter_ctx->first != it; ++iter_ctx->first)
Chris@16 1320 faulty_line.push_back(*iter_ctx->first);
Chris@16 1321
Chris@16 1322 token_sequence_type pending;
Chris@16 1323 if (ctx.get_hooks().found_unknown_directive(ctx, faulty_line, pending))
Chris@16 1324 {
Chris@16 1325 // if there is some replacement text, insert it into the pending queue
Chris@16 1326 if (!pending.empty())
Chris@16 1327 pending_queue.splice(pending_queue.begin(), pending);
Chris@16 1328 return true;
Chris@16 1329 }
Chris@16 1330
Chris@16 1331 // default behavior is to throw an exception
Chris@16 1332 on_illformed(str);
Chris@16 1333 }
Chris@16 1334 }
Chris@16 1335
Chris@16 1336 // this line does not contain a pp directive, so simply return
Chris@16 1337 return false;
Chris@16 1338 }
Chris@16 1339
Chris@16 1340 // found eof
Chris@16 1341 if (it == iter_ctx->last)
Chris@16 1342 return false;
Chris@16 1343
Chris@16 1344 // ignore/handle all pp directives not related to conditional compilation while
Chris@16 1345 // if block status is false
Chris@16 1346 if (handle_pp_directive(it)) {
Chris@16 1347 // we may skip pp directives only if the current if block status is
Chris@16 1348 // false or if it was a #include directive we could handle directly
Chris@16 1349 return true; // the pp directive has been handled/skipped
Chris@16 1350 }
Chris@16 1351
Chris@16 1352 // found a pp directive, so try to identify it, start with the pp_token
Chris@16 1353 bool found_eof = false;
Chris@16 1354 result_type found_directive;
Chris@16 1355 token_sequence_type found_eoltokens;
Chris@16 1356
Chris@16 1357 tree_parse_info_type hit = cpp_grammar_type::parse_cpp_grammar(
Chris@16 1358 it, iter_ctx->last, act_pos, found_eof, found_directive, found_eoltokens);
Chris@16 1359
Chris@16 1360 if (hit.match) {
Chris@16 1361 // position the iterator past the matched sequence to allow
Chris@16 1362 // resynchronization, if an error occurs
Chris@16 1363 iter_ctx->first = hit.stop;
Chris@16 1364 seen_newline = true;
Chris@16 1365 must_emit_line_directive = true;
Chris@16 1366
Chris@16 1367 // found a valid pp directive, dispatch to the correct function to handle
Chris@16 1368 // the found pp directive
Chris@16 1369 bool result = dispatch_directive (hit, found_directive, found_eoltokens);
Chris@16 1370
Chris@16 1371 if (found_eof && !need_single_line(ctx.get_language())) {
Chris@16 1372 // The line was terminated with an end of file token.
Chris@16 1373 // So trigger a warning, that the last line was not terminated with a
Chris@16 1374 // newline.
Chris@16 1375 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
Chris@16 1376 last_line_not_terminated, "", act_pos);
Chris@16 1377 }
Chris@16 1378 return result;
Chris@16 1379 }
Chris@16 1380 else if (token_id(found_directive) != T_EOF) {
Chris@16 1381 // recognized invalid directive
Chris@16 1382 impl::skip_to_eol(ctx, it, iter_ctx->last);
Chris@16 1383 seen_newline = true;
Chris@16 1384
Chris@16 1385 string_type str(boost::wave::util::impl::as_string<string_type>(
Chris@16 1386 iter_ctx->first, it));
Chris@16 1387 iter_ctx->first = it;
Chris@16 1388
Chris@16 1389 // report the ill formed directive
Chris@16 1390 on_illformed(str);
Chris@16 1391 }
Chris@16 1392 return false;
Chris@16 1393 }
Chris@16 1394
Chris@16 1395 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1396 //
Chris@16 1397 // dispatch_directive(): dispatch a recognized preprocessor directive
Chris@16 1398 //
Chris@16 1399 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1400 template <typename ContextT>
Chris@16 1401 inline bool
Chris@16 1402 pp_iterator_functor<ContextT>::dispatch_directive(
Chris@16 1403 tree_parse_info_type const &hit, result_type const& found_directive,
Chris@16 1404 token_sequence_type const& found_eoltokens)
Chris@16 1405 {
Chris@16 1406 using namespace cpplexer;
Chris@16 1407
Chris@16 1408 typedef typename parse_tree_type::const_iterator const_child_iterator_t;
Chris@16 1409
Chris@16 1410 // this iterator points to the root node of the parse tree
Chris@16 1411 const_child_iterator_t begin = hit.trees.begin();
Chris@16 1412
Chris@16 1413 // decide, which preprocessor directive was found
Chris@16 1414 parse_tree_type const &root = (*begin).children;
Chris@16 1415 parse_node_value_type const &nodeval = get_first_leaf(*root.begin()).value;
Chris@16 1416 //long node_id = nodeval.id().to_long();
Chris@16 1417
Chris@16 1418 const_child_iterator_t begin_child_it = (*root.begin()).children.begin();
Chris@16 1419 const_child_iterator_t end_child_it = (*root.begin()).children.end();
Chris@16 1420
Chris@16 1421 token_id id = token_id(found_directive);
Chris@16 1422
Chris@16 1423 // call preprocessing hook
Chris@16 1424 if (impl::call_found_directive_hook(ctx, found_directive))
Chris@16 1425 return true; // skip this directive and return newline only
Chris@16 1426
Chris@16 1427 switch (static_cast<unsigned int>(id)) {
Chris@16 1428 // case T_PP_QHEADER: // #include "..."
Chris@16 1429 // #if BOOST_WAVE_SUPPORT_INCLUDE_NEXT != 0
Chris@16 1430 // case T_PP_QHEADER_NEXT: // #include_next "..."
Chris@16 1431 // #endif
Chris@16 1432 // on_include ((*nodeval.begin()).get_value(), false,
Chris@16 1433 // T_PP_QHEADER_NEXT == id);
Chris@16 1434 // break;
Chris@16 1435
Chris@16 1436 // case T_PP_HHEADER: // #include <...>
Chris@16 1437 // #if BOOST_WAVE_SUPPORT_INCLUDE_NEXT != 0
Chris@16 1438 // case T_PP_HHEADER_NEXT: // #include_next <...>
Chris@16 1439 // #endif
Chris@16 1440 // on_include ((*nodeval.begin()).get_value(), true,
Chris@16 1441 // T_PP_HHEADER_NEXT == id);
Chris@16 1442 // break;
Chris@16 1443
Chris@16 1444 case T_PP_INCLUDE: // #include ...
Chris@16 1445 #if BOOST_WAVE_SUPPORT_INCLUDE_NEXT != 0
Chris@16 1446 case T_PP_INCLUDE_NEXT: // #include_next ...
Chris@16 1447 #endif
Chris@16 1448 on_include (begin_child_it, end_child_it, T_PP_INCLUDE_NEXT == id);
Chris@16 1449 break;
Chris@16 1450
Chris@16 1451 case T_PP_DEFINE: // #define
Chris@16 1452 on_define (*begin);
Chris@16 1453 break;
Chris@16 1454
Chris@16 1455 // case T_PP_UNDEF: // #undef
Chris@16 1456 // on_undefine(*nodeval.begin());
Chris@16 1457 // break;
Chris@16 1458 //
Chris@16 1459 // case T_PP_IFDEF: // #ifdef
Chris@16 1460 // on_ifdef(found_directive, begin_child_it, end_child_it);
Chris@16 1461 // break;
Chris@16 1462 //
Chris@16 1463 // case T_PP_IFNDEF: // #ifndef
Chris@16 1464 // on_ifndef(found_directive, begin_child_it, end_child_it);
Chris@16 1465 // break;
Chris@16 1466
Chris@16 1467 case T_PP_IF: // #if
Chris@16 1468 on_if(found_directive, begin_child_it, end_child_it);
Chris@16 1469 break;
Chris@16 1470
Chris@16 1471 case T_PP_ELIF: // #elif
Chris@16 1472 on_elif(found_directive, begin_child_it, end_child_it);
Chris@16 1473 break;
Chris@16 1474
Chris@16 1475 // case T_PP_ELSE: // #else
Chris@16 1476 // on_else();
Chris@16 1477 // break;
Chris@16 1478
Chris@16 1479 // case T_PP_ENDIF: // #endif
Chris@16 1480 // on_endif();
Chris@16 1481 // break;
Chris@16 1482
Chris@16 1483 case T_PP_LINE: // #line
Chris@16 1484 on_line(begin_child_it, end_child_it);
Chris@16 1485 break;
Chris@16 1486
Chris@16 1487 case T_PP_ERROR: // #error
Chris@16 1488 on_error(begin_child_it, end_child_it);
Chris@16 1489 break;
Chris@16 1490
Chris@16 1491 #if BOOST_WAVE_SUPPORT_WARNING_DIRECTIVE != 0
Chris@16 1492 case T_PP_WARNING: // #warning
Chris@16 1493 on_warning(begin_child_it, end_child_it);
Chris@16 1494 break;
Chris@16 1495 #endif
Chris@16 1496
Chris@16 1497 case T_PP_PRAGMA: // #pragma
Chris@16 1498 return on_pragma(begin_child_it, end_child_it);
Chris@16 1499
Chris@16 1500 #if BOOST_WAVE_SUPPORT_MS_EXTENSIONS != 0
Chris@16 1501 case T_MSEXT_PP_REGION:
Chris@16 1502 case T_MSEXT_PP_ENDREGION:
Chris@16 1503 break; // ignore these
Chris@16 1504 #endif
Chris@16 1505
Chris@16 1506 default: // #something else
Chris@16 1507 on_illformed((*nodeval.begin()).get_value());
Chris@16 1508
Chris@16 1509 // if we end up here, we have been instructed to ignore the error, so
Chris@16 1510 // we simply copy the whole construct to the output
Chris@16 1511 {
Chris@16 1512 token_sequence_type expanded;
Chris@16 1513 get_token_value<result_type, parse_node_type> get_value;
Chris@16 1514
Chris@16 1515 std::copy(make_ref_transform_iterator(begin_child_it, get_value),
Chris@16 1516 make_ref_transform_iterator(end_child_it, get_value),
Chris@16 1517 std::inserter(expanded, expanded.end()));
Chris@16 1518 pending_queue.splice(pending_queue.begin(), expanded);
Chris@16 1519 }
Chris@16 1520 break;
Chris@16 1521 }
Chris@16 1522
Chris@16 1523 // properly skip trailing newline for all directives
Chris@16 1524 typename token_sequence_type::const_iterator eol = found_eoltokens.begin();
Chris@16 1525 impl::skip_to_eol(ctx, eol, found_eoltokens.end());
Chris@16 1526 return true; // return newline only
Chris@16 1527 }
Chris@16 1528
Chris@16 1529 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1530 //
Chris@16 1531 // on_include: handle #include <...> or #include "..." directives
Chris@16 1532 //
Chris@16 1533 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1534 template <typename ContextT>
Chris@16 1535 inline void
Chris@16 1536 pp_iterator_functor<ContextT>::on_include (string_type const &s,
Chris@16 1537 bool is_system, bool include_next)
Chris@16 1538 {
Chris@16 1539 BOOST_ASSERT(ctx.get_if_block_status());
Chris@16 1540
Chris@16 1541 // strip quotes first, extract filename
Chris@16 1542 typename string_type::size_type pos_end = s.find_last_of(is_system ? '>' : '\"');
Chris@16 1543
Chris@16 1544 if (string_type::npos == pos_end) {
Chris@16 1545 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, bad_include_statement,
Chris@16 1546 s.c_str(), act_pos);
Chris@16 1547 return;
Chris@16 1548 }
Chris@16 1549
Chris@16 1550 typename string_type::size_type pos_begin =
Chris@16 1551 s.find_last_of(is_system ? '<' : '\"', pos_end-1);
Chris@16 1552
Chris@16 1553 if (string_type::npos == pos_begin) {
Chris@16 1554 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, bad_include_statement,
Chris@16 1555 s.c_str(), act_pos);
Chris@16 1556 return;
Chris@16 1557 }
Chris@16 1558
Chris@16 1559 std::string file_token(s.substr(pos_begin, pos_end-pos_begin+1).c_str());
Chris@16 1560 std::string file_path(s.substr(pos_begin+1, pos_end-pos_begin-1).c_str());
Chris@16 1561
Chris@16 1562 // finally include the file
Chris@16 1563 on_include_helper(file_token.c_str(), file_path.c_str(), is_system,
Chris@16 1564 include_next);
Chris@16 1565 }
Chris@16 1566
Chris@16 1567 template <typename ContextT>
Chris@16 1568 inline bool
Chris@16 1569 pp_iterator_functor<ContextT>::on_include_helper (char const *f, char const *s,
Chris@16 1570 bool is_system, bool include_next)
Chris@16 1571 {
Chris@16 1572 namespace fs = boost::filesystem;
Chris@16 1573
Chris@16 1574 // try to locate the given file, searching through the include path lists
Chris@16 1575 std::string file_path(s);
Chris@16 1576 std::string dir_path;
Chris@16 1577 #if BOOST_WAVE_SUPPORT_INCLUDE_NEXT != 0
Chris@16 1578 char const *current_name = include_next ? iter_ctx->real_filename.c_str() : 0;
Chris@16 1579 #else
Chris@16 1580 char const *current_name = 0; // never try to match current file name
Chris@16 1581 #endif
Chris@16 1582
Chris@16 1583 // call the 'found_include_directive' hook function
Chris@16 1584 #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
Chris@16 1585 ctx.get_hooks().found_include_directive(f, include_next);
Chris@16 1586 #else
Chris@16 1587 if (ctx.get_hooks().found_include_directive(ctx.derived(), f, include_next))
Chris@16 1588 return true; // client returned false: skip file to include
Chris@16 1589 #endif
Chris@16 1590
Chris@16 1591 file_path = util::impl::unescape_lit(file_path);
Chris@16 1592 std::string native_path_str;
Chris@16 1593
Chris@16 1594 if (!ctx.get_hooks().locate_include_file(ctx, file_path, is_system,
Chris@16 1595 current_name, dir_path, native_path_str))
Chris@16 1596 {
Chris@16 1597 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, bad_include_file,
Chris@16 1598 file_path.c_str(), act_pos);
Chris@16 1599 return false;
Chris@16 1600 }
Chris@16 1601
Chris@16 1602 // test, if this file is known through a #pragma once directive
Chris@16 1603 #if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0
Chris@16 1604 if (!ctx.has_pragma_once(native_path_str))
Chris@16 1605 #endif
Chris@16 1606 {
Chris@16 1607 // the new include file determines the actual current directory
Chris@16 1608 ctx.set_current_directory(native_path_str.c_str());
Chris@16 1609
Chris@16 1610 // preprocess the opened file
Chris@16 1611 boost::shared_ptr<base_iteration_context_type> new_iter_ctx (
Chris@16 1612 new iteration_context_type(ctx, native_path_str.c_str(), act_pos,
Chris@16 1613 boost::wave::enable_prefer_pp_numbers(ctx.get_language()),
Chris@16 1614 is_system ? base_iteration_context_type::system_header :
Chris@16 1615 base_iteration_context_type::user_header));
Chris@16 1616
Chris@16 1617 // call the include policy trace function
Chris@16 1618 #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
Chris@16 1619 ctx.get_hooks().opened_include_file(dir_path, file_path,
Chris@16 1620 ctx.get_iteration_depth(), is_system);
Chris@16 1621 #else
Chris@16 1622 ctx.get_hooks().opened_include_file(ctx.derived(), dir_path, file_path,
Chris@16 1623 is_system);
Chris@16 1624 #endif
Chris@16 1625
Chris@16 1626 // store current file position
Chris@16 1627 iter_ctx->real_relative_filename = ctx.get_current_relative_filename().c_str();
Chris@16 1628 iter_ctx->filename = act_pos.get_file();
Chris@16 1629 iter_ctx->line = act_pos.get_line();
Chris@16 1630 iter_ctx->if_block_depth = ctx.get_if_block_depth();
Chris@16 1631 iter_ctx->emitted_lines = (unsigned int)(-1); // force #line directive
Chris@16 1632
Chris@16 1633 // push the old iteration context onto the stack and continue with the new
Chris@16 1634 ctx.push_iteration_context(act_pos, iter_ctx);
Chris@16 1635 iter_ctx = new_iter_ctx;
Chris@16 1636 seen_newline = true; // fake a newline to trigger pp_directive
Chris@16 1637 must_emit_line_directive = true;
Chris@16 1638
Chris@16 1639 act_pos.set_file(iter_ctx->filename); // initialize file position
Chris@16 1640 #if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0
Chris@16 1641 fs::path rfp(wave::util::create_path(iter_ctx->real_filename.c_str()));
Chris@16 1642 std::string real_filename(rfp.string());
Chris@16 1643 ctx.set_current_filename(real_filename.c_str());
Chris@16 1644 #endif
Chris@16 1645
Chris@16 1646 ctx.set_current_relative_filename(dir_path.c_str());
Chris@16 1647 iter_ctx->real_relative_filename = dir_path.c_str();
Chris@16 1648
Chris@16 1649 act_pos.set_line(iter_ctx->line);
Chris@16 1650 act_pos.set_column(0);
Chris@16 1651 }
Chris@16 1652 return true;
Chris@16 1653 }
Chris@16 1654
Chris@16 1655 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1656 //
Chris@16 1657 // on_include(): handle #include ... directives
Chris@16 1658 //
Chris@16 1659 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1660
Chris@16 1661 namespace impl {
Chris@16 1662
Chris@16 1663 // trim all whitespace from the beginning and the end of the given string
Chris@16 1664 template <typename StringT>
Chris@16 1665 inline StringT
Chris@16 1666 trim_whitespace(StringT const &s)
Chris@16 1667 {
Chris@16 1668 typedef typename StringT::size_type size_type;
Chris@16 1669
Chris@16 1670 size_type first = s.find_first_not_of(" \t\v\f");
Chris@16 1671 if (StringT::npos == first)
Chris@16 1672 return StringT();
Chris@16 1673 size_type last = s.find_last_not_of(" \t\v\f");
Chris@16 1674 return s.substr(first, last-first+1);
Chris@16 1675 }
Chris@16 1676 }
Chris@16 1677
Chris@16 1678 template <typename ContextT>
Chris@16 1679 inline void
Chris@16 1680 pp_iterator_functor<ContextT>::on_include(
Chris@16 1681 typename parse_tree_type::const_iterator const &begin,
Chris@16 1682 typename parse_tree_type::const_iterator const &end, bool include_next)
Chris@16 1683 {
Chris@16 1684 BOOST_ASSERT(ctx.get_if_block_status());
Chris@16 1685
Chris@16 1686 // preprocess the given token sequence (the body of the #include directive)
Chris@16 1687 get_token_value<result_type, parse_node_type> get_value;
Chris@16 1688 token_sequence_type expanded;
Chris@16 1689 token_sequence_type toexpand;
Chris@16 1690
Chris@16 1691 std::copy(make_ref_transform_iterator(begin, get_value),
Chris@16 1692 make_ref_transform_iterator(end, get_value),
Chris@16 1693 std::inserter(toexpand, toexpand.end()));
Chris@16 1694
Chris@16 1695 typename token_sequence_type::iterator begin2 = toexpand.begin();
Chris@16 1696 ctx.expand_whole_tokensequence(begin2, toexpand.end(), expanded,
Chris@16 1697 false);
Chris@16 1698
Chris@16 1699 // now, include the file
Chris@16 1700 string_type s (impl::trim_whitespace(boost::wave::util::impl::as_string(expanded)));
Chris@16 1701 bool is_system = '<' == s[0] && '>' == s[s.size()-1];
Chris@16 1702
Chris@16 1703 if (!is_system && !('\"' == s[0] && '\"' == s[s.size()-1])) {
Chris@16 1704 // should resolve into something like <...> or "..."
Chris@16 1705 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, bad_include_statement,
Chris@16 1706 s.c_str(), act_pos);
Chris@16 1707 return;
Chris@16 1708 }
Chris@16 1709 on_include(s, is_system, include_next);
Chris@16 1710 }
Chris@16 1711
Chris@16 1712 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1713 //
Chris@16 1714 // on_define(): handle #define directives
Chris@16 1715 //
Chris@16 1716 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1717
Chris@16 1718 template <typename ContextT>
Chris@16 1719 inline void
Chris@16 1720 pp_iterator_functor<ContextT>::on_define (parse_node_type const &node)
Chris@16 1721 {
Chris@16 1722 BOOST_ASSERT(ctx.get_if_block_status());
Chris@16 1723
Chris@16 1724 // retrieve the macro definition from the parse tree
Chris@16 1725 result_type macroname;
Chris@16 1726 std::vector<result_type> macroparameters;
Chris@16 1727 token_sequence_type macrodefinition;
Chris@16 1728 bool has_parameters = false;
Chris@16 1729 position_type pos(act_token.get_position());
Chris@16 1730
Chris@16 1731 if (!boost::wave::util::retrieve_macroname(ctx, node,
Chris@16 1732 BOOST_WAVE_PLAIN_DEFINE_ID, macroname, pos, false))
Chris@16 1733 return;
Chris@16 1734 has_parameters = boost::wave::util::retrieve_macrodefinition(node,
Chris@16 1735 BOOST_WAVE_MACRO_PARAMETERS_ID, macroparameters, pos, false);
Chris@16 1736 boost::wave::util::retrieve_macrodefinition(node,
Chris@16 1737 BOOST_WAVE_MACRO_DEFINITION_ID, macrodefinition, pos, false);
Chris@16 1738
Chris@16 1739 if (has_parameters) {
Chris@16 1740 #if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
Chris@16 1741 if (boost::wave::need_variadics(ctx.get_language())) {
Chris@16 1742 // test whether ellipsis are given, and if yes, if these are placed as the
Chris@16 1743 // last argument, test if __VA_ARGS__ is used as a macro parameter name
Chris@16 1744 using namespace cpplexer;
Chris@16 1745 typedef typename std::vector<result_type>::iterator
Chris@16 1746 parameter_iterator_t;
Chris@16 1747
Chris@16 1748 bool seen_ellipses = false;
Chris@16 1749 parameter_iterator_t end = macroparameters.end();
Chris@16 1750 for (parameter_iterator_t pit = macroparameters.begin();
Chris@16 1751 pit != end; ++pit)
Chris@16 1752 {
Chris@16 1753 if (seen_ellipses) {
Chris@16 1754 // ellipses are not the last given formal argument
Chris@16 1755 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
Chris@16 1756 bad_define_statement, macroname.get_value().c_str(),
Chris@16 1757 (*pit).get_position());
Chris@16 1758 return;
Chris@16 1759 }
Chris@16 1760 if (T_ELLIPSIS == token_id(*pit))
Chris@16 1761 seen_ellipses = true;
Chris@16 1762
Chris@16 1763 // can't use __VA_ARGS__ as a argument name
Chris@16 1764 if ("__VA_ARGS__" == (*pit).get_value()) {
Chris@16 1765 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
Chris@16 1766 bad_define_statement_va_args,
Chris@16 1767 macroname.get_value().c_str(), (*pit).get_position());
Chris@16 1768 return;
Chris@16 1769 }
Chris@16 1770 }
Chris@16 1771
Chris@16 1772 // if there wasn't an ellipsis, then there shouldn't be a __VA_ARGS__
Chris@16 1773 // placeholder in the definition too [C99 Standard 6.10.3.5]
Chris@16 1774 if (!seen_ellipses) {
Chris@16 1775 typedef typename token_sequence_type::iterator definition_iterator_t;
Chris@16 1776
Chris@16 1777 bool seen_va_args = false;
Chris@16 1778 definition_iterator_t pend = macrodefinition.end();
Chris@16 1779 for (definition_iterator_t dit = macrodefinition.begin();
Chris@16 1780 dit != pend; ++dit)
Chris@16 1781 {
Chris@16 1782 if (T_IDENTIFIER == token_id(*dit) &&
Chris@16 1783 "__VA_ARGS__" == (*dit).get_value())
Chris@16 1784 {
Chris@16 1785 seen_va_args = true;
Chris@16 1786 }
Chris@16 1787 }
Chris@16 1788 if (seen_va_args) {
Chris@16 1789 // must not have seen __VA_ARGS__ placeholder
Chris@16 1790 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
Chris@16 1791 bad_define_statement_va_args,
Chris@16 1792 macroname.get_value().c_str(), act_token.get_position());
Chris@16 1793 return;
Chris@16 1794 }
Chris@16 1795 }
Chris@16 1796 }
Chris@16 1797 else
Chris@16 1798 #endif // BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
Chris@16 1799 {
Chris@16 1800 // test, that there is no T_ELLIPSES given
Chris@16 1801 using namespace cpplexer;
Chris@16 1802 typedef typename std::vector<result_type>::iterator
Chris@16 1803 parameter_iterator_t;
Chris@16 1804
Chris@16 1805 parameter_iterator_t end = macroparameters.end();
Chris@16 1806 for (parameter_iterator_t pit = macroparameters.begin();
Chris@16 1807 pit != end; ++pit)
Chris@16 1808 {
Chris@16 1809 if (T_ELLIPSIS == token_id(*pit)) {
Chris@16 1810 // if variadics are disabled, no ellipses should be given
Chris@16 1811 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
Chris@16 1812 bad_define_statement, macroname.get_value().c_str(),
Chris@16 1813 (*pit).get_position());
Chris@16 1814 return;
Chris@16 1815 }
Chris@16 1816 }
Chris@16 1817 }
Chris@16 1818 }
Chris@16 1819
Chris@16 1820 // add the new macro to the macromap
Chris@16 1821 ctx.add_macro_definition(macroname, has_parameters, macroparameters,
Chris@16 1822 macrodefinition);
Chris@16 1823 }
Chris@16 1824
Chris@16 1825 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1826 //
Chris@16 1827 // on_undefine(): handle #undef directives
Chris@16 1828 //
Chris@16 1829 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1830 template <typename ContextT>
Chris@16 1831 inline void
Chris@16 1832 pp_iterator_functor<ContextT>::on_undefine (lexer_type const &it)
Chris@16 1833 {
Chris@16 1834 BOOST_ASSERT(ctx.get_if_block_status());
Chris@16 1835
Chris@16 1836 // retrieve the macro name to undefine from the parse tree
Chris@16 1837 ctx.remove_macro_definition((*it).get_value()); // throws for predefined macros
Chris@16 1838 }
Chris@16 1839
Chris@16 1840 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1841 //
Chris@16 1842 // on_ifdef(): handle #ifdef directives
Chris@16 1843 //
Chris@16 1844 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1845 template <typename ContextT>
Chris@16 1846 inline void
Chris@16 1847 pp_iterator_functor<ContextT>::on_ifdef(
Chris@16 1848 result_type const& found_directive, lexer_type const &it)
Chris@16 1849 // typename parse_tree_type::const_iterator const &it)
Chris@16 1850 // typename parse_tree_type::const_iterator const &end)
Chris@16 1851 {
Chris@16 1852 // get_token_value<result_type, parse_node_type> get_value;
Chris@16 1853 // token_sequence_type toexpand;
Chris@16 1854 //
Chris@16 1855 // std::copy(make_ref_transform_iterator((*begin).children.begin(), get_value),
Chris@16 1856 // make_ref_transform_iterator((*begin).children.end(), get_value),
Chris@16 1857 // std::inserter(toexpand, toexpand.end()));
Chris@16 1858
Chris@16 1859 bool is_defined = false;
Chris@16 1860 token_sequence_type directive;
Chris@16 1861
Chris@16 1862 directive.insert(directive.end(), *it);
Chris@16 1863
Chris@16 1864 #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
Chris@16 1865 is_defined = ctx.is_defined_macro((*it).get_value()); // toexpand.begin(), toexpand.end());
Chris@16 1866 ctx.get_hooks().evaluated_conditional_expression(directive, is_defined);
Chris@16 1867 #else
Chris@16 1868 do {
Chris@16 1869 is_defined = ctx.is_defined_macro((*it).get_value()); // toexpand.begin(), toexpand.end());
Chris@16 1870 } while (ctx.get_hooks().evaluated_conditional_expression(ctx.derived(),
Chris@16 1871 found_directive, directive, is_defined));
Chris@16 1872 #endif
Chris@16 1873 ctx.enter_if_block(is_defined);
Chris@16 1874 }
Chris@16 1875
Chris@16 1876 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1877 //
Chris@16 1878 // on_ifndef(): handle #ifndef directives
Chris@16 1879 //
Chris@16 1880 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1881 template <typename ContextT>
Chris@16 1882 inline void
Chris@16 1883 pp_iterator_functor<ContextT>::on_ifndef(
Chris@16 1884 result_type const& found_directive, lexer_type const &it)
Chris@16 1885 // typename parse_tree_type::const_iterator const &it)
Chris@16 1886 // typename parse_tree_type::const_iterator const &end)
Chris@16 1887 {
Chris@16 1888 // get_token_value<result_type, parse_node_type> get_value;
Chris@16 1889 // token_sequence_type toexpand;
Chris@16 1890 //
Chris@16 1891 // std::copy(make_ref_transform_iterator((*begin).children.begin(), get_value),
Chris@16 1892 // make_ref_transform_iterator((*begin).children.end(), get_value),
Chris@16 1893 // std::inserter(toexpand, toexpand.end()));
Chris@16 1894
Chris@16 1895 bool is_defined = false;
Chris@16 1896 token_sequence_type directive;
Chris@16 1897
Chris@16 1898 directive.insert(directive.end(), *it);
Chris@16 1899
Chris@16 1900 #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
Chris@16 1901 is_defined = ctx.is_defined_macro((*it).get_value()); // toexpand.begin(), toexpand.end());
Chris@16 1902 ctx.get_hooks().evaluated_conditional_expression(directive, is_defined);
Chris@16 1903 #else
Chris@16 1904 do {
Chris@16 1905 is_defined = ctx.is_defined_macro((*it).get_value()); // toexpand.begin(), toexpand.end());
Chris@16 1906 } while (ctx.get_hooks().evaluated_conditional_expression(ctx.derived(),
Chris@16 1907 found_directive, directive, is_defined));
Chris@16 1908 #endif
Chris@16 1909 ctx.enter_if_block(!is_defined);
Chris@16 1910 }
Chris@16 1911
Chris@16 1912 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1913 //
Chris@16 1914 // on_else(): handle #else directives
Chris@16 1915 //
Chris@16 1916 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1917 template <typename ContextT>
Chris@16 1918 inline void
Chris@16 1919 pp_iterator_functor<ContextT>::on_else()
Chris@16 1920 {
Chris@16 1921 if (!ctx.enter_else_block()) {
Chris@16 1922 // #else without matching #if
Chris@16 1923 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, missing_matching_if,
Chris@16 1924 "#else", act_pos);
Chris@16 1925 }
Chris@16 1926 }
Chris@16 1927
Chris@16 1928 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1929 //
Chris@16 1930 // on_endif(): handle #endif directives
Chris@16 1931 //
Chris@16 1932 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1933 template <typename ContextT>
Chris@16 1934 inline void
Chris@16 1935 pp_iterator_functor<ContextT>::on_endif()
Chris@16 1936 {
Chris@16 1937 if (!ctx.exit_if_block()) {
Chris@16 1938 // #endif without matching #if
Chris@16 1939 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, missing_matching_if,
Chris@16 1940 "#endif", act_pos);
Chris@16 1941 }
Chris@16 1942 }
Chris@16 1943
Chris@16 1944 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1945 // replace all remaining (== undefined) identifiers with an integer literal '0'
Chris@16 1946 template <typename ContextT>
Chris@16 1947 inline void
Chris@16 1948 pp_iterator_functor<ContextT>::replace_undefined_identifiers(
Chris@16 1949 token_sequence_type &expanded)
Chris@16 1950 {
Chris@16 1951 typename token_sequence_type::iterator exp_end = expanded.end();
Chris@16 1952 for (typename token_sequence_type::iterator exp_it = expanded.begin();
Chris@16 1953 exp_it != exp_end; ++exp_it)
Chris@16 1954 {
Chris@16 1955 using namespace boost::wave;
Chris@16 1956
Chris@16 1957 token_id id = token_id(*exp_it);
Chris@16 1958 if (IS_CATEGORY(id, IdentifierTokenType) ||
Chris@16 1959 IS_CATEGORY(id, KeywordTokenType))
Chris@16 1960 {
Chris@16 1961 (*exp_it).set_token_id(T_INTLIT);
Chris@16 1962 (*exp_it).set_value("0");
Chris@16 1963 }
Chris@16 1964 }
Chris@16 1965 }
Chris@16 1966
Chris@16 1967 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1968 //
Chris@16 1969 // on_if(): handle #if directives
Chris@16 1970 //
Chris@16 1971 ///////////////////////////////////////////////////////////////////////////////
Chris@16 1972 template <typename ContextT>
Chris@16 1973 inline void
Chris@16 1974 pp_iterator_functor<ContextT>::on_if(
Chris@16 1975 result_type const& found_directive,
Chris@16 1976 typename parse_tree_type::const_iterator const &begin,
Chris@16 1977 typename parse_tree_type::const_iterator const &end)
Chris@16 1978 {
Chris@16 1979 // preprocess the given sequence into the provided list
Chris@16 1980 get_token_value<result_type, parse_node_type> get_value;
Chris@16 1981 token_sequence_type toexpand;
Chris@16 1982
Chris@16 1983 std::copy(make_ref_transform_iterator(begin, get_value),
Chris@16 1984 make_ref_transform_iterator(end, get_value),
Chris@16 1985 std::inserter(toexpand, toexpand.end()));
Chris@16 1986
Chris@16 1987 impl::remove_leading_whitespace(ctx, toexpand);
Chris@16 1988
Chris@16 1989 bool if_status = false;
Chris@16 1990 grammars::value_error status = grammars::error_noerror;
Chris@16 1991 token_sequence_type expanded;
Chris@16 1992
Chris@16 1993 do {
Chris@16 1994 expanded.clear();
Chris@16 1995
Chris@16 1996 typename token_sequence_type::iterator begin2 = toexpand.begin();
Chris@16 1997 ctx.expand_whole_tokensequence(begin2, toexpand.end(), expanded);
Chris@16 1998
Chris@16 1999 // replace all remaining (== undefined) identifiers with an integer literal '0'
Chris@16 2000 replace_undefined_identifiers(expanded);
Chris@16 2001
Chris@16 2002 #if BOOST_WAVE_DUMP_CONDITIONAL_EXPRESSIONS != 0
Chris@16 2003 {
Chris@16 2004 string_type outstr(boost::wave::util::impl::as_string(toexpand));
Chris@16 2005 outstr += "(" + boost::wave::util::impl::as_string(expanded) + ")";
Chris@16 2006 BOOST_WAVE_DUMP_CONDITIONAL_EXPRESSIONS_OUT << "#if " << outstr
Chris@16 2007 << std::endl;
Chris@16 2008 }
Chris@16 2009 #endif
Chris@16 2010 try {
Chris@16 2011 // parse the expression and enter the #if block
Chris@16 2012 if_status = grammars::expression_grammar_gen<result_type>::
Chris@16 2013 evaluate(expanded.begin(), expanded.end(), act_pos,
Chris@16 2014 ctx.get_if_block_status(), status);
Chris@16 2015 }
Chris@16 2016 catch (boost::wave::preprocess_exception const& e) {
Chris@16 2017 // any errors occurred have to be dispatched to the context hooks
Chris@16 2018 ctx.get_hooks().throw_exception(ctx.derived(), e);
Chris@16 2019 break;
Chris@16 2020 }
Chris@16 2021
Chris@16 2022 #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
Chris@16 2023 ctx.get_hooks().evaluated_conditional_expression(toexpand, if_status);
Chris@16 2024 } while (false);
Chris@16 2025 #else
Chris@16 2026 } while (ctx.get_hooks().evaluated_conditional_expression(ctx.derived(),
Chris@16 2027 found_directive, toexpand, if_status)
Chris@16 2028 && status == grammars::error_noerror);
Chris@16 2029 #endif
Chris@16 2030
Chris@16 2031 ctx.enter_if_block(if_status);
Chris@16 2032 if (grammars::error_noerror != status) {
Chris@16 2033 // division or other error by zero occurred
Chris@16 2034 string_type expression = util::impl::as_string(expanded);
Chris@16 2035 if (0 == expression.size())
Chris@16 2036 expression = "<empty expression>";
Chris@16 2037
Chris@16 2038 if (grammars::error_division_by_zero & status) {
Chris@16 2039 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, division_by_zero,
Chris@16 2040 expression.c_str(), act_pos);
Chris@16 2041 }
Chris@16 2042 else if (grammars::error_integer_overflow & status) {
Chris@16 2043 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, integer_overflow,
Chris@16 2044 expression.c_str(), act_pos);
Chris@16 2045 }
Chris@16 2046 else if (grammars::error_character_overflow & status) {
Chris@16 2047 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
Chris@16 2048 character_literal_out_of_range, expression.c_str(), act_pos);
Chris@16 2049 }
Chris@16 2050 }
Chris@16 2051 }
Chris@16 2052
Chris@16 2053 ///////////////////////////////////////////////////////////////////////////////
Chris@16 2054 //
Chris@16 2055 // on_elif(): handle #elif directives
Chris@16 2056 //
Chris@16 2057 ///////////////////////////////////////////////////////////////////////////////
Chris@16 2058 template <typename ContextT>
Chris@16 2059 inline void
Chris@16 2060 pp_iterator_functor<ContextT>::on_elif(
Chris@16 2061 result_type const& found_directive,
Chris@16 2062 typename parse_tree_type::const_iterator const &begin,
Chris@16 2063 typename parse_tree_type::const_iterator const &end)
Chris@16 2064 {
Chris@16 2065 // preprocess the given sequence into the provided list
Chris@16 2066 get_token_value<result_type, parse_node_type> get_value;
Chris@16 2067 token_sequence_type toexpand;
Chris@16 2068
Chris@16 2069 std::copy(make_ref_transform_iterator(begin, get_value),
Chris@16 2070 make_ref_transform_iterator(end, get_value),
Chris@16 2071 std::inserter(toexpand, toexpand.end()));
Chris@16 2072
Chris@16 2073 impl::remove_leading_whitespace(ctx, toexpand);
Chris@16 2074
Chris@16 2075 // check current if block status
Chris@16 2076 if (ctx.get_if_block_some_part_status()) {
Chris@16 2077 if (!ctx.enter_elif_block(false)) {
Chris@16 2078 // #else without matching #if
Chris@16 2079 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
Chris@16 2080 missing_matching_if, "#elif", act_pos);
Chris@16 2081 // fall through...
Chris@16 2082 }
Chris@16 2083
Chris@16 2084 // skip all the expression and the trailing whitespace
Chris@16 2085 typename token_sequence_type::iterator begin2 = toexpand.begin();
Chris@16 2086
Chris@16 2087 impl::skip_to_eol(ctx, begin2, toexpand.end());
Chris@16 2088 return; // one of previous #if/#elif was true, so don't enter this #elif
Chris@16 2089 }
Chris@16 2090
Chris@16 2091 // preprocess the given sequence into the provided list
Chris@16 2092 bool if_status = false;
Chris@16 2093 grammars::value_error status = grammars::error_noerror;
Chris@16 2094 token_sequence_type expanded;
Chris@16 2095
Chris@16 2096 do {
Chris@16 2097 expanded.clear();
Chris@16 2098
Chris@16 2099 typename token_sequence_type::iterator begin2 = toexpand.begin();
Chris@16 2100 ctx.expand_whole_tokensequence(begin2, toexpand.end(), expanded);
Chris@16 2101
Chris@16 2102 // replace all remaining (== undefined) identifiers with an integer literal '0'
Chris@16 2103 replace_undefined_identifiers(expanded);
Chris@16 2104
Chris@16 2105 #if BOOST_WAVE_DUMP_CONDITIONAL_EXPRESSIONS != 0
Chris@16 2106 {
Chris@16 2107 string_type outstr(boost::wave::util::impl::as_string(toexpand));
Chris@16 2108 outstr += "(" + boost::wave::util::impl::as_string(expanded) + ")";
Chris@16 2109 BOOST_WAVE_DUMP_CONDITIONAL_EXPRESSIONS_OUT << "#elif " << outstr << std::endl;
Chris@16 2110 }
Chris@16 2111 #endif
Chris@16 2112
Chris@16 2113 try {
Chris@16 2114 // parse the expression and enter the #elif block
Chris@16 2115 if_status = grammars::expression_grammar_gen<result_type>::
Chris@16 2116 evaluate(expanded.begin(), expanded.end(), act_pos,
Chris@16 2117 ctx.get_if_block_status(), status);
Chris@16 2118 }
Chris@16 2119 catch (boost::wave::preprocess_exception const& e) {
Chris@16 2120 // any errors occurred have to be dispatched to the context hooks
Chris@16 2121 ctx.get_hooks().throw_exception(ctx.derived(), e);
Chris@16 2122 }
Chris@16 2123
Chris@16 2124 #if BOOST_WAVE_USE_DEPRECIATED_PREPROCESSING_HOOKS != 0
Chris@16 2125 ctx.get_hooks().evaluated_conditional_expression(toexpand, if_status);
Chris@16 2126 } while (false);
Chris@16 2127 #else
Chris@16 2128 } while (ctx.get_hooks().evaluated_conditional_expression(ctx.derived(),
Chris@16 2129 found_directive, toexpand, if_status)
Chris@16 2130 && status == grammars::error_noerror);
Chris@16 2131 #endif
Chris@16 2132
Chris@16 2133 if (!ctx.enter_elif_block(if_status)) {
Chris@16 2134 // #elif without matching #if
Chris@16 2135 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, missing_matching_if,
Chris@16 2136 "#elif", act_pos);
Chris@16 2137 return;
Chris@16 2138 }
Chris@16 2139
Chris@16 2140 if (grammars::error_noerror != status) {
Chris@16 2141 // division or other error by zero occurred
Chris@16 2142 string_type expression = util::impl::as_string(expanded);
Chris@16 2143 if (0 == expression.size())
Chris@16 2144 expression = "<empty expression>";
Chris@16 2145
Chris@16 2146 if (grammars::error_division_by_zero & status) {
Chris@16 2147 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, division_by_zero,
Chris@16 2148 expression.c_str(), act_pos);
Chris@16 2149 }
Chris@16 2150 else if (grammars::error_integer_overflow & status) {
Chris@16 2151 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
Chris@16 2152 integer_overflow, expression.c_str(), act_pos);
Chris@16 2153 }
Chris@16 2154 else if (grammars::error_character_overflow & status) {
Chris@16 2155 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception,
Chris@16 2156 character_literal_out_of_range, expression.c_str(), act_pos);
Chris@16 2157 }
Chris@16 2158 }
Chris@16 2159 }
Chris@16 2160
Chris@16 2161 ///////////////////////////////////////////////////////////////////////////////
Chris@16 2162 //
Chris@16 2163 // on_illformed(): handles the illegal directive
Chris@16 2164 //
Chris@16 2165 ///////////////////////////////////////////////////////////////////////////////
Chris@16 2166 template <typename ContextT>
Chris@16 2167 inline void
Chris@16 2168 pp_iterator_functor<ContextT>::on_illformed(
Chris@16 2169 typename result_type::string_type s)
Chris@16 2170 {
Chris@16 2171 BOOST_ASSERT(ctx.get_if_block_status());
Chris@16 2172
Chris@16 2173 // some messages have more than one newline at the end
Chris@16 2174 typename string_type::size_type p = s.find_last_not_of('\n');
Chris@16 2175 if (string_type::npos != p)
Chris@16 2176 s = s.substr(0, p+1);
Chris@16 2177
Chris@16 2178 // throw the exception
Chris@16 2179 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, ill_formed_directive,
Chris@16 2180 s.c_str(), act_pos);
Chris@16 2181 }
Chris@16 2182
Chris@16 2183 ///////////////////////////////////////////////////////////////////////////////
Chris@16 2184 //
Chris@16 2185 // on_line(): handle #line directives
Chris@16 2186 //
Chris@16 2187 ///////////////////////////////////////////////////////////////////////////////
Chris@16 2188
Chris@16 2189 namespace impl {
Chris@16 2190
Chris@16 2191 template <typename IteratorT, typename StringT>
Chris@16 2192 bool retrieve_line_info (IteratorT first, IteratorT const &last,
Chris@16 2193 unsigned int &line, StringT &file,
Chris@16 2194 boost::wave::preprocess_exception::error_code& error)
Chris@16 2195 {
Chris@16 2196 using namespace boost::wave;
Chris@16 2197 token_id id = token_id(*first);
Chris@16 2198 if (T_PP_NUMBER == id || T_INTLIT == id) {
Chris@16 2199 // extract line number
Chris@16 2200 using namespace std; // some systems have atoi in namespace std
Chris@16 2201 line = (unsigned int)atoi((*first).get_value().c_str());
Chris@16 2202 if (0 == line)
Chris@16 2203 error = preprocess_exception::bad_line_number;
Chris@16 2204
Chris@16 2205 // re-extract line number with spirit to diagnose overflow
Chris@16 2206 using namespace boost::spirit::classic;
Chris@16 2207 if (!parse((*first).get_value().c_str(), int_p).full)
Chris@16 2208 error = preprocess_exception::bad_line_number;
Chris@16 2209
Chris@16 2210 // extract file name (if it is given)
Chris@16 2211 while (++first != last && IS_CATEGORY(*first, WhiteSpaceTokenType))
Chris@16 2212 /**/; // skip whitespace
Chris@16 2213
Chris@16 2214 if (first != last) {
Chris@16 2215 if (T_STRINGLIT != token_id(*first)) {
Chris@16 2216 error = preprocess_exception::bad_line_filename;
Chris@16 2217 return false;
Chris@16 2218 }
Chris@16 2219
Chris@16 2220 StringT const &file_lit = (*first).get_value();
Chris@16 2221
Chris@16 2222 if ('L' == file_lit[0]) {
Chris@16 2223 error = preprocess_exception::bad_line_filename;
Chris@16 2224 return false; // shouldn't be a wide character string
Chris@16 2225 }
Chris@16 2226
Chris@16 2227 file = file_lit.substr(1, file_lit.size()-2);
Chris@16 2228
Chris@16 2229 // test if there is other junk on this line
Chris@16 2230 while (++first != last && IS_CATEGORY(*first, WhiteSpaceTokenType))
Chris@16 2231 /**/; // skip whitespace
Chris@16 2232 }
Chris@16 2233 return first == last;
Chris@16 2234 }
Chris@16 2235 error = preprocess_exception::bad_line_statement;
Chris@16 2236 return false;
Chris@16 2237 }
Chris@16 2238 }
Chris@16 2239
Chris@16 2240 template <typename ContextT>
Chris@16 2241 inline void
Chris@16 2242 pp_iterator_functor<ContextT>::on_line(
Chris@16 2243 typename parse_tree_type::const_iterator const &begin,
Chris@16 2244 typename parse_tree_type::const_iterator const &end)
Chris@16 2245 {
Chris@16 2246 BOOST_ASSERT(ctx.get_if_block_status());
Chris@16 2247
Chris@16 2248 // Try to extract the line number and file name from the given token list
Chris@16 2249 // directly. If that fails, preprocess the whole token sequence and try again
Chris@16 2250 // to extract this information.
Chris@16 2251 token_sequence_type expanded;
Chris@16 2252 get_token_value<result_type, parse_node_type> get_value;
Chris@16 2253
Chris@16 2254 typedef typename ref_transform_iterator_generator<
Chris@16 2255 get_token_value<result_type, parse_node_type>,
Chris@16 2256 typename parse_tree_type::const_iterator
Chris@16 2257 >::type const_tree_iterator_t;
Chris@16 2258
Chris@16 2259 const_tree_iterator_t first = make_ref_transform_iterator(begin, get_value);
Chris@16 2260 const_tree_iterator_t last = make_ref_transform_iterator(end, get_value);
Chris@16 2261
Chris@16 2262 // try to interpret the #line body as a number followed by an optional
Chris@16 2263 // string literal
Chris@16 2264 unsigned int line = 0;
Chris@16 2265 preprocess_exception::error_code error = preprocess_exception::no_error;
Chris@16 2266 string_type file_name;
Chris@16 2267 token_sequence_type toexpand;
Chris@16 2268
Chris@16 2269 std::copy(first, last, std::inserter(toexpand, toexpand.end()));
Chris@16 2270 if (!impl::retrieve_line_info(first, last, line, file_name, error)) {
Chris@16 2271 // preprocess the body of this #line message
Chris@16 2272 typename token_sequence_type::iterator begin2 = toexpand.begin();
Chris@16 2273 ctx.expand_whole_tokensequence(begin2, toexpand.end(),
Chris@16 2274 expanded, false);
Chris@16 2275
Chris@16 2276 error = preprocess_exception::no_error;
Chris@16 2277 if (!impl::retrieve_line_info(expanded.begin(), expanded.end(),
Chris@16 2278 line, file_name, error))
Chris@16 2279 {
Chris@16 2280 typename ContextT::string_type msg(
Chris@16 2281 boost::wave::util::impl::as_string(expanded));
Chris@16 2282 BOOST_WAVE_THROW_VAR_CTX(ctx, preprocess_exception, error,
Chris@16 2283 msg.c_str(), act_pos);
Chris@16 2284 return;
Chris@16 2285 }
Chris@16 2286
Chris@16 2287 // call the corresponding pp hook function
Chris@16 2288 ctx.get_hooks().found_line_directive(ctx.derived(), expanded, line,
Chris@16 2289 file_name.c_str());
Chris@16 2290 }
Chris@16 2291 else {
Chris@16 2292 // call the corresponding pp hook function
Chris@16 2293 ctx.get_hooks().found_line_directive(ctx.derived(), toexpand, line,
Chris@16 2294 file_name.c_str());
Chris@16 2295 }
Chris@16 2296
Chris@16 2297 // the queues should be empty at this point
Chris@16 2298 BOOST_ASSERT(unput_queue.empty());
Chris@16 2299 BOOST_ASSERT(pending_queue.empty());
Chris@16 2300
Chris@16 2301 // make sure error recovery starts on the next line
Chris@16 2302 must_emit_line_directive = true;
Chris@16 2303
Chris@16 2304 // diagnose possible error in detected line directive
Chris@16 2305 if (error != preprocess_exception::no_error) {
Chris@16 2306 typename ContextT::string_type msg(
Chris@16 2307 boost::wave::util::impl::as_string(expanded));
Chris@16 2308 BOOST_WAVE_THROW_VAR_CTX(ctx, preprocess_exception, error,
Chris@16 2309 msg.c_str(), act_pos);
Chris@16 2310 return;
Chris@16 2311 }
Chris@16 2312
Chris@16 2313 // set new line number/filename only if ok
Chris@16 2314 if (!file_name.empty()) { // reuse current file name
Chris@16 2315 using boost::wave::util::impl::unescape_lit;
Chris@16 2316 act_pos.set_file(unescape_lit(file_name).c_str());
Chris@16 2317 }
Chris@16 2318 act_pos.set_line(line);
Chris@16 2319 iter_ctx->first.set_position(act_pos);
Chris@16 2320 }
Chris@16 2321
Chris@16 2322 ///////////////////////////////////////////////////////////////////////////////
Chris@16 2323 //
Chris@16 2324 // on_error(): handle #error directives
Chris@16 2325 //
Chris@16 2326 ///////////////////////////////////////////////////////////////////////////////
Chris@16 2327 template <typename ContextT>
Chris@16 2328 inline void
Chris@16 2329 pp_iterator_functor<ContextT>::on_error(
Chris@16 2330 typename parse_tree_type::const_iterator const &begin,
Chris@16 2331 typename parse_tree_type::const_iterator const &end)
Chris@16 2332 {
Chris@16 2333 BOOST_ASSERT(ctx.get_if_block_status());
Chris@16 2334
Chris@16 2335 // preprocess the given sequence into the provided list
Chris@16 2336 token_sequence_type expanded;
Chris@16 2337 get_token_value<result_type, parse_node_type> get_value;
Chris@16 2338
Chris@16 2339 typename ref_transform_iterator_generator<
Chris@16 2340 get_token_value<result_type, parse_node_type>,
Chris@16 2341 typename parse_tree_type::const_iterator
Chris@16 2342 >::type first = make_ref_transform_iterator(begin, get_value);
Chris@16 2343
Chris@16 2344 #if BOOST_WAVE_PREPROCESS_ERROR_MESSAGE_BODY != 0
Chris@16 2345 // preprocess the body of this #error message
Chris@16 2346 token_sequence_type toexpand;
Chris@16 2347
Chris@16 2348 std::copy(first, make_ref_transform_iterator(end, get_value),
Chris@16 2349 std::inserter(toexpand, toexpand.end()));
Chris@16 2350
Chris@16 2351 typename token_sequence_type::iterator begin2 = toexpand.begin();
Chris@16 2352 ctx.expand_whole_tokensequence(begin2, toexpand.end(), expanded,
Chris@16 2353 false);
Chris@16 2354 if (!ctx.get_hooks().found_error_directive(ctx.derived(), toexpand))
Chris@16 2355 #else
Chris@16 2356 // simply copy the body of this #error message to the issued diagnostic
Chris@16 2357 // message
Chris@16 2358 std::copy(first, make_ref_transform_iterator(end, get_value),
Chris@16 2359 std::inserter(expanded, expanded.end()));
Chris@16 2360 if (!ctx.get_hooks().found_error_directive(ctx.derived(), expanded))
Chris@16 2361 #endif
Chris@16 2362 {
Chris@16 2363 // report the corresponding error
Chris@16 2364 BOOST_WAVE_STRINGTYPE msg(boost::wave::util::impl::as_string(expanded));
Chris@16 2365 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, error_directive,
Chris@16 2366 msg.c_str(), act_pos);
Chris@16 2367 }
Chris@16 2368 }
Chris@16 2369
Chris@16 2370 #if BOOST_WAVE_SUPPORT_WARNING_DIRECTIVE != 0
Chris@16 2371 ///////////////////////////////////////////////////////////////////////////////
Chris@16 2372 //
Chris@16 2373 // on_warning(): handle #warning directives
Chris@16 2374 //
Chris@16 2375 ///////////////////////////////////////////////////////////////////////////////
Chris@16 2376 template <typename ContextT>
Chris@16 2377 inline void
Chris@16 2378 pp_iterator_functor<ContextT>::on_warning(
Chris@16 2379 typename parse_tree_type::const_iterator const &begin,
Chris@16 2380 typename parse_tree_type::const_iterator const &end)
Chris@16 2381 {
Chris@16 2382 BOOST_ASSERT(ctx.get_if_block_status());
Chris@16 2383
Chris@16 2384 // preprocess the given sequence into the provided list
Chris@16 2385 token_sequence_type expanded;
Chris@16 2386 get_token_value<result_type, parse_node_type> get_value;
Chris@16 2387
Chris@16 2388 typename ref_transform_iterator_generator<
Chris@16 2389 get_token_value<result_type, parse_node_type>,
Chris@16 2390 typename parse_tree_type::const_iterator
Chris@16 2391 >::type first = make_ref_transform_iterator(begin, get_value);
Chris@16 2392
Chris@16 2393 #if BOOST_WAVE_PREPROCESS_ERROR_MESSAGE_BODY != 0
Chris@16 2394 // preprocess the body of this #warning message
Chris@16 2395 token_sequence_type toexpand;
Chris@16 2396
Chris@16 2397 std::copy(first, make_ref_transform_iterator(end, get_value),
Chris@16 2398 std::inserter(toexpand, toexpand.end()));
Chris@16 2399
Chris@16 2400 typename token_sequence_type::iterator begin2 = toexpand.begin();
Chris@16 2401 ctx.expand_whole_tokensequence(begin2, toexpand.end(), expanded,
Chris@16 2402 false);
Chris@16 2403 if (!ctx.get_hooks().found_warning_directive(ctx.derived(), toexpand))
Chris@16 2404 #else
Chris@16 2405 // simply copy the body of this #warning message to the issued diagnostic
Chris@16 2406 // message
Chris@16 2407 std::copy(first, make_ref_transform_iterator(end, get_value),
Chris@16 2408 std::inserter(expanded, expanded.end()));
Chris@16 2409 if (!ctx.get_hooks().found_warning_directive(ctx.derived(), expanded))
Chris@16 2410 #endif
Chris@16 2411 {
Chris@16 2412 // report the corresponding error
Chris@16 2413 BOOST_WAVE_STRINGTYPE msg(boost::wave::util::impl::as_string(expanded));
Chris@16 2414 BOOST_WAVE_THROW_CTX(ctx, preprocess_exception, warning_directive,
Chris@16 2415 msg.c_str(), act_pos);
Chris@16 2416 }
Chris@16 2417 }
Chris@16 2418 #endif // BOOST_WAVE_SUPPORT_WARNING_DIRECTIVE != 0
Chris@16 2419
Chris@16 2420 ///////////////////////////////////////////////////////////////////////////////
Chris@16 2421 //
Chris@16 2422 // on_pragma(): handle #pragma directives
Chris@16 2423 //
Chris@16 2424 ///////////////////////////////////////////////////////////////////////////////
Chris@16 2425 template <typename ContextT>
Chris@16 2426 inline bool
Chris@16 2427 pp_iterator_functor<ContextT>::on_pragma(
Chris@16 2428 typename parse_tree_type::const_iterator const &begin,
Chris@16 2429 typename parse_tree_type::const_iterator const &end)
Chris@16 2430 {
Chris@16 2431 using namespace boost::wave;
Chris@16 2432
Chris@16 2433 BOOST_ASSERT(ctx.get_if_block_status());
Chris@16 2434
Chris@16 2435 // Look at the pragma token sequence and decide, if the first token is STDC
Chris@16 2436 // (see C99 standard [6.10.6.2]), if it is, the sequence must _not_ be
Chris@16 2437 // preprocessed.
Chris@16 2438 token_sequence_type expanded;
Chris@16 2439 get_token_value<result_type, parse_node_type> get_value;
Chris@16 2440
Chris@16 2441 typedef typename ref_transform_iterator_generator<
Chris@16 2442 get_token_value<result_type, parse_node_type>,
Chris@16 2443 typename parse_tree_type::const_iterator
Chris@16 2444 >::type const_tree_iterator_t;
Chris@16 2445
Chris@16 2446 const_tree_iterator_t first = make_ref_transform_iterator(begin, get_value);
Chris@16 2447 const_tree_iterator_t last = make_ref_transform_iterator(end, get_value);
Chris@16 2448
Chris@16 2449 expanded.push_back(result_type(T_PP_PRAGMA, "#pragma", act_token.get_position()));
Chris@16 2450 expanded.push_back(result_type(T_SPACE, " ", act_token.get_position()));
Chris@16 2451
Chris@16 2452 while (++first != last && IS_CATEGORY(*first, WhiteSpaceTokenType))
Chris@16 2453 expanded.push_back(*first); // skip whitespace
Chris@16 2454
Chris@16 2455 if (first != last) {
Chris@16 2456 if (T_IDENTIFIER == token_id(*first) &&
Chris@16 2457 boost::wave::need_c99(ctx.get_language()) &&
Chris@16 2458 (*first).get_value() == "STDC")
Chris@16 2459 {
Chris@16 2460 // do _not_ preprocess the token sequence
Chris@16 2461 std::copy(first, last, std::inserter(expanded, expanded.end()));
Chris@16 2462 }
Chris@16 2463 else {
Chris@16 2464 #if BOOST_WAVE_PREPROCESS_PRAGMA_BODY != 0
Chris@16 2465 // preprocess the given tokensequence
Chris@16 2466 token_sequence_type toexpand;
Chris@16 2467
Chris@16 2468 std::copy(first, last, std::inserter(toexpand, toexpand.end()));
Chris@16 2469
Chris@16 2470 typename token_sequence_type::iterator begin2 = toexpand.begin();
Chris@16 2471 ctx.expand_whole_tokensequence(begin2, toexpand.end(),
Chris@16 2472 expanded, false);
Chris@16 2473 #else
Chris@16 2474 // do _not_ preprocess the token sequence
Chris@16 2475 std::copy(first, last, std::inserter(expanded, expanded.end()));
Chris@16 2476 #endif
Chris@16 2477 }
Chris@16 2478 }
Chris@16 2479 expanded.push_back(result_type(T_NEWLINE, "\n", act_token.get_position()));
Chris@16 2480
Chris@16 2481 // the queues should be empty at this point
Chris@16 2482 BOOST_ASSERT(unput_queue.empty());
Chris@16 2483 BOOST_ASSERT(pending_queue.empty());
Chris@16 2484
Chris@16 2485 // try to interpret the expanded #pragma body
Chris@16 2486 token_sequence_type pending;
Chris@16 2487 if (interpret_pragma(expanded, pending)) {
Chris@16 2488 // if there is some replacement text, insert it into the pending queue
Chris@16 2489 if (!pending.empty())
Chris@16 2490 pending_queue.splice(pending_queue.begin(), pending);
Chris@16 2491 return true; // this #pragma was successfully recognized
Chris@16 2492 }
Chris@16 2493
Chris@16 2494 #if BOOST_WAVE_EMIT_PRAGMA_DIRECTIVES != 0
Chris@16 2495 // Move the resulting token sequence into the pending_queue, so it will be
Chris@16 2496 // returned to the caller.
Chris@16 2497 if (boost::wave::need_emit_pragma_directives(ctx.get_language())) {
Chris@16 2498 pending_queue.splice(pending_queue.begin(), expanded);
Chris@16 2499 return false; // return the whole #pragma directive
Chris@16 2500 }
Chris@16 2501 #endif
Chris@16 2502 return true; // skip the #pragma at all
Chris@16 2503 }
Chris@16 2504
Chris@16 2505 template <typename ContextT>
Chris@16 2506 inline bool
Chris@16 2507 pp_iterator_functor<ContextT>::interpret_pragma(
Chris@16 2508 token_sequence_type const &pragma_body, token_sequence_type &result)
Chris@16 2509 {
Chris@16 2510 using namespace cpplexer;
Chris@16 2511
Chris@16 2512 typename token_sequence_type::const_iterator end = pragma_body.end();
Chris@16 2513 typename token_sequence_type::const_iterator it = pragma_body.begin();
Chris@16 2514 for (++it; it != end && IS_CATEGORY(*it, WhiteSpaceTokenType); ++it)
Chris@16 2515 /**/; // skip whitespace
Chris@16 2516
Chris@16 2517 if (it == end) // eof reached
Chris@16 2518 return false;
Chris@16 2519
Chris@16 2520 return boost::wave::util::interpret_pragma(
Chris@16 2521 ctx.derived(), act_token, it, end, result);
Chris@16 2522 }
Chris@16 2523
Chris@16 2524 ///////////////////////////////////////////////////////////////////////////////
Chris@16 2525 } // namespace impl
Chris@16 2526
Chris@16 2527 ///////////////////////////////////////////////////////////////////////////////
Chris@16 2528 //
Chris@16 2529 // pp_iterator
Chris@16 2530 //
Chris@16 2531 // The boost::wave::pp_iterator template is the iterator, through which
Chris@16 2532 // the resulting preprocessed input stream is accessible.
Chris@16 2533 //
Chris@16 2534 ///////////////////////////////////////////////////////////////////////////////
Chris@16 2535
Chris@16 2536 template <typename ContextT>
Chris@16 2537 class pp_iterator
Chris@16 2538 : public boost::spirit::classic::multi_pass<
Chris@16 2539 boost::wave::impl::pp_iterator_functor<ContextT>,
Chris@16 2540 boost::wave::util::functor_input
Chris@16 2541 >
Chris@16 2542 {
Chris@16 2543 public:
Chris@16 2544 typedef boost::wave::impl::pp_iterator_functor<ContextT> input_policy_type;
Chris@16 2545
Chris@16 2546 private:
Chris@16 2547 typedef
Chris@16 2548 boost::spirit::classic::multi_pass<input_policy_type, boost::wave::util::functor_input>
Chris@16 2549 base_type;
Chris@16 2550 typedef pp_iterator<ContextT> self_type;
Chris@16 2551 typedef boost::wave::util::functor_input functor_input_type;
Chris@16 2552
Chris@16 2553 public:
Chris@16 2554 pp_iterator()
Chris@16 2555 {}
Chris@16 2556
Chris@16 2557 template <typename IteratorT>
Chris@16 2558 pp_iterator(ContextT &ctx, IteratorT const &first, IteratorT const &last,
Chris@16 2559 typename ContextT::position_type const &pos)
Chris@16 2560 : base_type(input_policy_type(ctx, first, last, pos))
Chris@16 2561 {}
Chris@16 2562
Chris@16 2563 bool force_include(char const *path_, bool is_last)
Chris@16 2564 {
Chris@16 2565 bool result = this->get_functor().on_include_helper(path_, path_,
Chris@16 2566 false, false);
Chris@16 2567 if (is_last) {
Chris@16 2568 this->functor_input_type::
Chris@16 2569 template inner<input_policy_type>::advance_input();
Chris@16 2570 }
Chris@16 2571 return result;
Chris@16 2572 }
Chris@16 2573 };
Chris@16 2574
Chris@16 2575 ///////////////////////////////////////////////////////////////////////////////
Chris@16 2576 } // namespace wave
Chris@16 2577 } // namespace boost
Chris@16 2578
Chris@16 2579 // the suffix header occurs after all of the code
Chris@16 2580 #ifdef BOOST_HAS_ABI_HEADERS
Chris@16 2581 #include BOOST_ABI_SUFFIX
Chris@16 2582 #endif
Chris@16 2583
Chris@16 2584 #endif // !defined(CPP_ITERATOR_HPP_175CA88F_7273_43FA_9039_BCF7459E1F29_INCLUDED)