| 1 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | // misc2.hpp |
|---|
| 3 | // |
|---|
| 4 | // Copyright 2004 Eric Niebler. Distributed under the Boost |
|---|
| 5 | // Software License, Version 1.0. (See accompanying file |
|---|
| 6 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 7 | |
|---|
| 8 | #include <iostream> |
|---|
| 9 | #include <boost/xpressive/xpressive.hpp> |
|---|
| 10 | #include <boost/xpressive/traits/cpp_regex_traits.hpp> |
|---|
| 11 | #include "./test_minimal.hpp" |
|---|
| 12 | |
|---|
| 13 | using namespace boost::xpressive; |
|---|
| 14 | |
|---|
| 15 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 16 | // test for a sub-match scoping |
|---|
| 17 | // |
|---|
| 18 | void test5() |
|---|
| 19 | { |
|---|
| 20 | sregex inner = sregex::compile( "(.)\\1" ); |
|---|
| 21 | sregex outer = (s1= _) >> inner >> s1; |
|---|
| 22 | std::string abba("ABBA"); |
|---|
| 23 | |
|---|
| 24 | BOOST_CHECK(regex_match(abba, outer)); |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 28 | // Ye olde calculator. Test recursive grammar. |
|---|
| 29 | // |
|---|
| 30 | void test6() |
|---|
| 31 | { |
|---|
| 32 | sregex group, factor, term, expression; |
|---|
| 33 | |
|---|
| 34 | group = '(' >> by_ref(expression) >> ')'; |
|---|
| 35 | factor = +_d | group; |
|---|
| 36 | term = factor >> *(('*' >> factor) | ('/' >> factor)); |
|---|
| 37 | expression = term >> *(('+' >> term) | ('-' >> term)); |
|---|
| 38 | |
|---|
| 39 | smatch what; |
|---|
| 40 | std::string str("foo 9*(10+3) bar"); |
|---|
| 41 | |
|---|
| 42 | BOOST_REQUIRE(regex_search(str, what, expression)); |
|---|
| 43 | BOOST_CHECK("9*(10+3)" == what[0]); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | /////////////////////////////////////////////////////////////////////////////// |
|---|
| 47 | // test_main |
|---|
| 48 | // |
|---|
| 49 | int test_main( int, char*[] ) |
|---|
| 50 | { |
|---|
| 51 | test5(); |
|---|
| 52 | test6(); |
|---|
| 53 | |
|---|
| 54 | return 0; |
|---|
| 55 | } |
|---|