1 // Copyright (c) 2001-2011 Hartmut Kaiser 2 // 3 // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5 6 #if !defined(BOOST_SPIRIT_LEX_STRING_TOKEN_DEF_MAR_28_2007_0722PM) 7 #define BOOST_SPIRIT_LEX_STRING_TOKEN_DEF_MAR_28_2007_0722PM 8 9 #if defined(_MSC_VER) 10 #pragma once 11 #endif 12 13 #include <boost/spirit/home/support/common_terminals.hpp> 14 #include <boost/spirit/home/support/string_traits.hpp> 15 #include <boost/spirit/home/lex/domain.hpp> 16 #include <boost/spirit/home/lex/lexer_type.hpp> 17 #include <boost/spirit/home/lex/meta_compiler.hpp> 18 #include <boost/type_traits/add_const.hpp> 19 #include <boost/type_traits/add_reference.hpp> 20 #include <boost/type_traits/is_integral.hpp> 21 #include <boost/type_traits/remove_const.hpp> 22 #include <boost/fusion/include/vector.hpp> 23 #include <boost/fusion/include/at.hpp> 24 25 namespace boost { namespace spirit 26 { 27 /////////////////////////////////////////////////////////////////////////// 28 // Enablers 29 /////////////////////////////////////////////////////////////////////////// 30 31 // enables strings 32 template <typename T> 33 struct use_terminal<lex::domain, T 34 , typename enable_if<traits::is_string<T> >::type> 35 : mpl::true_ {}; 36 37 // enables string(str) 38 template <typename CharEncoding, typename A0> 39 struct use_terminal<lex::domain 40 , terminal_ex< 41 tag::char_code<tag::string, CharEncoding> 42 , fusion::vector1<A0> > > 43 : traits::is_string<A0> {}; 44 45 // enables string(str, ID) 46 template <typename CharEncoding, typename A0, typename A1> 47 struct use_terminal<lex::domain 48 , terminal_ex< 49 tag::char_code<tag::string, CharEncoding> 50 , fusion::vector2<A0, A1> > > 51 : traits::is_string<A0> {}; 52 }} 53 54 namespace boost { namespace spirit { namespace lex 55 { 56 // use string from standard character set by default 57 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS 58 using spirit::standard::string; 59 #endif 60 using spirit::standard::string_type; 61 62 /////////////////////////////////////////////////////////////////////////// 63 // 64 // string_token_def 65 // represents a string based token definition 66 // 67 /////////////////////////////////////////////////////////////////////////// 68 template <typename String, typename IdType = std::size_t 69 , typename CharEncoding = char_encoding::standard> 70 struct string_token_def 71 : primitive_lexer<string_token_def<String, IdType, CharEncoding> > 72 { 73 typedef typename 74 remove_const<typename traits::char_type_of<String>::type>::type 75 char_type; 76 typedef std::basic_string<char_type> string_type; 77 string_token_defboost::spirit::lex::string_token_def78 string_token_def(typename add_reference<String>::type str, IdType const& id) 79 : str_(str), id_(id), unique_id_(std::size_t(~0)) 80 , token_state_(std::size_t(~0)) 81 {} 82 83 template <typename LexerDef, typename String_> collectboost::spirit::lex::string_token_def84 void collect(LexerDef& lexdef, String_ const& state 85 , String_ const& targetstate) const 86 { 87 std::size_t state_id = lexdef.add_state(state.c_str()); 88 89 // If the following assertion fires you are probably trying to use 90 // a single string_token_def instance in more than one lexer state. 91 // This is not possible. Please create a separate token_def instance 92 // from the same regular expression for each lexer state it needs 93 // to be associated with. 94 BOOST_ASSERT( 95 (std::size_t(~0) == token_state_ || state_id == token_state_) && 96 "Can't use single string_token_def with more than one lexer state"); 97 98 char_type const* target = targetstate.empty() ? 0 : targetstate.c_str(); 99 if (target) 100 lexdef.add_state(target); 101 102 token_state_ = state_id; 103 104 typedef typename LexerDef::id_type id_type; 105 if (IdType(~0) == id_) 106 id_ = IdType(lexdef.get_next_id()); 107 108 unique_id_ = lexdef.add_token (state.c_str(), str_, id_, target); 109 } 110 111 template <typename LexerDef> add_actionsboost::spirit::lex::string_token_def112 void add_actions(LexerDef&) const {} 113 idboost::spirit::lex::string_token_def114 std::size_t id() const { return id_; } unique_idboost::spirit::lex::string_token_def115 std::size_t unique_id() const { return unique_id_; } stateboost::spirit::lex::string_token_def116 std::size_t state() const { return token_state_; } 117 118 string_type str_; 119 mutable IdType id_; 120 mutable std::size_t unique_id_; 121 mutable std::size_t token_state_; 122 }; 123 124 /////////////////////////////////////////////////////////////////////////// 125 // Lex generators: make_xxx function (objects) 126 /////////////////////////////////////////////////////////////////////////// 127 template <typename T, typename Modifiers> 128 struct make_primitive<T, Modifiers 129 , typename enable_if<traits::is_string<T> >::type> 130 { 131 typedef typename add_const<T>::type const_string; 132 typedef string_token_def<const_string> result_type; 133 operator ()boost::spirit::lex::make_primitive134 result_type operator()( 135 typename add_reference<const_string>::type str, unused_type) const 136 { 137 return result_type(str, std::size_t(~0)); 138 } 139 }; 140 141 template <typename Modifiers, typename CharEncoding, typename A0> 142 struct make_primitive< 143 terminal_ex< 144 tag::char_code<tag::string, CharEncoding> 145 , fusion::vector1<A0> > 146 , Modifiers> 147 { 148 typedef typename add_const<A0>::type const_string; 149 typedef string_token_def<const_string, std::size_t, CharEncoding> 150 result_type; 151 152 template <typename Terminal> operator ()boost::spirit::lex::make_primitive153 result_type operator()(Terminal const& term, unused_type) const 154 { 155 return result_type(fusion::at_c<0>(term.args), std::size_t(~0)); 156 } 157 }; 158 159 template <typename Modifiers, typename CharEncoding, typename A0, typename A1> 160 struct make_primitive< 161 terminal_ex< 162 tag::char_code<tag::string, CharEncoding> 163 , fusion::vector2<A0, A1> > 164 , Modifiers> 165 { 166 typedef typename add_const<A0>::type const_string; 167 typedef string_token_def<const_string, A1, CharEncoding> result_type; 168 169 template <typename Terminal> operator ()boost::spirit::lex::make_primitive170 result_type operator()(Terminal const& term, unused_type) const 171 { 172 return result_type( 173 fusion::at_c<0>(term.args), fusion::at_c<1>(term.args)); 174 } 175 }; 176 }}} // namespace boost::spirit::lex 177 178 #endif 179