summaryrefslogtreecommitdiff
path: root/boost/wave/util/macro_helpers.hpp
blob: 4d156638dd6538ef376d3fe7cee65033c803e9e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*=============================================================================
    Boost.Wave: A Standard compliant C++ preprocessor library

    http://www.boost.org/

    Copyright (c) 2001-2011 Hartmut Kaiser. Distributed under the Boost
    Software License, Version 1.0. (See accompanying file
    LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/

#if !defined(MACRO_HELPERS_HPP_931BBC99_EBFA_4692_8FBE_B555998C2C39_INCLUDED)
#define MACRO_HELPERS_HPP_931BBC99_EBFA_4692_8FBE_B555998C2C39_INCLUDED

#include <vector>

#include <boost/assert.hpp>
#include <boost/wave/wave_config.hpp>
#include <boost/wave/token_ids.hpp>
#include <boost/wave/cpplexer/validate_universal_char.hpp>
#include <boost/wave/util/unput_queue_iterator.hpp>

// this must occur after all of the includes and before any code appears
#ifdef BOOST_HAS_ABI_HEADERS
#include BOOST_ABI_PREFIX
#endif

///////////////////////////////////////////////////////////////////////////////
namespace boost {
namespace wave {
namespace util {

namespace impl {

    // escape a string literal (insert '\\' before every '\"', '?' and '\\')
    template <typename StringT>
    inline StringT
    escape_lit(StringT const &value)
    {
        StringT result;
        typename StringT::size_type pos = 0;
        typename StringT::size_type pos1 = value.find_first_of ("\"\\?", 0);
        if (StringT::npos != pos1) {
            do {
                result += value.substr(pos, pos1-pos) 
                            + StringT("\\") 
                            + StringT(1, value[pos1]);
                pos1 = value.find_first_of ("\"\\?", pos = pos1+1);
            } while (StringT::npos != pos1);
            result += value.substr(pos);
        }
        else {
            result = value;
        }
        return result;
    }

    // un-escape a string literal (remove '\\' just before '\\', '\"' or '?')
    template <typename StringT>
    inline StringT
    unescape_lit(StringT const &value)
    {
        StringT result;
        typename StringT::size_type pos = 0;
        typename StringT::size_type pos1 = value.find_first_of ("\\", 0);
        if (StringT::npos != pos1) {
            do {
                switch (value[pos1+1]) {
                case '\\':
                case '\"':
                case '?':
                    result = result + value.substr(pos, pos1-pos);
                    pos1 = value.find_first_of ("\\", (pos = pos1+1)+1);
                    break;

                case 'n':
                    result = result + value.substr(pos, pos1-pos) + "\n";
                    pos1 = value.find_first_of ("\\", pos = pos1+1);
                    ++pos;
                    break;

                default:
                    result = result + value.substr(pos, pos1-pos+1);
                    pos1 = value.find_first_of ("\\", pos = pos1+1);
                }

            } while (pos1 != StringT::npos);
            result = result + value.substr(pos);
        }
        else {
        // the string doesn't contain any escaped character sequences
            result = value;
        }
        return result;
    }

    // return the string representation of a token sequence
    template <typename ContainerT, typename PositionT>
    inline typename ContainerT::value_type::string_type
    as_stringlit (ContainerT const &token_sequence, PositionT const &pos)
    {
        using namespace boost::wave;
        typedef typename ContainerT::value_type::string_type string_type;

        string_type result("\"");
        bool was_whitespace = false;
        typename ContainerT::const_iterator end = token_sequence.end();
        for (typename ContainerT::const_iterator it = token_sequence.begin(); 
             it != end; ++it) 
        {
            token_id id = token_id(*it);

            if (IS_CATEGORY(*it, WhiteSpaceTokenType) || T_NEWLINE == id) {
                if (!was_whitespace) {
                // C++ standard 16.3.2.2 [cpp.stringize]
                // Each occurrence of white space between the argument's 
                // preprocessing tokens becomes a single space character in the 
                // character string literal.
                    result += " ";
                    was_whitespace = true;
                }
            }
            else if (T_STRINGLIT == id || T_CHARLIT == id) {
            // string literals and character literals have to be escaped
                result += impl::escape_lit((*it).get_value());
                was_whitespace = false;
            }
            else 
#if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
            if (T_PLACEMARKER != id) 
#endif 
            {
            // now append this token to the string
                result += (*it).get_value();
                was_whitespace = false;
            }
        }
        result += "\"";

    // validate the resulting literal to contain no invalid universal character
    // value (throws if invalid chars found)
        boost::wave::cpplexer::impl::validate_literal(result, pos.get_line(), 
            pos.get_column(), pos.get_file()); 
        return result;
    }

#if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
    // return the string representation of a token sequence
    template <typename ContainerT, typename PositionT>
    inline typename ContainerT::value_type::string_type
    as_stringlit (std::vector<ContainerT> const &arguments, 
        typename std::vector<ContainerT>::size_type i, PositionT const &pos)
    {
        using namespace boost::wave;
        typedef typename ContainerT::value_type::string_type string_type;

        BOOST_ASSERT(i < arguments.size());

        string_type result("\"");
        bool was_whitespace = false;

        for (/**/; i < arguments.size(); ++i) {
        // stringize all remaining arguments
            typename ContainerT::const_iterator end = arguments[i].end();
            for (typename ContainerT::const_iterator it = arguments[i].begin(); 
                 it != end; ++it) 
            {
                token_id id = token_id(*it);
                
                if (IS_CATEGORY(*it, WhiteSpaceTokenType) || T_NEWLINE == id) {
                    if (!was_whitespace) {
                    // C++ standard 16.3.2.2 [cpp.stringize]
                    // Each occurrence of white space between the argument's 
                    // preprocessing tokens becomes a single space character in the 
                    // character string literal.
                        result += " ";
                        was_whitespace = true;
                    }
                }
                else if (T_STRINGLIT == id || T_CHARLIT == id) {
                // string literals and character literals have to be escaped
                    result += impl::escape_lit((*it).get_value());
                    was_whitespace = false;
                }
                else if (T_PLACEMARKER != id) {
                // now append this token to the string
                    result += (*it).get_value();
                    was_whitespace = false;
                }
            }

        // append comma, if not last argument
            if (i < arguments.size()-1) {
                result += ",";
                was_whitespace = false;
            }
        }
        result += "\"";

    // validate the resulting literal to contain no invalid universal character
    // value (throws if invalid chars found)
        boost::wave::cpplexer::impl::validate_literal(result, pos.get_line(), 
            pos.get_column(), pos.get_file()); 
        return result;
    }
#endif // BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0

    // return the string representation of a token sequence
    template <typename StringT, typename IteratorT>
    inline StringT
    as_string(IteratorT it, IteratorT const& end)
    {
        StringT result;
        for (/**/; it != end; ++it) 
        {
            result += (*it).get_value();
        }
        return result;
    }

    // return the string representation of a token sequence
    template <typename ContainerT>
    inline typename ContainerT::value_type::string_type
    as_string (ContainerT const &token_sequence)
    {
        typedef typename ContainerT::value_type::string_type string_type;
        return as_string<string_type>(token_sequence.begin(), 
            token_sequence.end());
    }

#if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
    ///////////////////////////////////////////////////////////////////////////
    //
    //  Copies all arguments beginning with the given index to the output 
    //  sequence. The arguments are separated by commas.
    //
    template <typename ContainerT, typename PositionT>
    void replace_ellipsis (std::vector<ContainerT> const &arguments,
        typename ContainerT::size_type index, 
        ContainerT &expanded, PositionT const &pos)
    {
        using namespace cpplexer;
        typedef typename ContainerT::value_type token_type;
        
        token_type comma(T_COMMA, ",", pos);
        for (/**/; index < arguments.size(); ++index) {
        ContainerT const &arg = arguments[index];

            std::copy(arg.begin(), arg.end(), 
                std::inserter(expanded, expanded.end()));
                
            if (index < arguments.size()-1) 
                expanded.push_back(comma);
        }
    }
#endif

    // Skip all whitespace characters and queue the skipped characters into the
    // given container
    template <typename IteratorT>
    inline boost::wave::token_id 
    skip_whitespace(IteratorT &first, IteratorT const &last)
    {
        token_id id = util::impl::next_token<IteratorT>::peek(first, last, false);
        if (IS_CATEGORY(id, WhiteSpaceTokenType)) {
            do {
                ++first;
                id = util::impl::next_token<IteratorT>::peek(first, last, false);
            } while (IS_CATEGORY(id, WhiteSpaceTokenType));
        }
        ++first;
        return id;
    }

    template <typename IteratorT, typename ContainerT>
    inline boost::wave::token_id 
    skip_whitespace(IteratorT &first, IteratorT const &last, ContainerT &queue)
    {
        queue.push_back (*first);       // queue up the current token

        token_id id = util::impl::next_token<IteratorT>::peek(first, last, false);
        if (IS_CATEGORY(id, WhiteSpaceTokenType)) {
            do {
                queue.push_back(*++first);  // queue up the next whitespace 
                id = util::impl::next_token<IteratorT>::peek(first, last, false);
            } while (IS_CATEGORY(id, WhiteSpaceTokenType));
        }
        ++first;
        return id;
    }

}   // namespace impl

///////////////////////////////////////////////////////////////////////////////
}   // namespace util
}   // namespace wave
}   // namespace boost

// the suffix header occurs after all of the code
#ifdef BOOST_HAS_ABI_HEADERS
#include BOOST_ABI_SUFFIX
#endif

#endif // !defined(MACRO_HELPERS_HPP_931BBC99_EBFA_4692_8FBE_B555998C2C39_INCLUDED)