1 // Copyright (c) 2001-2011 Hartmut Kaiser
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #if !defined(BOOST_SPIRIT_LEXERTL_ITERATOR_TOKENISER_MARCH_22_2007_0859AM)
7 #define BOOST_SPIRIT_LEXERTL_ITERATOR_TOKENISER_MARCH_22_2007_0859AM
13 #include <boost/spirit/home/support/detail/lexer/state_machine.hpp>
14 #include <boost/spirit/home/support/detail/lexer/consts.hpp>
15 #include <boost/spirit/home/support/detail/lexer/size_t.hpp>
16 #include <boost/spirit/home/support/detail/lexer/char_traits.hpp>
17 #include <iterator> // for std::iterator_traits
20 namespace boost { namespace spirit { namespace lex { namespace lexertl
22 ///////////////////////////////////////////////////////////////////////////
23 template<typename Iterator>
24 class basic_iterator_tokeniser
27 typedef std::vector<std::size_t> size_t_vector;
28 typedef typename std::iterator_traits<Iterator>::value_type char_type;
30 static std::size_t next (
31 boost::lexer::basic_state_machine<char_type> const& state_machine_
32 , std::size_t &dfa_state_, bool& bol_, Iterator &start_token_
33 , Iterator const& end_, std::size_t& unique_id_)
35 if (start_token_ == end_)
37 unique_id_ = boost::lexer::npos;
42 boost::lexer::detail::internals const& internals_ =
43 state_machine_.data();
46 std::size_t const* lookup_ = &internals_._lookup[dfa_state_]->
48 std::size_t dfa_alphabet_ = internals_._dfa_alphabet[dfa_state_];
49 std::size_t const* dfa_ = &internals_._dfa[dfa_state_]->front ();
51 std::size_t const* ptr_ = dfa_ + dfa_alphabet_;
52 Iterator curr_ = start_token_;
53 bool end_state_ = *ptr_ != 0;
54 std::size_t id_ = *(ptr_ + boost::lexer::id_index);
55 std::size_t uid_ = *(ptr_ + boost::lexer::unique_id_index);
56 std::size_t end_start_state_ = dfa_state_;
58 Iterator end_token_ = start_token_;
62 std::size_t const BOL_state_ = ptr_[boost::lexer::bol_index];
63 std::size_t const EOL_state_ = ptr_[boost::lexer::eol_index];
65 if (BOL_state_ && bol)
67 ptr_ = &dfa_[BOL_state_ * dfa_alphabet_];
69 else if (EOL_state_ && *curr_ == '\n')
71 ptr_ = &dfa_[EOL_state_ * dfa_alphabet_];
76 std::iterator_traits<Iterator>::value_type
79 boost::lexer::char_traits<value_type>::index_type
83 boost::lexer::char_traits<value_type>::call(*curr_++);
84 bol = (index == '\n') ? true : false;
85 std::size_t const state_ = ptr_[
86 lookup_[static_cast<std::size_t>(index)]];
93 ptr_ = &dfa_[state_ * dfa_alphabet_];
99 id_ = *(ptr_ + boost::lexer::id_index);
100 uid_ = *(ptr_ + boost::lexer::unique_id_index);
101 end_start_state_ = *(ptr_ + boost::lexer::state_index);
107 std::size_t const EOL_state_ = ptr_[boost::lexer::eol_index];
109 if (EOL_state_ && curr_ == end_)
111 ptr_ = &dfa_[EOL_state_ * dfa_alphabet_];
116 id_ = *(ptr_ + boost::lexer::id_index);
117 uid_ = *(ptr_ + boost::lexer::unique_id_index);
118 end_start_state_ = *(ptr_ + boost::lexer::state_index);
125 // return longest match
126 dfa_state_ = end_start_state_;
127 start_token_ = end_token_;
140 bol_ = (*start_token_ == '\n') ? true : false;
141 id_ = boost::lexer::npos;
142 uid_ = boost::lexer::npos;
149 ///////////////////////////////////////////////////////////////////////
150 static std::size_t next (
151 boost::lexer::basic_state_machine<char_type> const& state_machine_
152 , bool& bol_, Iterator &start_token_, Iterator const& end_
153 , std::size_t& unique_id_)
155 if (start_token_ == end_)
157 unique_id_ = boost::lexer::npos;
162 std::size_t const* lookup_ = &state_machine_.data()._lookup[0]->front();
163 std::size_t dfa_alphabet_ = state_machine_.data()._dfa_alphabet[0];
164 std::size_t const* dfa_ = &state_machine_.data()._dfa[0]->front ();
165 std::size_t const* ptr_ = dfa_ + dfa_alphabet_;
167 Iterator curr_ = start_token_;
168 bool end_state_ = *ptr_ != 0;
169 std::size_t id_ = *(ptr_ + boost::lexer::id_index);
170 std::size_t uid_ = *(ptr_ + boost::lexer::unique_id_index);
171 bool end_bol_ = bol_;
172 Iterator end_token_ = start_token_;
174 while (curr_ != end_)
176 std::size_t const BOL_state_ = ptr_[boost::lexer::bol_index];
177 std::size_t const EOL_state_ = ptr_[boost::lexer::eol_index];
179 if (BOL_state_ && bol)
181 ptr_ = &dfa_[BOL_state_ * dfa_alphabet_];
183 else if (EOL_state_ && *curr_ == '\n')
185 ptr_ = &dfa_[EOL_state_ * dfa_alphabet_];
190 std::iterator_traits<Iterator>::value_type
193 boost::lexer::char_traits<value_type>::index_type
197 boost::lexer::char_traits<value_type>::call(*curr_++);
198 bol = (index == '\n') ? true : false;
199 std::size_t const state_ = ptr_[
200 lookup_[static_cast<std::size_t>(index)]];
207 ptr_ = &dfa_[state_ * dfa_alphabet_];
213 id_ = *(ptr_ + boost::lexer::id_index);
214 uid_ = *(ptr_ + boost::lexer::unique_id_index);
220 std::size_t const EOL_state_ = ptr_[boost::lexer::eol_index];
222 if (EOL_state_ && curr_ == end_)
224 ptr_ = &dfa_[EOL_state_ * dfa_alphabet_];
229 id_ = *(ptr_ + boost::lexer::id_index);
230 uid_ = *(ptr_ + boost::lexer::unique_id_index);
237 // return longest match
239 start_token_ = end_token_;
242 bol_ = *start_token_ == '\n';
243 id_ = boost::lexer::npos;
244 uid_ = boost::lexer::npos;