1//////////////////////////////////////////////////////////////////////////////
2// test_dynamic_grammar.cpp
3//
4// (C) Copyright Eric Niebler 2004.
5// Use, modification and distribution are subject to the
6// Boost Software License, Version 1.0. (See accompanying file
7// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8
9/*
10 Revision history:
11 5 March 2007 : Initial version.
12*/
13
14// defining this causes regex_impl objects to be counted, allowing us to detect
15// leaks portably.
16#define BOOST_XPRESSIVE_DEBUG_CYCLE_TEST
17
18#include <boost/xpressive/xpressive.hpp>
19#include <boost/test/unit_test.hpp>
20
21void test_dynamic_grammar()
22{
23 using namespace boost::xpressive;
24
25 {
26 sregex expr;
27 {
28 sregex_compiler compiler;
29 regex_constants::syntax_option_type x = regex_constants::ignore_white_space;
30
31 compiler.compile( begin: "(? $group = ) \\( (? $expr ) \\) ", flags: x);
32 compiler.compile( begin: "(? $factor = ) \\d+ | (? $group ) ", flags: x);
33 compiler.compile( begin: "(? $term = ) (? $factor ) (?: \\* (? $factor ) | / (? $factor ) )* ", flags: x);
34 expr = compiler.compile( begin: "(? $expr = ) (? $term ) (?: \\+ (? $term ) | - (? $term ) )* ", flags: x);
35 }
36
37 std::string str("foo 9*(10+3) bar");
38 smatch what;
39
40 if(regex_search(rng&: str, what, re: expr))
41 {
42 BOOST_CHECK_EQUAL(what[0].str(), "9*(10+3)");
43 BOOST_CHECK_EQUAL((*what.nested_results().begin())[0].str(), "9*(10+3)");
44 BOOST_CHECK_EQUAL((*(*what.nested_results().begin()).nested_results().begin())[0].str(), "9");
45 BOOST_CHECK_EQUAL((*++(*what.nested_results().begin()).nested_results().begin())[0].str(), "(10+3)");
46 }
47 else
48 {
49 BOOST_ERROR("regex_search test 1 failed");
50 }
51 }
52
53 // Test that all regex_impl instances have been cleaned up correctly
54 BOOST_CHECK_EQUAL(0, detail::regex_impl<std::string::const_iterator>::instances);
55}
56
57void test_dynamic_grammar2()
58{
59 using namespace boost::xpressive;
60
61 {
62 sregex expr;
63 {
64 sregex_compiler compiler;
65 regex_constants::syntax_option_type x = regex_constants::ignore_white_space;
66
67 compiler["group"] = compiler.compile( begin: "\\( (? $expr ) \\) ", flags: x);
68 compiler["factor"] = compiler.compile( begin: "\\d+ | (? $group ) ", flags: x);
69 compiler["term"] = compiler.compile( begin: "(? $factor ) (?: \\* (? $factor ) | / (? $factor ) )* ", flags: x);
70 compiler["expr"] = compiler.compile( begin: "(? $term ) (?: \\+ (? $term ) | - (? $term ) )* ", flags: x);
71
72 expr = compiler["expr"];
73 }
74
75 std::string str("foo 9*(10+3) bar");
76 smatch what;
77
78 if(regex_search(rng&: str, what, re: expr))
79 {
80 BOOST_CHECK_EQUAL(what[0].str(), "9*(10+3)");
81 BOOST_CHECK_EQUAL((*what.nested_results().begin())[0].str(), "9*(10+3)");
82 BOOST_CHECK_EQUAL((*(*what.nested_results().begin()).nested_results().begin())[0].str(), "9");
83 BOOST_CHECK_EQUAL((*++(*what.nested_results().begin()).nested_results().begin())[0].str(), "(10+3)");
84 }
85 else
86 {
87 BOOST_ERROR("regex_search test 2 failed");
88 }
89 }
90
91 // Test that all regex_impl instances have been cleaned up correctly
92 BOOST_CHECK_EQUAL(0, detail::regex_impl<std::string::const_iterator>::instances);
93}
94
95using namespace boost;
96using namespace unit_test;
97
98///////////////////////////////////////////////////////////////////////////////
99// init_unit_test_suite
100//
101test_suite* init_unit_test_suite( int argc, char* argv[] )
102{
103 test_suite *test = BOOST_TEST_SUITE("testing dynamic grammars");
104 test->add(BOOST_TEST_CASE(&test_dynamic_grammar));
105 test->add(BOOST_TEST_CASE(&test_dynamic_grammar2));
106 return test;
107}
108

source code of boost/libs/xpressive/test/test_dynamic_grammar.cpp