PageRenderTime 35ms CodeModel.GetById 13ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/spirit/home/support/detail/lexer/debug.hpp

http://hadesmem.googlecode.com/
C++ Header | 285 lines | 228 code | 52 blank | 5 comment | 19 complexity | 144001d9896e182083c11ee055acbf3a MD5 | raw file
  1// debug.hpp
  2// Copyright (c) 2007-2009 Ben Hanson (http://www.benhanson.net/)
  3//
  4// Distributed under the Boost Software License, Version 1.0. (See accompanying
  5// file licence_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6#ifndef BOOST_LEXER_DEBUG_HPP
  7#define BOOST_LEXER_DEBUG_HPP
  8
  9#include <map>
 10#include <ostream>
 11#include "rules.hpp"
 12#include "size_t.hpp"
 13#include "state_machine.hpp"
 14#include "string_token.hpp"
 15#include <vector>
 16
 17namespace boost
 18{
 19namespace lexer
 20{
 21template<typename CharT>
 22class basic_debug
 23{
 24public:
 25    typedef std::basic_ostream<CharT> ostream;
 26    typedef std::basic_string<CharT> string;
 27    typedef std::vector<std::size_t> size_t_vector;
 28
 29    static void escape_control_chars (const string &in_, string &out_)
 30    {
 31        const CharT *ptr_ = in_.c_str ();
 32        std::size_t size_ = in_.size ();
 33
 34#if defined _MSC_VER && _MSC_VER <= 1200
 35        out_.erase ();
 36#else
 37        out_.clear ();
 38#endif
 39
 40        while (size_)
 41        {
 42            basic_string_token<CharT>::escape_char (*ptr_, out_);
 43            ++ptr_;
 44            --size_;
 45        }
 46    }
 47
 48    static void dump (const basic_state_machine<CharT> &state_machine_,
 49        basic_rules<CharT> &rules_, ostream &stream_)
 50    {
 51        typename basic_state_machine<CharT>::iterator iter_ =
 52            state_machine_.begin ();
 53        typename basic_state_machine<CharT>::iterator end_ =
 54            state_machine_.end ();
 55
 56        for (std::size_t dfa_ = 0, dfas_ = state_machine_.size ();
 57            dfa_ < dfas_; ++dfa_)
 58        {
 59            lexer_state (stream_);
 60            stream_ << rules_.state (dfa_) << std::endl << std::endl;
 61
 62            dump_ex (iter_, stream_);
 63        }
 64    }
 65
 66    static void dump (const basic_state_machine<CharT> &state_machine_,
 67        ostream &stream_)
 68    {
 69        typename basic_state_machine<CharT>::iterator iter_ =
 70            state_machine_.begin ();
 71        typename basic_state_machine<CharT>::iterator end_ =
 72            state_machine_.end ();
 73
 74        for (std::size_t dfa_ = 0, dfas_ = state_machine_.size ();
 75            dfa_ < dfas_; ++dfa_)
 76        {
 77            lexer_state (stream_);
 78            stream_ << dfa_ << std::endl << std::endl;
 79
 80            dump_ex (iter_, stream_);
 81        }
 82    }
 83
 84protected:
 85    typedef std::basic_stringstream<CharT> stringstream;
 86
 87    static void dump_ex (typename basic_state_machine<CharT>::iterator &iter_,
 88        ostream &stream_)
 89    {
 90        const std::size_t states_ = iter_->states;
 91
 92        for (std::size_t i_ = 0; i_ < states_; ++i_)
 93        {
 94            state (stream_);
 95            stream_ << i_ << std::endl;
 96
 97            if (iter_->end_state)
 98            {
 99                end_state (stream_);
100                stream_ << iter_->id;
101                unique_id (stream_);
102                stream_ << iter_->unique_id;
103                dfa (stream_);
104                stream_ << iter_->goto_dfa;
105                stream_ << std::endl;
106            }
107
108            if (iter_->bol_index != npos)
109            {
110                bol (stream_);
111                stream_ << iter_->bol_index << std::endl;
112            }
113
114            if (iter_->eol_index != npos)
115            {
116                eol (stream_);
117                stream_ << iter_->eol_index << std::endl;
118            }
119
120            const std::size_t transitions_ = iter_->transitions;
121
122            if (transitions_ == 0)
123            {
124                ++iter_;
125            }
126
127            for (std::size_t t_ = 0; t_ < transitions_; ++t_)
128            {
129                std::size_t goto_state_ = iter_->goto_state;
130
131                if (iter_->token.any ())
132                {
133                    any (stream_);
134                }
135                else
136                {
137                    open_bracket (stream_);
138
139                    if (iter_->token._negated)
140                    {
141                        negated (stream_);
142                    }
143
144                    string charset_;
145                    CharT c_ = 0;
146
147                    escape_control_chars (iter_->token._charset,
148                        charset_);
149                    c_ = *charset_.c_str ();
150
151                    if (!iter_->token._negated &&
152                        (c_ == '^' || c_ == ']'))
153                    {
154                        stream_ << '\\';
155                    }
156
157                    stream_ << charset_;
158                    close_bracket (stream_);
159                }
160
161                stream_ << goto_state_ << std::endl;
162                ++iter_;
163            }
164
165            stream_ << std::endl;
166        }
167    }
168
169    static void lexer_state (std::ostream &stream_)
170    {
171        stream_ << "Lexer state: ";
172    }
173
174    static void lexer_state (std::wostream &stream_)
175    {
176        stream_ << L"Lexer state: ";
177    }
178
179    static void state (std::ostream &stream_)
180    {
181        stream_ << "State: ";
182    }
183
184    static void state (std::wostream &stream_)
185    {
186        stream_ << L"State: ";
187    }
188
189    static void bol (std::ostream &stream_)
190    {
191        stream_ << "  BOL -> ";
192    }
193
194    static void bol (std::wostream &stream_)
195    {
196        stream_ << L"  BOL -> ";
197    }
198
199    static void eol (std::ostream &stream_)
200    {
201        stream_ << "  EOL -> ";
202    }
203
204    static void eol (std::wostream &stream_)
205    {
206        stream_ << L"  EOL -> ";
207    }
208
209    static void end_state (std::ostream &stream_)
210    {
211        stream_ << "  END STATE, Id = ";
212    }
213
214    static void end_state (std::wostream &stream_)
215    {
216        stream_ << L"  END STATE, Id = ";
217    }
218
219    static void unique_id (std::ostream &stream_)
220    {
221        stream_ << ", Unique Id = ";
222    }
223
224    static void unique_id (std::wostream &stream_)
225    {
226        stream_ << L", Unique Id = ";
227    }
228
229    static void any (std::ostream &stream_)
230    {
231        stream_ << "  . -> ";
232    }
233
234    static void any (std::wostream &stream_)
235    {
236        stream_ << L"  . -> ";
237    }
238
239    static void open_bracket (std::ostream &stream_)
240    {
241        stream_ << "  [";
242    }
243
244    static void open_bracket (std::wostream &stream_)
245    {
246        stream_ << L"  [";
247    }
248
249    static void negated (std::ostream &stream_)
250    {
251        stream_ << "^";
252    }
253
254    static void negated (std::wostream &stream_)
255    {
256        stream_ << L"^";
257    }
258
259    static void close_bracket (std::ostream &stream_)
260    {
261        stream_ << "] -> ";
262    }
263
264    static void close_bracket (std::wostream &stream_)
265    {
266        stream_ << L"] -> ";
267    }
268
269    static void dfa (std::ostream &stream_)
270    {
271        stream_ << ", dfa = ";
272    }
273
274    static void dfa (std::wostream &stream_)
275    {
276        stream_ << L", dfa = ";
277    }
278};
279
280typedef basic_debug<char> debug;
281typedef basic_debug<wchar_t> wdebug;
282}
283}
284
285#endif