1 | /////////////////////////////////////////////////////////////////////////////// |
---|
2 | // misc1.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 | void test1() |
---|
16 | { |
---|
17 | // make sure the following compiles: |
---|
18 | sregex a = _; |
---|
19 | sregex b = _; |
---|
20 | sregex c = a >> b; |
---|
21 | c = a | b; |
---|
22 | c = !a; |
---|
23 | c = *a; |
---|
24 | c = +a; |
---|
25 | } |
---|
26 | |
---|
27 | /////////////////////////////////////////////////////////////////////////////// |
---|
28 | // test for basic_regex in a keep |
---|
29 | // |
---|
30 | void test2() |
---|
31 | { |
---|
32 | std::locale loc; |
---|
33 | std::string str("Its a mad Mad mAd maD world"); |
---|
34 | sregex word = +_w; |
---|
35 | sregex sentence = imbue(loc)(*(keep(word) >> +_s) >> word); |
---|
36 | smatch what; |
---|
37 | |
---|
38 | BOOST_REQUIRE(regex_match(str, what, sentence)); |
---|
39 | BOOST_REQUIRE(7 == what.nested_results().size()); |
---|
40 | smatch::nested_results_type::const_iterator pword = what.nested_results().begin(); |
---|
41 | BOOST_CHECK((*pword++)[0] == "Its"); |
---|
42 | BOOST_CHECK((*pword++)[0] == "a"); |
---|
43 | BOOST_CHECK((*pword++)[0] == "mad"); |
---|
44 | BOOST_CHECK((*pword++)[0] == "Mad"); |
---|
45 | BOOST_CHECK((*pword++)[0] == "mAd"); |
---|
46 | BOOST_CHECK((*pword++)[0] == "maD"); |
---|
47 | BOOST_CHECK((*pword++)[0] == "world"); |
---|
48 | BOOST_CHECK(pword == what.nested_results().end()); |
---|
49 | } |
---|
50 | |
---|
51 | /////////////////////////////////////////////////////////////////////////////// |
---|
52 | // test for a simple non-recursive grammar |
---|
53 | // |
---|
54 | void test3() |
---|
55 | { |
---|
56 | // test for a simple regex grammar |
---|
57 | std::string buffer = |
---|
58 | "FROGGIE\r\n" |
---|
59 | "Volume = 1\r\n" |
---|
60 | "Other1= 2\r\n" |
---|
61 | "Channel=3\r\n" |
---|
62 | "Other =4\r\n" |
---|
63 | "\r\n" |
---|
64 | "FROGGIE\r\n" |
---|
65 | "Volume = 5\r\n" |
---|
66 | "Other1= 6\r\n" |
---|
67 | "Channel=7\r\n" |
---|
68 | "Other =8\r\n" |
---|
69 | "\r\n" |
---|
70 | "FROGGIE\r\n" |
---|
71 | "Volume = 9\r\n" |
---|
72 | "Other1= 0\r\n" |
---|
73 | "Channel=10\r\n" |
---|
74 | "\r\n"; |
---|
75 | |
---|
76 | mark_tag name(1), value(2); |
---|
77 | |
---|
78 | sregex name_value_pair_ = |
---|
79 | (name= +alnum) >> *_s >> "=" >> *_s >> |
---|
80 | (value= +_d) >> *_s >> _ln; |
---|
81 | |
---|
82 | sregex message_ = |
---|
83 | *_s >> "FROGGIE" >> _ln >> +name_value_pair_ >> _ln; |
---|
84 | |
---|
85 | sregex re_ = +message_; |
---|
86 | |
---|
87 | smatch::nested_results_type::const_iterator msg, nvp; |
---|
88 | smatch tmpwhat; |
---|
89 | |
---|
90 | BOOST_REQUIRE(regex_search(buffer, tmpwhat, re_)); |
---|
91 | // for giggles, make a deep-copy of the tree of results |
---|
92 | smatch what = tmpwhat; |
---|
93 | BOOST_REQUIRE(3 == what.nested_results().size()); |
---|
94 | |
---|
95 | msg = what.nested_results().begin(); |
---|
96 | BOOST_REQUIRE(4 == msg->nested_results().size()); |
---|
97 | |
---|
98 | nvp = msg->nested_results().begin(); |
---|
99 | BOOST_REQUIRE(3 == nvp->size()); |
---|
100 | BOOST_CHECK("Volume" == (*nvp)[name]); |
---|
101 | BOOST_CHECK("1" == (*nvp)[value]); |
---|
102 | ++nvp; |
---|
103 | BOOST_REQUIRE(3 == nvp->size()); |
---|
104 | BOOST_CHECK("Other1" == (*nvp)[name]); |
---|
105 | BOOST_CHECK("2" == (*nvp)[value]); |
---|
106 | ++nvp; |
---|
107 | BOOST_REQUIRE(3 == nvp->size()); |
---|
108 | BOOST_CHECK("Channel" == (*nvp)[name]); |
---|
109 | BOOST_CHECK("3" == (*nvp)[value]); |
---|
110 | ++nvp; |
---|
111 | BOOST_REQUIRE(3 == nvp->size()); |
---|
112 | BOOST_CHECK("Other" == (*nvp)[name]); |
---|
113 | BOOST_CHECK("4" == (*nvp)[value]); |
---|
114 | |
---|
115 | ++msg; |
---|
116 | BOOST_REQUIRE(4 == msg->nested_results().size()); |
---|
117 | |
---|
118 | nvp = msg->nested_results().begin(); |
---|
119 | BOOST_REQUIRE(3 == nvp->size()); |
---|
120 | BOOST_CHECK("Volume" == (*nvp)[name]); |
---|
121 | BOOST_CHECK("5" == (*nvp)[value]); |
---|
122 | ++nvp; |
---|
123 | BOOST_REQUIRE(3 == nvp->size()); |
---|
124 | BOOST_CHECK("Other1" == (*nvp)[name]); |
---|
125 | BOOST_CHECK("6" == (*nvp)[value]); |
---|
126 | ++nvp; |
---|
127 | BOOST_REQUIRE(3 == nvp->size()); |
---|
128 | BOOST_CHECK("Channel" == (*nvp)[name]); |
---|
129 | BOOST_CHECK("7" == (*nvp)[value]); |
---|
130 | ++nvp; |
---|
131 | BOOST_REQUIRE(3 == nvp->size()); |
---|
132 | BOOST_CHECK("Other" == (*nvp)[name]); |
---|
133 | BOOST_CHECK("8" == (*nvp)[value]); |
---|
134 | |
---|
135 | ++msg; |
---|
136 | BOOST_REQUIRE(3 == msg->nested_results().size()); |
---|
137 | |
---|
138 | nvp = msg->nested_results().begin(); |
---|
139 | BOOST_REQUIRE(3 == nvp->size()); |
---|
140 | BOOST_CHECK("Volume" == (*nvp)[name]); |
---|
141 | BOOST_CHECK("9" == (*nvp)[value]); |
---|
142 | ++nvp; |
---|
143 | BOOST_REQUIRE(3 == nvp->size()); |
---|
144 | BOOST_CHECK("Other1" == (*nvp)[name]); |
---|
145 | BOOST_CHECK("0" == (*nvp)[value]); |
---|
146 | ++nvp; |
---|
147 | BOOST_REQUIRE(3 == nvp->size()); |
---|
148 | BOOST_CHECK("Channel" == (*nvp)[name]); |
---|
149 | BOOST_CHECK("10" == (*nvp)[value]); |
---|
150 | } |
---|
151 | |
---|
152 | /////////////////////////////////////////////////////////////////////////////// |
---|
153 | // test for a self-recursive regex |
---|
154 | // |
---|
155 | void test4() |
---|
156 | { |
---|
157 | sregex parentheses; |
---|
158 | parentheses // A balanced set of parentheses ... |
---|
159 | = '(' // is an opening parenthesis ... |
---|
160 | >> // followed by ... |
---|
161 | *( // zero or more ... |
---|
162 | keep( +~(set='(',')') ) // of a bunch of things that are not parentheses ... |
---|
163 | | // or ... |
---|
164 | by_ref(parentheses) // a balanced set of parentheses |
---|
165 | ) // (ooh, recursion!) ... |
---|
166 | >> // followed by ... |
---|
167 | ')' // a closing parenthesis |
---|
168 | ; |
---|
169 | |
---|
170 | smatch what; |
---|
171 | smatch::nested_results_type::const_iterator pwhat, pwhat2; |
---|
172 | std::string str( "blah blah( a(b)c (c(e)f (g)h )i (j)6 )blah" ); |
---|
173 | |
---|
174 | BOOST_REQUIRE(regex_search(str, what, parentheses)); |
---|
175 | BOOST_REQUIRE(1 == what.size()); |
---|
176 | BOOST_CHECK("( a(b)c (c(e)f (g)h )i (j)6 )" == what[0]); |
---|
177 | |
---|
178 | BOOST_REQUIRE(3 == what.nested_results().size()); |
---|
179 | pwhat = what.nested_results().begin(); |
---|
180 | BOOST_REQUIRE(1 == pwhat->size()); |
---|
181 | BOOST_CHECK("(b)" == (*pwhat)[0]); |
---|
182 | |
---|
183 | ++pwhat; |
---|
184 | BOOST_REQUIRE(1 == pwhat->size()); |
---|
185 | BOOST_CHECK("(c(e)f (g)h )" == (*pwhat)[0]); |
---|
186 | |
---|
187 | BOOST_REQUIRE(2 == pwhat->nested_results().size()); |
---|
188 | pwhat2 = pwhat->nested_results().begin(); |
---|
189 | BOOST_REQUIRE(1 == pwhat2->size()); |
---|
190 | BOOST_CHECK("(e)" == (*pwhat2)[0]); |
---|
191 | |
---|
192 | ++pwhat2; |
---|
193 | BOOST_REQUIRE(1 == pwhat2->size()); |
---|
194 | BOOST_CHECK("(g)" == (*pwhat2)[0]); |
---|
195 | |
---|
196 | ++pwhat; |
---|
197 | BOOST_REQUIRE(1 == pwhat->size()); |
---|
198 | BOOST_CHECK("(j)" == (*pwhat)[0]); |
---|
199 | } |
---|
200 | |
---|
201 | /////////////////////////////////////////////////////////////////////////////// |
---|
202 | // test_main |
---|
203 | // |
---|
204 | int test_main( int, char*[] ) |
---|
205 | { |
---|
206 | test1(); |
---|
207 | test2(); |
---|
208 | test3(); |
---|
209 | test4(); |
---|
210 | |
---|
211 | return 0; |
---|
212 | } |
---|