token.hpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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_TOKEN_FEB_10_2008_0751PM)
  6. #define BOOST_SPIRIT_LEX_TOKEN_FEB_10_2008_0751PM
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <boost/config.hpp>
  11. #include <boost/detail/workaround.hpp>
  12. #include <boost/spirit/home/qi/detail/assign_to.hpp>
  13. #include <boost/spirit/home/support/attributes.hpp>
  14. #include <boost/spirit/home/support/argument.hpp>
  15. #include <boost/spirit/home/support/detail/lexer/generator.hpp>
  16. #include <boost/spirit/home/support/detail/lexer/rules.hpp>
  17. #include <boost/spirit/home/support/detail/lexer/consts.hpp>
  18. #include <boost/spirit/home/support/utree/utree_traits_fwd.hpp>
  19. #include <boost/spirit/home/lex/lexer/terminals.hpp>
  20. #include <boost/fusion/include/vector.hpp>
  21. #include <boost/fusion/include/at.hpp>
  22. #include <boost/fusion/include/value_at.hpp>
  23. #include <boost/detail/iterator.hpp>
  24. #include <boost/variant.hpp>
  25. #include <boost/mpl/bool.hpp>
  26. #include <boost/mpl/vector.hpp>
  27. #include <boost/mpl/is_sequence.hpp>
  28. #include <boost/mpl/begin.hpp>
  29. #include <boost/mpl/insert.hpp>
  30. #include <boost/mpl/vector.hpp>
  31. #include <boost/mpl/if.hpp>
  32. #include <boost/mpl/or.hpp>
  33. #include <boost/type_traits/is_same.hpp>
  34. #include <boost/range/iterator_range.hpp>
  35. #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
  36. #include <boost/static_assert.hpp>
  37. #endif
  38. #if defined(BOOST_SPIRIT_DEBUG)
  39. #include <iosfwd>
  40. #endif
  41. namespace boost { namespace spirit { namespace lex { namespace lexertl
  42. {
  43. ///////////////////////////////////////////////////////////////////////////
  44. //
  45. // The token is the type of the objects returned by default by the
  46. // iterator.
  47. //
  48. // template parameters:
  49. // Iterator The type of the iterator used to access the
  50. // underlying character stream.
  51. // AttributeTypes A mpl sequence containing the types of all
  52. // required different token values to be supported
  53. // by this token type.
  54. // HasState A mpl::bool_ indicating, whether this token type
  55. // should support lexer states.
  56. // Idtype The type to use for the token id (defaults to
  57. // std::size_t).
  58. //
  59. // It is possible to use other token types with the spirit::lex
  60. // framework as well. If you plan to use a different type as your token
  61. // type, you'll need to expose the following things from your token type
  62. // to make it compatible with spirit::lex:
  63. //
  64. // typedefs
  65. // iterator_type The type of the iterator used to access the
  66. // underlying character stream.
  67. //
  68. // id_type The type of the token id used.
  69. //
  70. // methods
  71. // default constructor
  72. // This should initialize the token as an end of
  73. // input token.
  74. // constructors The prototype of the other required
  75. // constructors should be:
  76. //
  77. // token(int)
  78. // This constructor should initialize the token as
  79. // an invalid token (not carrying any specific
  80. // values)
  81. //
  82. // where: the int is used as a tag only and its value is
  83. // ignored
  84. //
  85. // and:
  86. //
  87. // token(Idtype id, std::size_t state,
  88. // iterator_type first, iterator_type last);
  89. //
  90. // where: id: token id
  91. // state: lexer state this token was matched in
  92. // first, last: pair of iterators marking the matched
  93. // range in the underlying input stream
  94. //
  95. // accessors
  96. // id() return the token id of the matched input sequence
  97. // id(newid) set the token id of the token instance
  98. //
  99. // state() return the lexer state this token was matched in
  100. //
  101. // value() return the token value
  102. //
  103. // Additionally, you will have to implement a couple of helper functions
  104. // in the same namespace as the token type: a comparison operator==() to
  105. // compare your token instances, a token_is_valid() function and different
  106. // specializations of the Spirit customization point
  107. // assign_to_attribute_from_value as shown below.
  108. //
  109. ///////////////////////////////////////////////////////////////////////////
  110. template <typename Iterator = char const*
  111. , typename AttributeTypes = mpl::vector0<>
  112. , typename HasState = mpl::true_
  113. , typename Idtype = std::size_t>
  114. struct token;
  115. ///////////////////////////////////////////////////////////////////////////
  116. // This specialization of the token type doesn't contain any item data and
  117. // doesn't support working with lexer states.
  118. ///////////////////////////////////////////////////////////////////////////
  119. template <typename Iterator, typename Idtype>
  120. struct token<Iterator, lex::omit, mpl::false_, Idtype>
  121. {
  122. typedef Iterator iterator_type;
  123. typedef mpl::false_ has_state;
  124. typedef Idtype id_type;
  125. typedef unused_type token_value_type;
  126. // default constructed tokens correspond to EOI tokens
  127. token() : id_(id_type(boost::lexer::npos)) {}
  128. // construct an invalid token
  129. explicit token(int) : id_(id_type(0)) {}
  130. token(id_type id, std::size_t) : id_(id) {}
  131. token(id_type id, std::size_t, token_value_type)
  132. : id_(id) {}
  133. token_value_type& value() { static token_value_type u; return u; }
  134. token_value_type const& value() const { return unused; }
  135. #if defined(BOOST_SPIRIT_DEBUG)
  136. token(id_type id, std::size_t, Iterator const& first
  137. , Iterator const& last)
  138. : matched_(first, last)
  139. , id_(id) {}
  140. #else
  141. token(id_type id, std::size_t, Iterator const&, Iterator const&)
  142. : id_(id) {}
  143. #endif
  144. // this default conversion operator is needed to allow the direct
  145. // usage of tokens in conjunction with the primitive parsers defined
  146. // in Qi
  147. operator id_type() const { return id_; }
  148. // Retrieve or set the token id of this token instance.
  149. id_type id() const { return id_; }
  150. void id(id_type newid) { id_ = newid; }
  151. std::size_t state() const { return 0; } // always '0' (INITIAL state)
  152. bool is_valid() const
  153. {
  154. return 0 != id_ && id_type(boost::lexer::npos) != id_;
  155. }
  156. #if defined(BOOST_SPIRIT_DEBUG)
  157. #if BOOST_WORKAROUND(BOOST_MSVC, == 1600)
  158. // workaround for MSVC10 which has problems copying a default
  159. // constructed iterator_range
  160. token& operator= (token const& rhs)
  161. {
  162. if (this != &rhs)
  163. {
  164. id_ = rhs.id_;
  165. if (is_valid())
  166. matched_ = rhs.matched_;
  167. }
  168. return *this;
  169. }
  170. #endif
  171. std::pair<Iterator, Iterator> matched_;
  172. #endif
  173. protected:
  174. id_type id_; // token id, 0 if nothing has been matched
  175. };
  176. #if defined(BOOST_SPIRIT_DEBUG)
  177. template <typename Char, typename Traits, typename Iterator
  178. , typename AttributeTypes, typename HasState, typename Idtype>
  179. inline std::basic_ostream<Char, Traits>&
  180. operator<< (std::basic_ostream<Char, Traits>& os
  181. , token<Iterator, AttributeTypes, HasState, Idtype> const& t)
  182. {
  183. if (t.is_valid()) {
  184. Iterator end = t.matched_.second;
  185. for (Iterator it = t.matched_.first; it != end; ++it)
  186. os << *it;
  187. }
  188. else {
  189. os << "<invalid token>";
  190. }
  191. return os;
  192. }
  193. #endif
  194. ///////////////////////////////////////////////////////////////////////////
  195. // This specialization of the token type doesn't contain any item data but
  196. // supports working with lexer states.
  197. ///////////////////////////////////////////////////////////////////////////
  198. template <typename Iterator, typename Idtype>
  199. struct token<Iterator, lex::omit, mpl::true_, Idtype>
  200. : token<Iterator, lex::omit, mpl::false_, Idtype>
  201. {
  202. private:
  203. typedef token<Iterator, lex::omit, mpl::false_, Idtype> base_type;
  204. public:
  205. typedef typename base_type::id_type id_type;
  206. typedef Iterator iterator_type;
  207. typedef mpl::true_ has_state;
  208. typedef unused_type token_value_type;
  209. // default constructed tokens correspond to EOI tokens
  210. token() : state_(boost::lexer::npos) {}
  211. // construct an invalid token
  212. explicit token(int) : base_type(0), state_(boost::lexer::npos) {}
  213. token(id_type id, std::size_t state)
  214. : base_type(id, boost::lexer::npos), state_(state) {}
  215. token(id_type id, std::size_t state, token_value_type)
  216. : base_type(id, boost::lexer::npos, unused)
  217. , state_(state) {}
  218. token(id_type id, std::size_t state
  219. , Iterator const& first, Iterator const& last)
  220. : base_type(id, boost::lexer::npos, first, last)
  221. , state_(state) {}
  222. std::size_t state() const { return state_; }
  223. #if defined(BOOST_SPIRIT_DEBUG) && BOOST_WORKAROUND(BOOST_MSVC, == 1600)
  224. // workaround for MSVC10 which has problems copying a default
  225. // constructed iterator_range
  226. token& operator= (token const& rhs)
  227. {
  228. if (this != &rhs)
  229. {
  230. this->base_type::operator=(static_cast<base_type const&>(rhs));
  231. state_ = rhs.state_;
  232. }
  233. return *this;
  234. }
  235. #endif
  236. protected:
  237. std::size_t state_; // lexer state this token was matched in
  238. };
  239. ///////////////////////////////////////////////////////////////////////////
  240. // The generic version of the token type derives from the
  241. // specialization above and adds a single data member holding the item
  242. // data carried by the token instance.
  243. ///////////////////////////////////////////////////////////////////////////
  244. namespace detail
  245. {
  246. ///////////////////////////////////////////////////////////////////////
  247. // Meta-function to calculate the type of the variant data item to be
  248. // stored with each token instance.
  249. //
  250. // Note: The iterator pair needs to be the first type in the list of
  251. // types supported by the generated variant type (this is being
  252. // used to identify whether the stored data item in a particular
  253. // token instance needs to be converted from the pair of
  254. // iterators (see the first of the assign_to_attribute_from_value
  255. // specializations below).
  256. ///////////////////////////////////////////////////////////////////////
  257. template <typename IteratorPair, typename AttributeTypes>
  258. struct token_value_typesequence
  259. {
  260. typedef typename mpl::insert<
  261. AttributeTypes
  262. , typename mpl::begin<AttributeTypes>::type
  263. , IteratorPair
  264. >::type sequence_type;
  265. typedef typename make_variant_over<sequence_type>::type type;
  266. };
  267. ///////////////////////////////////////////////////////////////////////
  268. // The type of the data item stored with a token instance is defined
  269. // by the template parameter 'AttributeTypes' and may be:
  270. //
  271. // lex::omit: no data item is stored with the token
  272. // instance (this is handled by the
  273. // specializations of the token class
  274. // below)
  275. // mpl::vector0<>: each token instance stores a pair of
  276. // iterators pointing to the matched input
  277. // sequence
  278. // mpl::vector<...>: each token instance stores a variant being
  279. // able to store the pair of iterators pointing
  280. // to the matched input sequence, or any of the
  281. // types a specified in the mpl::vector<>
  282. //
  283. // All this is done to ensure the token type is as small (in terms
  284. // of its byte-size) as possible.
  285. ///////////////////////////////////////////////////////////////////////
  286. template <typename IteratorPair, typename AttributeTypes>
  287. struct token_value_type
  288. : mpl::eval_if<
  289. mpl::or_<
  290. is_same<AttributeTypes, mpl::vector0<> >
  291. , is_same<AttributeTypes, mpl::vector<> > >
  292. , mpl::identity<IteratorPair>
  293. , token_value_typesequence<IteratorPair, AttributeTypes> >
  294. {};
  295. }
  296. template <typename Iterator, typename AttributeTypes, typename HasState
  297. , typename Idtype>
  298. struct token : token<Iterator, lex::omit, HasState, Idtype>
  299. {
  300. private: // precondition assertions
  301. #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
  302. BOOST_STATIC_ASSERT((mpl::is_sequence<AttributeTypes>::value ||
  303. is_same<AttributeTypes, lex::omit>::value));
  304. #endif
  305. typedef token<Iterator, lex::omit, HasState, Idtype> base_type;
  306. protected:
  307. // If no additional token value types are given, the the token will
  308. // hold the plain pair of iterators pointing to the matched range
  309. // in the underlying input sequence. Otherwise the token value is
  310. // stored as a variant and will again hold the pair of iterators but
  311. // is able to hold any of the given data types as well. The conversion
  312. // from the iterator pair to the required data type is done when it is
  313. // accessed for the first time.
  314. typedef iterator_range<Iterator> iterpair_type;
  315. public:
  316. typedef typename base_type::id_type id_type;
  317. typedef typename detail::token_value_type<
  318. iterpair_type, AttributeTypes
  319. >::type token_value_type;
  320. typedef Iterator iterator_type;
  321. // default constructed tokens correspond to EOI tokens
  322. token() : value_(iterpair_type(iterator_type(), iterator_type())) {}
  323. // construct an invalid token
  324. explicit token(int)
  325. : base_type(0)
  326. , value_(iterpair_type(iterator_type(), iterator_type())) {}
  327. token(id_type id, std::size_t state, token_value_type const& value)
  328. : base_type(id, state, value)
  329. , value_(value) {}
  330. token(id_type id, std::size_t state, Iterator const& first
  331. , Iterator const& last)
  332. : base_type(id, state, first, last)
  333. , value_(iterpair_type(first, last)) {}
  334. token_value_type& value() { return value_; }
  335. token_value_type const& value() const { return value_; }
  336. #if BOOST_WORKAROUND(BOOST_MSVC, == 1600)
  337. // workaround for MSVC10 which has problems copying a default
  338. // constructed iterator_range
  339. token& operator= (token const& rhs)
  340. {
  341. if (this != &rhs)
  342. {
  343. this->base_type::operator=(static_cast<base_type const&>(rhs));
  344. if (this->is_valid())
  345. value_ = rhs.value_;
  346. }
  347. return *this;
  348. }
  349. #endif
  350. protected:
  351. token_value_type value_; // token value, by default a pair of iterators
  352. };
  353. ///////////////////////////////////////////////////////////////////////////
  354. // tokens are considered equal, if their id's match (these are unique)
  355. template <typename Iterator, typename AttributeTypes, typename HasState
  356. , typename Idtype>
  357. inline bool
  358. operator== (token<Iterator, AttributeTypes, HasState, Idtype> const& lhs,
  359. token<Iterator, AttributeTypes, HasState, Idtype> const& rhs)
  360. {
  361. return lhs.id() == rhs.id();
  362. }
  363. ///////////////////////////////////////////////////////////////////////////
  364. // This overload is needed by the multi_pass/functor_input_policy to
  365. // validate a token instance. It has to be defined in the same namespace
  366. // as the token class itself to allow ADL to find it.
  367. ///////////////////////////////////////////////////////////////////////////
  368. template <typename Iterator, typename AttributeTypes, typename HasState
  369. , typename Idtype>
  370. inline bool
  371. token_is_valid(token<Iterator, AttributeTypes, HasState, Idtype> const& t)
  372. {
  373. return t.is_valid();
  374. }
  375. }}}}
  376. namespace boost { namespace spirit { namespace traits
  377. {
  378. ///////////////////////////////////////////////////////////////////////////
  379. // We have to provide specializations for the customization point
  380. // assign_to_attribute_from_value allowing to extract the needed value
  381. // from the token.
  382. ///////////////////////////////////////////////////////////////////////////
  383. // This is called from the parse function of token_def if the token_def
  384. // has been defined to carry a special attribute type
  385. template <typename Attribute, typename Iterator, typename AttributeTypes
  386. , typename HasState, typename Idtype>
  387. struct assign_to_attribute_from_value<Attribute
  388. , lex::lexertl::token<Iterator, AttributeTypes, HasState, Idtype> >
  389. {
  390. static void
  391. call(lex::lexertl::token<Iterator, AttributeTypes, HasState, Idtype> const& t
  392. , Attribute& attr)
  393. {
  394. // The goal of this function is to avoid the conversion of the pair of
  395. // iterators (to the matched character sequence) into the token value
  396. // of the required type being done more than once. For this purpose it
  397. // checks whether the stored value type is still the default one (pair
  398. // of iterators) and if yes, replaces the pair of iterators with the
  399. // converted value to be returned from subsequent calls.
  400. if (0 == t.value().which()) {
  401. // first access to the token value
  402. typedef iterator_range<Iterator> iterpair_type;
  403. iterpair_type const& ip = boost::get<iterpair_type>(t.value());
  404. // Interestingly enough we use the assign_to() framework defined in
  405. // Spirit.Qi allowing to convert the pair of iterators to almost any
  406. // required type (assign_to(), if available, uses the standard Spirit
  407. // parsers to do the conversion).
  408. spirit::traits::assign_to(ip.begin(), ip.end(), attr);
  409. // If you get an error during the compilation of the following
  410. // assignment expression, you probably forgot to list one or more
  411. // types used as token value types (in your token_def<...>
  412. // definitions) in your definition of the token class. I.e. any token
  413. // value type used for a token_def<...> definition has to be listed
  414. // during the declaration of the token type to use. For instance let's
  415. // assume we have two token_def's:
  416. //
  417. // token_def<int> number; number = "...";
  418. // token_def<std::string> identifier; identifier = "...";
  419. //
  420. // Then you'll have to use the following token type definition
  421. // (assuming you are using the token class):
  422. //
  423. // typedef mpl::vector<int, std::string> token_values;
  424. // typedef token<base_iter_type, token_values> token_type;
  425. //
  426. // where: base_iter_type is the iterator type used to expose the
  427. // underlying input stream.
  428. //
  429. // This token_type has to be used as the second template parameter
  430. // to the lexer class:
  431. //
  432. // typedef lexer<base_iter_type, token_type> lexer_type;
  433. //
  434. // again, assuming you're using the lexer<> template for your
  435. // tokenization.
  436. typedef lex::lexertl::token<
  437. Iterator, AttributeTypes, HasState, Idtype> token_type;
  438. spirit::traits::assign_to(
  439. attr, const_cast<token_type&>(t).value()); // re-assign value
  440. }
  441. else {
  442. // reuse the already assigned value
  443. spirit::traits::assign_to(boost::get<Attribute>(t.value()), attr);
  444. }
  445. }
  446. };
  447. template <typename Attribute, typename Iterator, typename AttributeTypes
  448. , typename HasState, typename Idtype>
  449. struct assign_to_container_from_value<Attribute
  450. , lex::lexertl::token<Iterator, AttributeTypes, HasState, Idtype> >
  451. : assign_to_attribute_from_value<Attribute
  452. , lex::lexertl::token<Iterator, AttributeTypes, HasState, Idtype> >
  453. {};
  454. template <typename Iterator, typename AttributeTypes
  455. , typename HasState, typename Idtype>
  456. struct assign_to_container_from_value<utree
  457. , lex::lexertl::token<Iterator, AttributeTypes, HasState, Idtype> >
  458. : assign_to_attribute_from_value<utree
  459. , lex::lexertl::token<Iterator, AttributeTypes, HasState, Idtype> >
  460. {};
  461. template <typename Iterator>
  462. struct assign_to_container_from_value<
  463. iterator_range<Iterator>, iterator_range<Iterator> >
  464. {
  465. static void
  466. call(iterator_range<Iterator> const& val, iterator_range<Iterator>& attr)
  467. {
  468. attr = val;
  469. }
  470. };
  471. // These are called from the parse function of token_def if the token type
  472. // has no special attribute type assigned
  473. template <typename Attribute, typename Iterator, typename HasState
  474. , typename Idtype>
  475. struct assign_to_attribute_from_value<Attribute
  476. , lex::lexertl::token<Iterator, mpl::vector0<>, HasState, Idtype> >
  477. {
  478. static void
  479. call(lex::lexertl::token<Iterator, mpl::vector0<>, HasState, Idtype> const& t
  480. , Attribute& attr)
  481. {
  482. // The default type returned by the token_def parser component (if
  483. // it has no token value type assigned) is the pair of iterators
  484. // to the matched character sequence.
  485. spirit::traits::assign_to(t.value().begin(), t.value().end(), attr);
  486. }
  487. };
  488. // template <typename Attribute, typename Iterator, typename HasState
  489. // , typename Idtype>
  490. // struct assign_to_container_from_value<Attribute
  491. // , lex::lexertl::token<Iterator, mpl::vector0<>, HasState, Idtype> >
  492. // : assign_to_attribute_from_value<Attribute
  493. // , lex::lexertl::token<Iterator, mpl::vector0<>, HasState, Idtype> >
  494. // {};
  495. // same as above but using mpl::vector<> instead of mpl::vector0<>
  496. template <typename Attribute, typename Iterator, typename HasState
  497. , typename Idtype>
  498. struct assign_to_attribute_from_value<Attribute
  499. , lex::lexertl::token<Iterator, mpl::vector<>, HasState, Idtype> >
  500. {
  501. static void
  502. call(lex::lexertl::token<Iterator, mpl::vector<>, HasState, Idtype> const& t
  503. , Attribute& attr)
  504. {
  505. // The default type returned by the token_def parser component (if
  506. // it has no token value type assigned) is the pair of iterators
  507. // to the matched character sequence.
  508. spirit::traits::assign_to(t.value().begin(), t.value().end(), attr);
  509. }
  510. };
  511. // template <typename Attribute, typename Iterator, typename HasState
  512. // , typename Idtype>
  513. // struct assign_to_container_from_value<Attribute
  514. // , lex::lexertl::token<Iterator, mpl::vector<>, HasState, Idtype> >
  515. // : assign_to_attribute_from_value<Attribute
  516. // , lex::lexertl::token<Iterator, mpl::vector<>, HasState, Idtype> >
  517. // {};
  518. // This is called from the parse function of token_def if the token type
  519. // has been explicitly omitted (i.e. no attribute value is used), which
  520. // essentially means that every attribute gets initialized using default
  521. // constructed values.
  522. template <typename Attribute, typename Iterator, typename HasState
  523. , typename Idtype>
  524. struct assign_to_attribute_from_value<Attribute
  525. , lex::lexertl::token<Iterator, lex::omit, HasState, Idtype> >
  526. {
  527. static void
  528. call(lex::lexertl::token<Iterator, lex::omit, HasState, Idtype> const& t
  529. , Attribute& attr)
  530. {
  531. // do nothing
  532. }
  533. };
  534. template <typename Attribute, typename Iterator, typename HasState
  535. , typename Idtype>
  536. struct assign_to_container_from_value<Attribute
  537. , lex::lexertl::token<Iterator, lex::omit, HasState, Idtype> >
  538. : assign_to_attribute_from_value<Attribute
  539. , lex::lexertl::token<Iterator, lex::omit, HasState, Idtype> >
  540. {};
  541. // This is called from the parse function of lexer_def_
  542. template <typename Iterator, typename AttributeTypes, typename HasState
  543. , typename Idtype_, typename Idtype>
  544. struct assign_to_attribute_from_value<
  545. fusion::vector2<Idtype_, iterator_range<Iterator> >
  546. , lex::lexertl::token<Iterator, AttributeTypes, HasState, Idtype> >
  547. {
  548. static void
  549. call(lex::lexertl::token<Iterator, AttributeTypes, HasState, Idtype> const& t
  550. , fusion::vector2<Idtype_, iterator_range<Iterator> >& attr)
  551. {
  552. // The type returned by the lexer_def_ parser components is a
  553. // fusion::vector containing the token id of the matched token
  554. // and the pair of iterators to the matched character sequence.
  555. typedef iterator_range<Iterator> iterpair_type;
  556. typedef fusion::vector2<Idtype_, iterator_range<Iterator> >
  557. attribute_type;
  558. iterpair_type const& ip = boost::get<iterpair_type>(t.value());
  559. attr = attribute_type(t.id(), ip);
  560. }
  561. };
  562. template <typename Iterator, typename AttributeTypes, typename HasState
  563. , typename Idtype_, typename Idtype>
  564. struct assign_to_container_from_value<
  565. fusion::vector2<Idtype_, iterator_range<Iterator> >
  566. , lex::lexertl::token<Iterator, AttributeTypes, HasState, Idtype> >
  567. : assign_to_attribute_from_value<
  568. fusion::vector2<Idtype_, iterator_range<Iterator> >
  569. , lex::lexertl::token<Iterator, AttributeTypes, HasState, Idtype> >
  570. {};
  571. ///////////////////////////////////////////////////////////////////////////
  572. // Overload debug output for a single token, this integrates lexer tokens
  573. // with Qi's simple_trace debug facilities
  574. template <typename Iterator, typename Attribute, typename HasState
  575. , typename Idtype>
  576. struct token_printer_debug<
  577. lex::lexertl::token<Iterator, Attribute, HasState, Idtype> >
  578. {
  579. typedef lex::lexertl::token<Iterator, Attribute, HasState, Idtype> token_type;
  580. template <typename Out>
  581. static void print(Out& out, token_type const& val)
  582. {
  583. out << '[';
  584. spirit::traits::print_token(out, val.value());
  585. out << ']';
  586. }
  587. };
  588. }}}
  589. #endif