/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. #include <map>
  9. #include <ostream>
  10. #include "rules.hpp"
  11. #include "size_t.hpp"
  12. #include "state_machine.hpp"
  13. #include "string_token.hpp"
  14. #include <vector>
  15. namespace boost
  16. {
  17. namespace lexer
  18. {
  19. template<typename CharT>
  20. class basic_debug
  21. {
  22. public:
  23. typedef std::basic_ostream<CharT> ostream;
  24. typedef std::basic_string<CharT> string;
  25. typedef std::vector<std::size_t> size_t_vector;
  26. static void escape_control_chars (const string &in_, string &out_)
  27. {
  28. const CharT *ptr_ = in_.c_str ();
  29. std::size_t size_ = in_.size ();
  30. #if defined _MSC_VER && _MSC_VER <= 1200
  31. out_.erase ();
  32. #else
  33. out_.clear ();
  34. #endif
  35. while (size_)
  36. {
  37. basic_string_token<CharT>::escape_char (*ptr_, out_);
  38. ++ptr_;
  39. --size_;
  40. }
  41. }
  42. static void dump (const basic_state_machine<CharT> &state_machine_,
  43. basic_rules<CharT> &rules_, ostream &stream_)
  44. {
  45. typename basic_state_machine<CharT>::iterator iter_ =
  46. state_machine_.begin ();
  47. typename basic_state_machine<CharT>::iterator end_ =
  48. state_machine_.end ();
  49. for (std::size_t dfa_ = 0, dfas_ = state_machine_.size ();
  50. dfa_ < dfas_; ++dfa_)
  51. {
  52. lexer_state (stream_);
  53. stream_ << rules_.state (dfa_) << std::endl << std::endl;
  54. dump_ex (iter_, stream_);
  55. }
  56. }
  57. static void dump (const basic_state_machine<CharT> &state_machine_,
  58. ostream &stream_)
  59. {
  60. typename basic_state_machine<CharT>::iterator iter_ =
  61. state_machine_.begin ();
  62. typename basic_state_machine<CharT>::iterator end_ =
  63. state_machine_.end ();
  64. for (std::size_t dfa_ = 0, dfas_ = state_machine_.size ();
  65. dfa_ < dfas_; ++dfa_)
  66. {
  67. lexer_state (stream_);
  68. stream_ << dfa_ << std::endl << std::endl;
  69. dump_ex (iter_, stream_);
  70. }
  71. }
  72. protected:
  73. typedef std::basic_stringstream<CharT> stringstream;
  74. static void dump_ex (typename basic_state_machine<CharT>::iterator &iter_,
  75. ostream &stream_)
  76. {
  77. const std::size_t states_ = iter_->states;
  78. for (std::size_t i_ = 0; i_ < states_; ++i_)
  79. {
  80. state (stream_);
  81. stream_ << i_ << std::endl;
  82. if (iter_->end_state)
  83. {
  84. end_state (stream_);
  85. stream_ << iter_->id;
  86. unique_id (stream_);
  87. stream_ << iter_->unique_id;
  88. dfa (stream_);
  89. stream_ << iter_->goto_dfa;
  90. stream_ << std::endl;
  91. }
  92. if (iter_->bol_index != npos)
  93. {
  94. bol (stream_);
  95. stream_ << iter_->bol_index << std::endl;
  96. }
  97. if (iter_->eol_index != npos)
  98. {
  99. eol (stream_);
  100. stream_ << iter_->eol_index << std::endl;
  101. }
  102. const std::size_t transitions_ = iter_->transitions;
  103. if (transitions_ == 0)
  104. {
  105. ++iter_;
  106. }
  107. for (std::size_t t_ = 0; t_ < transitions_; ++t_)
  108. {
  109. std::size_t goto_state_ = iter_->goto_state;
  110. if (iter_->token.any ())
  111. {
  112. any (stream_);
  113. }
  114. else
  115. {
  116. open_bracket (stream_);
  117. if (iter_->token._negated)
  118. {
  119. negated (stream_);
  120. }
  121. string charset_;
  122. CharT c_ = 0;
  123. escape_control_chars (iter_->token._charset,
  124. charset_);
  125. c_ = *charset_.c_str ();
  126. if (!iter_->token._negated &&
  127. (c_ == '^' || c_ == ']'))
  128. {
  129. stream_ << '\\';
  130. }
  131. stream_ << charset_;
  132. close_bracket (stream_);
  133. }
  134. stream_ << goto_state_ << std::endl;
  135. ++iter_;
  136. }
  137. stream_ << std::endl;
  138. }
  139. }
  140. static void lexer_state (std::ostream &stream_)
  141. {
  142. stream_ << "Lexer state: ";
  143. }
  144. static void lexer_state (std::wostream &stream_)
  145. {
  146. stream_ << L"Lexer state: ";
  147. }
  148. static void state (std::ostream &stream_)
  149. {
  150. stream_ << "State: ";
  151. }
  152. static void state (std::wostream &stream_)
  153. {
  154. stream_ << L"State: ";
  155. }
  156. static void bol (std::ostream &stream_)
  157. {
  158. stream_ << " BOL -> ";
  159. }
  160. static void bol (std::wostream &stream_)
  161. {
  162. stream_ << L" BOL -> ";
  163. }
  164. static void eol (std::ostream &stream_)
  165. {
  166. stream_ << " EOL -> ";
  167. }
  168. static void eol (std::wostream &stream_)
  169. {
  170. stream_ << L" EOL -> ";
  171. }
  172. static void end_state (std::ostream &stream_)
  173. {
  174. stream_ << " END STATE, Id = ";
  175. }
  176. static void end_state (std::wostream &stream_)
  177. {
  178. stream_ << L" END STATE, Id = ";
  179. }
  180. static void unique_id (std::ostream &stream_)
  181. {
  182. stream_ << ", Unique Id = ";
  183. }
  184. static void unique_id (std::wostream &stream_)
  185. {
  186. stream_ << L", Unique Id = ";
  187. }
  188. static void any (std::ostream &stream_)
  189. {
  190. stream_ << " . -> ";
  191. }
  192. static void any (std::wostream &stream_)
  193. {
  194. stream_ << L" . -> ";
  195. }
  196. static void open_bracket (std::ostream &stream_)
  197. {
  198. stream_ << " [";
  199. }
  200. static void open_bracket (std::wostream &stream_)
  201. {
  202. stream_ << L" [";
  203. }
  204. static void negated (std::ostream &stream_)
  205. {
  206. stream_ << "^";
  207. }
  208. static void negated (std::wostream &stream_)
  209. {
  210. stream_ << L"^";
  211. }
  212. static void close_bracket (std::ostream &stream_)
  213. {
  214. stream_ << "] -> ";
  215. }
  216. static void close_bracket (std::wostream &stream_)
  217. {
  218. stream_ << L"] -> ";
  219. }
  220. static void dfa (std::ostream &stream_)
  221. {
  222. stream_ << ", dfa = ";
  223. }
  224. static void dfa (std::wostream &stream_)
  225. {
  226. stream_ << L", dfa = ";
  227. }
  228. };
  229. typedef basic_debug<char> debug;
  230. typedef basic_debug<wchar_t> wdebug;
  231. }
  232. }
  233. #endif