File: subrule.cpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (396 lines) | stat: -rw-r--r-- 11,004 bytes parent folder | download | duplicates (7)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*=============================================================================
    Copyright (c) 2001-2010 Joel de Guzman
    Copyright (c) 2009 Francois Barel

    Distributed under 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/repository/include/qi_subrule.hpp>

#include <boost/spirit/include/qi_operator.hpp>
#include <boost/spirit/include/qi_char.hpp>
#include <boost/spirit/include/qi_string.hpp>
#include <boost/spirit/include/qi_numeric.hpp>
#include <boost/spirit/include/qi_auxiliary.hpp>
#include <boost/spirit/include/qi_nonterminal.hpp>
#include <boost/spirit/include/qi_action.hpp>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/object.hpp>
#include <boost/phoenix/bind.hpp>
#include <boost/fusion/include/std_pair.hpp>

#include <string>
#include <cstring>
#include <iostream>
#include "test.hpp"

int
main()
{
    using spirit_test::test_attr;
    using spirit_test::test;

    using namespace boost::spirit::ascii;
    using namespace boost::spirit::qi::labels;
    using boost::spirit::qi::locals;
    using boost::spirit::qi::rule;
    using boost::spirit::qi::int_;
    using boost::spirit::qi::fail;
    using boost::spirit::qi::on_error;
    using boost::spirit::qi::debug;
    using boost::spirit::repository::qi::subrule;

    namespace phx = boost::phoenix;


    { // basic tests

        subrule<99> entry;
        subrule<42> a;
        subrule<48> b;
        subrule<16> c;
        rule<char const*> start;

        entry.name("entry");
        a.name("a");
        b.name("b");
        c.name("c");
        start.name("start");

//        debug(entry);
//        debug(a);
//        debug(b);
//        debug(c);
        debug(start);

        // subrules with no rule
        BOOST_TEST(test("abcabcacb", (
            entry = *(a | b | c)
          , a = 'a'
          , b = 'b'
          , c = 'c'
        )));

        // check subrule group behaves as a parser
        BOOST_TEST(test("xabcabcacb", 'x' >> (
            entry = *(a | b | c)
          , a = 'a'
          , b = 'b'
          , c = 'c'
        )));

        // subrules in a rule
        start = (
            entry = *(a | b | c)
          , a = 'a'
          , b = 'b'
          , c = 'c'
        );
        BOOST_TEST(test("abcabcacb", start));

        // subrule -> rule call
        start = (
            entry = (a | b) >> (start | b)
          , a = 'a'
          , b = 'b'
        );
        BOOST_TEST(test("aaaabababaaabbb", start));
        BOOST_TEST(test("aaaabababaaabba", start, false));

        // subrule recursion
        start = (
            entry = (a | b) >> (entry | b)
          , a = 'a'
          , b = 'b'
        );
        BOOST_TEST(test("aaaabababaaabbb", start));
        BOOST_TEST(test("aaaabababaaabba", start, false));

        // no-ops
#if defined(BOOST_CLANG) && defined(__has_warning)
# pragma clang diagnostic push
# if __has_warning("-Wself-assign-overloaded")
#  pragma clang diagnostic ignored "-Wself-assign-overloaded"
# endif
#endif
        a = a;
#if defined(BOOST_CLANG) && defined(__has_warning)
# pragma clang diagnostic pop
#endif
        subrule<42> aa(a);
    }

    { // basic tests w/ skipper, subrules declared const

        subrule<0> const entry("entry");
        subrule<1> const a("a");
        subrule<2> const b("b");
        subrule<3> const c("c");
        rule<char const*, space_type> start("start");

//        debug(entry);
//        debug(a);
//        debug(b);
//        debug(c);
        debug(start);

        start = (
            entry = *(a | b | c)
          , a = 'a'
          , b = 'b'
          , c = 'c'
        );
        BOOST_TEST(test(" a b c a b c a c b ", start, space));

        start = (
            entry = (a | b) >> (entry | b)
          , a = 'a'
          , b = 'b'
        );
        BOOST_TEST(test(" a a a a b a b a b a a a b b b ", start, space));
        BOOST_TEST(test(" a a a a b a b a b a a a b b a ", start, space, false));

        // no-ops
#if defined(BOOST_CLANG) && defined(__has_warning)
# pragma clang diagnostic push
# if __has_warning("-Wself-assign-overloaded")
#  pragma clang diagnostic ignored "-Wself-assign-overloaded"
# endif
#endif
        a = a;
#if defined(BOOST_CLANG) && defined(__has_warning)
# pragma clang diagnostic pop
#endif
        subrule<1> aa(a);
    }

    { // context tests

        char ch;
        rule<char const*, char()> a;
        subrule<0, char()> entry;

        a = (entry = alpha[_val = _1])[_val = _1];
        BOOST_TEST(test("x", a[phx::ref(ch) = _1]));
        BOOST_TEST(ch == 'x');

        a %= (entry = alpha[_val = _1]);
        BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
        BOOST_TEST(ch == 'z');
    }

    { // auto subrules tests

        char ch;
        rule<char const*, char()> a;
        subrule<0, char()> entry;

        a = (entry %= alpha)[_val = _1];
        BOOST_TEST(test("x", a[phx::ref(ch) = _1]));
        BOOST_TEST(ch == 'x');

        a %= (entry %= alpha);
        BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
        BOOST_TEST(ch == 'z');
    }

    { // auto subrules tests: allow stl containers as attributes to
      // sequences (in cases where attributes of the elements
      // are convertible to the value_type of the container or if
      // the element itself is an stl container with value_type
      // that is convertible to the value_type of the attribute).

        std::string s;
        rule<char const*, std::string()> r;
        subrule<0, std::string()> entry;

        r %= (entry %= char_ >> *(',' >> char_));
        BOOST_TEST(test("a,b,c,d,e,f", r[phx::ref(s) = _1]));
        BOOST_TEST(s == "abcdef");

        BOOST_TEST(test("abcdef", (
            entry %= char_ >> char_ >> char_ >> char_ >> char_ >> char_
        )[phx::ref(s) = _1]));
        BOOST_TEST(s == "abcdef");
    }

    { // synth attribute value-init

        std::string s;
        subrule<0, char()> sr;
        BOOST_TEST(test_attr("abcdef", +(sr = alpha[_val += _1]), s));
        BOOST_TEST(s == "abcdef");
    }

    { // auto subrules aliasing tests

        char ch;
        rule<char const*, char()> r;
        subrule<0, char()> a;
        subrule<1, char()> b;
        r %= (
            a %= b
          , b %= alpha
        );

        BOOST_TEST(test("x", r[phx::ref(ch) = _1]));
        BOOST_TEST(ch == 'x');

        BOOST_TEST(test_attr("z", r, ch)); // attribute is given.
        BOOST_TEST(ch == 'z');
    }

    { // context (w/arg) tests

        char ch;

        // entry subrule with 1 arg
        rule<char const*, char(int)> a;
        subrule<1, char(int)> sr1;
        a %= (
            sr1 = alpha[_val = _1 + _r1]
        )(_r1);
        BOOST_TEST(test("x", a(phx::val(1))[phx::ref(ch) = _1]));
        BOOST_TEST(ch == 'x' + 1);

        // other subrule with 1 arg
        subrule<0, char()> sr0;
        a %= (
            sr0 %= sr1(1)
          , sr1 = alpha[_val = _1 + _r1]
        );

        // allow scalars as subrule args too
        rule<char const*, char()> b;
        b %= (
            sr1 = alpha[_val = _1 + _r1]
        )(1);
        BOOST_TEST(test_attr("b", b, ch));
        BOOST_TEST(ch == 'b' + 1);

        // entry subrule with 2 args
        subrule<2, char(int, int)> sr2;
        BOOST_TEST(test_attr("a", (
            sr2 = alpha[_val = _1 + _r1 + _r2]
        )(1, 2), ch));
        BOOST_TEST(ch == 'a' + 1 + 2);

        // multiple subrules + args
        BOOST_TEST(test_attr("ba", (
            sr2 = alpha[_val = _1 + _r1 + _r2] >> sr1(3)[_val -= _1]
          , sr1 = alpha[_val = _1 + _r1]
        )(1, 2), ch));
        BOOST_TEST(ch == ('b' + 1 + 2) - ('a' + 3));
    }

    { // context (w/ reference arg) tests

        char ch;
        subrule<0, void(char&)> sr; // 1 arg (reference) - direct
        BOOST_TEST(test("x", (sr = alpha[_r1 = _1])(phx::ref(ch))));
        BOOST_TEST(ch == 'x');

        rule<char const*, void(char&)> a; // forwarded via a rule
        a = (sr = alpha[_r1 = _1])(_r1);
        BOOST_TEST(test("y", a(phx::ref(ch))));
        BOOST_TEST(ch == 'y');
    }

    { // context (w/locals) tests

        rule<char const*> r;
        subrule<0, locals<char> > a; // 1 local
        r = (
            a = alpha[_a = _1] >> char_(_a)
        );
        BOOST_TEST(test("aa", r));
        BOOST_TEST(!test("ax", r));
    }

    { // context (w/args and locals) tests

        rule<char const*, void(int)> a;
        subrule<0, void(int), locals<char> > sr; // 1 arg + 1 local
        a = (
            sr = alpha[_a = _1 + _r1] >> char_(_a)
        )(_r1);
        BOOST_TEST(test("ab", a(phx::val(1))));
        BOOST_TEST(test("xy", a(phx::val(1))));
        BOOST_TEST(!test("ax", a(phx::val(1))));
    }

    { // void() has unused type (void == unused_type)

        std::pair<int, char> attr;
        subrule<0, void()> sr;
        BOOST_TEST(test_attr("123ax", int_ >> char_ >> (sr = char_), attr));
        BOOST_TEST(attr.first == 123);
        BOOST_TEST(attr.second == 'a');
    }

    { // test that injected attributes are ok

        rule<char const*> r;
        subrule<0, char(int)> sr;

        r = (
            sr = char_(_r1)[_val = _1]
        )(42);
    }

    { // show that sra = srb and sra %= srb works as expected
        subrule<0, int()> sra;
        subrule<1, int()> srb;
        int attr;

        BOOST_TEST(test_attr("123", (sra %= int_), attr));
        BOOST_TEST(attr == 123);

        BOOST_TEST(test_attr("123", (srb %= sra, sra %= int_), attr));
        BOOST_TEST(attr == 123);

        BOOST_TEST(test_attr("123", (srb = sra, sra %= int_), attr));
        BOOST_TEST(attr == 123);
    }

    { // std::string as container attribute with auto subrules

        subrule<0, std::string()> text;
        std::string attr;
        BOOST_TEST(test_attr("x", (
            text %= +(!char_(')') >> !char_('>') >> char_)
        ), attr));
        BOOST_TEST(attr == "x");
    }

//    { // error handling
//
//        using namespace boost::spirit::ascii;
//        using boost::phoenix::construct;
//        using boost::phoenix::bind;
//
//        rule<char const*> r;
//        r = '(' > int_ > ',' > int_ > ')';
//
//        on_error<fail>
//        (
//            r, std::cout
//                << phx::val("Error! Expecting: ")
//                << _4
//                << phx::val(", got: \"")
//                << construct<std::string>(_3, _2)
//                << phx::val("\"")
//                << std::endl
//        );
//
//        BOOST_TEST(test("(123,456)", r));
//        BOOST_TEST(!test("(abc,def)", r));
//        BOOST_TEST(!test("(123,456]", r));
//        BOOST_TEST(!test("(123;456)", r));
//        BOOST_TEST(!test("[123,456]", r));
//    }

    return boost::report_errors();
}