token_def.hpp 9.1 KB

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