| 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 | namespace boost { |
|---|
| 22 | namespace spirit { |
|---|
| 23 | //----------------------------------------------------------------------------- |
|---|
| 24 | // distinct_parser class |
|---|
| 25 | |
|---|
| 26 | template <typename CharT = char, typename TailT = chset<CharT> > |
|---|
| 27 | class distinct_parser |
|---|
| 28 | { |
|---|
| 29 | public: |
|---|
| 30 | typedef |
|---|
| 31 | contiguous< |
|---|
| 32 | sequence< |
|---|
| 33 | chseq<CharT const*>, |
|---|
| 34 | negated_empty_match_parser< |
|---|
| 35 | TailT |
|---|
| 36 | > |
|---|
| 37 | > |
|---|
| 38 | > |
|---|
| 39 | result_t; |
|---|
| 40 | |
|---|
| 41 | distinct_parser() |
|---|
| 42 | : tail(chset<CharT>()) |
|---|
| 43 | { |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | explicit distinct_parser(parser<TailT> const & tail_) |
|---|
| 47 | : tail(tail_.derived()) |
|---|
| 48 | { |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | explicit distinct_parser(CharT const* letters) |
|---|
| 52 | : tail(chset_p(letters)) |
|---|
| 53 | { |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | result_t operator()(CharT const* str) const |
|---|
| 57 | { |
|---|
| 58 | return lexeme_d[chseq_p(str) >> ~epsilon_p(tail)]; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | TailT tail; |
|---|
| 62 | }; |
|---|
| 63 | |
|---|
| 64 | //----------------------------------------------------------------------------- |
|---|
| 65 | // distinct_directive class |
|---|
| 66 | |
|---|
| 67 | template <typename CharT = char, typename TailT = chset<CharT> > |
|---|
| 68 | class distinct_directive |
|---|
| 69 | { |
|---|
| 70 | public: |
|---|
| 71 | template<typename ParserT> |
|---|
| 72 | struct result { |
|---|
| 73 | typedef |
|---|
| 74 | contiguous< |
|---|
| 75 | sequence< |
|---|
| 76 | ParserT, |
|---|
| 77 | negated_empty_match_parser< |
|---|
| 78 | TailT |
|---|
| 79 | > |
|---|
| 80 | > |
|---|
| 81 | > |
|---|
| 82 | type; |
|---|
| 83 | }; |
|---|
| 84 | |
|---|
| 85 | distinct_directive() |
|---|
| 86 | : tail(chset<CharT>()) |
|---|
| 87 | { |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | explicit distinct_directive(CharT const* letters) |
|---|
| 91 | : tail(chset_p(letters)) |
|---|
| 92 | { |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | explicit distinct_directive(parser<TailT> const & tail_) |
|---|
| 96 | : tail(tail_.derived()) |
|---|
| 97 | { |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | template<typename ParserT> |
|---|
| 101 | typename result<typename as_parser<ParserT>::type>::type |
|---|
| 102 | operator[](ParserT const &subject) const |
|---|
| 103 | { |
|---|
| 104 | return |
|---|
| 105 | lexeme_d[as_parser<ParserT>::convert(subject) >> ~epsilon_p(tail)]; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | TailT tail; |
|---|
| 109 | }; |
|---|
| 110 | |
|---|
| 111 | //----------------------------------------------------------------------------- |
|---|
| 112 | // dynamic_distinct_parser class |
|---|
| 113 | |
|---|
| 114 | template <typename ScannerT = scanner<> > |
|---|
| 115 | class dynamic_distinct_parser |
|---|
| 116 | { |
|---|
| 117 | public: |
|---|
| 118 | typedef typename ScannerT::value_t char_t; |
|---|
| 119 | |
|---|
| 120 | typedef |
|---|
| 121 | rule< |
|---|
| 122 | typename no_actions_scanner< |
|---|
| 123 | typename lexeme_scanner<ScannerT>::type |
|---|
| 124 | >::type |
|---|
| 125 | > |
|---|
| 126 | tail_t; |
|---|
| 127 | |
|---|
| 128 | typedef |
|---|
| 129 | contiguous< |
|---|
| 130 | sequence< |
|---|
| 131 | chseq<char_t const*>, |
|---|
| 132 | negated_empty_match_parser< |
|---|
| 133 | tail_t |
|---|
| 134 | > |
|---|
| 135 | > |
|---|
| 136 | > |
|---|
| 137 | result_t; |
|---|
| 138 | |
|---|
| 139 | dynamic_distinct_parser() |
|---|
| 140 | : tail(nothing_p) |
|---|
| 141 | { |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | template<typename ParserT> |
|---|
| 145 | explicit dynamic_distinct_parser(parser<ParserT> const & tail_) |
|---|
| 146 | : tail(tail_.derived()) |
|---|
| 147 | { |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | explicit dynamic_distinct_parser(char_t const* letters) |
|---|
| 151 | : tail(chset_p(letters)) |
|---|
| 152 | { |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | result_t operator()(char_t const* str) const |
|---|
| 156 | { |
|---|
| 157 | return lexeme_d[chseq_p(str) >> ~epsilon_p(tail)]; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | tail_t tail; |
|---|
| 161 | }; |
|---|
| 162 | |
|---|
| 163 | //----------------------------------------------------------------------------- |
|---|
| 164 | // dynamic_distinct_directive class |
|---|
| 165 | |
|---|
| 166 | template <typename ScannerT = scanner<> > |
|---|
| 167 | class dynamic_distinct_directive |
|---|
| 168 | { |
|---|
| 169 | public: |
|---|
| 170 | typedef typename ScannerT::value_t char_t; |
|---|
| 171 | |
|---|
| 172 | typedef |
|---|
| 173 | rule< |
|---|
| 174 | typename no_actions_scanner< |
|---|
| 175 | typename lexeme_scanner<ScannerT>::type |
|---|
| 176 | >::type |
|---|
| 177 | > |
|---|
| 178 | tail_t; |
|---|
| 179 | |
|---|
| 180 | template<typename ParserT> |
|---|
| 181 | struct result { |
|---|
| 182 | typedef |
|---|
| 183 | contiguous< |
|---|
| 184 | sequence< |
|---|
| 185 | ParserT, |
|---|
| 186 | negated_empty_match_parser< |
|---|
| 187 | tail_t |
|---|
| 188 | > |
|---|
| 189 | > |
|---|
| 190 | > |
|---|
| 191 | type; |
|---|
| 192 | }; |
|---|
| 193 | |
|---|
| 194 | dynamic_distinct_directive() |
|---|
| 195 | : tail(nothing_p) |
|---|
| 196 | { |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | template<typename ParserT> |
|---|
| 200 | explicit dynamic_distinct_directive(parser<ParserT> const & tail_) |
|---|
| 201 | : tail(tail_.derived()) |
|---|
| 202 | { |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | explicit dynamic_distinct_directive(char_t const* letters) |
|---|
| 206 | : tail(chset_p(letters)) |
|---|
| 207 | { |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | template<typename ParserT> |
|---|
| 211 | typename result<typename as_parser<ParserT>::type>::type |
|---|
| 212 | operator[](ParserT const &subject) const |
|---|
| 213 | { |
|---|
| 214 | return |
|---|
| 215 | lexeme_d[as_parser<ParserT>::convert(subject) >> ~epsilon_p(tail)]; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | tail_t tail; |
|---|
| 219 | }; |
|---|
| 220 | |
|---|
| 221 | //----------------------------------------------------------------------------- |
|---|
| 222 | } // namespace spirit |
|---|
| 223 | } // namespace boost |
|---|
| 224 | |
|---|
| 225 | #endif // !defined(BOOST_SPIRIT_DISTINCT_HPP) |
|---|