summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/spirit/classic/test/distinct_tests.cpp
blob: 0ef66b0a6958a3a85e4778412c1d1de1e1c07899 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*=============================================================================
    Copyright (c) 2003 Vaclav Vesely
    http://spirit.sourceforge.net/

    Use, modification and distribution is subject to the Boost Software
    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#include <boost/spirit/include/classic_core.hpp>
#include <boost/spirit/include/classic_distinct.hpp>
#include <boost/detail/lightweight_test.hpp>

using namespace boost;
using namespace BOOST_SPIRIT_CLASSIC_NS;

typedef
    scanner<char const*, scanner_policies<skipper_iteration_policy<> > >
        scanner_t;

typedef
    rule<scanner_t>
        rule_t;

void distinct_parser_test()
{
    // distinct_parser()
    {
        distinct_parser<> distinct_p;

        // operator()(CharT const* str) const
        rule_t r = distinct_p("keyword") >> !ch_p('-') >> int_p;
        BOOST_TEST(parse("keyword123", r, space_p).full);
    }

    // distinct_parser(CharT const* letters)
    {
        distinct_parser<> distinct_p("0-9a-zA-Z_");

        // operator()(CharT const* str) const
        rule_t r = distinct_p("keyword") >> !ch_p('-') >> int_p;
        BOOST_TEST(parse("keyword 123", r, space_p).full);
        BOOST_TEST(parse("keyword-123", r, space_p).full);
        BOOST_TEST(!parse("keyword123", r, space_p).hit);
    }

    // distinct_parser(parser<TailT> const & tail_)
    {
        distinct_parser<
            char,
            alternative<
                alnum_parser,
                sequence<
                    chlit<>,
                    negated_char_parser<chlit<> >
                >
            >
        >
            distinct_p(alnum_p | ('-' >> ~ch_p('-')));

        // operator()(CharT const* str) const
        rule_t r = distinct_p("keyword") >> !str_p("--") >> int_p;
        BOOST_TEST(parse("keyword 123", r, space_p).full);
        BOOST_TEST(parse("keyword--123", r, space_p).full);
        BOOST_TEST(!parse("keyword-123", r, space_p).hit);
    }
}

void distinct_directive_test()
{
    // distinct_directive()
    {
        distinct_directive<> distinct_d;

        // operator[](CharT const* str) const
        {
            rule_t r = distinct_d["keyword"] >> !ch_p('-') >> int_p;
            BOOST_TEST(parse("keyword123", r, space_p).full);
        }

        // operator[](parser<ParserT> const &subject) const
        {
            rule_t r = distinct_d[str_p("keyword")] >> !ch_p('-') >> int_p;
            BOOST_TEST(parse("keyword123", r, space_p).full);
        }
    }

    // distinct_directive(CharT const* letters)
    {
        distinct_directive<> distinct_d("0-9a-zA-Z_");

        // operator[](CharT const* str) const
        {
            rule_t r = distinct_d["keyword"] >> !ch_p('-') >> int_p;
            BOOST_TEST(parse("keyword 123", r, space_p).full);
            BOOST_TEST(parse("keyword-123", r, space_p).full);
            BOOST_TEST(!parse("keyword123", r, space_p).hit);
        }

        // operator[](parser<ParserT> const &subject) const
        {
            rule_t r = distinct_d[str_p("keyword")] >> !ch_p('-') >> int_p;
            BOOST_TEST(parse("keyword 123", r, space_p).full);
            BOOST_TEST(parse("keyword-123", r, space_p).full);
            BOOST_TEST(!parse("keyword123", r, space_p).hit);
        }
    }

    // distinct_directive(parser<TailT> const & tail_)
    {
        distinct_directive<
            char,
            alternative<
                alnum_parser,
                sequence<
                    chlit<>,
                    negated_char_parser<chlit<> >
                >
            >
        >
            distinct_d(alnum_p | ('-' >> ~ch_p('-')));

        // operator[](CharT const* str) const
        {
            rule_t r = distinct_d["keyword"] >> !str_p("--") >> int_p;
            BOOST_TEST(parse("keyword 123", r, space_p).full);
            BOOST_TEST(parse("keyword--123", r, space_p).full);
            BOOST_TEST(!parse("keyword-123", r, space_p).hit);
        }

        // operator[](parser<ParserT> const &subject) const
        {
            rule_t r = distinct_d[str_p("keyword")] >> !str_p("--") >> int_p;
            BOOST_TEST(parse("keyword 123", r, space_p).full);
            BOOST_TEST(parse("keyword--123", r, space_p).full);
            BOOST_TEST(!parse("keyword-123", r, space_p).hit);
        }
    }
}

