// Copyright (c) 2001-2011 Hartmut Kaiser // Copyright (c) 2010 Bryce Lelbach // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #if !defined(BOOST_SPIRIT_LEX_STATE_SWITCHER_SEP_23_2007_0714PM) #define BOOST_SPIRIT_LEX_STATE_SWITCHER_SEP_23_2007_0714PM #if defined(_MSC_VER) #pragma once #endif #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace spirit { /////////////////////////////////////////////////////////////////////////// // Enablers /////////////////////////////////////////////////////////////////////////// // enables set_state(s) template struct use_terminal > > : traits::is_string {}; // enables *lazy* set_state(s) template <> struct use_lazy_terminal< qi::domain, tag::set_state, 1 > : mpl::true_ {}; // enables in_state(s)[p] template struct use_directive > > : traits::is_string {}; // enables *lazy* in_state(s)[p] template <> struct use_lazy_directive< qi::domain, tag::in_state, 1 > : mpl::true_ {}; }} namespace boost { namespace spirit { namespace qi { #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS using spirit::set_state; using spirit::in_state; #endif using spirit::set_state_type; using spirit::in_state_type; /////////////////////////////////////////////////////////////////////////// namespace detail { template inline std::size_t set_lexer_state(Iterator& it, std::size_t state) { return it.set_state(state); } template inline std::size_t set_lexer_state(Iterator& it, Char const* statename) { std::size_t state = it.map_state(statename); // If the following assertion fires you probably used the // set_state(...) or in_state(...)[...] lexer state switcher with // a lexer state name unknown to the lexer (no token definitions // have been associated with this lexer state). BOOST_ASSERT(std::size_t(~0) != state); return it.set_state(state); } } /////////////////////////////////////////////////////////////////////////// // Parser switching the state of the underlying lexer component. // This parser gets used for the set_state(...) construct. /////////////////////////////////////////////////////////////////////////// template struct state_switcher : primitive_parser > { typedef typename remove_const::type>::type char_type; typedef std::basic_string string_type; template struct attribute { typedef unused_type type; }; state_switcher(char_type const* state) : state(state) {} template bool parse(Iterator& first, Iterator const& last , Context& /*context*/, Skipper const& skipper , Attribute& /*attr*/) const { qi::skip_over(first, last, skipper); // always do a pre-skip // just switch the state and return success detail::set_lexer_state(first, state.c_str()); return true; } template info what(Context& /*context*/) const { return info("set_state"); } string_type state; }; /////////////////////////////////////////////////////////////////////////// namespace detail { template struct reset_state_on_exit { template reset_state_on_exit(Iterator& it_, State state_) : it(it_) , state(set_lexer_state(it_, traits::get_c_string(state_))) {} ~reset_state_on_exit() { // reset the state of the underlying lexer instance set_lexer_state(it, state); } Iterator& it; std::size_t state; private: // silence MSVC warning C4512: assignment operator could not be generated reset_state_on_exit& operator= (reset_state_on_exit const&); }; } /////////////////////////////////////////////////////////////////////////// // Parser, which switches the state of the underlying lexer component // for the execution of the embedded sub-parser, switching the state back // afterwards. This parser gets used for the in_state(...)[p] construct. /////////////////////////////////////////////////////////////////////////// template struct state_switcher_context : unary_parser > { typedef Subject subject_type; typedef typename traits::char_type_of::type char_type; typedef typename remove_const::type non_const_char_type; template struct attribute { typedef typename traits::attribute_of::type type; }; state_switcher_context(Subject const& subject , typename add_reference::type state) : subject(subject), state(state) {} // The following conversion constructors are needed to make the // in_state_switcher template usable template state_switcher_context( state_switcher_context const& rhs) : subject(rhs.subject), state(traits::get_c_string(rhs.state)) {} template bool parse(Iterator& first, Iterator const& last , Context& context, Skipper const& skipper , Attribute& attr) const { qi::skip_over(first, last, skipper); // always do a pre-skip // the state has to be reset at exit in any case detail::reset_state_on_exit guard(first, state); return subject.parse(first, last, context, skipper, attr); } template info what(Context& context) const { return info("in_state", subject.what(context)); } Subject subject; State state; private: // silence MSVC warning C4512: assignment operator could not be generated state_switcher_context& operator= (state_switcher_context const&); }; /////////////////////////////////////////////////////////////////////////// // Parser generators: make_xxx function (objects) /////////////////////////////////////////////////////////////////////////// template struct make_primitive > , Modifiers, typename enable_if >::type> { typedef typename add_const::type const_string; typedef state_switcher result_type; template result_type operator()(Terminal const& term, unused_type) const { return result_type(traits::get_c_string(fusion::at_c<0>(term.args))); } }; template struct make_directive > , Subject, Modifiers> { typedef typename add_const::type const_string; typedef state_switcher_context result_type; template result_type operator()(Terminal const& term, Subject const& subject , unused_type) const { return result_type(subject, fusion::at_c<0>(term.args)); } }; }}} namespace boost { namespace spirit { namespace traits { /////////////////////////////////////////////////////////////////////////// template struct has_semantic_action > : unary_has_semantic_action {}; /////////////////////////////////////////////////////////////////////////// template struct handles_container , Attribute, Context, Iterator> : unary_handles_container {}; }}} #endif