2 // Copyright (c) 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 LEXERTL_GENERATE_RE2C_HPP
7 #define LEXERTL_GENERATE_RE2C_HPP
9 #include "char_traits.hpp"
11 #include "internals.hpp"
13 #include "runtime_error.hpp"
15 #include "state_machine.hpp"
22 // check whether state0_0 is referenced from any of the other states
23 template <typename Char>
24 bool need_label0_0(boost::lexer::basic_state_machine<Char> const &sm_)
26 typedef typename boost::lexer::basic_state_machine<Char>::iterator
28 iterator_type iter_ = sm_.begin();
29 std::size_t states_ = iter_->states;
31 for (std::size_t state_ = 0; state_ < states_; ++state_)
33 if (0 == iter_->bol_index || 0 == iter_->eol_index)
38 std::size_t const transitions_ = iter_->transitions;
39 for (std::size_t t_ = 0; t_ < transitions_; ++t_)
41 if (0 == iter_->goto_state)
47 if (transitions_ == 0) ++iter_;
52 template<typename CharT>
53 void generate_re2c (const basic_state_machine<CharT> &state_machine_,
54 std::ostream &os_, const bool use_pointers_ = false,
55 const bool skip_unknown_ = true, const bool optimise_parameters_ = true,
56 const char *name_ = "next_token")
58 typedef typename boost::lexer::basic_string_token<CharT> string_token;
59 const detail::internals &sm_ = state_machine_.data ();
61 if (sm_._lookup->size () == 0)
63 throw runtime_error ("Cannot generate code from an empty "
67 std::string upper_name_ (__DATE__);
68 const std::size_t lookups_ = sm_._lookup->front ()->size ();
69 typename boost::lexer::basic_state_machine<CharT>::iterator iter_ =
70 state_machine_.begin();
71 typename boost::lexer::basic_state_machine<CharT>::iterator end_ =
73 const std::size_t dfas_ = sm_._dfa->size ();
74 std::string::size_type pos_ = upper_name_.find (' ');
75 const char *iterator_ = 0;
81 iterator_ = "const char *";
85 iterator_ = "const wchar_t *";
90 iterator_ = "Iterator &";
93 while (pos_ != std::string::npos)
95 upper_name_.replace (pos_, 1, "_");
96 pos_ = upper_name_.find (' ', pos_);
100 upper_name_ += __TIME__;
102 pos_ = upper_name_.find (':');
104 while (pos_ != std::string::npos)
106 upper_name_.erase (pos_, 1);
107 pos_ = upper_name_.find (':', pos_);
110 upper_name_ = '_' + upper_name_;
111 upper_name_ = name_ + upper_name_;
112 std::transform (upper_name_.begin (), upper_name_.end (),
113 upper_name_.begin (), ::toupper);
114 os_ << "#ifndef " << upper_name_ + '\n';
115 os_ << "#define " << upper_name_ + '\n';
116 os_ << "// Copyright (c) 2008-2009 Ben Hanson\n";
118 os_ << "// Distributed under the Boost Software License, "
119 "Version 1.0. (See accompanying\n";
120 os_ << "// file licence_1_0.txt or copy at "
121 "http://www.boost.org/LICENSE_1_0.txt)\n\n";
122 os_ << "// Auto-generated by boost::lexer\n";
123 os_ << "template<typename Iterator>\n";
124 os_ << "std::size_t " << name_ << " (";
126 if (dfas_ > 1 || !optimise_parameters_)
128 os_ << "std::size_t &start_state_, ";
133 os_ << iterator_ << " &";
140 os_ << "start_token_, ";
144 os_ << iterator_ << " const ";
148 os_ << "const " << iterator_;
152 os_ << " std::size_t &unique_id_";
154 if (sm_._seen_BOL_assertion || !optimise_parameters_)
156 os_ << ", bool &beg_of_line_";
161 os_ << " static const std::size_t npos = static_cast"
162 "<std::size_t>(~0);\n";
163 os_ << "\n if (start_token_ == end_)\n";
165 os_ << " unique_id_ = npos;\n";
166 os_ << " return 0;\n";
174 os_ << " Iterator curr_ = start_token_;\n";
175 os_ << " bool end_state_ = false;\n";
176 os_ << " std::size_t id_ = npos;\n";
177 os_ << " std::size_t uid_ = npos;\n";
181 os_ << " std::size_t end_start_state_ = start_state_;\n";
184 if (sm_._seen_BOL_assertion)
186 os_ << " bool bol_ = beg_of_line_;\n";
187 os_ << " bool end_bol_ = bol_;\n";
190 os_ << " Iterator end_token_ = start_token_;\n";
195 os_ << " switch (start_state_)\n";
198 for (std::size_t i_ = 0; i_ < dfas_; ++i_)
200 os_ << " case " << i_ << ":\n";
201 os_ << " goto " << i_ << "_0;\n";
202 os_ << " // Not needed, but to prevent warnings\n";
206 os_ << " default:\n";
207 os_ << " throw std::runtime_error (\"Invalid start state!\")\n";
223 os_ << " ch_ = 0;\n\n";
225 bool need_state0_0_label = need_label0_0(state_machine_);
227 for (std::size_t dfa_ = 0; dfa_ < dfas_; ++dfa_)
229 const std::size_t states_ = iter_->states;
231 for (std::size_t state_ = 0; state_ < states_; ++state_)
233 const std::size_t transitions_ = iter_->transitions;
236 if (dfas_ > 1 || dfa_ != 0 || state_ != 0 || need_state0_0_label)
238 os_ << "state" << dfa_ << '_' << state_ << ":\n";
241 if (iter_->end_state)
243 os_ << " end_state_ = true;\n";
244 os_ << " id_ = " << iter_->id << ";\n";
245 os_ << " uid_ = " << iter_->unique_id << ";\n";
246 os_ << " end_token_ = curr_;\n";
250 os_ << " end_start_state_ = " << iter_->goto_dfa <<
254 if (sm_._seen_BOL_assertion)
256 os_ << " end_bol_ = bol_;\n";
259 if (transitions_) os_ << '\n';
262 if (t_ < transitions_ || iter_->bol_index != boost::lexer::npos ||
263 iter_->eol_index != boost::lexer::npos)
265 os_ << " if (curr_ == end_) goto end;\n\n";
266 os_ << " ch_ = *curr_;\n";
268 if (iter_->bol_index != boost::lexer::npos)
270 os_ << "\n if (bol_) goto state" << dfa_ << '_' <<
271 iter_->bol_index << ";\n\n";
274 if (iter_->eol_index != boost::lexer::npos)
276 os_ << "\n if (ch_ == '\n') goto state" << dfa_ << '_' <<
277 iter_->eol_index << ";\n\n";
280 os_ << " ++curr_;\n";
283 for (; t_ < transitions_; ++t_)
285 const char *ptr_ = iter_->token._charset.c_str();
286 const char *end_ = ptr_ + iter_->token._charset.size();
287 char start_char_ = 0;
290 bool first_char_ = true;
296 curr_char_ = *ptr_++;
298 if (*ptr_ == curr_char_ + 1)
302 start_char_ = curr_char_;
311 if (iter_->token._negated)
325 typename string_token::string temp_;
327 if (iter_->token._negated)
332 string_token::escape_char (start_char_, temp_);
333 os_ << "(ch_ >= '" << temp_;
335 string_token::escape_char (curr_char_, temp_);
336 os_ << "' && ch_ <= '" << temp_ << "')";
341 typename string_token::string temp_;
345 if (iter_->token._negated)
354 string_token::escape_char (curr_char_, temp_);
355 os_ << " '" << temp_ << "'";
360 os_ << ") goto state" << dfa_ << '_' << iter_->goto_state <<
365 if (!(dfa_ == dfas_ - 1 && state_ == states_ - 1))
367 os_ << " goto end;\n";
370 if (transitions_ == 0) ++iter_;
375 os_ << " if (end_state_)\n";
377 os_ << " // return longest match\n";
381 os_ << " start_state_ = end_start_state_;\n";
384 if (sm_._seen_BOL_assertion && dfas_ < 2)
386 os_ << " beg_of_line_ = end_bol_;\n";
389 os_ << " start_token_ = end_token_;\n";
394 os_ << " if (id_ == 0)\n";
397 if (sm_._seen_BOL_assertion)
399 os_ << " bol_ = end_bol_;\n";
402 os_ << " goto again;\n";
405 if (sm_._seen_BOL_assertion)
409 os_ << " beg_of_line_ = end_bol_;\n";
418 if (sm_._seen_BOL_assertion)
420 os_ << " beg_of_line_ = *start_token_ == '\\n';\n";
425 os_ << " // No match causes char to be skipped\n";
426 os_ << " ++start_token_;\n";
429 os_ << " id_ = npos;\n";
430 os_ << " uid_ = npos;\n";
433 os_ << " unique_id_ = uid_;\n";
434 os_ << " return id_;\n";