void dynamic_distinct_parser_test()
{
    // dynamic_distinct_parser()
    {
        dynamic_distinct_parser<scanner_t> distinct_p;

        // operator()(CharT const* str) const
        rule_t r = distinct_p("keyword") >> !ch_p('-') >> int_p;
        BOOST_TEST(parse("keyword123", r, space_p).full);
    }

    // dynamic_distinct_parser(CharT const* letters)
    {
        dynamic_distinct_parser<scanner_t> distinct_p("0-9a-zA-Z_");

        // operator()(CharT const* str) const
        rule_t r = distinct_p("keyword") >> !ch_p('-') >> int_p;
        BOOST_TEST(parse("keyword 123", r, space_p).full);
        BOOST_TEST(parse("keyword-123", r, space_p).full);
        BOOST_TEST(!parse("keyword123", r, space_p).hit);
    }

    // dynamic_distinct_parser(parser<TailT> const & tail_)
    {
        dynamic_distinct_parser<scanner_t>
            distinct_p(alnum_p | ('-' >> ~ch_p('-')));

        // operator()(CharT const* str) const
        rule_t r = distinct_p("keyword") >> !str_p("--") >> int_p;
        BOOST_TEST(parse("keyword 123", r, space_p).full);
        BOOST_TEST(parse("keyword--123", r, space_p).full);
        BOOST_TEST(!parse("keyword-123", r, space_p).hit);
    }
}

void dynamic_distinct_directive_test()
{
    // dynamic_distinct_directive()
    {
        dynamic_distinct_directive<scanner_t> distinct_d;

        // operator[](CharT const* str) const
        {
            rule_t r = distinct_d["keyword"] >> !ch_p('-') >> int_p;
            BOOST_TEST(parse("keyword123", r, space_p).full);
        }

        // operator[](parser<ParserT> const &subject) const
        {
            rule_t r = distinct_d[str_p("keyword")] >> !ch_p('-') >> int_p;
            BOOST_TEST(parse("keyword123", r, space_p).full);
        }
    }

    // dynamic_distinct_directive(CharT const* letters)
    {
        dynamic_distinct_directive<scanner_t> distinct_d("0-9a-zA-Z_");

        // operator[](CharT const* str) const
        {
            rule_t r = distinct_d["keyword"] >> !ch_p('-') >> int_p;
            BOOST_TEST(parse("keyword 123", r, space_p).full);
            BOOST_TEST(parse("keyword-123", r, space_p).full);
            BOOST_TEST(!parse("keyword123", r, space_p).hit);
        }

        // operator[](parser<ParserT> const &subject) const
        {
            rule_t r = distinct_d[str_p("keyword")] >> !ch_p('-') >> int_p;
            BOOST_TEST(parse("keyword 123", r, space_p).full);
            BOOST_TEST(parse("keyword-123", r, space_p).full);
            BOOST_TEST(!parse("keyword123", r, space_p).hit);
        }
    }

    // dynamic_distinct_directive(parser<TailT> const & tail_)
    {
        dynamic_distinct_directive<scanner_t>
            distinct_d(alnum_p | ('-' >> ~ch_p('-')));

        // operator[](CharT const* str) const
        {
            rule_t r = distinct_d["keyword"] >> !str_p("--") >> int_p;
            BOOST_TEST(parse("keyword 123", r, space_p).full);
            BOOST_TEST(parse("keyword--123", r, space_p).full);
            BOOST_TEST(!parse("keyword-123", r, space_p).hit);
        }

        // operator[](parser<ParserT> const &subject) const
        {
            rule_t r = distinct_d[str_p("keyword")] >> !str_p("--") >> int_p;
            BOOST_TEST(parse("keyword 123", r, space_p).full);
            BOOST_TEST(parse("keyword--123", r, space_p).full);
            BOOST_TEST(!parse("keyword-123", r, space_p).hit);
        }
    }
}

int
main()
{
    distinct_parser_test();
    distinct_directive_test();
    dynamic_distinct_parser_test();
    dynamic_distinct_directive_test();

    return boost::report_errors();
}