lexer.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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_LEXER_MAR_17_2007_0139PM)
  6. #define BOOST_SPIRIT_LEX_LEXER_MAR_17_2007_0139PM
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <iosfwd>
  11. #include <boost/spirit/home/support/detail/lexer/generator.hpp>
  12. #include <boost/spirit/home/support/detail/lexer/rules.hpp>
  13. #include <boost/spirit/home/support/detail/lexer/consts.hpp>
  14. #include <boost/spirit/home/support/unused.hpp>
  15. #include <boost/spirit/home/lex/lexer/lexertl/token.hpp>
  16. #include <boost/spirit/home/lex/lexer/lexertl/functor.hpp>
  17. #include <boost/spirit/home/lex/lexer/lexertl/functor_data.hpp>
  18. #include <boost/spirit/home/lex/lexer/lexertl/iterator.hpp>
  19. #if defined(BOOST_SPIRIT_LEXERTL_DEBUG)
  20. #include <boost/spirit/home/support/detail/lexer/debug.hpp>
  21. #endif
  22. #include <boost/foreach.hpp>
  23. #include <iterator> // for std::iterator_traits
  24. namespace boost { namespace spirit { namespace lex { namespace lexertl
  25. {
  26. ///////////////////////////////////////////////////////////////////////////
  27. namespace detail
  28. {
  29. ///////////////////////////////////////////////////////////////////////
  30. // The must_escape function checks if the given character value needs
  31. // to be preceded by a backslash character to disable its special
  32. // meaning in the context of a regular expression
  33. ///////////////////////////////////////////////////////////////////////
  34. template <typename Char>
  35. inline bool must_escape(Char c)
  36. {
  37. // FIXME: more needed?
  38. switch (c) {
  39. case '+': case '/': case '*': case '?':
  40. case '|':
  41. case '(': case ')':
  42. case '[': case ']':
  43. case '{': case '}':
  44. case '.':
  45. case '^': case '$':
  46. case '\\':
  47. case '"':
  48. return true;
  49. default:
  50. break;
  51. }
  52. return false;
  53. }
  54. ///////////////////////////////////////////////////////////////////////
  55. // The escape function returns the string representation of the given
  56. // character value, possibly escaped with a backslash character, to
  57. // allow it being safely used in a regular expression definition.
  58. ///////////////////////////////////////////////////////////////////////
  59. template <typename Char>
  60. inline std::basic_string<Char> escape(Char ch)
  61. {
  62. std::basic_string<Char> result(1, ch);
  63. if (detail::must_escape(ch))
  64. {
  65. typedef typename std::basic_string<Char>::size_type size_type;
  66. result.insert((size_type)0, 1, '\\');
  67. }
  68. return result;
  69. }
  70. ///////////////////////////////////////////////////////////////////////
  71. //
  72. ///////////////////////////////////////////////////////////////////////
  73. inline boost::lexer::regex_flags map_flags(unsigned int flags)
  74. {
  75. unsigned int retval = boost::lexer::none;
  76. if (flags & match_flags::match_not_dot_newline)
  77. retval |= boost::lexer::dot_not_newline;
  78. if (flags & match_flags::match_icase)
  79. retval |= boost::lexer::icase;
  80. return boost::lexer::regex_flags(retval);
  81. }
  82. }
  83. ///////////////////////////////////////////////////////////////////////////
  84. template <typename Lexer, typename F>
  85. bool generate_static(Lexer const&
  86. , std::basic_ostream<typename Lexer::char_type>&
  87. , typename Lexer::char_type const*, F);
  88. ///////////////////////////////////////////////////////////////////////////
  89. //
  90. // Every lexer type to be used as a lexer for Spirit has to conform to
  91. // the following public interface:
  92. //
  93. // typedefs:
  94. // iterator_type The type of the iterator exposed by this lexer.
  95. // token_type The type of the tokens returned from the exposed
  96. // iterators.
  97. //
  98. // functions:
  99. // default constructor
  100. // Since lexers are instantiated as base classes
  101. // only it might be a good idea to make this
  102. // constructor protected.
  103. // begin, end Return a pair of iterators, when dereferenced
  104. // returning the sequence of tokens recognized in
  105. // the input stream given as the parameters to the
  106. // begin() function.
  107. // add_token Should add the definition of a token to be
  108. // recognized by this lexer.
  109. // clear Should delete all current token definitions
  110. // associated with the given state of this lexer
  111. // object.
  112. //
  113. // template parameters:
  114. // Iterator The type of the iterator used to access the
  115. // underlying character stream.
  116. // Token The type of the tokens to be returned from the
  117. // exposed token iterator.
  118. // Functor The type of the InputPolicy to use to instantiate
  119. // the multi_pass iterator type to be used as the
  120. // token iterator (returned from begin()/end()).
  121. //
  122. ///////////////////////////////////////////////////////////////////////////
  123. ///////////////////////////////////////////////////////////////////////////
  124. //
  125. // The lexer class is a implementation of a Spirit.Lex lexer on
  126. // top of Ben Hanson's lexertl library as outlined above (For more
  127. // information about lexertl go here: http://www.benhanson.net/lexertl.html).
  128. //
  129. // This class is supposed to be used as the first and only template
  130. // parameter while instantiating instances of a lex::lexer class.
  131. //
  132. ///////////////////////////////////////////////////////////////////////////
  133. template <typename Token = token<>
  134. , typename Iterator = typename Token::iterator_type
  135. , typename Functor = functor<Token, lexertl::detail::data, Iterator> >
  136. class lexer
  137. {
  138. private:
  139. struct dummy { void true_() {} };
  140. typedef void (dummy::*safe_bool)();
  141. static std::size_t const all_states_id = static_cast<std::size_t>(-2);
  142. public:
  143. operator safe_bool() const
  144. { return initialized_dfa_ ? &dummy::true_ : 0; }
  145. typedef typename std::iterator_traits<Iterator>::value_type char_type;
  146. typedef std::basic_string<char_type> string_type;
  147. typedef boost::lexer::basic_rules<char_type> basic_rules_type;
  148. // Every lexer type to be used as a lexer for Spirit has to conform to
  149. // a public interface .
  150. typedef Token token_type;
  151. typedef typename Token::id_type id_type;
  152. typedef iterator<Functor> iterator_type;
  153. private:
  154. // this type is purely used for the iterator_type construction below
  155. struct iterator_data_type
  156. {
  157. typedef typename Functor::semantic_actions_type semantic_actions_type;
  158. iterator_data_type(
  159. boost::lexer::basic_state_machine<char_type> const& sm
  160. , boost::lexer::basic_rules<char_type> const& rules
  161. , semantic_actions_type const& actions)
  162. : state_machine_(sm), rules_(rules), actions_(actions)
  163. {}
  164. boost::lexer::basic_state_machine<char_type> const& state_machine_;
  165. boost::lexer::basic_rules<char_type> const& rules_;
  166. semantic_actions_type const& actions_;
  167. // silence MSVC warning C4512: assignment operator could not be generated
  168. BOOST_DELETED_FUNCTION(iterator_data_type& operator= (iterator_data_type const&))
  169. };
  170. public:
  171. // Return the start iterator usable for iterating over the generated
  172. // tokens.
  173. iterator_type begin(Iterator& first, Iterator const& last
  174. , char_type const* initial_state = 0) const
  175. {
  176. if (!init_dfa()) // never minimize DFA for dynamic lexers
  177. return iterator_type();
  178. iterator_data_type iterator_data(state_machine_, rules_, actions_);
  179. return iterator_type(iterator_data, first, last, initial_state);
  180. }
  181. // Return the end iterator usable to stop iterating over the generated
  182. // tokens.
  183. iterator_type end() const
  184. {
  185. return iterator_type();
  186. }
  187. protected:
  188. // Lexer instances can be created by means of a derived class only.
  189. lexer(unsigned int flags)
  190. : flags_(detail::map_flags(flags))
  191. , rules_(flags_)
  192. , initialized_dfa_(false)
  193. {}
  194. public:
  195. // interface for token definition management
  196. std::size_t add_token(char_type const* state, char_type tokendef,
  197. std::size_t token_id, char_type const* targetstate)
  198. {
  199. add_state(state);
  200. initialized_dfa_ = false;
  201. if (state == all_states())
  202. return rules_.add(state, detail::escape(tokendef), token_id, rules_.dot());
  203. if (0 == targetstate)
  204. targetstate = state;
  205. else
  206. add_state(targetstate);
  207. return rules_.add(state, detail::escape(tokendef), token_id, targetstate);
  208. }
  209. std::size_t add_token(char_type const* state, string_type const& tokendef,
  210. std::size_t token_id, char_type const* targetstate)
  211. {
  212. add_state(state);
  213. initialized_dfa_ = false;
  214. if (state == all_states())
  215. return rules_.add(state, tokendef, token_id, rules_.dot());
  216. if (0 == targetstate)
  217. targetstate = state;
  218. else
  219. add_state(targetstate);
  220. return rules_.add(state, tokendef, token_id, targetstate);
  221. }
  222. // interface for pattern definition management
  223. void add_pattern (char_type const* state, string_type const& name,
  224. string_type const& patterndef)
  225. {
  226. add_state(state);
  227. rules_.add_macro(name.c_str(), patterndef);
  228. initialized_dfa_ = false;
  229. }
  230. boost::lexer::rules const& get_rules() const { return rules_; }
  231. void clear(char_type const* state)
  232. {
  233. std::size_t s = rules_.state(state);
  234. if (boost::lexer::npos != s)
  235. rules_.clear(state);
  236. initialized_dfa_ = false;
  237. }
  238. std::size_t add_state(char_type const* state)
  239. {
  240. if (state == all_states())
  241. return all_states_id;
  242. std::size_t stateid = rules_.state(state);
  243. if (boost::lexer::npos == stateid) {
  244. stateid = rules_.add_state(state);
  245. initialized_dfa_ = false;
  246. }
  247. return stateid;
  248. }
  249. string_type initial_state() const
  250. {
  251. return string_type(rules_.initial());
  252. }
  253. string_type all_states() const
  254. {
  255. return string_type(rules_.all_states());
  256. }
  257. // Register a semantic action with the given id
  258. template <typename F>
  259. void add_action(std::size_t unique_id, std::size_t state, F act)
  260. {
  261. // If you see an error here stating add_action is not a member of
  262. // fusion::unused_type then you are probably having semantic actions
  263. // attached to at least one token in the lexer definition without
  264. // using the lex::lexertl::actor_lexer<> as its base class.
  265. typedef typename Functor::wrap_action_type wrapper_type;
  266. if (state == all_states_id) {
  267. // add the action to all known states
  268. typedef typename
  269. basic_rules_type::string_size_t_map::value_type
  270. state_type;
  271. std::size_t states = rules_.statemap().size();
  272. BOOST_FOREACH(state_type const& s, rules_.statemap()) {
  273. for (std::size_t j = 0; j < states; ++j)
  274. actions_.add_action(unique_id + j, s.second, wrapper_type::call(act));
  275. }
  276. }
  277. else {
  278. actions_.add_action(unique_id, state, wrapper_type::call(act));
  279. }
  280. }
  281. // template <typename F>
  282. // void add_action(std::size_t unique_id, char_type const* state, F act)
  283. // {
  284. // typedef typename Functor::wrap_action_type wrapper_type;
  285. // actions_.add_action(unique_id, add_state(state), wrapper_type::call(act));
  286. // }
  287. // We do not minimize the state machine by default anymore because
  288. // Ben said: "If you can afford to generate a lexer at runtime, there
  289. // is little point in calling minimise."
  290. // Go figure.
  291. bool init_dfa(bool minimize = false) const
  292. {
  293. if (!initialized_dfa_) {
  294. state_machine_.clear();
  295. typedef boost::lexer::basic_generator<char_type> generator;
  296. generator::build (rules_, state_machine_);
  297. if (minimize)
  298. generator::minimise (state_machine_);
  299. #if defined(BOOST_SPIRIT_LEXERTL_DEBUG)
  300. boost::lexer::debug::dump(state_machine_, std::cerr);
  301. #endif
  302. initialized_dfa_ = true;
  303. // // release memory held by rules description
  304. // basic_rules_type rules;
  305. // rules.init_state_info(rules_); // preserve states
  306. // std::swap(rules, rules_);
  307. }
  308. return true;
  309. }
  310. private:
  311. // lexertl specific data
  312. mutable boost::lexer::basic_state_machine<char_type> state_machine_;
  313. boost::lexer::regex_flags flags_;
  314. /*mutable*/ basic_rules_type rules_;
  315. typename Functor::semantic_actions_type actions_;
  316. mutable bool initialized_dfa_;
  317. // generator functions must be able to access members directly
  318. template <typename Lexer, typename F>
  319. friend bool generate_static(Lexer const&
  320. , std::basic_ostream<typename Lexer::char_type>&
  321. , typename Lexer::char_type const*, F);
  322. };
  323. ///////////////////////////////////////////////////////////////////////////
  324. //
  325. // The actor_lexer class is another implementation of a Spirit.Lex
  326. // lexer on top of Ben Hanson's lexertl library as outlined above (For
  327. // more information about lexertl go here:
  328. // http://www.benhanson.net/lexertl.html).
  329. //
  330. // The only difference to the lexer class above is that token_def
  331. // definitions may have semantic (lexer) actions attached while being
  332. // defined:
  333. //
  334. // int w;
  335. // token_def word = "[^ \t\n]+";
  336. // self = word[++ref(w)]; // see example: word_count_lexer
  337. //
  338. // This class is supposed to be used as the first and only template
  339. // parameter while instantiating instances of a lex::lexer class.
  340. //
  341. ///////////////////////////////////////////////////////////////////////////
  342. template <typename Token = token<>
  343. , typename Iterator = typename Token::iterator_type
  344. , typename Functor = functor<Token, lexertl::detail::data, Iterator, mpl::true_> >
  345. class actor_lexer : public lexer<Token, Iterator, Functor>
  346. {
  347. protected:
  348. // Lexer instances can be created by means of a derived class only.
  349. actor_lexer(unsigned int flags)
  350. : lexer<Token, Iterator, Functor>(flags) {}
  351. };
  352. }}}}
  353. #endif