/src/contrib/boost/spirit/home/lex/lexer/lexertl/static_lexer.hpp

http://pythonocc.googlecode.com/ · C++ Header · 278 lines · 127 code · 28 blank · 123 comment · 1 complexity · e4101029c3beb6e3c7f7e0254650329c MD5 · raw file

  1. // Copyright (c) 2001-2010 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. namespace boost { namespace spirit { namespace lex { namespace lexertl
  19. {
  20. ///////////////////////////////////////////////////////////////////////////
  21. // forward declaration
  22. ///////////////////////////////////////////////////////////////////////////
  23. namespace static_
  24. {
  25. struct lexer;
  26. }
  27. ///////////////////////////////////////////////////////////////////////////
  28. //
  29. // Every lexer type to be used as a lexer for Spirit has to conform to
  30. // the following public interface:
  31. //
  32. // typedefs:
  33. // iterator_type The type of the iterator exposed by this lexer.
  34. // token_type The type of the tokens returned from the exposed
  35. // iterators.
  36. //
  37. // functions:
  38. // default constructor
  39. // Since lexers are instantiated as base classes
  40. // only it might be a good idea to make this
  41. // constructor protected.
  42. // begin, end Return a pair of iterators, when dereferenced
  43. // returning the sequence of tokens recognized in
  44. // the input stream given as the parameters to the
  45. // begin() function.
  46. // add_token Should add the definition of a token to be
  47. // recognized by this lexer.
  48. // clear Should delete all current token definitions
  49. // associated with the given state of this lexer
  50. // object.
  51. //
  52. // template parameters:
  53. // Token The type of the tokens to be returned from the
  54. // exposed token iterator.
  55. // LexerTables See explanations below.
  56. // Iterator The type of the iterator used to access the
  57. // underlying character stream.
  58. // Functor The type of the InputPolicy to use to instantiate
  59. // the multi_pass iterator type to be used as the
  60. // token iterator (returned from begin()/end()).
  61. //
  62. // Additionally, this implementation of a static lexer has a template
  63. // parameter LexerTables allowing to customize the static lexer tables
  64. // to be used. The LexerTables is expected to be a type exposing
  65. // the following functions:
  66. //
  67. // static std::size_t const state_count()
  68. //
  69. // This function needs toreturn the number of lexer states
  70. // contained in the table returned from the state_names()
  71. // function.
  72. //
  73. // static char const* const* state_names()
  74. //
  75. // This function needs to return a pointer to a table of
  76. // names of all lexer states. The table needs to have as
  77. // much entries as the state_count() function returns
  78. //
  79. // template<typename Iterator>
  80. // std::size_t next(std::size_t &start_state_, Iterator const& start_
  81. // , Iterator &start_token_, Iterator const& end_
  82. // , std::size_t& unique_id_);
  83. //
  84. // This function is expected to return the next matched
  85. // token from the underlying input stream.
  86. //
  87. ///////////////////////////////////////////////////////////////////////////
  88. ///////////////////////////////////////////////////////////////////////////
  89. //
  90. // The static_lexer class is a implementation of a Spirit.Lex
  91. // lexer on top of Ben Hanson's lexertl library (For more information
  92. // about lexertl go here: http://www.benhanson.net/lexertl.html).
  93. //
  94. // This class is designed to be used in conjunction with a generated,
  95. // static lexer. For more information see the documentation (The Static
  96. // Lexer Model).
  97. //
  98. // This class is supposed to be used as the first and only template
  99. // parameter while instantiating instances of a lex::lexer class.
  100. //
  101. ///////////////////////////////////////////////////////////////////////////
  102. template <typename Token = token<>
  103. , typename LexerTables = static_::lexer
  104. , typename Iterator = typename Token::iterator_type
  105. , typename Functor = functor<Token, detail::static_data, Iterator> >
  106. class static_lexer
  107. {
  108. private:
  109. struct dummy { void true_() {}; };
  110. typedef void (dummy::*safe_bool)();
  111. public:
  112. // object is always valid
  113. operator safe_bool() const { return &dummy::true_; }
  114. typedef typename boost::detail::iterator_traits<Iterator>::value_type
  115. 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. private:
  142. // silence MSVC warning C4512: assignment operator could not be generated
  143. iterator_data_type& operator= (iterator_data_type const&);
  144. };
  145. typedef LexerTables tables_type;
  146. // The following static assertion fires if the referenced static lexer
  147. // tables are generated by a different static lexer version as used for
  148. // the current compilation unit. Please regenerate your static lexer
  149. // tables before trying to create a static_lexer<> instance.
  150. BOOST_SPIRIT_ASSERT_MSG(
  151. tables_type::static_version == SPIRIT_STATIC_LEXER_VERSION
  152. , incompatible_static_lexer_version, (LexerTables));
  153. public:
  154. // Return the start iterator usable for iterating over the generated
  155. // tokens, the generated function next_token(...) is called to match
  156. // the next token from the input.
  157. template <typename Iterator_>
  158. iterator_type begin(Iterator_& first, Iterator_ const& last
  159. , char_type const* initial_state = 0) const
  160. {
  161. iterator_data_type iterator_data(
  162. &tables_type::template next<Iterator_>, actions_
  163. , &tables_type::state_name, tables_type::state_count()
  164. , tables_type::supports_bol
  165. );
  166. return iterator_type(iterator_data, first, last, initial_state);
  167. }
  168. // Return the end iterator usable to stop iterating over the generated
  169. // tokens.
  170. iterator_type end() const
  171. {
  172. return iterator_type();
  173. }
  174. protected:
  175. // Lexer instances can be created by means of a derived class only.
  176. static_lexer(unsigned int) : unique_id_(0) {}
  177. public:
  178. // interface for token definition management
  179. std::size_t add_token (char_type const*, char_type, std::size_t)
  180. {
  181. return unique_id_++;
  182. }
  183. std::size_t add_token (char_type const*, string_type const&
  184. , std::size_t)
  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