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 ();
34 #if defined _MSC_VER && _MSC_VER <= 1200
42 basic_string_token<CharT>::escape_char (*ptr_, out_);
48 static void dump (const basic_state_machine<CharT> &state_machine_,
49 basic_rules<CharT> &rules_, ostream &stream_)
51 typename basic_state_machine<CharT>::iterator iter_ =
52 state_machine_.begin ();
53 typename basic_state_machine<CharT>::iterator end_ =
54 state_machine_.end ();
56 for (std::size_t dfa_ = 0, dfas_ = state_machine_.size ();
59 lexer_state (stream_);
60 stream_ << rules_.state (dfa_) << std::endl << std::endl;
62 dump_ex (iter_, stream_);
66 static void dump (const basic_state_machine<CharT> &state_machine_,
69 typename basic_state_machine<CharT>::iterator iter_ =
70 state_machine_.begin ();
71 typename basic_state_machine<CharT>::iterator end_ =
72 state_machine_.end ();
74 for (std::size_t dfa_ = 0, dfas_ = state_machine_.size ();
77 lexer_state (stream_);
78 stream_ << dfa_ << std::endl << std::endl;
80 dump_ex (iter_, stream_);
85 typedef std::basic_stringstream<CharT> stringstream;
87 static void dump_ex (typename basic_state_machine<CharT>::iterator &iter_,
90 const std::size_t states_ = iter_->states;
92 for (std::size_t i_ = 0; i_ < states_; ++i_)
95 stream_ << i_ << std::endl;
100 stream_ << iter_->id;
102 stream_ << iter_->unique_id;
104 stream_ << iter_->goto_dfa;
105 stream_ << std::endl;
108 if (iter_->bol_index != npos)
111 stream_ << iter_->bol_index << std::endl;
114 if (iter_->eol_index != npos)
117 stream_ << iter_->eol_index << std::endl;
120 const std::size_t transitions_ = iter_->transitions;
122 if (transitions_ == 0)
127 for (std::size_t t_ = 0; t_ < transitions_; ++t_)
129 std::size_t goto_state_ = iter_->goto_state;
131 if (iter_->token.any ())
137 open_bracket (stream_);
139 if (iter_->token._negated)
147 escape_control_chars (iter_->token._charset,
149 c_ = *charset_.c_str ();
151 if (!iter_->token._negated &&
152 (c_ == '^' || c_ == ']'))
158 close_bracket (stream_);
161 stream_ << goto_state_ << std::endl;
165 stream_ << std::endl;
169 static void lexer_state (std::ostream &stream_)
171 stream_ << "Lexer state: ";
174 static void lexer_state (std::wostream &stream_)
176 stream_ << L"Lexer state: ";
179 static void state (std::ostream &stream_)
181 stream_ << "State: ";
184 static void state (std::wostream &stream_)
186 stream_ << L"State: ";
189 static void bol (std::ostream &stream_)
191 stream_ << " BOL -> ";
194 static void bol (std::wostream &stream_)
196 stream_ << L" BOL -> ";
199 static void eol (std::ostream &stream_)
201 stream_ << " EOL -> ";
204 static void eol (std::wostream &stream_)
206 stream_ << L" EOL -> ";
209 static void end_state (std::ostream &stream_)
211 stream_ << " END STATE, Id = ";
214 static void end_state (std::wostream &stream_)
216 stream_ << L" END STATE, Id = ";
219 static void unique_id (std::ostream &stream_)
221 stream_ << ", Unique Id = ";
224 static void unique_id (std::wostream &stream_)
226 stream_ << L", Unique Id = ";
229 static void any (std::ostream &stream_)
234 static void any (std::wostream &stream_)
236 stream_ << L" . -> ";
239 static void open_bracket (std::ostream &stream_)
244 static void open_bracket (std::wostream &stream_)
249 static void negated (std::ostream &stream_)
254 static void negated (std::wostream &stream_)
259 static void close_bracket (std::ostream &stream_)
264 static void close_bracket (std::wostream &stream_)
269 static void dfa (std::ostream &stream_)
271 stream_ << ", dfa = ";
274 static void dfa (std::wostream &stream_)
276 stream_ << L", dfa = ";
280 typedef basic_debug<char> debug;
281 typedef basic_debug<wchar_t> wdebug;