functor_data.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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_FUNCTOR_DATA_JUN_10_2009_0954AM)
  6. #define BOOST_SPIRIT_LEX_LEXER_FUNCTOR_DATA_JUN_10_2009_0954AM
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <boost/spirit/home/qi/detail/assign_to.hpp>
  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/state_machine.hpp>
  14. #include <boost/spirit/home/lex/lexer/lexertl/iterator_tokenizer.hpp>
  15. #include <boost/spirit/home/lex/lexer/lexertl/semantic_action_data.hpp>
  16. #include <boost/spirit/home/lex/lexer/lexertl/wrap_action.hpp>
  17. #include <boost/mpl/bool.hpp>
  18. #include <boost/optional.hpp>
  19. #include <iterator> // for std::iterator_traits
  20. namespace boost { namespace spirit { namespace lex { namespace lexertl
  21. {
  22. namespace detail
  23. {
  24. ///////////////////////////////////////////////////////////////////////
  25. template <typename Iterator, typename HasActors, typename HasState
  26. , typename TokenValue>
  27. class data; // no default specialization
  28. ///////////////////////////////////////////////////////////////////////
  29. // neither supports state, nor actors
  30. template <typename Iterator, typename TokenValue>
  31. class data<Iterator, mpl::false_, mpl::false_, TokenValue>
  32. {
  33. protected:
  34. typedef typename
  35. std::iterator_traits<Iterator>::value_type
  36. char_type;
  37. public:
  38. typedef Iterator base_iterator_type;
  39. typedef iterator_range<Iterator> token_value_type;
  40. typedef token_value_type get_value_type;
  41. typedef std::size_t state_type;
  42. typedef char_type const* state_name_type;
  43. typedef unused_type semantic_actions_type;
  44. typedef detail::wrap_action<unused_type, Iterator, data, std::size_t>
  45. wrap_action_type;
  46. typedef unused_type next_token_functor;
  47. typedef unused_type get_state_name_type;
  48. // initialize the shared data
  49. template <typename IterData>
  50. data (IterData const& data_, Iterator& first, Iterator const& last)
  51. : first_(first), last_(last)
  52. , state_machine_(data_.state_machine_)
  53. , rules_(data_.rules_)
  54. , bol_(data_.state_machine_.data()._seen_BOL_assertion) {}
  55. // The following functions are used by the implementation of the
  56. // placeholder '_state'.
  57. template <typename Char>
  58. void set_state_name (Char const*)
  59. {
  60. // some (random) versions of gcc instantiate this function even if it's not
  61. // needed leading to false static asserts
  62. #if !defined(__GNUC__)
  63. // If you see a compile time assertion below you're probably
  64. // using a token type not supporting lexer states (the 3rd
  65. // template parameter of the token is mpl::false_), but your
  66. // code uses state changes anyways.
  67. BOOST_STATIC_ASSERT(false);
  68. #endif
  69. }
  70. char_type const* get_state_name() const { return rules_.initial(); }
  71. std::size_t get_state_id (char_type const*) const
  72. {
  73. return 0;
  74. }
  75. // The function get_eoi() is used by the implementation of the
  76. // placeholder '_eoi'.
  77. Iterator const& get_eoi() const { return last_; }
  78. // The function less() is used by the implementation of the support
  79. // function lex::less(). Its functionality is equivalent to flex'
  80. // function yyless(): it returns an iterator positioned to the
  81. // nth input character beyond the current start iterator (i.e. by
  82. // assigning the return value to the placeholder '_end' it is
  83. // possible to return all but the first n characters of the current
  84. // token back to the input stream.
  85. //
  86. // This function does nothing as long as no semantic actions are
  87. // used.
  88. Iterator const& less(Iterator const& it, int)
  89. {
  90. // The following assertion fires most likely because you are
  91. // using lexer semantic actions without using the actor_lexer
  92. // as the base class for your token definition class.
  93. BOOST_ASSERT(false &&
  94. "Are you using lexer semantic actions without using the "
  95. "actor_lexer base?");
  96. return it;
  97. }
  98. // The function more() is used by the implementation of the support
  99. // function lex::more(). Its functionality is equivalent to flex'
  100. // function yymore(): it tells the lexer that the next time it
  101. // matches a rule, the corresponding token should be appended onto
  102. // the current token value rather than replacing it.
  103. //
  104. // These functions do nothing as long as no semantic actions are
  105. // used.
  106. void more()
  107. {
  108. // The following assertion fires most likely because you are
  109. // using lexer semantic actions without using the actor_lexer
  110. // as the base class for your token definition class.
  111. BOOST_ASSERT(false &&
  112. "Are you using lexer semantic actions without using the "
  113. "actor_lexer base?");
  114. }
  115. bool adjust_start() { return false; }
  116. void revert_adjust_start() {}
  117. // The function lookahead() is used by the implementation of the
  118. // support function lex::lookahead. It can be used to implement
  119. // lookahead for lexer engines not supporting constructs like flex'
  120. // a/b (match a, but only when followed by b):
  121. //
  122. // This function does nothing as long as no semantic actions are
  123. // used.
  124. bool lookahead(std::size_t, std::size_t /*state*/ = std::size_t(~0))
  125. {
  126. // The following assertion fires most likely because you are
  127. // using lexer semantic actions without using the actor_lexer
  128. // as the base class for your token definition class.
  129. BOOST_ASSERT(false &&
  130. "Are you using lexer semantic actions without using the "
  131. "actor_lexer base?");
  132. return false;
  133. }
  134. // the functions next, invoke_actions, and get_state are used by
  135. // the functor implementation below
  136. // The function next() tries to match the next token from the
  137. // underlying input sequence.
  138. std::size_t next(Iterator& end, std::size_t& unique_id, bool& prev_bol)
  139. {
  140. prev_bol = bol_;
  141. typedef basic_iterator_tokeniser<Iterator> tokenizer;
  142. return tokenizer::next(state_machine_, bol_, end, last_
  143. , unique_id);
  144. }
  145. // nothing to invoke, so this is empty
  146. BOOST_SCOPED_ENUM(pass_flags) invoke_actions(std::size_t
  147. , std::size_t, std::size_t, Iterator const&)
  148. {
  149. return pass_flags::pass_normal; // always accept
  150. }
  151. std::size_t get_state() const { return 0; }
  152. void set_state(std::size_t) {}
  153. void set_end(Iterator const& /*it*/) {}
  154. Iterator& get_first() { return first_; }
  155. Iterator const& get_first() const { return first_; }
  156. Iterator const& get_last() const { return last_; }
  157. iterator_range<Iterator> get_value() const
  158. {
  159. return iterator_range<Iterator>(first_, last_);
  160. }
  161. bool has_value() const { return false; }
  162. void reset_value() {}
  163. void reset_bol(bool bol) { bol_ = bol; }
  164. protected:
  165. Iterator& first_;
  166. Iterator last_;
  167. boost::lexer::basic_state_machine<char_type> const& state_machine_;
  168. boost::lexer::basic_rules<char_type> const& rules_;
  169. bool bol_; // helper storing whether last character was \n
  170. // silence MSVC warning C4512: assignment operator could not be generated
  171. BOOST_DELETED_FUNCTION(data& operator= (data const&))
  172. };
  173. ///////////////////////////////////////////////////////////////////////
  174. // doesn't support lexer semantic actions, but supports state
  175. template <typename Iterator, typename TokenValue>
  176. class data<Iterator, mpl::false_, mpl::true_, TokenValue>
  177. : public data<Iterator, mpl::false_, mpl::false_, TokenValue>
  178. {
  179. protected:
  180. typedef data<Iterator, mpl::false_, mpl::false_, TokenValue> base_type;
  181. typedef typename base_type::char_type char_type;
  182. public:
  183. typedef Iterator base_iterator_type;
  184. typedef iterator_range<Iterator> token_value_type;
  185. typedef token_value_type get_value_type;
  186. typedef typename base_type::state_type state_type;
  187. typedef typename base_type::state_name_type state_name_type;
  188. typedef typename base_type::semantic_actions_type
  189. semantic_actions_type;
  190. // initialize the shared data
  191. template <typename IterData>
  192. data (IterData const& data_, Iterator& first, Iterator const& last)
  193. : base_type(data_, first, last)
  194. , state_(0) {}
  195. // The following functions are used by the implementation of the
  196. // placeholder '_state'.
  197. void set_state_name (char_type const* new_state)
  198. {
  199. std::size_t state_id = this->rules_.state(new_state);
  200. // If the following assertion fires you've probably been using
  201. // a lexer state name which was not defined in your token
  202. // definition.
  203. BOOST_ASSERT(state_id != boost::lexer::npos);
  204. if (state_id != boost::lexer::npos)
  205. state_ = state_id;
  206. }
  207. char_type const* get_state_name() const
  208. {
  209. return this->rules_.state(state_);
  210. }
  211. std::size_t get_state_id (char_type const* state) const
  212. {
  213. return this->rules_.state(state);
  214. }
  215. // the functions next() and get_state() are used by the functor
  216. // implementation below
  217. // The function next() tries to match the next token from the
  218. // underlying input sequence.
  219. std::size_t next(Iterator& end, std::size_t& unique_id, bool& prev_bol)
  220. {
  221. prev_bol = this->bol_;
  222. typedef basic_iterator_tokeniser<Iterator> tokenizer;
  223. return tokenizer::next(this->state_machine_, state_,
  224. this->bol_, end, this->get_eoi(), unique_id);
  225. }
  226. std::size_t& get_state() { return state_; }
  227. void set_state(std::size_t state) { state_ = state; }
  228. protected:
  229. std::size_t state_;
  230. // silence MSVC warning C4512: assignment operator could not be generated
  231. BOOST_DELETED_FUNCTION(data& operator= (data const&))
  232. };
  233. ///////////////////////////////////////////////////////////////////////
  234. // does support lexer semantic actions, may support state
  235. template <typename Iterator, typename HasState, typename TokenValue>
  236. class data<Iterator, mpl::true_, HasState, TokenValue>
  237. : public data<Iterator, mpl::false_, HasState, TokenValue>
  238. {
  239. public:
  240. typedef semantic_actions<Iterator, HasState, data>
  241. semantic_actions_type;
  242. protected:
  243. typedef data<Iterator, mpl::false_, HasState, TokenValue> base_type;
  244. typedef typename base_type::char_type char_type;
  245. typedef typename semantic_actions_type::functor_wrapper_type
  246. functor_wrapper_type;
  247. public:
  248. typedef Iterator base_iterator_type;
  249. typedef TokenValue token_value_type;
  250. typedef TokenValue const& get_value_type;
  251. typedef typename base_type::state_type state_type;
  252. typedef typename base_type::state_name_type state_name_type;
  253. typedef detail::wrap_action<functor_wrapper_type
  254. , Iterator, data, std::size_t> wrap_action_type;
  255. template <typename IterData>
  256. data (IterData const& data_, Iterator& first, Iterator const& last)
  257. : base_type(data_, first, last)
  258. , actions_(data_.actions_), hold_(), end_()
  259. , value_(iterator_range<Iterator>(last, last))
  260. , has_value_(false), has_hold_(false) {}
  261. // invoke attached semantic actions, if defined
  262. BOOST_SCOPED_ENUM(pass_flags) invoke_actions(std::size_t state
  263. , std::size_t& id, std::size_t unique_id, Iterator& end)
  264. {
  265. return actions_.invoke_actions(state, id, unique_id, end, *this);
  266. }
  267. // The function less() is used by the implementation of the support
  268. // function lex::less(). Its functionality is equivalent to flex'
  269. // function yyless(): it returns an iterator positioned to the
  270. // nth input character beyond the current start iterator (i.e. by
  271. // assigning the return value to the placeholder '_end' it is
  272. // possible to return all but the first n characters of the current
  273. // token back to the input stream).
  274. Iterator const& less(Iterator& it, int n)
  275. {
  276. it = this->get_first();
  277. std::advance(it, n);
  278. return it;
  279. }
  280. // The function more() is used by the implementation of the support
  281. // function lex::more(). Its functionality is equivalent to flex'
  282. // function yymore(): it tells the lexer that the next time it
  283. // matches a rule, the corresponding token should be appended onto
  284. // the current token value rather than replacing it.
  285. void more()
  286. {
  287. hold_ = this->get_first();
  288. has_hold_ = true;
  289. }
  290. // The function lookahead() is used by the implementation of the
  291. // support function lex::lookahead. It can be used to implement
  292. // lookahead for lexer engines not supporting constructs like flex'
  293. // a/b (match a, but only when followed by b)
  294. bool lookahead(std::size_t id, std::size_t state = std::size_t(~0))
  295. {
  296. Iterator end = end_;
  297. std::size_t unique_id = boost::lexer::npos;
  298. bool bol = this->bol_;
  299. if (std::size_t(~0) == state)
  300. state = this->state_;
  301. typedef basic_iterator_tokeniser<Iterator> tokenizer;
  302. return id == tokenizer::next(this->state_machine_, state,
  303. bol, end, this->get_eoi(), unique_id);
  304. }
  305. // The adjust_start() and revert_adjust_start() are helper
  306. // functions needed to implement the functionality required for
  307. // lex::more(). It is called from the functor body below.
  308. bool adjust_start()
  309. {
  310. if (!has_hold_)
  311. return false;
  312. std::swap(this->get_first(), hold_);
  313. has_hold_ = false;
  314. return true;
  315. }
  316. void revert_adjust_start()
  317. {
  318. // this will be called only if adjust_start above returned true
  319. std::swap(this->get_first(), hold_);
  320. has_hold_ = true;
  321. }
  322. TokenValue const& get_value() const
  323. {
  324. if (!has_value_) {
  325. value_ = iterator_range<Iterator>(this->get_first(), end_);
  326. has_value_ = true;
  327. }
  328. return value_;
  329. }
  330. template <typename Value>
  331. void set_value(Value const& val)
  332. {
  333. value_ = val;
  334. has_value_ = true;
  335. }
  336. void set_end(Iterator const& it)
  337. {
  338. end_ = it;
  339. }
  340. bool has_value() const { return has_value_; }
  341. void reset_value() { has_value_ = false; }
  342. protected:
  343. semantic_actions_type const& actions_;
  344. Iterator hold_; // iterator needed to support lex::more()
  345. Iterator end_; // iterator pointing to end of matched token
  346. mutable TokenValue value_; // token value to use
  347. mutable bool has_value_; // 'true' if value_ is valid
  348. bool has_hold_; // 'true' if hold_ is valid
  349. // silence MSVC warning C4512: assignment operator could not be generated
  350. BOOST_DELETED_FUNCTION(data& operator= (data const&))
  351. };
  352. ///////////////////////////////////////////////////////////////////////
  353. // does support lexer semantic actions, may support state, is used for
  354. // position_token exposing exactly one type
  355. template <typename Iterator, typename HasState, typename TokenValue>
  356. class data<Iterator, mpl::true_, HasState, boost::optional<TokenValue> >
  357. : public data<Iterator, mpl::false_, HasState, TokenValue>
  358. {
  359. public:
  360. typedef semantic_actions<Iterator, HasState, data>
  361. semantic_actions_type;
  362. protected:
  363. typedef data<Iterator, mpl::false_, HasState, TokenValue> base_type;
  364. typedef typename base_type::char_type char_type;
  365. typedef typename semantic_actions_type::functor_wrapper_type
  366. functor_wrapper_type;
  367. public:
  368. typedef Iterator base_iterator_type;
  369. typedef boost::optional<TokenValue> token_value_type;
  370. typedef boost::optional<TokenValue> const& get_value_type;
  371. typedef typename base_type::state_type state_type;
  372. typedef typename base_type::state_name_type state_name_type;
  373. typedef detail::wrap_action<functor_wrapper_type
  374. , Iterator, data, std::size_t> wrap_action_type;
  375. template <typename IterData>
  376. data (IterData const& data_, Iterator& first, Iterator const& last)
  377. : base_type(data_, first, last)
  378. , actions_(data_.actions_), hold_()
  379. , has_value_(false), has_hold_(false)
  380. {
  381. spirit::traits::assign_to(first, last, value_);
  382. has_value_ = true;
  383. }
  384. // invoke attached semantic actions, if defined
  385. BOOST_SCOPED_ENUM(pass_flags) invoke_actions(std::size_t state
  386. , std::size_t& id, std::size_t unique_id, Iterator& end)
  387. {
  388. return actions_.invoke_actions(state, id, unique_id, end, *this);
  389. }
  390. // The function less() is used by the implementation of the support
  391. // function lex::less(). Its functionality is equivalent to flex'
  392. // function yyless(): it returns an iterator positioned to the
  393. // nth input character beyond the current start iterator (i.e. by
  394. // assigning the return value to the placeholder '_end' it is
  395. // possible to return all but the first n characters of the current
  396. // token back to the input stream).
  397. Iterator const& less(Iterator& it, int n)
  398. {
  399. it = this->get_first();
  400. std::advance(it, n);
  401. return it;
  402. }
  403. // The function more() is used by the implementation of the support
  404. // function lex::more(). Its functionality is equivalent to flex'
  405. // function yymore(): it tells the lexer that the next time it
  406. // matches a rule, the corresponding token should be appended onto
  407. // the current token value rather than replacing it.
  408. void more()
  409. {
  410. hold_ = this->get_first();
  411. has_hold_ = true;
  412. }
  413. // The function lookahead() is used by the implementation of the
  414. // support function lex::lookahead. It can be used to implement
  415. // lookahead for lexer engines not supporting constructs like flex'
  416. // a/b (match a, but only when followed by b)
  417. bool lookahead(std::size_t id, std::size_t state = std::size_t(~0))
  418. {
  419. Iterator end = end_;
  420. std::size_t unique_id = boost::lexer::npos;
  421. bool bol = this->bol_;
  422. if (std::size_t(~0) == state)
  423. state = this->state_;
  424. typedef basic_iterator_tokeniser<Iterator> tokenizer;
  425. return id == tokenizer::next(this->state_machine_, state,
  426. bol, end, this->get_eoi(), unique_id);
  427. }
  428. // The adjust_start() and revert_adjust_start() are helper
  429. // functions needed to implement the functionality required for
  430. // lex::more(). It is called from the functor body below.
  431. bool adjust_start()
  432. {
  433. if (!has_hold_)
  434. return false;
  435. std::swap(this->get_first(), hold_);
  436. has_hold_ = false;
  437. return true;
  438. }
  439. void revert_adjust_start()
  440. {
  441. // this will be called only if adjust_start above returned true
  442. std::swap(this->get_first(), hold_);
  443. has_hold_ = true;
  444. }
  445. token_value_type const& get_value() const
  446. {
  447. if (!has_value_) {
  448. spirit::traits::assign_to(this->get_first(), end_, value_);
  449. has_value_ = true;
  450. }
  451. return value_;
  452. }
  453. template <typename Value>
  454. void set_value(Value const& val)
  455. {
  456. value_ = val;
  457. has_value_ = true;
  458. }
  459. void set_end(Iterator const& it)
  460. {
  461. end_ = it;
  462. }
  463. bool has_value() const { return has_value_; }
  464. void reset_value() { has_value_ = false; }
  465. protected:
  466. semantic_actions_type const& actions_;
  467. Iterator hold_; // iterator needed to support lex::more()
  468. Iterator end_; // iterator pointing to end of matched token
  469. mutable token_value_type value_; // token value to use
  470. mutable bool has_value_; // 'true' if value_ is valid
  471. bool has_hold_; // 'true' if hold_ is valid
  472. // silence MSVC warning C4512: assignment operator could not be generated
  473. BOOST_DELETED_FUNCTION(data& operator= (data const&))
  474. };
  475. }
  476. }}}}
  477. #endif