1 /*=============================================================================
2     Copyright (c) 2003 Vaclav Vesely
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 <boost/spirit/include/classic_core.hpp>
10 #include <boost/spirit/include/classic_distinct.hpp>
11 #include <boost/detail/lightweight_test.hpp>
12 
13 using namespace boost;
14 using namespace BOOST_SPIRIT_CLASSIC_NS;
15 
16 typedef
17     scanner<char const*, scanner_policies<skipper_iteration_policy<> > >
18         scanner_t;
19 
20 typedef
21     rule<scanner_t>
22         rule_t;
23 
distinct_parser_test()24 void distinct_parser_test()
25 {
26     // distinct_parser()
27     {
28         distinct_parser<> distinct_p;
29 
30         // operator()(CharT const* str) const
31         rule_t r = distinct_p("keyword") >> !ch_p('-') >> int_p;
32         BOOST_TEST(parse("keyword123", r, space_p).full);
33     }
34 
35     // distinct_parser(CharT const* letters)
36     {
37         distinct_parser<> distinct_p("0-9a-zA-Z_");
38 
39         // operator()(CharT const* str) const
40         rule_t r = distinct_p("keyword") >> !ch_p('-') >> int_p;
41         BOOST_TEST(parse("keyword 123", r, space_p).full);
42         BOOST_TEST(parse("keyword-123", r, space_p).full);
43         BOOST_TEST(!parse("keyword123", r, space_p).hit);
44     }
45 
46     // distinct_parser(parser<TailT> const & tail_)
47     {
48         distinct_parser<
49             char,
50             alternative<
51                 alnum_parser,
52                 sequence<
53                     chlit<>,
54                     negated_char_parser<chlit<> >
55                 >
56             >
57         >
58             distinct_p(alnum_p | ('-' >> ~ch_p('-')));
59 
60         // operator()(CharT const* str) const
61         rule_t r = distinct_p("keyword") >> !str_p("--") >> int_p;
62         BOOST_TEST(parse("keyword 123", r, space_p).full);
63         BOOST_TEST(parse("keyword--123", r, space_p).full);
64         BOOST_TEST(!parse("keyword-123", r, space_p).hit);
65     }
66 }
67 
distinct_directive_test()68 void distinct_directive_test()
69 {
70     // distinct_directive()
71     {
72         distinct_directive<> distinct_d;
73 
74         // operator[](CharT const* str) const
75         {
76             rule_t r = distinct_d["keyword"] >> !ch_p('-') >> int_p;
77             BOOST_TEST(parse("keyword123", r, space_p).full);
78         }
79 
80         // operator[](parser<ParserT> const &subject) const
81         {
82             rule_t r = distinct_d[str_p("keyword")] >> !ch_p('-') >> int_p;
83             BOOST_TEST(parse("keyword123", r, space_p).full);
84         }
85     }
86 
87     // distinct_directive(CharT const* letters)
88     {
89         distinct_directive<> distinct_d("0-9a-zA-Z_");
90 
91         // operator[](CharT const* str) const
92         {
93             rule_t r = distinct_d["keyword"] >> !ch_p('-') >> int_p;
94             BOOST_TEST(parse("keyword 123", r, space_p).full);
95             BOOST_TEST(parse("keyword-123", r, space_p).full);
96             BOOST_TEST(!parse("keyword123", r, space_p).hit);
97         }
98 
99         // operator[](parser<ParserT> const &subject) const
100         {
101             rule_t r = distinct_d[str_p("keyword")] >> !ch_p('-') >> int_p;
102             BOOST_TEST(parse("keyword 123", r, space_p).full);
103             BOOST_TEST(parse("keyword-123", r, space_p).full);
104             BOOST_TEST(!parse("keyword123", r, space_p).hit);
105         }
106     }
107 
108     // distinct_directive(parser<TailT> const & tail_)
109     {
110         distinct_directive<
111             char,
112             alternative<
113                 alnum_parser,
114                 sequence<
115                     chlit<>,
116                     negated_char_parser<chlit<> >
117                 >
118             >
119         >
120             distinct_d(alnum_p | ('-' >> ~ch_p('-')));
121 
122         // operator[](CharT const* str) const
123         {
124             rule_t r = distinct_d["keyword"] >> !str_p("--") >> int_p;
125             BOOST_TEST(parse("keyword 123", r, space_p).full);
126             BOOST_TEST(parse("keyword--123", r, space_p).full);
127             BOOST_TEST(!parse("keyword-123", r, space_p).hit);
128         }
129 
130         // operator[](parser<ParserT> const &subject) const
131         {
132             rule_t r = distinct_d[str_p("keyword")] >> !str_p("--") >> int_p;
133             BOOST_TEST(parse("keyword 123", r, space_p).full);
134             BOOST_TEST(parse("keyword--123", r, space_p).full);
135             BOOST_TEST(!parse("keyword-123", r, space_p).hit);
136         }
137     }
138 }
139 
dynamic_distinct_parser_test()140 void dynamic_distinct_parser_test()
141 {
142     // dynamic_distinct_parser()
143     {
144         dynamic_distinct_parser<scanner_t> distinct_p;
145 
146         // operator()(CharT const* str) const
147         rule_t r = distinct_p("keyword") >> !ch_p('-') >> int_p;
148         BOOST_TEST(parse("keyword123", r, space_p).full);
149     }
150 
151     // dynamic_distinct_parser(CharT const* letters)
152     {
153         dynamic_distinct_parser<scanner_t> distinct_p("0-9a-zA-Z_");
154 
155         // operator()(CharT const* str) const
156         rule_t r = distinct_p("keyword") >> !ch_p('-') >> int_p;
157         BOOST_TEST(parse("keyword 123", r, space_p).full);
158         BOOST_TEST(parse("keyword-123", r, space_p).full);
159         BOOST_TEST(!parse("keyword123", r, space_p).hit);
160     }
161 
162     // dynamic_distinct_parser(parser<TailT> const & tail_)
163     {
164         dynamic_distinct_parser<scanner_t>
165             distinct_p(alnum_p | ('-' >> ~ch_p('-')));
166 
167         // operator()(CharT const* str) const
168         rule_t r = distinct_p("keyword") >> !str_p("--") >> int_p;
169         BOOST_TEST(parse("keyword 123", r, space_p).full);
170         BOOST_TEST(parse("keyword--123", r, space_p).full);
171         BOOST_TEST(!parse("keyword-123", r, space_p).hit);
172     }
173 }
174 
dynamic_distinct_directive_test()175 void dynamic_distinct_directive_test()
176 {
177     // dynamic_distinct_directive()
178     {
179         dynamic_distinct_directive<scanner_t> distinct_d;
180 
181         // operator[](CharT const* str) const
182         {
183             rule_t r = distinct_d["keyword"] >> !ch_p('-') >> int_p;
184             BOOST_TEST(parse("keyword123", r, space_p).full);
185         }
186 
187         // operator[](parser<ParserT> const &subject) const
188         {
189             rule_t r = distinct_d[str_p("keyword")] >> !ch_p('-') >> int_p;
190             BOOST_TEST(parse("keyword123", r, space_p).full);
191         }
192     }
193 
194     // dynamic_distinct_directive(CharT const* letters)
195     {
196         dynamic_distinct_directive<scanner_t> distinct_d("0-9a-zA-Z_");
197 
198         // operator[](CharT const* str) const
199         {
200             rule_t r = distinct_d["keyword"] >> !ch_p('-') >> int_p;
201             BOOST_TEST(parse("keyword 123", r, space_p).full);
202             BOOST_TEST(parse("keyword-123", r, space_p).full);
203             BOOST_TEST(!parse("keyword123", r, space_p).hit);
204         }
205 
206         // operator[](parser<ParserT> const &subject) const
207         {
208             rule_t r = distinct_d[str_p("keyword")] >> !ch_p('-') >> int_p;
209             BOOST_TEST(parse("keyword 123", r, space_p).full);
210             BOOST_TEST(parse("keyword-123", r, space_p).full);
211             BOOST_TEST(!parse("keyword123", r, space_p).hit);
212         }
213     }
214 
215     // dynamic_distinct_directive(parser<TailT> const & tail_)
216     {
217         dynamic_distinct_directive<scanner_t>
218             distinct_d(alnum_p | ('-' >> ~ch_p('-')));
219 
220         // operator[](CharT const* str) const
221         {
222             rule_t r = distinct_d["keyword"] >> !str_p("--") >> int_p;
223             BOOST_TEST(parse("keyword 123", r, space_p).full);
224             BOOST_TEST(parse("keyword--123", r, space_p).full);
225             BOOST_TEST(!parse("keyword-123", r, space_p).hit);
226         }
227 
228         // operator[](parser<ParserT> const &subject) const
229         {
230             rule_t r = distinct_d[str_p("keyword")] >> !str_p("--") >> int_p;
231             BOOST_TEST(parse("keyword 123", r, space_p).full);
232             BOOST_TEST(parse("keyword--123", r, space_p).full);
233             BOOST_TEST(!parse("keyword-123", r, space_p).hit);
234         }
235     }
236 }
237 
238 int
main()239 main()
240 {
241     distinct_parser_test();
242     distinct_directive_test();
243     dynamic_distinct_parser_test();
244     dynamic_distinct_directive_test();
245 
246     return boost::report_errors();
247 }
248 
249