token_def.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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_DEF_MAR_13_2007_0145PM)
  6. #define BOOST_SPIRIT_LEX_TOKEN_DEF_MAR_13_2007_0145PM
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <boost/spirit/home/support/unused.hpp>
  11. #include <boost/spirit/home/support/argument.hpp>
  12. #include <boost/spirit/home/support/info.hpp>
  13. #include <boost/spirit/home/support/handles_container.hpp>
  14. #include <boost/spirit/home/qi/parser.hpp>
  15. #include <boost/spirit/home/qi/skip_over.hpp>
  16. #include <boost/spirit/home/qi/detail/construct.hpp>
  17. #include <boost/spirit/home/qi/detail/assign_to.hpp>
  18. #include <boost/spirit/home/lex/reference.hpp>
  19. #include <boost/spirit/home/lex/lexer_type.hpp>
  20. #include <boost/spirit/home/lex/lexer/terminals.hpp>
  21. #include <boost/fusion/include/vector.hpp>
  22. #include <boost/mpl/if.hpp>
  23. #include <boost/type_traits/is_same.hpp>
  24. #include <boost/variant.hpp>
  25. #include <iterator> // for std::iterator_traits
  26. #include <string>
  27. #include <cstdlib>
  28. #if defined(BOOST_MSVC)
  29. # pragma warning(push)
  30. # pragma warning(disable: 4355) // 'this' : used in base member initializer list warning
  31. #endif
  32. namespace boost { namespace spirit { namespace lex
  33. {
  34. ///////////////////////////////////////////////////////////////////////////
  35. // This component represents a token definition
  36. ///////////////////////////////////////////////////////////////////////////
  37. template<typename Attribute = unused_type
  38. , typename Char = char
  39. , typename Idtype = std::size_t>
  40. struct token_def
  41. : proto::extends<
  42. typename proto::terminal<
  43. lex::reference<token_def<Attribute, Char, Idtype> const, Idtype>
  44. >::type
  45. , token_def<Attribute, Char, Idtype> >
  46. , qi::parser<token_def<Attribute, Char, Idtype> >
  47. , lex::lexer_type<token_def<Attribute, Char, Idtype> >
  48. {
  49. private:
  50. // initialize proto base class
  51. typedef lex::reference<token_def const, Idtype> reference_;
  52. typedef typename proto::terminal<reference_>::type terminal_type;
  53. typedef proto::extends<terminal_type, token_def> proto_base_type;
  54. static std::size_t const all_states_id = static_cast<std::size_t>(-2);
  55. public:
  56. // Qi interface: meta-function calculating parser return type
  57. template <typename Context, typename Iterator>
  58. struct attribute
  59. {
  60. // The return value of the token_def is either the specified
  61. // attribute type, or the pair of iterators from the match of the
  62. // corresponding token (if no attribute type has been specified),
  63. // or unused_type (if omit has been specified).
  64. typedef typename Iterator::base_iterator_type iterator_type;
  65. typedef typename mpl::if_<
  66. traits::not_is_unused<Attribute>
  67. , typename mpl::if_<
  68. is_same<Attribute, lex::omit>, unused_type, Attribute
  69. >::type
  70. , iterator_range<iterator_type>
  71. >::type type;
  72. };
  73. public:
  74. // Qi interface: parse functionality
  75. template <typename Iterator, typename Context
  76. , typename Skipper, typename Attribute_>
  77. bool parse(Iterator& first, Iterator const& last
  78. , Context& /*context*/, Skipper const& skipper
  79. , Attribute_& attr) const
  80. {
  81. qi::skip_over(first, last, skipper); // always do a pre-skip
  82. if (first != last) {
  83. typedef typename
  84. std::iterator_traits<Iterator>::value_type
  85. token_type;
  86. // If the following assertion fires you probably forgot to
  87. // associate this token definition with a lexer instance.
  88. BOOST_ASSERT(std::size_t(~0) != token_state_);
  89. token_type const& t = *first;
  90. if (token_id_ == t.id() &&
  91. (all_states_id == token_state_ || token_state_ == t.state()))
  92. {
  93. spirit::traits::assign_to(t, attr);
  94. ++first;
  95. return true;
  96. }
  97. }
  98. return false;
  99. }
  100. template <typename Context>
  101. info what(Context& /*context*/) const
  102. {
  103. if (0 == def_.which())
  104. return info("token_def", boost::get<string_type>(def_));
  105. return info("token_def", boost::get<char_type>(def_));
  106. }
  107. ///////////////////////////////////////////////////////////////////////
  108. // Lex interface: collect token definitions and put it into the
  109. // provided lexer def
  110. template <typename LexerDef, typename String>
  111. void collect(LexerDef& lexdef, String const& state
  112. , String const& targetstate) const
  113. {
  114. std::size_t state_id = lexdef.add_state(state.c_str());
  115. // If the following assertion fires you are probably trying to use
  116. // a single token_def instance in more than one lexer state. This
  117. // is not possible. Please create a separate token_def instance
  118. // from the same regular expression for each lexer state it needs
  119. // to be associated with.
  120. BOOST_ASSERT(
  121. (std::size_t(~0) == token_state_ || state_id == token_state_) &&
  122. "Can't use single token_def with more than one lexer state");
  123. char_type const* target = targetstate.empty() ? 0 : targetstate.c_str();
  124. if (target)
  125. lexdef.add_state(target);
  126. token_state_ = state_id;
  127. if (0 == token_id_)
  128. token_id_ = lexdef.get_next_id();
  129. if (0 == def_.which()) {
  130. unique_id_ = lexdef.add_token(state.c_str()
  131. , boost::get<string_type>(def_), token_id_, target);
  132. }
  133. else {
  134. unique_id_ = lexdef.add_token(state.c_str()
  135. , boost::get<char_type>(def_), token_id_, target);
  136. }
  137. }
  138. template <typename LexerDef>
  139. void add_actions(LexerDef&) const {}
  140. public:
  141. typedef Char char_type;
  142. typedef Idtype id_type;
  143. typedef std::basic_string<char_type> string_type;
  144. // Lex interface: constructing token definitions
  145. token_def()
  146. : proto_base_type(terminal_type::make(reference_(*this)))
  147. , def_('\0'), token_id_()
  148. , unique_id_(std::size_t(~0)), token_state_(std::size_t(~0)) {}
  149. token_def(token_def const& rhs)
  150. : proto_base_type(terminal_type::make(reference_(*this)))
  151. , def_(rhs.def_), token_id_(rhs.token_id_)
  152. , unique_id_(rhs.unique_id_), token_state_(rhs.token_state_) {}
  153. explicit token_def(char_type def_, Idtype id_ = Idtype())
  154. : proto_base_type(terminal_type::make(reference_(*this)))
  155. , def_(def_)
  156. , token_id_(Idtype() == id_ ? Idtype(def_) : id_)
  157. , unique_id_(std::size_t(~0)), token_state_(std::size_t(~0)) {}
  158. explicit token_def(string_type const& def_, Idtype id_ = Idtype())
  159. : proto_base_type(terminal_type::make(reference_(*this)))
  160. , def_(def_), token_id_(id_)
  161. , unique_id_(std::size_t(~0)), token_state_(std::size_t(~0)) {}
  162. template <typename String>
  163. token_def& operator= (String const& definition)
  164. {
  165. def_ = definition;
  166. token_id_ = Idtype();
  167. unique_id_ = std::size_t(~0);
  168. token_state_ = std::size_t(~0);
  169. return *this;
  170. }
  171. token_def& operator= (token_def const& rhs)
  172. {
  173. def_ = rhs.def_;
  174. token_id_ = rhs.token_id_;
  175. unique_id_ = rhs.unique_id_;
  176. token_state_ = rhs.token_state_;
  177. return *this;
  178. }
  179. // general accessors
  180. Idtype const& id() const { return token_id_; }
  181. void id(Idtype const& id) { token_id_ = id; }
  182. std::size_t unique_id() const { return unique_id_; }
  183. string_type definition() const
  184. {
  185. return (0 == def_.which()) ?
  186. boost::get<string_type>(def_) :
  187. string_type(1, boost::get<char_type>(def_));
  188. }
  189. std::size_t state() const { return token_state_; }
  190. private:
  191. variant<string_type, char_type> def_;
  192. mutable Idtype token_id_;
  193. mutable std::size_t unique_id_;
  194. mutable std::size_t token_state_;
  195. };
  196. }}}
  197. namespace boost { namespace spirit { namespace traits
  198. {
  199. ///////////////////////////////////////////////////////////////////////////
  200. template<typename Attribute, typename Char, typename Idtype
  201. , typename Attr, typename Context, typename Iterator>
  202. struct handles_container<
  203. lex::token_def<Attribute, Char, Idtype>, Attr, Context, Iterator>
  204. : traits::is_container<
  205. typename attribute_of<
  206. lex::token_def<Attribute, Char, Idtype>, Context, Iterator
  207. >::type>
  208. {};
  209. }}}
  210. #if defined(BOOST_MSVC)
  211. # pragma warning(pop)
  212. #endif
  213. #endif