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_CHAR_TOKEN_DEF_MAR_28_2007_0626PM)
7 #define BOOST_SPIRIT_LEX_CHAR_TOKEN_DEF_MAR_28_2007_0626PM
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>
19 namespace boost { namespace spirit
21 ///////////////////////////////////////////////////////////////////////////
23 ///////////////////////////////////////////////////////////////////////////
27 struct use_terminal<lex::domain, char>
32 struct use_terminal<lex::domain, char[2]>
37 struct use_terminal<lex::domain, wchar_t>
42 struct use_terminal<lex::domain, wchar_t[2]>
45 // enables char_('x'), char_("x")
46 template <typename CharEncoding, typename A0>
47 struct use_terminal<lex::domain
49 tag::char_code<tag::char_, CharEncoding>
50 , fusion::vector1<A0> > >
53 // enables char_('x', ID), char_("x", ID)
54 template <typename CharEncoding, typename A0, typename A1>
55 struct use_terminal<lex::domain
57 tag::char_code<tag::char_, CharEncoding>
58 , fusion::vector2<A0, A1> > >
62 namespace boost { namespace spirit { namespace lex
64 // use char_ from standard character set by default
65 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
66 using spirit::standard::char_;
68 using spirit::standard::char_type;
70 ///////////////////////////////////////////////////////////////////////////
73 // represents a single character token definition
75 ///////////////////////////////////////////////////////////////////////////
76 template <typename CharEncoding = char_encoding::standard
77 , typename IdType = std::size_t>
79 : primitive_lexer<char_token_def<CharEncoding, IdType> >
81 typedef typename CharEncoding::char_type char_type;
83 char_token_def(char_type ch, IdType const& id)
84 : ch(ch), id_(id), unique_id_(std::size_t(~0))
85 , token_state_(std::size_t(~0))
88 template <typename LexerDef, typename String>
89 void collect(LexerDef& lexdef, String const& state
90 , String const& targetstate) const
92 std::size_t state_id = lexdef.add_state(state.c_str());
94 // If the following assertion fires you are probably trying to use
95 // a single char_token_def instance in more than one lexer state.
96 // This is not possible. Please create a separate token_def instance
97 // from the same regular expression for each lexer state it needs
98 // to be associated with.
100 (std::size_t(~0) == token_state_ || state_id == token_state_) &&
101 "Can't use single char_token_def with more than one lexer state");
103 char_type const* target = targetstate.empty() ? 0 : targetstate.c_str();
105 lexdef.add_state(target);
107 token_state_ = state_id;
108 unique_id_ = lexdef.add_token (state.c_str(), ch, id_, target);
111 template <typename LexerDef>
112 void add_actions(LexerDef&) const {}
114 IdType id() const { return id_; }
115 std::size_t unique_id() const { return unique_id_; }
116 std::size_t state() const { return token_state_; }
120 mutable std::size_t unique_id_;
121 mutable std::size_t token_state_;
124 ///////////////////////////////////////////////////////////////////////////
125 // Lexer generators: make_xxx function (objects)
126 ///////////////////////////////////////////////////////////////////////////
129 template <typename CharEncoding>
132 typedef char_token_def<CharEncoding> result_type;
134 template <typename Char>
135 result_type operator()(Char ch, unused_type) const
137 return result_type(ch, ch);
140 template <typename Char>
141 result_type operator()(Char const* str, unused_type) const
143 return result_type(str[0], str[0]);
148 // literals: 'x', "x"
149 template <typename Modifiers>
150 struct make_primitive<char, Modifiers>
151 : detail::basic_literal<char_encoding::standard> {};
153 template <typename Modifiers>
154 struct make_primitive<char const(&)[2], Modifiers>
155 : detail::basic_literal<char_encoding::standard> {};
157 // literals: L'x', L"x"
158 template <typename Modifiers>
159 struct make_primitive<wchar_t, Modifiers>
160 : detail::basic_literal<char_encoding::standard_wide> {};
162 template <typename Modifiers>
163 struct make_primitive<wchar_t const(&)[2], Modifiers>
164 : detail::basic_literal<char_encoding::standard_wide> {};
167 template <typename CharEncoding, typename Modifiers, typename A0>
168 struct make_primitive<
170 tag::char_code<tag::char_, CharEncoding>
171 , fusion::vector1<A0>
175 typedef char_token_def<CharEncoding> result_type;
177 template <typename Terminal>
178 result_type operator()(Terminal const& term, unused_type) const
180 return result_type(fusion::at_c<0>(term.args), fusion::at_c<0>(term.args));
185 template <typename CharEncoding, typename Modifiers, typename Char>
186 struct make_primitive<
188 tag::char_code<tag::char_, CharEncoding>
189 , fusion::vector1<Char(&)[2]> // single char strings
193 typedef char_token_def<CharEncoding> result_type;
195 template <typename Terminal>
196 result_type operator()(Terminal const& term, unused_type) const
198 Char ch = fusion::at_c<0>(term.args)[0];
199 return result_type(ch, ch);
203 // handle char_('x', ID)
204 template <typename CharEncoding, typename Modifiers, typename A0, typename A1>
205 struct make_primitive<
207 tag::char_code<tag::char_, CharEncoding>
208 , fusion::vector2<A0, A1>
212 typedef char_token_def<CharEncoding> result_type;
214 template <typename Terminal>
215 result_type operator()(Terminal const& term, unused_type) const
218 fusion::at_c<0>(term.args), fusion::at_c<1>(term.args));
222 // handle char_("x", ID)
223 template <typename CharEncoding, typename Modifiers, typename Char, typename A1>
224 struct make_primitive<
226 tag::char_code<tag::char_, CharEncoding>
227 , fusion::vector2<Char(&)[2], A1> // single char strings
231 typedef char_token_def<CharEncoding> result_type;
233 template <typename Terminal>
234 result_type operator()(Terminal const& term, unused_type) const
237 fusion::at_c<0>(term.args)[0], fusion::at_c<1>(term.args));
240 }}} // namespace boost::spirit::lex