1 // Copyright (c) 2001-2011 Hartmut Kaiser
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)
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
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>
25 namespace boost { namespace spirit
27 ///////////////////////////////////////////////////////////////////////////
29 ///////////////////////////////////////////////////////////////////////////
33 struct use_terminal<lex::domain, T
34 , typename enable_if<traits::is_string<T> >::type>
37 // enables string(str)
38 template <typename CharEncoding, typename A0>
39 struct use_terminal<lex::domain
41 tag::char_code<tag::string, CharEncoding>
42 , fusion::vector1<A0> > >
43 : traits::is_string<A0> {};
45 // enables string(str, ID)
46 template <typename CharEncoding, typename A0, typename A1>
47 struct use_terminal<lex::domain
49 tag::char_code<tag::string, CharEncoding>
50 , fusion::vector2<A0, A1> > >
51 : traits::is_string<A0> {};
54 namespace boost { namespace spirit { namespace lex
56 // use string from standard character set by default
57 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
58 using spirit::standard::string;
60 using spirit::standard::string_type;
62 ///////////////////////////////////////////////////////////////////////////
65 // represents a string based token definition
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> >
74 remove_const<typename traits::char_type_of<String>::type>::type
76 typedef std::basic_string<char_type> string_type;
78 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))
83 template <typename LexerDef, typename String_>
84 void collect(LexerDef& lexdef, String_ const& state
85 , String_ const& targetstate) const
87 std::size_t state_id = lexdef.add_state(state.c_str());
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.
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");
98 char_type const* target = targetstate.empty() ? 0 : targetstate.c_str();
100 lexdef.add_state(target);
102 token_state_ = state_id;
104 if (IdType(~0) == id_)
105 id_ = IdType(lexdef.get_next_id());
107 unique_id_ = lexdef.add_token (state.c_str(), str_, id_, target);
110 template <typename LexerDef>
111 void add_actions(LexerDef&) const {}
113 std::size_t id() const { return id_; }
114 std::size_t unique_id() const { return unique_id_; }
115 std::size_t state() const { return token_state_; }
119 mutable std::size_t unique_id_;
120 mutable std::size_t token_state_;
123 ///////////////////////////////////////////////////////////////////////////
124 // Lex generators: make_xxx function (objects)
125 ///////////////////////////////////////////////////////////////////////////
126 template <typename T, typename Modifiers>
127 struct make_primitive<T, Modifiers
128 , typename enable_if<traits::is_string<T> >::type>
130 typedef typename add_const<T>::type const_string;
131 typedef string_token_def<const_string> result_type;
133 result_type operator()(
134 typename add_reference<const_string>::type str, unused_type) const
136 return result_type(str, std::size_t(~0));
140 template <typename Modifiers, typename CharEncoding, typename A0>
141 struct make_primitive<
143 tag::char_code<tag::string, CharEncoding>
144 , fusion::vector1<A0> >
147 typedef typename add_const<A0>::type const_string;
148 typedef string_token_def<const_string, std::size_t, CharEncoding>
151 template <typename Terminal>
152 result_type operator()(Terminal const& term, unused_type) const
154 return result_type(fusion::at_c<0>(term.args), std::size_t(~0));
158 template <typename Modifiers, typename CharEncoding, typename A0, typename A1>
159 struct make_primitive<
161 tag::char_code<tag::string, CharEncoding>
162 , fusion::vector2<A0, A1> >
165 typedef typename add_const<A0>::type const_string;
166 typedef string_token_def<const_string, A1, CharEncoding> result_type;
168 template <typename Terminal>
169 result_type operator()(Terminal const& term, unused_type) const
172 fusion::at_c<0>(term.args), fusion::at_c<1>(term.args));
175 }}} // namespace boost::spirit::lex