debug.hpp 6.5 KB

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