1 // Copyright (c) 2001-2011 Hartmut Kaiser
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)
6 #if !defined(BOOST_SPIRIT_LEX_TOKEN_DEF_MAR_13_2007_0145PM)
7 #define BOOST_SPIRIT_LEX_TOKEN_DEF_MAR_13_2007_0145PM
13 #include <boost/spirit/home/support/unused.hpp>
14 #include <boost/spirit/home/support/argument.hpp>
15 #include <boost/spirit/home/support/info.hpp>
16 #include <boost/spirit/home/support/handles_container.hpp>
17 #include <boost/spirit/home/qi/parser.hpp>
18 #include <boost/spirit/home/qi/skip_over.hpp>
19 #include <boost/spirit/home/qi/detail/construct.hpp>
20 #include <boost/spirit/home/qi/detail/assign_to.hpp>
21 #include <boost/spirit/home/lex/reference.hpp>
22 #include <boost/spirit/home/lex/lexer_type.hpp>
23 #include <boost/spirit/home/lex/lexer/terminals.hpp>
25 #include <boost/fusion/include/vector.hpp>
26 #include <boost/mpl/if.hpp>
27 #include <boost/detail/iterator.hpp>
28 #include <boost/type_traits/is_same.hpp>
29 #include <boost/variant.hpp>
34 #if defined(BOOST_MSVC)
35 # pragma warning(push)
36 # pragma warning(disable: 4355) // 'this' : used in base member initializer list warning
39 namespace boost { namespace spirit { namespace lex
41 ///////////////////////////////////////////////////////////////////////////
42 // This component represents a token definition
43 ///////////////////////////////////////////////////////////////////////////
44 template<typename Attribute = unused_type
45 , typename Char = char
46 , typename Idtype = std::size_t>
49 typename proto::terminal<
50 lex::reference<token_def<Attribute, Char, Idtype> const, Idtype>
52 , token_def<Attribute, Char, Idtype> >
53 , qi::parser<token_def<Attribute, Char, Idtype> >
54 , lex::lexer_type<token_def<Attribute, Char, Idtype> >
57 // initialize proto base class
58 typedef lex::reference<token_def const, Idtype> reference_;
59 typedef typename proto::terminal<reference_>::type terminal_type;
60 typedef proto::extends<terminal_type, token_def> proto_base_type;
62 static std::size_t const all_states_id = static_cast<std::size_t>(-2);
65 // Qi interface: meta-function calculating parser return type
66 template <typename Context, typename Iterator>
69 // The return value of the token_def is either the specified
70 // attribute type, or the pair of iterators from the match of the
71 // corresponding token (if no attribute type has been specified),
72 // or unused_type (if omit has been specified).
73 typedef typename Iterator::base_iterator_type iterator_type;
74 typedef typename mpl::if_<
75 traits::not_is_unused<Attribute>
77 is_same<Attribute, lex::omit>, unused_type, Attribute
79 , iterator_range<iterator_type>
84 // Qi interface: parse functionality
85 template <typename Iterator, typename Context
86 , typename Skipper, typename Attribute_>
87 bool parse(Iterator& first, Iterator const& last
88 , Context& /*context*/, Skipper const& skipper
89 , Attribute_& attr) const
91 qi::skip_over(first, last, skipper); // always do a pre-skip
95 boost::detail::iterator_traits<Iterator>::value_type
98 // If the following assertion fires you probably forgot to
99 // associate this token definition with a lexer instance.
100 BOOST_ASSERT(std::size_t(~0) != token_state_);
102 token_type const& t = *first;
103 if (token_id_ == t.id() &&
104 (all_states_id == token_state_ || token_state_ == t.state()))
106 spirit::traits::assign_to(t, attr);
114 template <typename Context>
115 info what(Context& /*context*/) const
117 if (0 == def_.which())
118 return info("token_def", boost::get<string_type>(def_));
120 return info("token_def", boost::get<char_type>(def_));
123 ///////////////////////////////////////////////////////////////////////
124 // Lex interface: collect token definitions and put it into the
125 // provided lexer def
126 template <typename LexerDef, typename String>
127 void collect(LexerDef& lexdef, String const& state
128 , String const& targetstate) const
130 std::size_t state_id = lexdef.add_state(state.c_str());
132 // If the following assertion fires you are probably trying to use
133 // a single token_def instance in more than one lexer state. This
134 // is not possible. Please create a separate token_def instance
135 // from the same regular expression for each lexer state it needs
136 // to be associated with.
138 (std::size_t(~0) == token_state_ || state_id == token_state_) &&
139 "Can't use single token_def with more than one lexer state");
141 char_type const* target = targetstate.empty() ? 0 : targetstate.c_str();
143 lexdef.add_state(target);
145 token_state_ = state_id;
147 token_id_ = lexdef.get_next_id();
149 if (0 == def_.which()) {
150 unique_id_ = lexdef.add_token(state.c_str()
151 , boost::get<string_type>(def_), token_id_, target);
154 unique_id_ = lexdef.add_token(state.c_str()
155 , boost::get<char_type>(def_), token_id_, target);
159 template <typename LexerDef>
160 void add_actions(LexerDef&) const {}
163 typedef Char char_type;
164 typedef Idtype id_type;
165 typedef std::basic_string<char_type> string_type;
167 // Lex interface: constructing token definitions
169 : proto_base_type(terminal_type::make(reference_(*this)))
170 , def_('\0'), token_id_()
171 , unique_id_(std::size_t(~0)), token_state_(std::size_t(~0)) {}
173 token_def(token_def const& rhs)
174 : proto_base_type(terminal_type::make(reference_(*this)))
175 , def_(rhs.def_), token_id_(rhs.token_id_)
176 , unique_id_(rhs.unique_id_), token_state_(rhs.token_state_) {}
178 explicit token_def(char_type def_, Idtype id_ = Idtype())
179 : proto_base_type(terminal_type::make(reference_(*this)))
181 , token_id_(Idtype() == id_ ? Idtype(def_) : id_)
182 , unique_id_(std::size_t(~0)), token_state_(std::size_t(~0)) {}
184 explicit token_def(string_type const& def_, Idtype id_ = Idtype())
185 : proto_base_type(terminal_type::make(reference_(*this)))
186 , def_(def_), token_id_(id_)
187 , unique_id_(std::size_t(~0)), token_state_(std::size_t(~0)) {}
189 template <typename String>
190 token_def& operator= (String const& definition)
193 token_id_ = Idtype();
194 unique_id_ = std::size_t(~0);
195 token_state_ = std::size_t(~0);
198 token_def& operator= (token_def const& rhs)
201 token_id_ = rhs.token_id_;
202 unique_id_ = rhs.unique_id_;
203 token_state_ = rhs.token_state_;
208 Idtype const& id() const { return token_id_; }
209 void id(Idtype const& id) { token_id_ = id; }
210 std::size_t unique_id() const { return unique_id_; }
212 string_type definition() const
214 return (0 == def_.which()) ?
215 boost::get<string_type>(def_) :
216 string_type(1, boost::get<char_type>(def_));
218 std::size_t state() const { return token_state_; }
221 variant<string_type, char_type> def_;
222 mutable Idtype token_id_;
223 mutable std::size_t unique_id_;
224 mutable std::size_t token_state_;
228 namespace boost { namespace spirit { namespace traits
230 ///////////////////////////////////////////////////////////////////////////
231 template<typename Attribute, typename Char, typename Idtype
232 , typename Attr, typename Context, typename Iterator>
233 struct handles_container<
234 lex::token_def<Attribute, Char, Idtype>, Attr, Context, Iterator>
235 : traits::is_container<
236 typename attribute_of<
237 lex::token_def<Attribute, Char, Idtype>, Context, Iterator
242 #if defined(BOOST_MSVC)
243 # pragma warning(pop)