summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/xpressive/test/test_dynamic_grammar.cpp
blob: c11f82672d23a6e1b5cc9a45d61fd1ab6790bc6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//////////////////////////////////////////////////////////////////////////////
// test_dynamic_grammar.cpp
//
//  (C) Copyright Eric Niebler 2004.
//  Use, modification and distribution are subject to the
//  Boost Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

/*
 Revision history:
   5 March 2007 : Initial version.
*/

// defining this causes regex_impl objects to be counted, allowing us to detect
// leaks portably.
#define BOOST_XPRESSIVE_DEBUG_CYCLE_TEST

#include <boost/xpressive/xpressive.hpp>
#include <boost/test/unit_test.hpp>

void test_dynamic_grammar()
{
    using namespace boost::xpressive;

    {
        sregex expr;
        {
            sregex_compiler compiler;
            regex_constants::syntax_option_type x = regex_constants::ignore_white_space;

                   compiler.compile( "(? $group  = ) \\( (? $expr ) \\) ", x);
                   compiler.compile( "(? $factor = ) \\d+ | (? $group ) ", x);
                   compiler.compile( "(? $term   = ) (? $factor ) (?: \\* (? $factor ) | / (? $factor ) )* ", x);
            expr = compiler.compile( "(? $expr   = ) (? $term )   (?: \\+ (? $term )   | - (? $term )   )* ", x);
        }

        std::string str("foo 9*(10+3) bar");
        smatch what;

        if(regex_search(str, what, expr))
        {
             BOOST_CHECK_EQUAL(what[0].str(), "9*(10+3)");
             BOOST_CHECK_EQUAL((*what.nested_results().begin())[0].str(), "9*(10+3)");
             BOOST_CHECK_EQUAL((*(*what.nested_results().begin()).nested_results().begin())[0].str(), "9");
             BOOST_CHECK_EQUAL((*++(*what.nested_results().begin()).nested_results().begin())[0].str(), "(10+3)");
        }
        else
        {
            BOOST_ERROR("regex_search test 1 failed");
        }
    }

    // Test that all regex_impl instances have been cleaned up correctly
    BOOST_CHECK_EQUAL(0, detail::regex_impl<std::string::const_iterator>::instances);
}

void test_dynamic_grammar2()
{
    using namespace boost::xpressive;

    {
        sregex expr;
        {
            sregex_compiler compiler;
            regex_constants::syntax_option_type x = regex_constants::ignore_white_space;

            compiler["group"]   = compiler.compile( "\\( (? $expr ) \\) ", x);
            compiler["factor"]  = compiler.compile( "\\d+ | (? $group ) ", x);
            compiler["term"]    = compiler.compile( "(? $factor ) (?: \\* (? $factor ) | / (? $factor ) )* ", x);
            compiler["expr"]    = compiler.compile( "(? $term )   (?: \\+ (? $term )   | - (? $term )   )* ", x);

            expr = compiler["expr"];
        }

        std::string str("foo 9*(10+3) bar");
        smatch what;

        if(regex_search(str, what, expr))
        {
             BOOST_CHECK_EQUAL(what[0].str(), "9*(10+3)");
             BOOST_CHECK_EQUAL((*what.nested_results().begin())[0].str(), "9*(10+3)");
             BOOST_CHECK_EQUAL((*(*what.nested_results().begin()).nested_results().begin())[0].str(), "9");
             BOOST_CHECK_EQUAL((*++(*what.nested_results().begin()).nested_results().begin())[0].str(), "(10+3)");
        }
        else
        {
            BOOST_ERROR("regex_search test 2 failed");
        }
    }

    // Test that all regex_impl instances have been cleaned up correctly
    BOOST_CHECK_EQUAL(0, detail::regex_impl<std::string::const_iterator>::instances);
}

using namespace boost;
using namespace unit_test;

///////////////////////////////////////////////////////////////////////////////
// init_unit_test_suite
//
test_suite* init_unit_test_suite( int argc, char* argv[] )
{
    test_suite *test = BOOST_TEST_SUITE("testing dynamic grammars");
    test->add(BOOST_TEST_CASE(&test_dynamic_grammar));
    test->add(BOOST_TEST_CASE(&test_dynamic_grammar2));
    return test;
}