File indexing completed on 2025-01-31 10:02:17
0001
0002
0003
0004
0005
0006 #if !defined(BOOST_SPIRIT_LEX_TOKEN_DEF_MAR_13_2007_0145PM)
0007 #define BOOST_SPIRIT_LEX_TOKEN_DEF_MAR_13_2007_0145PM
0008
0009 #if defined(_MSC_VER)
0010 #pragma once
0011 #endif
0012
0013 #include <boost/spirit/home/support/unused.hpp>
0014 #include <boost/spirit/home/support/argument.hpp>
0015 #include <boost/spirit/home/support/info.hpp>
0016 #include <boost/spirit/home/support/handles_container.hpp>
0017 #include <boost/spirit/home/qi/parser.hpp>
0018 #include <boost/spirit/home/qi/skip_over.hpp>
0019 #include <boost/spirit/home/qi/detail/construct.hpp>
0020 #include <boost/spirit/home/qi/detail/assign_to.hpp>
0021 #include <boost/spirit/home/lex/reference.hpp>
0022 #include <boost/spirit/home/lex/lexer_type.hpp>
0023 #include <boost/spirit/home/lex/lexer/terminals.hpp>
0024
0025 #include <boost/fusion/include/vector.hpp>
0026 #include <boost/mpl/if.hpp>
0027 #include <boost/proto/extends.hpp>
0028 #include <boost/proto/traits.hpp>
0029 #include <boost/type_traits/is_same.hpp>
0030 #include <boost/variant.hpp>
0031
0032 #include <iterator> // for std::iterator_traits
0033 #include <string>
0034 #include <cstdlib>
0035
0036 #if defined(BOOST_MSVC)
0037 # pragma warning(push)
0038 # pragma warning(disable: 4355)
0039 #endif
0040
0041 namespace boost { namespace spirit { namespace lex
0042 {
0043
0044
0045
0046 template<typename Attribute = unused_type
0047 , typename Char = char
0048 , typename Idtype = std::size_t>
0049 struct token_def
0050 : proto::extends<
0051 typename proto::terminal<
0052 lex::reference<token_def<Attribute, Char, Idtype> const, Idtype>
0053 >::type
0054 , token_def<Attribute, Char, Idtype> >
0055 , qi::parser<token_def<Attribute, Char, Idtype> >
0056 , lex::lexer_type<token_def<Attribute, Char, Idtype> >
0057 {
0058 private:
0059
0060 typedef lex::reference<token_def const, Idtype> reference_;
0061 typedef typename proto::terminal<reference_>::type terminal_type;
0062 typedef proto::extends<terminal_type, token_def> proto_base_type;
0063
0064 static std::size_t const all_states_id = static_cast<std::size_t>(-2);
0065
0066 public:
0067
0068 template <typename Context, typename Iterator>
0069 struct attribute
0070 {
0071
0072
0073
0074
0075 typedef typename Iterator::base_iterator_type iterator_type;
0076 typedef typename mpl::if_<
0077 traits::not_is_unused<Attribute>
0078 , typename mpl::if_<
0079 is_same<Attribute, lex::omit>, unused_type, Attribute
0080 >::type
0081 , iterator_range<iterator_type>
0082 >::type type;
0083 };
0084
0085 public:
0086
0087 template <typename Iterator, typename Context
0088 , typename Skipper, typename Attribute_>
0089 bool parse(Iterator& first, Iterator const& last
0090 , Context& , Skipper const& skipper
0091 , Attribute_& attr) const
0092 {
0093 qi::skip_over(first, last, skipper);
0094
0095 if (first != last) {
0096 typedef typename
0097 std::iterator_traits<Iterator>::value_type
0098 token_type;
0099
0100
0101
0102 BOOST_ASSERT(std::size_t(~0) != token_state_);
0103
0104 token_type const& t = *first;
0105 if (token_id_ == t.id() &&
0106 (all_states_id == token_state_ || token_state_ == t.state()))
0107 {
0108 spirit::traits::assign_to(t, attr);
0109 ++first;
0110 return true;
0111 }
0112 }
0113 return false;
0114 }
0115
0116 template <typename Context>
0117 info what(Context& ) const
0118 {
0119 if (0 == def_.which())
0120 return info("token_def", boost::get<string_type>(def_));
0121
0122 return info("token_def", boost::get<char_type>(def_));
0123 }
0124
0125
0126
0127
0128 template <typename LexerDef, typename String>
0129 void collect(LexerDef& lexdef, String const& state
0130 , String const& targetstate) const
0131 {
0132 std::size_t state_id = lexdef.add_state(state.c_str());
0133
0134
0135
0136
0137
0138
0139 BOOST_ASSERT(
0140 (std::size_t(~0) == token_state_ || state_id == token_state_) &&
0141 "Can't use single token_def with more than one lexer state");
0142
0143 char_type const* target = targetstate.empty() ? 0 : targetstate.c_str();
0144 if (target)
0145 lexdef.add_state(target);
0146
0147 token_state_ = state_id;
0148 if (0 == token_id_)
0149 token_id_ = lexdef.get_next_id();
0150
0151 if (0 == def_.which()) {
0152 unique_id_ = lexdef.add_token(state.c_str()
0153 , boost::get<string_type>(def_), token_id_, target);
0154 }
0155 else {
0156 unique_id_ = lexdef.add_token(state.c_str()
0157 , boost::get<char_type>(def_), token_id_, target);
0158 }
0159 }
0160
0161 template <typename LexerDef>
0162 void add_actions(LexerDef&) const {}
0163
0164 public:
0165 typedef Char char_type;
0166 typedef Idtype id_type;
0167 typedef std::basic_string<char_type> string_type;
0168
0169
0170 token_def()
0171 : proto_base_type(terminal_type::make(reference_(*this)))
0172 , def_('\0'), token_id_()
0173 , unique_id_(std::size_t(~0)), token_state_(std::size_t(~0)) {}
0174
0175 token_def(token_def const& rhs)
0176 : proto_base_type(terminal_type::make(reference_(*this)))
0177 , def_(rhs.def_), token_id_(rhs.token_id_)
0178 , unique_id_(rhs.unique_id_), token_state_(rhs.token_state_) {}
0179
0180 explicit token_def(char_type def_, Idtype id_ = Idtype())
0181 : proto_base_type(terminal_type::make(reference_(*this)))
0182 , def_(def_)
0183 , token_id_(Idtype() == id_ ? Idtype(def_) : id_)
0184 , unique_id_(std::size_t(~0)), token_state_(std::size_t(~0)) {}
0185
0186 explicit token_def(string_type const& def_, Idtype id_ = Idtype())
0187 : proto_base_type(terminal_type::make(reference_(*this)))
0188 , def_(def_), token_id_(id_)
0189 , unique_id_(std::size_t(~0)), token_state_(std::size_t(~0)) {}
0190
0191 template <typename String>
0192 token_def& operator= (String const& definition)
0193 {
0194 def_ = definition;
0195 token_id_ = Idtype();
0196 unique_id_ = std::size_t(~0);
0197 token_state_ = std::size_t(~0);
0198 return *this;
0199 }
0200 token_def& operator= (token_def const& rhs)
0201 {
0202 def_ = rhs.def_;
0203 token_id_ = rhs.token_id_;
0204 unique_id_ = rhs.unique_id_;
0205 token_state_ = rhs.token_state_;
0206 return *this;
0207 }
0208
0209
0210 Idtype const& id() const { return token_id_; }
0211 void id(Idtype const& id) { token_id_ = id; }
0212 std::size_t unique_id() const { return unique_id_; }
0213
0214 string_type definition() const
0215 {
0216 return (0 == def_.which()) ?
0217 boost::get<string_type>(def_) :
0218 string_type(1, boost::get<char_type>(def_));
0219 }
0220 std::size_t state() const { return token_state_; }
0221
0222 private:
0223 variant<string_type, char_type> def_;
0224 mutable Idtype token_id_;
0225 mutable std::size_t unique_id_;
0226 mutable std::size_t token_state_;
0227 };
0228 }}}
0229
0230 namespace boost { namespace spirit { namespace traits
0231 {
0232
0233 template<typename Attribute, typename Char, typename Idtype
0234 , typename Attr, typename Context, typename Iterator>
0235 struct handles_container<
0236 lex::token_def<Attribute, Char, Idtype>, Attr, Context, Iterator>
0237 : traits::is_container<
0238 typename attribute_of<
0239 lex::token_def<Attribute, Char, Idtype>, Context, Iterator
0240 >::type>
0241 {};
0242 }}}
0243
0244 #if defined(BOOST_MSVC)
0245 # pragma warning(pop)
0246 #endif
0247
0248 #endif