1 /*=============================================================================
2 Copyright (c) 2001-2014 Joel de Guzman
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
7 #if !defined(BOOST_SPIRIT_X3_CHAR_SET_OCT_12_2014_1051AM)
8 #define BOOST_SPIRIT_X3_CHAR_SET_OCT_12_2014_1051AM
10 #include <boost/spirit/home/x3/char/char_parser.hpp>
11 #include <boost/spirit/home/x3/char/detail/cast_char.hpp>
12 #include <boost/spirit/home/x3/support/traits/string_traits.hpp>
13 #include <boost/spirit/home/x3/support/utility/utf8.hpp>
14 #include <boost/spirit/home/x3/support/no_case.hpp>
15 #include <boost/spirit/home/support/char_set/basic_chset.hpp>
17 #include <boost/type_traits/is_same.hpp>
19 namespace boost { namespace spirit { namespace x3
21 ///////////////////////////////////////////////////////////////////////////
22 // Parser for a character range
23 ///////////////////////////////////////////////////////////////////////////
24 template <typename Encoding, typename Attribute = typename Encoding::char_type>
26 : char_parser< char_range<Encoding, Attribute> >
29 typedef typename Encoding::char_type char_type;
30 typedef Encoding encoding;
31 typedef Attribute attribute_type;
32 static bool const has_attribute =
33 !is_same<unused_type, attribute_type>::value;
36 constexpr char_range(char_type from_, char_type to_)
37 : from(from_), to(to_) {}
39 template <typename Char, typename Context>
40 bool test(Char ch_, Context const& context) const
43 char_type ch = char_type(ch_); // optimize for token based parsing
44 return (get_case_compare<encoding>(context)(ch, from) >= 0)
45 && (get_case_compare<encoding>(context)(ch , to) <= 0);
52 ///////////////////////////////////////////////////////////////////////////
53 // Parser for a character set
54 ///////////////////////////////////////////////////////////////////////////
55 template <typename Encoding, typename Attribute = typename Encoding::char_type>
56 struct char_set : char_parser<char_set<Encoding, Attribute>>
58 typedef typename Encoding::char_type char_type;
59 typedef Encoding encoding;
60 typedef Attribute attribute_type;
61 static bool const has_attribute =
62 !is_same<unused_type, attribute_type>::value;
64 template <typename String>
65 char_set(String const& str)
67 using spirit::x3::detail::cast_char;
69 auto* definition = traits::get_c_string(str);
70 auto ch = *definition++;
73 auto next = *definition++;
79 chset.set(cast_char<char_type>(ch));
84 cast_char<char_type>(ch),
85 cast_char<char_type>(next)
90 chset.set(cast_char<char_type>(ch));
96 template <typename Char, typename Context>
97 bool test(Char ch_, Context const& context) const
99 return get_case_compare<encoding>(context).in_set(ch_, chset);
102 support::detail::basic_chset<char_type> chset;
105 template <typename Encoding, typename Attribute>
106 struct get_info<char_set<Encoding, Attribute>>
108 typedef std::string result_type;
109 std::string operator()(char_set<Encoding, Attribute> const& /* p */) const
115 template <typename Encoding, typename Attribute>
116 struct get_info<char_range<Encoding, Attribute>>
118 typedef std::string result_type;
119 std::string operator()(char_range<Encoding, Attribute> const& p) const
121 return "char_range \"" + to_utf8(Encoding::toucs4(p.from)) + '-' + to_utf8(Encoding::toucs4(p.to))+ '"';