| 1 | /*============================================================================= |
|---|
| 2 | Copyright (c) 2005 Joel de Guzman |
|---|
| 3 | http://spirit.sourceforge.net/ |
|---|
| 4 | |
|---|
| 5 | Use, modification and distribution is subject to the Boost Software |
|---|
| 6 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
|---|
| 7 | http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 8 | =============================================================================*/ |
|---|
| 9 | #include <iostream> |
|---|
| 10 | |
|---|
| 11 | #define BOOST_SPIRIT_DEBUG |
|---|
| 12 | #include <boost/spirit/core.hpp> |
|---|
| 13 | |
|---|
| 14 | using namespace boost::spirit; |
|---|
| 15 | |
|---|
| 16 | struct non_greedy_kleene : public grammar<non_greedy_kleene> |
|---|
| 17 | { |
|---|
| 18 | template <typename ScannerT> |
|---|
| 19 | struct definition |
|---|
| 20 | { |
|---|
| 21 | typedef rule<ScannerT> rule_t; |
|---|
| 22 | rule_t r; |
|---|
| 23 | |
|---|
| 24 | definition(non_greedy_kleene const& self) |
|---|
| 25 | { |
|---|
| 26 | r = (alnum_p >> r) | digit_p; |
|---|
| 27 | BOOST_SPIRIT_DEBUG_RULE(r); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | rule_t const& |
|---|
| 31 | start() const |
|---|
| 32 | { |
|---|
| 33 | return r; |
|---|
| 34 | } |
|---|
| 35 | }; |
|---|
| 36 | }; |
|---|
| 37 | |
|---|
| 38 | struct non_greedy_plus : public grammar<non_greedy_plus> |
|---|
| 39 | { |
|---|
| 40 | template <typename ScannerT> |
|---|
| 41 | struct definition |
|---|
| 42 | { |
|---|
| 43 | typedef rule<ScannerT> rule_t; |
|---|
| 44 | rule_t r; |
|---|
| 45 | |
|---|
| 46 | definition(non_greedy_plus const& self) |
|---|
| 47 | { |
|---|
| 48 | r = alnum_p >> (r | digit_p); |
|---|
| 49 | BOOST_SPIRIT_DEBUG_RULE(r); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | rule_t const& |
|---|
| 53 | start() const |
|---|
| 54 | { |
|---|
| 55 | return r; |
|---|
| 56 | } |
|---|
| 57 | }; |
|---|
| 58 | }; |
|---|
| 59 | int |
|---|
| 60 | main() |
|---|
| 61 | { |
|---|
| 62 | bool success; |
|---|
| 63 | { |
|---|
| 64 | non_greedy_kleene k; |
|---|
| 65 | success = parse("3", k).full; |
|---|
| 66 | assert(success); |
|---|
| 67 | success = parse("abcdef3", k).full; |
|---|
| 68 | assert(success); |
|---|
| 69 | success = parse("abc2def3", k).full; |
|---|
| 70 | assert(success); |
|---|
| 71 | success = parse("abc", k).full; |
|---|
| 72 | assert(!success); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | { |
|---|
| 76 | non_greedy_plus p; |
|---|
| 77 | success = parse("3", p).full; |
|---|
| 78 | assert(!success); |
|---|
| 79 | success = parse("abcdef3", p).full; |
|---|
| 80 | assert(success); |
|---|
| 81 | success = parse("abc2def3", p).full; |
|---|
| 82 | assert(success); |
|---|
| 83 | success = parse("abc", p).full; |
|---|
| 84 | assert(!success); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | std::cout << "SUCCESS!!!\n"; |
|---|
| 88 | return 0; |
|---|
| 89 | } |
|---|