Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / spirit / home / x3 / char / char_set.hpp
1 /*=============================================================================
2     Copyright (c) 2001-2014 Joel de Guzman
3
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
9
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>
16
17 #include <boost/type_traits/is_same.hpp>
18
19 namespace boost { namespace spirit { namespace x3
20 {
21     ///////////////////////////////////////////////////////////////////////////
22     // Parser for a character range
23     ///////////////////////////////////////////////////////////////////////////
24     template <typename Encoding, typename Attribute = typename Encoding::char_type>
25     struct char_range
26       : char_parser< char_range<Encoding, Attribute> >
27     {
28
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;
34
35
36         char_range(char_type from_, char_type to_)
37           : from(from_), to(to_) {}
38
39         template <typename Char, typename Context>
40         bool test(Char ch_, Context const& context) const
41         {
42
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);
46         }
47
48         char_type from, to;
49     };
50
51
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>>
57     {
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;
63
64         template <typename String>
65         char_set(String const& str)
66         {
67             using spirit::x3::detail::cast_char;
68
69             auto* definition = traits::get_c_string(str);
70             auto ch = *definition++;
71             while (ch)
72             {
73                 auto next = *definition++;
74                 if (next == '-')
75                 {
76                     next = *definition++;
77                     if (next == 0)
78                     {
79                         chset.set(cast_char<char_type>(ch));
80                         chset.set('-');
81                         break;
82                     }
83                     chset.set(
84                         cast_char<char_type>(ch),
85                         cast_char<char_type>(next)
86                     );
87                 }
88                 else
89                 {
90                     chset.set(cast_char<char_type>(ch));
91                 }
92                 ch = next;
93             }
94         }
95
96         template <typename Char, typename Context>
97         bool test(Char ch_, Context const& context) const
98         {
99             return get_case_compare<encoding>(context).in_set(ch_, chset);
100         }
101
102         support::detail::basic_chset<char_type> chset;
103     };
104
105     template <typename Encoding, typename Attribute>
106     struct get_info<char_set<Encoding, Attribute>>
107     {
108         typedef std::string result_type;
109         std::string operator()(char_set<Encoding, Attribute> const& /* p */) const
110         {
111             return "char-set";
112         }
113     };
114
115     template <typename Encoding, typename Attribute>
116     struct get_info<char_range<Encoding, Attribute>>
117     {
118         typedef std::string result_type;
119         std::string operator()(char_range<Encoding, Attribute> const& p) const
120         {
121             return "char_range \"" + to_utf8(Encoding::toucs4(p.from)) + '-' + to_utf8(Encoding::toucs4(p.to))+ '"';
122         }
123     };
124
125 }}}
126
127 #endif