static_lexer.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  2. //
  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)
  5. #if !defined(BOOST_SPIRIT_LEX_STATIC_LEXER_FEB_10_2008_0753PM)
  6. #define BOOST_SPIRIT_LEX_STATIC_LEXER_FEB_10_2008_0753PM
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <boost/spirit/home/lex/lexer/lexertl/token.hpp>
  11. #include <boost/spirit/home/lex/lexer/lexertl/functor.hpp>
  12. #include <boost/spirit/home/lex/lexer/lexertl/static_functor_data.hpp>
  13. #include <boost/spirit/home/lex/lexer/lexertl/iterator.hpp>
  14. #include <boost/spirit/home/lex/lexer/lexertl/static_version.hpp>
  15. #if defined(BOOST_SPIRIT_DEBUG)
  16. #include <boost/spirit/home/support/detail/lexer/debug.hpp>
  17. #endif
  18. #include <iterator> // for std::iterator_traits
  19. namespace boost { namespace spirit { namespace lex { namespace lexertl
  20. {
  21. ///////////////////////////////////////////////////////////////////////////
  22. // forward declaration
  23. ///////////////////////////////////////////////////////////////////////////
  24. namespace static_
  25. {
  26. struct lexer;
  27. }
  28. ///////////////////////////////////////////////////////////////////////////
  29. //
  30. // Every lexer type to be used as a lexer for Spirit has to conform to
  31. // the following public interface:
  32. //
  33. // typedefs:
  34. // iterator_type The type of the iterator exposed by this lexer.
  35. // token_type The type of the tokens returned from the exposed
  36. // iterators.
  37. //
  38. // functions:
  39. // default constructor
  40. // Since lexers are instantiated as base classes
  41. // only it might be a good idea to make this
  42. // constructor protected.
  43. // begin, end Return a pair of iterators, when dereferenced
  44. // returning the sequence of tokens recognized in
  45. // the input stream given as the parameters to the
  46. // begin() function.
  47. // add_token Should add the definition of a token to be
  48. // recognized by this lexer.
  49. // clear Should delete all current token definitions
  50. // associated with the given state of this lexer
  51. // object.
  52. //
  53. // template parameters:
  54. // Token The type of the tokens to be returned from the
  55. // exposed token iterator.
  56. // LexerTables See explanations below.
  57. // Iterator The type of the iterator used to access the
  58. // underlying character stream.
  59. // Functor The type of the InputPolicy to use to instantiate
  60. // the multi_pass iterator type to be used as the
  61. // token iterator (returned from begin()/end()).
  62. //
  63. // Additionally, this implementation of a static lexer has a template
  64. // parameter LexerTables allowing to customize the static lexer tables
  65. // to be used. The LexerTables is expected to be a type exposing
  66. // the following functions:
  67. //
  68. // static std::size_t const state_count()
  69. //
  70. // This function needs toreturn the number of lexer states
  71. // contained in the table returned from the state_names()
  72. // function.
  73. //
  74. // static char const* const* state_names()
  75. //
  76. // This function needs to return a pointer to a table of
  77. // names of all lexer states. The table needs to have as
  78. // much entries as the state_count() function returns
  79. //
  80. // template<typename Iterator>
  81. // std::size_t next(std::size_t &start_state_, Iterator const& start_
  82. // , Iterator &start_token_, Iterator const& end_
  83. // , std::size_t& unique_id_);
  84. //
  85. // This function is expected to return the next matched
  86. // token from the underlying input stream.
  87. //
  88. ///////////////////////////////////////////////////////////////////////////
  89. ///////////////////////////////////////////////////////////////////////////
  90. //
  91. // The static_lexer class is a implementation of a Spirit.Lex
  92. // lexer on top of Ben Hanson's lexertl library (For more information
  93. // about lexertl go here: http://www.benhanson.net/lexertl.html).
  94. //
  95. // This class is designed to be used in conjunction with a generated,
  96. // static lexer. For more information see the documentation (The Static
  97. // Lexer Model).
  98. //
  99. // This class is supposed to be used as the first and only template
  100. // parameter while instantiating instances of a lex::lexer class.
  101. //
  102. ///////////////////////////////////////////////////////////////////////////
  103. template <typename Token = token<>
  104. , typename LexerTables = static_::lexer
  105. , typename Iterator = typename Token::iterator_type
  106. , typename Functor = functor<Token, detail::static_data, Iterator> >
  107. class static_lexer
  108. {
  109. private:
  110. struct dummy { void true_() {} };
  111. typedef void (dummy::*safe_bool)();
  112. public:
  113. // object is always valid
  114. operator safe_bool() const { return &dummy::true_; }
  115. typedef typename std::iterator_traits<Iterator>::value_type char_type;
  116. typedef std::basic_string<char_type> string_type;
  117. // Every lexer type to be used as a lexer for Spirit has to conform to
  118. // a public interface
  119. typedef Token token_type;
  120. typedef typename Token::id_type id_type;
  121. typedef iterator<Functor> iterator_type;
  122. private:
  123. // this type is purely used for the iterator_type construction below
  124. struct iterator_data_type
  125. {
  126. typedef typename Functor::next_token_functor next_token_functor;
  127. typedef typename Functor::semantic_actions_type semantic_actions_type;
  128. typedef typename Functor::get_state_name_type get_state_name_type;
  129. iterator_data_type(next_token_functor next
  130. , semantic_actions_type const& actions
  131. , get_state_name_type get_state_name, std::size_t num_states
  132. , bool bol)
  133. : next_(next), actions_(actions), get_state_name_(get_state_name)
  134. , num_states_(num_states), bol_(bol)
  135. {}
  136. next_token_functor next_;
  137. semantic_actions_type const& actions_;
  138. get_state_name_type get_state_name_;
  139. std::size_t num_states_;
  140. bool bol_;
  141. // silence MSVC warning C4512: assignment operator could not be generated
  142. BOOST_DELETED_FUNCTION(iterator_data_type& operator= (iterator_data_type const&))
  143. };
  144. typedef LexerTables tables_type;
  145. // The following static assertion fires if the referenced static lexer
  146. // tables are generated by a different static lexer version as used for
  147. // the current compilation unit. Please regenerate your static lexer
  148. // tables before trying to create a static_lexer<> instance.
  149. BOOST_SPIRIT_ASSERT_MSG(
  150. tables_type::static_version == SPIRIT_STATIC_LEXER_VERSION
  151. , incompatible_static_lexer_version, (LexerTables));
  152. public:
  153. // Return the start iterator usable for iterating over the generated
  154. // tokens, the generated function next_token(...) is called to match
  155. // the next token from the input.
  156. template <typename Iterator_>
  157. iterator_type begin(Iterator_& first, Iterator_ const& last
  158. , char_type const* initial_state = 0) const
  159. {
  160. iterator_data_type iterator_data(
  161. &tables_type::template next<Iterator_>, actions_
  162. , &tables_type::state_name, tables_type::state_count()
  163. , tables_type::supports_bol
  164. );
  165. return iterator_type(iterator_data, first, last, initial_state);
  166. }
  167. // Return the end iterator usable to stop iterating over the generated
  168. // tokens.
  169. iterator_type end() const
  170. {
  171. return iterator_type();
  172. }
  173. protected:
  174. // Lexer instances can be created by means of a derived class only.
  175. static_lexer(unsigned int) : unique_id_(0) {}
  176. public:
  177. // interface for token definition management
  178. std::size_t add_token (char_type const*, char_type, std::size_t
  179. , char_type const*)
  180. {
  181. return unique_id_++;
  182. }
  183. std::size_t add_token (char_type const*, string_type const&
  184. , std::size_t, char_type const*)
  185. {
  186. return unique_id_++;
  187. }
  188. // interface for pattern definition management
  189. void add_pattern (char_type const*, string_type const&
  190. , string_type const&) {}
  191. void clear(char_type const*) {}
  192. std::size_t add_state(char_type const* state)
  193. {
  194. return detail::get_state_id(state, &tables_type::state_name
  195. , tables_type::state_count());
  196. }
  197. string_type initial_state() const
  198. {
  199. return tables_type::state_name(0);
  200. }
  201. // register a semantic action with the given id
  202. template <typename F>
  203. void add_action(id_type unique_id, std::size_t state, F act)
  204. {
  205. typedef typename Functor::wrap_action_type wrapper_type;
  206. actions_.add_action(unique_id, state, wrapper_type::call(act));
  207. }
  208. bool init_dfa(bool minimize = false) const { return true; }
  209. private:
  210. typename Functor::semantic_actions_type actions_;
  211. std::size_t unique_id_;
  212. };
  213. ///////////////////////////////////////////////////////////////////////////
  214. //
  215. // The static_actor_lexer class is another implementation of a
  216. // Spirit.Lex lexer on top of Ben Hanson's lexertl library as outlined
  217. // above (For more information about lexertl go here:
  218. // http://www.benhanson.net/lexertl.html).
  219. //
  220. // Just as the static_lexer class it is meant to be used with
  221. // a statically generated lexer as outlined above.
  222. //
  223. // The only difference to the static_lexer class above is that
  224. // token_def definitions may have semantic (lexer) actions attached while
  225. // being defined:
  226. //
  227. // int w;
  228. // token_def<> word = "[^ \t\n]+";
  229. // self = word[++ref(w)]; // see example: word_count_lexer
  230. //
  231. // This class is supposed to be used as the first and only template
  232. // parameter while instantiating instances of a lex::lexer class.
  233. //
  234. ///////////////////////////////////////////////////////////////////////////
  235. template <typename Token = token<>
  236. , typename LexerTables = static_::lexer
  237. , typename Iterator = typename Token::iterator_type
  238. , typename Functor
  239. = functor<Token, detail::static_data, Iterator, mpl::true_> >
  240. class static_actor_lexer
  241. : public static_lexer<Token, LexerTables, Iterator, Functor>
  242. {
  243. protected:
  244. // Lexer instances can be created by means of a derived class only.
  245. static_actor_lexer(unsigned int flags)
  246. : static_lexer<Token, LexerTables, Iterator, Functor>(flags)
  247. {}
  248. };
  249. }}}}
  250. #endif