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