1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ossrv_pub/boost_apis/boost/wave/whitespace_handling.hpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,228 @@
1.4 +/*=============================================================================
1.5 + Boost.Wave: A Standard compliant C++ preprocessor library
1.6 + Whitespace eater
1.7 +
1.8 + http://www.boost.org/
1.9 +
1.10 + Copyright (c) 2003 Paul Mensonides
1.11 + Copyright (c) 2001-2007 Hartmut Kaiser.
1.12 + Distributed under the Boost Software License, Version 1.0. (See accompanying
1.13 + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
1.14 +=============================================================================*/
1.15 +
1.16 +#if !defined(WHITESPACE_HANDLING_HPP_INCLUDED)
1.17 +#define WHITESPACE_HANDLING_HPP_INCLUDED
1.18 +
1.19 +#include <boost/wave/wave_config.hpp>
1.20 +#include <boost/wave/token_ids.hpp>
1.21 +#include <boost/wave/preprocessing_hooks.hpp>
1.22 +
1.23 +// this must occur after all of the includes and before any code appears
1.24 +#ifdef BOOST_HAS_ABI_HEADERS
1.25 +#include BOOST_ABI_PREFIX
1.26 +#endif
1.27 +
1.28 +///////////////////////////////////////////////////////////////////////////////
1.29 +namespace boost {
1.30 +namespace wave {
1.31 +namespace context_policies {
1.32 +
1.33 +namespace util {
1.34 + ///////////////////////////////////////////////////////////////////////////
1.35 + // This function returns true if the given C style comment contains at
1.36 + // least one newline
1.37 + template <typename TokenT>
1.38 + bool ccomment_has_newline(TokenT const& token)
1.39 + {
1.40 + using namespace boost::wave;
1.41 +
1.42 + if (T_CCOMMENT == token_id(token) &&
1.43 + TokenT::string_type::npos != token.get_value().find_first_of("\n"))
1.44 + {
1.45 + return true;
1.46 + }
1.47 + return false;
1.48 + }
1.49 +
1.50 + ///////////////////////////////////////////////////////////////////////////
1.51 + // This function returns the number of newlines in the given C style
1.52 + // comment
1.53 + template <typename TokenT>
1.54 + int ccomment_count_newlines(TokenT const& token)
1.55 + {
1.56 + using namespace boost::wave;
1.57 + int newlines = 0;
1.58 + if (T_CCOMMENT == token_id(token)) {
1.59 + typename TokenT::string_type const& value = token.get_value();
1.60 + typename TokenT::string_type::size_type p = value.find_first_of("\n");
1.61 +
1.62 + while (TokenT::string_type::npos != p) {
1.63 + ++newlines;
1.64 + p = value.find_first_of("\n", p+1);
1.65 + }
1.66 + }
1.67 + return newlines;
1.68 + }
1.69 +}
1.70 +
1.71 +///////////////////////////////////////////////////////////////////////////////
1.72 +template <typename TokenT>
1.73 +class eat_whitespace
1.74 +: public default_preprocessing_hooks
1.75 +{
1.76 +public:
1.77 + eat_whitespace();
1.78 +
1.79 + template <typename ContextT>
1.80 + bool may_skip_whitespace(ContextT const& ctx, TokenT &token,
1.81 + bool &skipped_newline);
1.82 +
1.83 +protected:
1.84 + bool skip_cppcomment(boost::wave::token_id id)
1.85 + {
1.86 + return !preserve_comments && T_CPPCOMMENT == id;
1.87 + }
1.88 +
1.89 +private:
1.90 + typedef bool state_t(TokenT &token, bool &skipped_newline);
1.91 + state_t eat_whitespace::* state;
1.92 + state_t general, newline, newline_2nd, whitespace;
1.93 + bool preserve_comments;
1.94 +};
1.95 +
1.96 +template <typename TokenT>
1.97 +inline
1.98 +eat_whitespace<TokenT>::eat_whitespace()
1.99 +: state(&eat_whitespace::newline), preserve_comments(false)
1.100 +{
1.101 +}
1.102 +
1.103 +template <typename TokenT>
1.104 +template <typename ContextT>
1.105 +inline bool
1.106 +eat_whitespace<TokenT>::may_skip_whitespace(ContextT const& ctx, TokenT &token,
1.107 + bool &skipped_newline)
1.108 +{
1.109 + // re-initialize the preserve comments state
1.110 + preserve_comments = boost::wave::need_preserve_comments(ctx.get_language());
1.111 + return (this->*state)(token, skipped_newline);
1.112 +}
1.113 +
1.114 +template <typename TokenT>
1.115 +inline bool
1.116 +eat_whitespace<TokenT>::general(TokenT &token, bool &skipped_newline)
1.117 +{
1.118 + using namespace boost::wave;
1.119 +
1.120 + token_id id = token_id(token);
1.121 + if (T_NEWLINE == id || T_CPPCOMMENT == id) {
1.122 + state = &eat_whitespace::newline;
1.123 + }
1.124 + else if (T_SPACE == id || T_SPACE2 == id || T_CCOMMENT == id) {
1.125 + state = &eat_whitespace::whitespace;
1.126 +
1.127 + if (util::ccomment_has_newline(token))
1.128 + skipped_newline = true;
1.129 +
1.130 + if ((!preserve_comments || T_CCOMMENT != id) &&
1.131 + token.get_value().size() > 1)
1.132 + {
1.133 + token.set_value(" "); // replace with a single space
1.134 + }
1.135 + }
1.136 + else {
1.137 + state = &eat_whitespace::general;
1.138 + }
1.139 + return false;
1.140 +}
1.141 +
1.142 +template <typename TokenT>
1.143 +inline bool
1.144 +eat_whitespace<TokenT>::newline(TokenT &token, bool &skipped_newline)
1.145 +{
1.146 + using namespace boost::wave;
1.147 +
1.148 + token_id id = token_id(token);
1.149 + if (T_NEWLINE == id || T_CPPCOMMENT == id) {
1.150 + skipped_newline = true;
1.151 + state = &eat_whitespace::newline_2nd;
1.152 + return T_NEWLINE == id || skip_cppcomment(id);
1.153 + }
1.154 + else if (T_SPACE != id && T_SPACE2 != id && T_CCOMMENT != id) {
1.155 + return general(token, skipped_newline);
1.156 + }
1.157 +
1.158 + if (T_CCOMMENT == id) {
1.159 + if (util::ccomment_has_newline(token)) {
1.160 + skipped_newline = true;
1.161 + state = &eat_whitespace::newline_2nd;
1.162 + }
1.163 + if (preserve_comments) {
1.164 + state = &eat_whitespace::general;
1.165 + return false;
1.166 + }
1.167 + // fall through...
1.168 + }
1.169 + return true;
1.170 +}
1.171 +
1.172 +template <typename TokenT>
1.173 +inline bool
1.174 +eat_whitespace<TokenT>::newline_2nd(TokenT &token, bool &skipped_newline)
1.175 +{
1.176 + using namespace boost::wave;
1.177 +
1.178 + token_id id = token_id(token);
1.179 + if (T_SPACE == id || T_SPACE2 == id)
1.180 + return true;
1.181 + if (T_CCOMMENT == id) {
1.182 + if (util::ccomment_has_newline(token))
1.183 + skipped_newline = true;
1.184 +
1.185 + if (preserve_comments) {
1.186 + state = &eat_whitespace::general;
1.187 + return false;
1.188 + }
1.189 + return true;
1.190 + }
1.191 + if (T_NEWLINE != id && T_CPPCOMMENT != id)
1.192 + return general(token, skipped_newline);
1.193 +
1.194 + skipped_newline = true;
1.195 + return T_NEWLINE == id || skip_cppcomment(id);
1.196 +}
1.197 +
1.198 +template <typename TokenT>
1.199 +inline bool
1.200 +eat_whitespace<TokenT>::whitespace(TokenT &token, bool &skipped_newline)
1.201 +{
1.202 + using namespace boost::wave;
1.203 +
1.204 + token_id id = token_id(token);
1.205 + if (T_SPACE != id && T_SPACE2 != id &&
1.206 + T_CCOMMENT != id && T_CPPCOMMENT != id)
1.207 + {
1.208 + return general(token, skipped_newline);
1.209 + }
1.210 +
1.211 + if (T_CCOMMENT == id) {
1.212 + if (util::ccomment_has_newline(token))
1.213 + skipped_newline = true;
1.214 + return !preserve_comments;
1.215 + }
1.216 +
1.217 + return T_SPACE == id || T_SPACE2 == id || skip_cppcomment(id);
1.218 +}
1.219 +
1.220 +///////////////////////////////////////////////////////////////////////////////
1.221 +} // namespace context_policies
1.222 +} // namespace wave
1.223 +} // namespace boost
1.224 +
1.225 +// the suffix header occurs after all of the code
1.226 +#ifdef BOOST_HAS_ABI_HEADERS
1.227 +#include BOOST_ABI_SUFFIX
1.228 +#endif
1.229 +
1.230 +#endif // !defined(WHITESPACE_HANDLING_HPP_INCLUDED)
1.231 +