[29] | 1 | /*============================================================================= |
---|
| 2 | Copyright (c) 1998-2003 Joel de Guzman |
---|
| 3 | Copyright (c) 2003 Vaclav Vesely |
---|
| 4 | http://spirit.sourceforge.net/ |
---|
| 5 | |
---|
| 6 | Use, modification and distribution is subject to the Boost Software |
---|
| 7 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
| 8 | http://www.boost.org/LICENSE_1_0.txt) |
---|
| 9 | =============================================================================*/ |
---|
| 10 | #if !defined(BOOST_SPIRIT_DISTINCT_HPP) |
---|
| 11 | #define BOOST_SPIRIT_DISTINCT_HPP |
---|
| 12 | |
---|
| 13 | #include <boost/spirit/core/parser.hpp> |
---|
| 14 | #include <boost/spirit/core/primitives/primitives.hpp> |
---|
| 15 | #include <boost/spirit/core/composite/operators.hpp> |
---|
| 16 | #include <boost/spirit/core/composite/directives.hpp> |
---|
| 17 | #include <boost/spirit/core/composite/epsilon.hpp> |
---|
| 18 | #include <boost/spirit/core/non_terminal/rule.hpp> |
---|
| 19 | #include <boost/spirit/utility/chset.hpp> |
---|
| 20 | |
---|
| 21 | #include <boost/spirit/utility/distinct_fwd.hpp> |
---|
| 22 | |
---|
| 23 | namespace boost { |
---|
| 24 | namespace spirit { |
---|
| 25 | //----------------------------------------------------------------------------- |
---|
| 26 | // distinct_parser class |
---|
| 27 | |
---|
| 28 | template <typename CharT, typename TailT> |
---|
| 29 | class distinct_parser |
---|
| 30 | { |
---|
| 31 | public: |
---|
| 32 | typedef |
---|
| 33 | contiguous< |
---|
| 34 | sequence< |
---|
| 35 | chseq<CharT const*>, |
---|
| 36 | negated_empty_match_parser< |
---|
| 37 | TailT |
---|
| 38 | > |
---|
| 39 | > |
---|
| 40 | > |
---|
| 41 | result_t; |
---|
| 42 | |
---|
| 43 | distinct_parser() |
---|
| 44 | : tail(chset<CharT>()) |
---|
| 45 | { |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | explicit distinct_parser(parser<TailT> const & tail_) |
---|
| 49 | : tail(tail_.derived()) |
---|
| 50 | { |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | explicit distinct_parser(CharT const* letters) |
---|
| 54 | : tail(chset_p(letters)) |
---|
| 55 | { |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | result_t operator()(CharT const* str) const |
---|
| 59 | { |
---|
| 60 | return lexeme_d[chseq_p(str) >> ~epsilon_p(tail)]; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | TailT tail; |
---|
| 64 | }; |
---|
| 65 | |
---|
| 66 | //----------------------------------------------------------------------------- |
---|
| 67 | // distinct_directive class |
---|
| 68 | |
---|
| 69 | template <typename CharT, typename TailT> |
---|
| 70 | class distinct_directive |
---|
| 71 | { |
---|
| 72 | public: |
---|
| 73 | template<typename ParserT> |
---|
| 74 | struct result { |
---|
| 75 | typedef |
---|
| 76 | contiguous< |
---|
| 77 | sequence< |
---|
| 78 | ParserT, |
---|
| 79 | negated_empty_match_parser< |
---|
| 80 | TailT |
---|
| 81 | > |
---|
| 82 | > |
---|
| 83 | > |
---|
| 84 | type; |
---|
| 85 | }; |
---|
| 86 | |
---|
| 87 | distinct_directive() |
---|
| 88 | : tail(chset<CharT>()) |
---|
| 89 | { |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | explicit distinct_directive(CharT const* letters) |
---|
| 93 | : tail(chset_p(letters)) |
---|
| 94 | { |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | explicit distinct_directive(parser<TailT> const & tail_) |
---|
| 98 | : tail(tail_.derived()) |
---|
| 99 | { |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | template<typename ParserT> |
---|
| 103 | typename result<typename as_parser<ParserT>::type>::type |
---|
| 104 | operator[](ParserT const &subject) const |
---|
| 105 | { |
---|
| 106 | return |
---|
| 107 | lexeme_d[as_parser<ParserT>::convert(subject) >> ~epsilon_p(tail)]; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | TailT tail; |
---|
| 111 | }; |
---|
| 112 | |
---|
| 113 | //----------------------------------------------------------------------------- |
---|
| 114 | // dynamic_distinct_parser class |
---|
| 115 | |
---|
| 116 | template <typename ScannerT> |
---|
| 117 | class dynamic_distinct_parser |
---|
| 118 | { |
---|
| 119 | public: |
---|
| 120 | typedef typename ScannerT::value_t char_t; |
---|
| 121 | |
---|
| 122 | typedef |
---|
| 123 | rule< |
---|
| 124 | typename no_actions_scanner< |
---|
| 125 | typename lexeme_scanner<ScannerT>::type |
---|
| 126 | >::type |
---|
| 127 | > |
---|
| 128 | tail_t; |
---|
| 129 | |
---|
| 130 | typedef |
---|
| 131 | contiguous< |
---|
| 132 | sequence< |
---|
| 133 | chseq<char_t const*>, |
---|
| 134 | negated_empty_match_parser< |
---|
| 135 | tail_t |
---|
| 136 | > |
---|
| 137 | > |
---|
| 138 | > |
---|
| 139 | result_t; |
---|
| 140 | |
---|
| 141 | dynamic_distinct_parser() |
---|
| 142 | : tail(nothing_p) |
---|
| 143 | { |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | template<typename ParserT> |
---|
| 147 | explicit dynamic_distinct_parser(parser<ParserT> const & tail_) |
---|
| 148 | : tail(tail_.derived()) |
---|
| 149 | { |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | explicit dynamic_distinct_parser(char_t const* letters) |
---|
| 153 | : tail(chset_p(letters)) |
---|
| 154 | { |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | result_t operator()(char_t const* str) const |
---|
| 158 | { |
---|
| 159 | return lexeme_d[chseq_p(str) >> ~epsilon_p(tail)]; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | tail_t tail; |
---|
| 163 | }; |
---|
| 164 | |
---|
| 165 | //----------------------------------------------------------------------------- |
---|
| 166 | // dynamic_distinct_directive class |
---|
| 167 | |
---|
| 168 | template <typename ScannerT> |
---|
| 169 | class dynamic_distinct_directive |
---|
| 170 | { |
---|
| 171 | public: |
---|
| 172 | typedef typename ScannerT::value_t char_t; |
---|
| 173 | |
---|
| 174 | typedef |
---|
| 175 | rule< |
---|
| 176 | typename no_actions_scanner< |
---|
| 177 | typename lexeme_scanner<ScannerT>::type |
---|
| 178 | >::type |
---|
| 179 | > |
---|
| 180 | tail_t; |
---|
| 181 | |
---|
| 182 | template<typename ParserT> |
---|
| 183 | struct result { |
---|
| 184 | typedef |
---|
| 185 | contiguous< |
---|
| 186 | sequence< |
---|
| 187 | ParserT, |
---|
| 188 | negated_empty_match_parser< |
---|
| 189 | tail_t |
---|
| 190 | > |
---|
| 191 | > |
---|
| 192 | > |
---|
| 193 | type; |
---|
| 194 | }; |
---|
| 195 | |
---|
| 196 | dynamic_distinct_directive() |
---|
| 197 | : tail(nothing_p) |
---|
| 198 | { |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | template<typename ParserT> |
---|
| 202 | explicit dynamic_distinct_directive(parser<ParserT> const & tail_) |
---|
| 203 | : tail(tail_.derived()) |
---|
| 204 | { |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | explicit dynamic_distinct_directive(char_t const* letters) |
---|
| 208 | : tail(chset_p(letters)) |
---|
| 209 | { |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | template<typename ParserT> |
---|
| 213 | typename result<typename as_parser<ParserT>::type>::type |
---|
| 214 | operator[](ParserT const &subject) const |
---|
| 215 | { |
---|
| 216 | return |
---|
| 217 | lexeme_d[as_parser<ParserT>::convert(subject) >> ~epsilon_p(tail)]; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | tail_t tail; |
---|
| 221 | }; |
---|
| 222 | |
---|
| 223 | //----------------------------------------------------------------------------- |
---|
| 224 | } // namespace spirit |
---|
| 225 | } // namespace boost |
---|
| 226 | |
---|
| 227 | #endif // !defined(BOOST_SPIRIT_DISTINCT_HPP) |
---|