1 /*=============================================================================
2 Copyright (c) 2001-2003 Joel de Guzman
3 Copyright (c) 2003 Martin Wille
4 http://spirit.sourceforge.net/
5
6 Use, modification and distribution is subject to the Boost Software
7 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 http://www.boost.org/LICENSE_1_0.txt)
9 =============================================================================*/
10 #include <iostream>
11 #include <boost/detail/lightweight_test.hpp>
12 #include <string>
13
14
15 #include <boost/spirit/include/classic_core.hpp>
16 #include <boost/spirit/include/classic_parametric.hpp>
17 #include <boost/spirit/include/phoenix1_primitives.hpp>
18 #include <boost/spirit/include/phoenix1_operators.hpp>
19 using namespace BOOST_SPIRIT_CLASSIC_NS;
20 using namespace phoenix;
21
22 #include <boost/detail/lightweight_test.hpp>
23
24 ///////////////////////////////////////////////////////////////////////////////
25 //
26 // Parametric tests
27 //
28 ///////////////////////////////////////////////////////////////////////////////
29
30 template <typename T>
31 static unsigned
length(T const * p)32 length(T const *p)
33 {
34 unsigned result = 0;
35 while (*p++)
36 ++result;
37 return result;
38 }
39
40 template <typename T>
41 bool
is_equal(T const * a,T const * b)42 is_equal(T const* a, T const* b)
43 {
44 while (*a && *b)
45 if (*a++ != *b++)
46 return false;
47 return true;
48 }
49
50 typedef rule< scanner<wchar_t const *> > wrule_t;
51
52 void
narrow_f_ch_p()53 narrow_f_ch_p()
54 {
55 char ch;
56 rule<> r = anychar_p[var(ch) = arg1] >> *f_ch_p(const_(ch));
57 parse_info<char const*> pi;
58
59 pi = parse("aaaaaaaaa", r);
60 BOOST_TEST(pi.hit);
61 BOOST_TEST(pi.full);
62
63 pi = parse("aaaaabaaa", r);
64 BOOST_TEST(pi.hit);
65 BOOST_TEST(!pi.full);
66 BOOST_TEST(is_equal(pi.stop, "baaa"));
67 }
68
69 void
wide_f_ch_p()70 wide_f_ch_p()
71 {
72 wchar_t ch;
73 wrule_t r = anychar_p[var(ch) = arg1] >> *f_ch_p(const_(ch));
74 parse_info<wchar_t const*> pi;
75
76 pi = parse(L"aaaaaaaaa", r);
77 BOOST_TEST(pi.hit);
78 BOOST_TEST(pi.full);
79
80 pi = parse(L"aaaaabaaa", r);
81 BOOST_TEST(pi.hit);
82 BOOST_TEST(!pi.full);
83 BOOST_TEST(is_equal(pi.stop, L"baaa"));
84 }
85
86 void
narrow_f_range_p()87 narrow_f_range_p()
88 {
89 char from = 'a';
90 char to = 'z';
91
92 parse_info<char const*> pi;
93
94 rule<> r2 = *f_range_p(const_(from), const_(to));
95 pi = parse("abcdefghijklmnopqrstuvwxyz", r2);
96 BOOST_TEST(pi.hit);
97 BOOST_TEST(pi.full);
98
99 pi = parse("abcdefghijklmnopqrstuvwxyz123", r2);
100 BOOST_TEST(pi.hit);
101 BOOST_TEST(!pi.full);
102 BOOST_TEST(is_equal(pi.stop, "123"));
103 }
104
105 void
wide_f_range_p()106 wide_f_range_p()
107 {
108 wchar_t from = L'a';
109 wchar_t to = L'z';
110
111 parse_info<wchar_t const*> pi;
112
113 wrule_t r2 = *f_range_p(const_(from), const_(to));
114 pi = parse(L"abcdefghijklmnopqrstuvwxyz", r2);
115 BOOST_TEST(pi.hit);
116 BOOST_TEST(pi.full);
117
118 pi = parse(L"abcdefghijklmnopqrstuvwxyz123", r2);
119 BOOST_TEST(pi.hit);
120 BOOST_TEST(!pi.full);
121 BOOST_TEST(is_equal(pi.stop, L"123"));
122 }
123
124 void
narrow_f_str_p()125 narrow_f_str_p()
126 {
127 parse_info<char const*> pi;
128
129 char const* start = "kim";
130 char const* end = start + length(start);
131 rule<> r3 = +f_str_p(const_(start), const_(end));
132
133 pi = parse("kimkimkimkimkimkimkimkimkim", r3);
134 BOOST_TEST(pi.hit);
135 BOOST_TEST(pi.full);
136
137 pi = parse("kimkimkimkimkimkimkimkimkimmama", r3);
138 BOOST_TEST(pi.hit);
139 BOOST_TEST(!pi.full);
140 BOOST_TEST(is_equal(pi.stop, "mama"));
141
142 pi = parse("joel", r3);
143 BOOST_TEST(!pi.hit);
144 }
145
146 void
wide_f_str_p()147 wide_f_str_p()
148 {
149 parse_info<wchar_t const*> pi;
150
151 wchar_t const* start = L"kim";
152 wchar_t const* end = start + length(start);
153 wrule_t r3 = +f_str_p(const_(start), const_(end));
154
155 pi = parse(L"kimkimkimkimkimkimkimkimkim", r3);
156 BOOST_TEST(pi.hit);
157 BOOST_TEST(pi.full);
158
159 pi = parse(L"kimkimkimkimkimkimkimkimkimmama", r3);
160 BOOST_TEST(pi.hit);
161 BOOST_TEST(!pi.full);
162 BOOST_TEST(is_equal(pi.stop, L"mama"));
163
164 pi = parse(L"joel", r3);
165 BOOST_TEST(!pi.hit);
166 }
167
168 ///////////////////////////////////////////////////////////////////////////////
169 //
170 // test suite
171 //
172 ///////////////////////////////////////////////////////////////////////////////
173 int
main()174 main()
175 {
176 narrow_f_ch_p();
177 wide_f_ch_p();
178 narrow_f_range_p();
179 wide_f_range_p();
180 narrow_f_str_p();
181 wide_f_str_p();
182
183 return boost::report_errors();
184 }
185
186