source: NonGTP/Boost/boost/wave/util/eat_whitespace.hpp @ 857

Revision 857, 5.3 KB checked in by igarcia, 19 years ago (diff)
Line 
1/*=============================================================================
2    Boost.Wave: A Standard compliant C++ preprocessor library
3    Whitespace eater
4   
5    http://www.boost.org/
6
7    Copyright (c) 2003 Paul Mensonides
8    Copyright (c) 2001-2005 Hartmut Kaiser.
9    Distributed under the Boost Software License, Version 1.0. (See accompanying
10    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11=============================================================================*/
12
13#if !defined(EAT_WHITESPACE_HPP_4CE9AD17_F82D_4AB2_A117_555DF0DCC801_INCLUDED)
14#define EAT_WHITESPACE_HPP_4CE9AD17_F82D_4AB2_A117_555DF0DCC801_INCLUDED
15
16#include <boost/wave/wave_config.hpp>   
17#include <boost/wave/token_ids.hpp>   
18
19///////////////////////////////////////////////////////////////////////////////
20namespace boost {
21namespace wave {
22namespace util {
23
24///////////////////////////////////////////////////////////////////////////////
25template <typename TokenT>
26bool ccomment_has_newline(TokenT const& token)
27{
28    using namespace boost::wave;
29
30    if (T_CCOMMENT == token_id(token)) {
31        if (TokenT::string_type::npos !=
32            token.get_value().find_first_of("\n"))
33        {
34            return true;
35        }
36    }
37    return false;
38}
39
40///////////////////////////////////////////////////////////////////////////////
41template <typename TokenT>
42class eat_whitespace {
43
44public:
45    eat_whitespace(bool preserve_comments);
46   
47    bool may_skip (TokenT &token, bool &skipped_newline);
48
49protected:
50    bool skip_cppcomment(boost::wave::token_id id)
51    {
52        return !preserve_comments && T_CPPCOMMENT == id;
53    }
54   
55private:
56    typedef bool state_t(TokenT &token, bool &skipped_newline);
57    state_t eat_whitespace::* state;
58    state_t general, newline, newline_2nd, whitespace;
59    bool preserve_comments;
60};
61
62template <typename TokenT>
63inline
64eat_whitespace<TokenT>::eat_whitespace(bool preserve_comments)
65:   state(&eat_whitespace::newline), preserve_comments(preserve_comments)
66{
67}
68
69template <typename TokenT>
70inline bool
71eat_whitespace<TokenT>::may_skip(TokenT &token, bool &skipped_newline)
72{
73    return (this->*state)(token, skipped_newline);
74}
75
76template <typename TokenT>
77inline bool
78eat_whitespace<TokenT>::general(TokenT &token, bool &skipped_newline)
79{
80    using namespace boost::wave;
81
82    token_id id = token_id(token);
83    if (T_NEWLINE == id || T_CPPCOMMENT == id) {
84        state = &eat_whitespace::newline;
85    }
86    else if (T_SPACE == id || T_SPACE2 == id || T_CCOMMENT == id) {
87        state = &eat_whitespace::whitespace;
88
89        if (ccomment_has_newline(token))
90            skipped_newline = true;
91
92        if ((!preserve_comments || T_CCOMMENT != id) &&
93            token.get_value().size() > 1)
94        {
95            token.set_value(" ");   // replace with a single space
96        }
97    }
98    else {
99        state = &eat_whitespace::general;
100    }
101    return false;
102}
103
104template <typename TokenT>
105inline bool
106eat_whitespace<TokenT>::newline(TokenT &token, bool &skipped_newline)
107{
108    using namespace boost::wave;
109   
110    token_id id = token_id(token);
111    if (T_NEWLINE == id || T_CPPCOMMENT == id) {
112        skipped_newline = true;
113        state = &eat_whitespace::newline_2nd;
114        return T_NEWLINE == id || skip_cppcomment(id);
115    }
116    else if (T_SPACE != id && T_SPACE2 != id && T_CCOMMENT != id) {
117        return general(token, skipped_newline);
118    }
119
120    if (T_CCOMMENT == id) {
121        if (ccomment_has_newline(token))
122            skipped_newline = true;
123
124        if (preserve_comments) {
125            state = &eat_whitespace::general;
126            return false;
127        }
128        // fall through...
129    }
130    return true;
131}
132
133template <typename TokenT>
134inline bool
135eat_whitespace<TokenT>::newline_2nd(TokenT &token, bool &skipped_newline)
136{
137    using namespace boost::wave;
138   
139    token_id id = token_id(token);
140    if (T_SPACE == id || T_SPACE2 == id)
141        return true;
142    if (T_CCOMMENT == id) {
143        if (ccomment_has_newline(token))
144            skipped_newline = true;
145
146        if (preserve_comments) {
147            state = &eat_whitespace::general;
148            return false;
149        }
150        return  true;
151    }
152    if (T_NEWLINE != id && T_CPPCOMMENT != id)
153        return general(token, skipped_newline);
154
155    skipped_newline = true;
156    return T_NEWLINE == id || skip_cppcomment(id);
157}
158
159template <typename TokenT>
160inline bool
161eat_whitespace<TokenT>::whitespace(TokenT &token, bool &skipped_newline)
162{
163    using namespace boost::wave;
164   
165    token_id id = token_id(token);
166    if (T_SPACE != id && T_SPACE2 != id &&
167        T_CCOMMENT != id && T_CPPCOMMENT != id)
168    {
169        return general(token, skipped_newline);
170    }
171   
172    if (T_CCOMMENT == id) {
173        if (ccomment_has_newline(token))
174            skipped_newline = true;
175        return !preserve_comments;
176    }
177
178    return T_SPACE == id || T_SPACE2 == id || skip_cppcomment(id);
179}
180
181///////////////////////////////////////////////////////////////////////////////
182}   // namespace util
183}   // namespace wave
184}   // namespace boost
185
186#endif // !defined(EAT_WHITESPACE_HPP_4CE9AD17_F82D_4AB2_A117_555DF0DCC801_INCLUDED)
187
Note: See TracBrowser for help on using the repository browser.