// Copyright (c) 2001-2011 Hartmut Kaiser // // 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_STRING_TOKEN_DEF_MAR_28_2007_0722PM) #define BOOST_SPIRIT_LEX_STRING_TOKEN_DEF_MAR_28_2007_0722PM #if defined(_MSC_VER) #pragma once #endif #include #include #include #include #include #include #include #include #include #include namespace boost { namespace spirit { /////////////////////////////////////////////////////////////////////////// // Enablers /////////////////////////////////////////////////////////////////////////// template struct use_terminal >::type> // enables strings : mpl::true_ {}; template struct use_terminal // enables string(str) , fusion::vector1 > > : traits::is_string {}; }} namespace boost { namespace spirit { namespace lex { // use string from standard character set by default using spirit::standard::string_type; using spirit::standard::string; /////////////////////////////////////////////////////////////////////////// // // string_token_def // represents a string based token definition // /////////////////////////////////////////////////////////////////////////// template struct string_token_def : primitive_lexer > { typedef typename remove_const::type>::type char_type; typedef std::basic_string string_type; string_token_def(typename add_reference::type str) : str_(str), id_(std::size_t(~0)), unique_id_(std::size_t(~0)) , token_state_(std::size_t(~0)) {} template void collect(LexerDef& lexdef, String_ const& state , String_ const& targetstate) const { std::size_t state_id = lexdef.add_state(state.c_str()); // If the following assertion fires you are probably trying to use // a single string_token_def instance in more than one lexer state. // This is not possible. Please create a separate token_def instance // from the same regular expression for each lexer state it needs // to be associated with. BOOST_ASSERT( (std::size_t(~0) == token_state_ || state_id == token_state_) && "Can't use single string_token_def with more than one lexer state"); char_type const* target = targetstate.empty() ? 0 : targetstate.c_str(); if (target) lexdef.add_state(target); token_state_ = state_id; typedef typename LexerDef::id_type id_type; if (std::size_t(~0) == id_) id_ = lexdef.get_next_id(); unique_id_ = lexdef.add_token (state.c_str(), str_, id_, target); } template void add_actions(LexerDef&) const {} std::size_t id() const { return id_; } std::size_t unique_id() const { return unique_id_; } std::size_t state() const { return token_state_; } string_type str_; mutable std::size_t id_; mutable std::size_t unique_id_; mutable std::size_t token_state_; }; /////////////////////////////////////////////////////////////////////////// // Lex generators: make_xxx function (objects) /////////////////////////////////////////////////////////////////////////// template struct make_primitive >::type> { typedef typename add_const::type const_string; typedef string_token_def result_type; result_type operator()( typename add_reference::type str, unused_type) const { return result_type(str); } }; template struct make_primitive< terminal_ex< tag::char_code , fusion::vector1 > , Modifiers> { typedef typename add_const::type const_string; typedef string_token_def result_type; template result_type operator()(Terminal const& term, unused_type) const { return result_type(fusion::at_c<0>(term.args)); } }; }}} // namespace boost::spirit::lex #endif