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