2 // Copyright (c) 2007-2009 Ben Hanson (http://www.benhanson.net/)
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
13 #include "state_machine.hpp"
14 #include "string_token.hpp"
21 template<typename CharT>
25 typedef std::basic_ostream<CharT> ostream;
26 typedef std::basic_string<CharT> string;
27 typedef std::vector<std::size_t> size_t_vector;
29 static void escape_control_chars (const string &in_, string &out_)
31 const CharT *ptr_ = in_.c_str ();
32 std::size_t size_ = in_.size ();
38 basic_string_token<CharT>::escape_char (*ptr_, out_);
44 static void dump (const basic_state_machine<CharT> &state_machine_,
45 basic_rules<CharT> &rules_, ostream &stream_)
47 typename basic_state_machine<CharT>::iterator iter_ =
48 state_machine_.begin ();
49 typename basic_state_machine<CharT>::iterator end_ =
50 state_machine_.end ();
52 for (std::size_t dfa_ = 0, dfas_ = state_machine_.size ();
55 lexer_state (stream_);
56 stream_ << rules_.state (dfa_) << std::endl << std::endl;
58 dump_ex (iter_, stream_);
62 static void dump (const basic_state_machine<CharT> &state_machine_,
65 typename basic_state_machine<CharT>::iterator iter_ =
66 state_machine_.begin ();
67 typename basic_state_machine<CharT>::iterator end_ =
68 state_machine_.end ();
70 for (std::size_t dfa_ = 0, dfas_ = state_machine_.size ();
73 lexer_state (stream_);
74 stream_ << dfa_ << std::endl << std::endl;
76 dump_ex (iter_, stream_);
81 typedef std::basic_stringstream<CharT> stringstream;
83 static void dump_ex (typename basic_state_machine<CharT>::iterator &iter_,
86 const std::size_t states_ = iter_->states;
88 for (std::size_t i_ = 0; i_ < states_; ++i_)
91 stream_ << i_ << std::endl;
98 stream_ << iter_->unique_id;
100 stream_ << iter_->goto_dfa;
101 stream_ << std::endl;
104 if (iter_->bol_index != npos)
107 stream_ << iter_->bol_index << std::endl;
110 if (iter_->eol_index != npos)
113 stream_ << iter_->eol_index << std::endl;
116 const std::size_t transitions_ = iter_->transitions;
118 if (transitions_ == 0)
123 for (std::size_t t_ = 0; t_ < transitions_; ++t_)
125 std::size_t goto_state_ = iter_->goto_state;
127 if (iter_->token.any ())
133 open_bracket (stream_);
135 if (iter_->token._negated)
143 escape_control_chars (iter_->token._charset,
145 c_ = *charset_.c_str ();
147 if (!iter_->token._negated &&
148 (c_ == '^' || c_ == ']'))
154 close_bracket (stream_);
157 stream_ << goto_state_ << std::endl;
161 stream_ << std::endl;
165 static void lexer_state (std::ostream &stream_)
167 stream_ << "Lexer state: ";
170 static void lexer_state (std::wostream &stream_)
172 stream_ << L"Lexer state: ";
175 static void state (std::ostream &stream_)
177 stream_ << "State: ";
180 static void state (std::wostream &stream_)
182 stream_ << L"State: ";
185 static void bol (std::ostream &stream_)
187 stream_ << " BOL -> ";
190 static void bol (std::wostream &stream_)
192 stream_ << L" BOL -> ";
195 static void eol (std::ostream &stream_)
197 stream_ << " EOL -> ";
200 static void eol (std::wostream &stream_)
202 stream_ << L" EOL -> ";
205 static void end_state (std::ostream &stream_)
207 stream_ << " END STATE, Id = ";
210 static void end_state (std::wostream &stream_)
212 stream_ << L" END STATE, Id = ";
215 static void unique_id (std::ostream &stream_)
217 stream_ << ", Unique Id = ";
220 static void unique_id (std::wostream &stream_)
222 stream_ << L", Unique Id = ";
225 static void any (std::ostream &stream_)
230 static void any (std::wostream &stream_)
232 stream_ << L" . -> ";
235 static void open_bracket (std::ostream &stream_)
240 static void open_bracket (std::wostream &stream_)
245 static void negated (std::ostream &stream_)
250 static void negated (std::wostream &stream_)
255 static void close_bracket (std::ostream &stream_)
260 static void close_bracket (std::wostream &stream_)
265 static void dfa (std::ostream &stream_)
267 stream_ << ", dfa = ";
270 static void dfa (std::wostream &stream_)
272 stream_ << L", dfa = ";
276 typedef basic_debug<char> debug;
277 typedef basic_debug<wchar_t> wdebug;