File indexing completed on 2025-01-19 09:47:49
0001
0002
0003
0004
0005
0006 #ifndef BOOST_SPIRIT_SUPPORT_DETAIL_LEXER_DEBUG_HPP
0007 #define BOOST_SPIRIT_SUPPORT_DETAIL_LEXER_DEBUG_HPP
0008
0009 #include <map>
0010 #include "rules.hpp"
0011 #include "size_t.hpp"
0012 #include "state_machine.hpp"
0013 #include "string_token.hpp"
0014 #include <ios>
0015 #include <vector>
0016
0017 namespace boost
0018 {
0019 namespace lexer
0020 {
0021 template<typename CharT>
0022 class basic_debug
0023 {
0024 public:
0025 typedef std::basic_ostream<CharT> ostream;
0026 typedef std::basic_string<CharT> string;
0027 typedef std::vector<std::size_t> size_t_vector;
0028
0029 static void escape_control_chars (const string &in_, string &out_)
0030 {
0031 const CharT *ptr_ = in_.c_str ();
0032 std::size_t size_ = in_.size ();
0033
0034 out_.clear ();
0035
0036 while (size_)
0037 {
0038 basic_string_token<CharT>::escape_char (*ptr_, out_);
0039 ++ptr_;
0040 --size_;
0041 }
0042 }
0043
0044 static void dump (const basic_state_machine<CharT> &state_machine_,
0045 basic_rules<CharT> &rules_, ostream &stream_)
0046 {
0047 typename basic_state_machine<CharT>::iterator iter_ =
0048 state_machine_.begin ();
0049 typename basic_state_machine<CharT>::iterator end_ =
0050 state_machine_.end ();
0051
0052 for (std::size_t dfa_ = 0, dfas_ = state_machine_.size ();
0053 dfa_ < dfas_; ++dfa_)
0054 {
0055 lexer_state (stream_);
0056 stream_ << rules_.state (dfa_) << std::endl << std::endl;
0057
0058 dump_ex (iter_, stream_);
0059 }
0060 }
0061
0062 static void dump (const basic_state_machine<CharT> &state_machine_,
0063 ostream &stream_)
0064 {
0065 typename basic_state_machine<CharT>::iterator iter_ =
0066 state_machine_.begin ();
0067 typename basic_state_machine<CharT>::iterator end_ =
0068 state_machine_.end ();
0069
0070 for (std::size_t dfa_ = 0, dfas_ = state_machine_.size ();
0071 dfa_ < dfas_; ++dfa_)
0072 {
0073 lexer_state (stream_);
0074 stream_ << dfa_ << std::endl << std::endl;
0075
0076 dump_ex (iter_, stream_);
0077 }
0078 }
0079
0080 protected:
0081 typedef std::basic_stringstream<CharT> stringstream;
0082
0083 static void dump_ex (typename basic_state_machine<CharT>::iterator &iter_,
0084 ostream &stream_)
0085 {
0086 const std::size_t states_ = iter_->states;
0087
0088 for (std::size_t i_ = 0; i_ < states_; ++i_)
0089 {
0090 state (stream_);
0091 stream_ << i_ << std::endl;
0092
0093 if (iter_->end_state)
0094 {
0095 end_state (stream_);
0096 stream_ << iter_->id;
0097 unique_id (stream_);
0098 stream_ << iter_->unique_id;
0099 dfa (stream_);
0100 stream_ << iter_->goto_dfa;
0101 stream_ << std::endl;
0102 }
0103
0104 if (iter_->bol_index != npos)
0105 {
0106 bol (stream_);
0107 stream_ << iter_->bol_index << std::endl;
0108 }
0109
0110 if (iter_->eol_index != npos)
0111 {
0112 eol (stream_);
0113 stream_ << iter_->eol_index << std::endl;
0114 }
0115
0116 const std::size_t transitions_ = iter_->transitions;
0117
0118 if (transitions_ == 0)
0119 {
0120 ++iter_;
0121 }
0122
0123 for (std::size_t t_ = 0; t_ < transitions_; ++t_)
0124 {
0125 std::size_t goto_state_ = iter_->goto_state;
0126
0127 if (iter_->token.any ())
0128 {
0129 any (stream_);
0130 }
0131 else
0132 {
0133 open_bracket (stream_);
0134
0135 if (iter_->token._negated)
0136 {
0137 negated (stream_);
0138 }
0139
0140 string charset_;
0141 CharT c_ = 0;
0142
0143 escape_control_chars (iter_->token._charset,
0144 charset_);
0145 c_ = *charset_.c_str ();
0146
0147 if (!iter_->token._negated &&
0148 (c_ == '^' || c_ == ']'))
0149 {
0150 stream_ << '\\';
0151 }
0152
0153 stream_ << charset_;
0154 close_bracket (stream_);
0155 }
0156
0157 stream_ << goto_state_ << std::endl;
0158 ++iter_;
0159 }
0160
0161 stream_ << std::endl;
0162 }
0163 }
0164
0165 static void lexer_state (std::ostream &stream_)
0166 {
0167 stream_ << "Lexer state: ";
0168 }
0169
0170 static void lexer_state (std::wostream &stream_)
0171 {
0172 stream_ << L"Lexer state: ";
0173 }
0174
0175 static void state (std::ostream &stream_)
0176 {
0177 stream_ << "State: ";
0178 }
0179
0180 static void state (std::wostream &stream_)
0181 {
0182 stream_ << L"State: ";
0183 }
0184
0185 static void bol (std::ostream &stream_)
0186 {
0187 stream_ << " BOL -> ";
0188 }
0189
0190 static void bol (std::wostream &stream_)
0191 {
0192 stream_ << L" BOL -> ";
0193 }
0194
0195 static void eol (std::ostream &stream_)
0196 {
0197 stream_ << " EOL -> ";
0198 }
0199
0200 static void eol (std::wostream &stream_)
0201 {
0202 stream_ << L" EOL -> ";
0203 }
0204
0205 static void end_state (std::ostream &stream_)
0206 {
0207 stream_ << " END STATE, Id = ";
0208 }
0209
0210 static void end_state (std::wostream &stream_)
0211 {
0212 stream_ << L" END STATE, Id = ";
0213 }
0214
0215 static void unique_id (std::ostream &stream_)
0216 {
0217 stream_ << ", Unique Id = ";
0218 }
0219
0220 static void unique_id (std::wostream &stream_)
0221 {
0222 stream_ << L", Unique Id = ";
0223 }
0224
0225 static void any (std::ostream &stream_)
0226 {
0227 stream_ << " . -> ";
0228 }
0229
0230 static void any (std::wostream &stream_)
0231 {
0232 stream_ << L" . -> ";
0233 }
0234
0235 static void open_bracket (std::ostream &stream_)
0236 {
0237 stream_ << " [";
0238 }
0239
0240 static void open_bracket (std::wostream &stream_)
0241 {
0242 stream_ << L" [";
0243 }
0244
0245 static void negated (std::ostream &stream_)
0246 {
0247 stream_ << "^";
0248 }
0249
0250 static void negated (std::wostream &stream_)
0251 {
0252 stream_ << L"^";
0253 }
0254
0255 static void close_bracket (std::ostream &stream_)
0256 {
0257 stream_ << "] -> ";
0258 }
0259
0260 static void close_bracket (std::wostream &stream_)
0261 {
0262 stream_ << L"] -> ";
0263 }
0264
0265 static void dfa (std::ostream &stream_)
0266 {
0267 stream_ << ", dfa = ";
0268 }
0269
0270 static void dfa (std::wostream &stream_)
0271 {
0272 stream_ << L", dfa = ";
0273 }
0274 };
0275
0276 typedef basic_debug<char> debug;
0277 typedef basic_debug<wchar_t> wdebug;
0278 }
0279 }
0280
0281 #endif