1 | /*============================================================================= |
---|
2 | Copyright (c) 2001-2003 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 | #include <boost/detail/lightweight_test.hpp> |
---|
11 | #include <string> |
---|
12 | |
---|
13 | using namespace std; |
---|
14 | |
---|
15 | #include "impl/string_length.hpp" |
---|
16 | #include <boost/spirit/core.hpp> |
---|
17 | #include <boost/spirit/actor/assign_actor.hpp> |
---|
18 | using namespace boost::spirit; |
---|
19 | |
---|
20 | /////////////////////////////////////////////////////////////////////////////// |
---|
21 | // |
---|
22 | // Directives tests |
---|
23 | // |
---|
24 | /////////////////////////////////////////////////////////////////////////////// |
---|
25 | void |
---|
26 | directives_test1() |
---|
27 | { |
---|
28 | char const* cpx = "H e l l o"; |
---|
29 | char const* cpx_first = cpx; |
---|
30 | char const* cpx_last = cpx + test_impl::string_length(cpx); |
---|
31 | |
---|
32 | match<> hit; |
---|
33 | typedef skipper_iteration_policy<iteration_policy> iter_policy; |
---|
34 | scanner<char const*, scanner_policies<iter_policy> > |
---|
35 | scanx(cpx_first, cpx_last); |
---|
36 | |
---|
37 | hit = str_p("Hello").parse(scanx); |
---|
38 | BOOST_TEST(!hit); |
---|
39 | scanx.first = cpx; |
---|
40 | |
---|
41 | hit = chseq_p("Hello").parse(scanx); |
---|
42 | BOOST_TEST(!!hit); |
---|
43 | scanx.first = cpx; |
---|
44 | |
---|
45 | char const* cp = "Hello \n\tWorld"; |
---|
46 | char const* cp_first = cp; |
---|
47 | char const* cp_last = cp + test_impl::string_length(cp); |
---|
48 | |
---|
49 | scanner<char const*, scanner_policies<iter_policy> > |
---|
50 | scan(cp_first, cp_last); |
---|
51 | |
---|
52 | hit = (+(alpha_p | punct_p)).parse(scan); |
---|
53 | BOOST_TEST(!!hit); |
---|
54 | BOOST_TEST(scan.first == scan.last); |
---|
55 | scan.first = cp; |
---|
56 | |
---|
57 | hit = (+(lexeme_d[+(alpha_p | '\'')])).parse(scan); |
---|
58 | BOOST_TEST(!!hit); |
---|
59 | BOOST_TEST(scan.first == scan.last); |
---|
60 | scan.first = cp; |
---|
61 | |
---|
62 | hit = (+(lexeme_d[lexeme_d[+anychar_p]])).parse(scan); |
---|
63 | BOOST_TEST(!!hit); |
---|
64 | BOOST_TEST(scan.first == scan.last); |
---|
65 | scan.first = cp; |
---|
66 | |
---|
67 | hit = (str_p("Hello") >> "World").parse(scan); |
---|
68 | BOOST_TEST(!!hit); |
---|
69 | BOOST_TEST(scan.first == scan.last); |
---|
70 | scan.first = cp; |
---|
71 | |
---|
72 | hit = as_lower_d[str_p("hello") >> "world"].parse(scan); |
---|
73 | BOOST_TEST(!!hit); |
---|
74 | BOOST_TEST(scan.first == scan.last); |
---|
75 | scan.first = cp; |
---|
76 | |
---|
77 | hit = (+(as_lower_d[as_lower_d[+lower_p | '\'']])).parse(scan); |
---|
78 | BOOST_TEST(!!hit); |
---|
79 | BOOST_TEST(scan.first == scan.last); |
---|
80 | scan.first = cp; |
---|
81 | |
---|
82 | char const* cpy = "123.456"; |
---|
83 | char const* cpy_first = cpy; |
---|
84 | char const* cpy_last = cpy + test_impl::string_length(cpy); |
---|
85 | |
---|
86 | scanner<> scany(cpy_first, cpy_last); |
---|
87 | hit = longest_d[(+digit_p >> '.' >> +digit_p) | (+digit_p)].parse(scany); |
---|
88 | BOOST_TEST(!!hit); |
---|
89 | BOOST_TEST(scany.first == scany.last); |
---|
90 | scany.first = cpy; |
---|
91 | |
---|
92 | hit = shortest_d[(+digit_p >> '.' >> +digit_p) | (+digit_p)].parse(scany); |
---|
93 | BOOST_TEST(!!hit); |
---|
94 | BOOST_TEST(scany.first != scany.last); |
---|
95 | scany.first = cpy; |
---|
96 | |
---|
97 | char const* cpz = "razamanaz"; |
---|
98 | char const* cpz_first = cpz; |
---|
99 | char const* cpz_last = cpz + test_impl::string_length(cpz); |
---|
100 | |
---|
101 | scanner<> scanz(cpz_first, cpz_last); |
---|
102 | hit = longest_d[str_p("raza") | "razaman" | "razamanaz"].parse(scanz); |
---|
103 | BOOST_TEST(!!hit); |
---|
104 | BOOST_TEST(scanz.first == scanz.last); |
---|
105 | scanz.first = cpz; |
---|
106 | |
---|
107 | hit = shortest_d[str_p("raza") | "razaman" | "razamanaz"].parse(scanz); |
---|
108 | BOOST_TEST(!!hit); |
---|
109 | BOOST_TEST(scanz.first == cpz+4); |
---|
110 | scanz.first = cpz; |
---|
111 | |
---|
112 | // bounds_d |
---|
113 | |
---|
114 | parse_info<> pr = parse("123", limit_d(0, 60)[int_p]); |
---|
115 | BOOST_TEST(!pr.hit); |
---|
116 | |
---|
117 | pr = parse("-2", limit_d(0, 60)[int_p]); |
---|
118 | BOOST_TEST(!pr.hit); |
---|
119 | |
---|
120 | pr = parse("60", limit_d(0, 60)[int_p]); |
---|
121 | BOOST_TEST(pr.hit); |
---|
122 | |
---|
123 | pr = parse("0", limit_d(0, 60)[int_p]); |
---|
124 | BOOST_TEST(pr.hit); |
---|
125 | |
---|
126 | pr = parse("-2", min_limit_d(0)[int_p]); |
---|
127 | BOOST_TEST(!pr.hit); |
---|
128 | |
---|
129 | pr = parse("-2", min_limit_d(-5)[int_p]); |
---|
130 | BOOST_TEST(pr.hit); |
---|
131 | |
---|
132 | pr = parse("101", max_limit_d(100)[int_p]); |
---|
133 | BOOST_TEST(!pr.hit); |
---|
134 | |
---|
135 | pr = parse("100", max_limit_d(100)[int_p]); |
---|
136 | BOOST_TEST(pr.hit); |
---|
137 | } |
---|
138 | |
---|
139 | struct identifier : public grammar<identifier> |
---|
140 | { |
---|
141 | template <typename ScannerT> |
---|
142 | struct definition |
---|
143 | { |
---|
144 | definition(identifier const& /*self*/) |
---|
145 | { |
---|
146 | rr = +(alpha_p | '_'); |
---|
147 | r = lexeme_d[rr]; |
---|
148 | } |
---|
149 | |
---|
150 | rule<typename lexeme_scanner<ScannerT>::type> rr; |
---|
151 | rule<ScannerT> r; |
---|
152 | |
---|
153 | rule<ScannerT> const& |
---|
154 | start() const { return r; } |
---|
155 | }; |
---|
156 | }; |
---|
157 | |
---|
158 | void |
---|
159 | directives_test2() |
---|
160 | { |
---|
161 | // Test that lexeme_d does not skip trailing spaces |
---|
162 | |
---|
163 | string str1, str2; |
---|
164 | identifier ident; |
---|
165 | |
---|
166 | parse("rock_n_roll never_dies ", |
---|
167 | |
---|
168 | ident[assign_a(str1)] >> ident[assign_a(str2)], space_p |
---|
169 | ); |
---|
170 | |
---|
171 | cout << '*' << str1 << ',' << str2 << '*' << endl; |
---|
172 | |
---|
173 | |
---|
174 | BOOST_TEST(str1 == "rock_n_roll"); |
---|
175 | BOOST_TEST(str2 == "never_dies"); |
---|
176 | } |
---|
177 | |
---|
178 | /////////////////////////////////////////////////////////////////////////////// |
---|
179 | // |
---|
180 | // Main |
---|
181 | // |
---|
182 | /////////////////////////////////////////////////////////////////////////////// |
---|
183 | int |
---|
184 | main() |
---|
185 | { |
---|
186 | directives_test1(); |
---|
187 | directives_test2(); |
---|
188 | return boost::report_errors(); |
---|
189 | } |
---|
190 | |
---|