whitespace_handling.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*=============================================================================
  2. Boost.Wave: A Standard compliant C++ preprocessor library
  3. Whitespace eater
  4. http://www.boost.org/
  5. Copyright (c) 2003 Paul Mensonides
  6. Copyright (c) 2001-2012 Hartmut Kaiser.
  7. Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. =============================================================================*/
  10. #if !defined(BOOST_WHITESPACE_HANDLING_HPP_INCLUDED)
  11. #define BOOST_WHITESPACE_HANDLING_HPP_INCLUDED
  12. #include <boost/wave/wave_config.hpp>
  13. #include <boost/wave/token_ids.hpp>
  14. #include <boost/wave/preprocessing_hooks.hpp>
  15. #include <boost/wave/language_support.hpp>
  16. // this must occur after all of the includes and before any code appears
  17. #ifdef BOOST_HAS_ABI_HEADERS
  18. #include BOOST_ABI_PREFIX
  19. #endif
  20. ///////////////////////////////////////////////////////////////////////////////
  21. namespace boost {
  22. namespace wave {
  23. namespace context_policies {
  24. namespace util {
  25. ///////////////////////////////////////////////////////////////////////////
  26. // This function returns true if the given C style comment contains at
  27. // least one newline
  28. template <typename TokenT>
  29. bool ccomment_has_newline(TokenT const& token)
  30. {
  31. using namespace boost::wave;
  32. if (T_CCOMMENT == token_id(token) &&
  33. TokenT::string_type::npos != token.get_value().find_first_of("\n"))
  34. {
  35. return true;
  36. }
  37. return false;
  38. }
  39. ///////////////////////////////////////////////////////////////////////////
  40. // This function returns the number of newlines in the given C style
  41. // comment
  42. template <typename TokenT>
  43. int ccomment_count_newlines(TokenT const& token)
  44. {
  45. using namespace boost::wave;
  46. int newlines = 0;
  47. if (T_CCOMMENT == token_id(token)) {
  48. typename TokenT::string_type const& value = token.get_value();
  49. typename TokenT::string_type::size_type p = value.find_first_of("\n");
  50. while (TokenT::string_type::npos != p) {
  51. ++newlines;
  52. p = value.find_first_of("\n", p+1);
  53. }
  54. }
  55. return newlines;
  56. }
  57. #if BOOST_WAVE_SUPPORT_CPP0X != 0
  58. ///////////////////////////////////////////////////////////////////////////
  59. // This function returns the number of newlines in the given C++11 style
  60. // raw string
  61. template <typename TokenT>
  62. int rawstring_count_newlines(TokenT const& token)
  63. {
  64. using namespace boost::wave;
  65. int newlines = 0;
  66. if (T_RAWSTRINGLIT == token_id(token)) {
  67. typename TokenT::string_type const& value = token.get_value();
  68. typename TokenT::string_type::size_type p = value.find_first_of("\n");
  69. while (TokenT::string_type::npos != p) {
  70. ++newlines;
  71. p = value.find_first_of("\n", p+1);
  72. }
  73. }
  74. return newlines;
  75. }
  76. #endif
  77. }
  78. ///////////////////////////////////////////////////////////////////////////////
  79. template <typename TokenT>
  80. class eat_whitespace
  81. : public default_preprocessing_hooks
  82. {
  83. public:
  84. eat_whitespace();
  85. template <typename ContextT>
  86. bool may_skip_whitespace(ContextT const& ctx, TokenT &token,
  87. bool &skipped_newline);
  88. template <typename ContextT>
  89. bool may_skip_whitespace(ContextT const& ctx, TokenT &token,
  90. bool preserve_comments_, bool preserve_bol_whitespace_,
  91. bool &skipped_newline);
  92. protected:
  93. bool skip_cppcomment(boost::wave::token_id id)
  94. {
  95. return !preserve_comments && T_CPPCOMMENT == id;
  96. }
  97. private:
  98. typedef bool state_t(TokenT &token, bool &skipped_newline);
  99. state_t eat_whitespace::* state;
  100. state_t general, newline, newline_2nd, whitespace, bol_whitespace;
  101. bool preserve_comments;
  102. bool preserve_bol_whitespace;
  103. };
  104. template <typename TokenT>
  105. inline
  106. eat_whitespace<TokenT>::eat_whitespace()
  107. : state(&eat_whitespace::newline), preserve_comments(false),
  108. preserve_bol_whitespace(false)
  109. {
  110. }
  111. template <typename TokenT>
  112. template <typename ContextT>
  113. inline bool
  114. eat_whitespace<TokenT>::may_skip_whitespace(ContextT const& ctx, TokenT &token,
  115. bool &skipped_newline)
  116. {
  117. // re-initialize the preserve comments state
  118. preserve_comments = boost::wave::need_preserve_comments(ctx.get_language());
  119. return (this->*state)(token, skipped_newline);
  120. }
  121. template <typename TokenT>
  122. template <typename ContextT>
  123. inline bool
  124. eat_whitespace<TokenT>::may_skip_whitespace(ContextT const& ctx, TokenT &token,
  125. bool preserve_comments_, bool preserve_bol_whitespace_,
  126. bool &skipped_newline)
  127. {
  128. // re-initialize the preserve comments state
  129. preserve_comments = preserve_comments_;
  130. preserve_bol_whitespace = preserve_bol_whitespace_;
  131. return (this->*state)(token, skipped_newline);
  132. }
  133. template <typename TokenT>
  134. inline bool
  135. eat_whitespace<TokenT>::general(TokenT &token, bool &skipped_newline)
  136. {
  137. using namespace boost::wave;
  138. token_id id = token_id(token);
  139. if (T_NEWLINE == id || T_CPPCOMMENT == id) {
  140. state = &eat_whitespace::newline;
  141. }
  142. else if (T_SPACE == id || T_SPACE2 == id || T_CCOMMENT == id) {
  143. state = &eat_whitespace::whitespace;
  144. if (util::ccomment_has_newline(token))
  145. skipped_newline = true;
  146. if ((!preserve_comments || T_CCOMMENT != id) &&
  147. token.get_value().size() > 1)
  148. {
  149. token.set_value(" "); // replace with a single space
  150. }
  151. }
  152. else {
  153. state = &eat_whitespace::general;
  154. }
  155. return false;
  156. }
  157. template <typename TokenT>
  158. inline bool
  159. eat_whitespace<TokenT>::newline(TokenT &token, bool &skipped_newline)
  160. {
  161. using namespace boost::wave;
  162. token_id id = token_id(token);
  163. if (T_NEWLINE == id || T_CPPCOMMENT == id) {
  164. skipped_newline = true;
  165. state = &eat_whitespace::newline_2nd;
  166. return T_NEWLINE == id || skip_cppcomment(id);
  167. }
  168. if (T_SPACE != id && T_SPACE2 != id && T_CCOMMENT != id)
  169. return general(token, skipped_newline);
  170. if (T_CCOMMENT == id) {
  171. if (util::ccomment_has_newline(token)) {
  172. skipped_newline = true;
  173. state = &eat_whitespace::newline_2nd;
  174. }
  175. if (preserve_comments) {
  176. state = &eat_whitespace::general;
  177. return false;
  178. }
  179. return true;
  180. }
  181. if (preserve_bol_whitespace) {
  182. state = &eat_whitespace::bol_whitespace;
  183. return false;
  184. }
  185. return true;
  186. }
  187. template <typename TokenT>
  188. inline bool
  189. eat_whitespace<TokenT>::newline_2nd(TokenT &token, bool &skipped_newline)
  190. {
  191. using namespace boost::wave;
  192. token_id id = token_id(token);
  193. if (T_SPACE == id || T_SPACE2 == id) {
  194. if (preserve_bol_whitespace) {
  195. state = &eat_whitespace::bol_whitespace;
  196. return false;
  197. }
  198. return true;
  199. }
  200. if (T_CCOMMENT == id) {
  201. if (util::ccomment_has_newline(token))
  202. skipped_newline = true;
  203. if (preserve_comments) {
  204. state = &eat_whitespace::general;
  205. return false;
  206. }
  207. return true;
  208. }
  209. if (T_NEWLINE != id && T_CPPCOMMENT != id)
  210. return general(token, skipped_newline);
  211. skipped_newline = true;
  212. return T_NEWLINE == id || skip_cppcomment(id);
  213. }
  214. template <typename TokenT>
  215. inline bool
  216. eat_whitespace<TokenT>::bol_whitespace(TokenT &token, bool &skipped_newline)
  217. {
  218. using namespace boost::wave;
  219. token_id id = token_id(token);
  220. if (T_SPACE == id || T_SPACE2 == id)
  221. return !preserve_bol_whitespace;
  222. return general(token, skipped_newline);
  223. }
  224. template <typename TokenT>
  225. inline bool
  226. eat_whitespace<TokenT>::whitespace(TokenT &token, bool &skipped_newline)
  227. {
  228. using namespace boost::wave;
  229. token_id id = token_id(token);
  230. if (T_SPACE != id && T_SPACE2 != id &&
  231. T_CCOMMENT != id && T_CPPCOMMENT != id)
  232. {
  233. return general(token, skipped_newline);
  234. }
  235. if (T_CCOMMENT == id) {
  236. if (util::ccomment_has_newline(token))
  237. skipped_newline = true;
  238. return !preserve_comments;
  239. }
  240. return T_SPACE == id || T_SPACE2 == id || skip_cppcomment(id);
  241. }
  242. ///////////////////////////////////////////////////////////////////////////////
  243. } // namespace context_policies
  244. } // namespace wave
  245. } // namespace boost
  246. // the suffix header occurs after all of the code
  247. #ifdef BOOST_HAS_ABI_HEADERS
  248. #include BOOST_ABI_SUFFIX
  249. #endif
  250. #endif // !defined(BOOST_WHITESPACE_HANDLING_HPP_INCLUDED)