Chris@16: // Copyright (c) 2001-2011 Hartmut Kaiser Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #if !defined(BOOST_SPIRIT_LEX_TOKEN_DEF_MAR_13_2007_0145PM) Chris@16: #define BOOST_SPIRIT_LEX_TOKEN_DEF_MAR_13_2007_0145PM Chris@16: Chris@16: #if defined(_MSC_VER) Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: #if defined(BOOST_MSVC) Chris@16: # pragma warning(push) Chris@16: # pragma warning(disable: 4355) // 'this' : used in base member initializer list warning Chris@16: #endif Chris@16: Chris@16: namespace boost { namespace spirit { namespace lex Chris@16: { Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: // This component represents a token definition Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct token_def Chris@16: : proto::extends< Chris@16: typename proto::terminal< Chris@16: lex::reference const, Idtype> Chris@16: >::type Chris@16: , token_def > Chris@16: , qi::parser > Chris@16: , lex::lexer_type > Chris@16: { Chris@16: private: Chris@16: // initialize proto base class Chris@16: typedef lex::reference reference_; Chris@16: typedef typename proto::terminal::type terminal_type; Chris@16: typedef proto::extends proto_base_type; Chris@16: Chris@16: static std::size_t const all_states_id = static_cast(-2); Chris@16: Chris@16: public: Chris@16: // Qi interface: meta-function calculating parser return type Chris@16: template Chris@16: struct attribute Chris@16: { Chris@16: // The return value of the token_def is either the specified Chris@16: // attribute type, or the pair of iterators from the match of the Chris@16: // corresponding token (if no attribute type has been specified), Chris@16: // or unused_type (if omit has been specified). Chris@16: typedef typename Iterator::base_iterator_type iterator_type; Chris@16: typedef typename mpl::if_< Chris@16: traits::not_is_unused Chris@16: , typename mpl::if_< Chris@16: is_same, unused_type, Attribute Chris@16: >::type Chris@16: , iterator_range Chris@16: >::type type; Chris@16: }; Chris@16: Chris@16: public: Chris@16: // Qi interface: parse functionality Chris@16: template Chris@16: bool parse(Iterator& first, Iterator const& last Chris@16: , Context& /*context*/, Skipper const& skipper Chris@16: , Attribute_& attr) const Chris@16: { Chris@16: qi::skip_over(first, last, skipper); // always do a pre-skip Chris@16: Chris@16: if (first != last) { Chris@16: typedef typename Chris@16: boost::detail::iterator_traits::value_type Chris@16: token_type; Chris@16: Chris@16: // If the following assertion fires you probably forgot to Chris@16: // associate this token definition with a lexer instance. Chris@16: BOOST_ASSERT(std::size_t(~0) != token_state_); Chris@16: Chris@16: token_type const& t = *first; Chris@16: if (token_id_ == t.id() && Chris@16: (all_states_id == token_state_ || token_state_ == t.state())) Chris@16: { Chris@16: spirit::traits::assign_to(t, attr); Chris@16: ++first; Chris@16: return true; Chris@16: } Chris@16: } Chris@16: return false; Chris@16: } Chris@16: Chris@16: template Chris@16: info what(Context& /*context*/) const Chris@16: { Chris@16: if (0 == def_.which()) Chris@16: return info("token_def", boost::get(def_)); Chris@16: Chris@16: return info("token_def", boost::get(def_)); Chris@16: } Chris@16: Chris@16: /////////////////////////////////////////////////////////////////////// Chris@16: // Lex interface: collect token definitions and put it into the Chris@16: // provided lexer def Chris@16: template Chris@16: void collect(LexerDef& lexdef, String const& state Chris@16: , String const& targetstate) const Chris@16: { Chris@16: std::size_t state_id = lexdef.add_state(state.c_str()); Chris@16: Chris@16: // If the following assertion fires you are probably trying to use Chris@16: // a single token_def instance in more than one lexer state. This Chris@16: // is not possible. Please create a separate token_def instance Chris@16: // from the same regular expression for each lexer state it needs Chris@16: // to be associated with. Chris@16: BOOST_ASSERT( Chris@16: (std::size_t(~0) == token_state_ || state_id == token_state_) && Chris@16: "Can't use single token_def with more than one lexer state"); Chris@16: Chris@16: char_type const* target = targetstate.empty() ? 0 : targetstate.c_str(); Chris@16: if (target) Chris@16: lexdef.add_state(target); Chris@16: Chris@16: token_state_ = state_id; Chris@16: if (0 == token_id_) Chris@16: token_id_ = lexdef.get_next_id(); Chris@16: Chris@16: if (0 == def_.which()) { Chris@16: unique_id_ = lexdef.add_token(state.c_str() Chris@16: , boost::get(def_), token_id_, target); Chris@16: } Chris@16: else { Chris@16: unique_id_ = lexdef.add_token(state.c_str() Chris@16: , boost::get(def_), token_id_, target); Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: void add_actions(LexerDef&) const {} Chris@16: Chris@16: public: Chris@16: typedef Char char_type; Chris@16: typedef Idtype id_type; Chris@16: typedef std::basic_string string_type; Chris@16: Chris@16: // Lex interface: constructing token definitions Chris@16: token_def() Chris@16: : proto_base_type(terminal_type::make(reference_(*this))) Chris@16: , def_('\0'), token_id_() Chris@16: , unique_id_(std::size_t(~0)), token_state_(std::size_t(~0)) {} Chris@16: Chris@16: token_def(token_def const& rhs) Chris@16: : proto_base_type(terminal_type::make(reference_(*this))) Chris@16: , def_(rhs.def_), token_id_(rhs.token_id_) Chris@16: , unique_id_(rhs.unique_id_), token_state_(rhs.token_state_) {} Chris@16: Chris@16: explicit token_def(char_type def_, Idtype id_ = Idtype()) Chris@16: : proto_base_type(terminal_type::make(reference_(*this))) Chris@16: , def_(def_) Chris@16: , token_id_(Idtype() == id_ ? Idtype(def_) : id_) Chris@16: , unique_id_(std::size_t(~0)), token_state_(std::size_t(~0)) {} Chris@16: Chris@16: explicit token_def(string_type const& def_, Idtype id_ = Idtype()) Chris@16: : proto_base_type(terminal_type::make(reference_(*this))) Chris@16: , def_(def_), token_id_(id_) Chris@16: , unique_id_(std::size_t(~0)), token_state_(std::size_t(~0)) {} Chris@16: Chris@16: template Chris@16: token_def& operator= (String const& definition) Chris@16: { Chris@16: def_ = definition; Chris@16: token_id_ = Idtype(); Chris@16: unique_id_ = std::size_t(~0); Chris@16: token_state_ = std::size_t(~0); Chris@16: return *this; Chris@16: } Chris@16: token_def& operator= (token_def const& rhs) Chris@16: { Chris@16: def_ = rhs.def_; Chris@16: token_id_ = rhs.token_id_; Chris@16: unique_id_ = rhs.unique_id_; Chris@16: token_state_ = rhs.token_state_; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // general accessors Chris@16: Idtype const& id() const { return token_id_; } Chris@16: void id(Idtype const& id) { token_id_ = id; } Chris@16: std::size_t unique_id() const { return unique_id_; } Chris@16: Chris@16: string_type definition() const Chris@16: { Chris@16: return (0 == def_.which()) ? Chris@16: boost::get(def_) : Chris@16: string_type(1, boost::get(def_)); Chris@16: } Chris@16: std::size_t state() const { return token_state_; } Chris@16: Chris@16: private: Chris@16: variant def_; Chris@16: mutable Idtype token_id_; Chris@16: mutable std::size_t unique_id_; Chris@16: mutable std::size_t token_state_; Chris@16: }; Chris@16: }}} Chris@16: Chris@16: namespace boost { namespace spirit { namespace traits Chris@16: { Chris@16: /////////////////////////////////////////////////////////////////////////// Chris@16: template Chris@16: struct handles_container< Chris@16: lex::token_def, Attr, Context, Iterator> Chris@16: : traits::is_container< Chris@16: typename attribute_of< Chris@16: lex::token_def, Context, Iterator Chris@16: >::type> Chris@16: {}; Chris@16: }}} Chris@16: Chris@16: #if defined(BOOST_MSVC) Chris@16: # pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: #endif