functor_data.hpp 23 KB

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