summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/spirit/test/qi/grammar.cpp
blob: 8a7ca14c50587c771cedf1f8fb9c02fdac056dc3 (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
/*=============================================================================
    Copyright (c) 2001-2010 Joel de Guzman

    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/detail/lightweight_test.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_nonterminal.hpp>
#include <boost/spirit/include/qi_action.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>

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

using spirit_test::test;
using spirit_test::test_attr;

using boost::spirit::ascii::space_type;
using boost::spirit::ascii::space;
using boost::spirit::int_;
using boost::spirit::qi::grammar;
using boost::spirit::qi::rule;
using boost::spirit::_val;
using boost::spirit::_r1;
using boost::spirit::lit;

struct num_list : grammar<char const*, space_type>
{
    num_list() : base_type(start)
    {
        using boost::spirit::int_;
        num = int_;
        start = num >> *(',' >> num);
    }

    rule<char const*, space_type> start, num;
};

struct inh_g : grammar<char const*, int(int), space_type>
{
    inh_g() : base_type(start)
    {
        start = lit("inherited")[_val = _r1];
    }

    rule<char const*, int(int), space_type> start, num;
};

struct my_skipper : grammar<char const*>
{
    my_skipper() : base_type(start)
    {
        start = space;
    }

    rule<char const*> start, num;
};

struct num_list2 : grammar<char const*, my_skipper>
{
    num_list2() : base_type(start)
    {
        using boost::spirit::int_;
        num = int_;
        start = num >> *(',' >> num);
    }

    rule<char const*, my_skipper> start, num;
};

template <typename Iterator, typename Skipper>
struct num_list3 : grammar<Iterator, Skipper>
{
    template <typename Class>
    num_list3(Class&) : grammar<Iterator, Skipper>(start)
    {
        using boost::spirit::int_;
        num = int_;
        start = num >> *(',' >> num);
    }

    rule<Iterator, Skipper> start, num;
};

int
main()
{
    { // simple grammar test

        num_list nlist;
        BOOST_TEST(test("123, 456, 789", nlist, space));
    }

    { // simple grammar test with user-skipper

        num_list2 nlist;
        my_skipper skip;
        BOOST_TEST(test("123, 456, 789", nlist, skip));
    }

    { // direct access to the rules

        num_list g;
        BOOST_TEST(test("123", g.num, space));
        BOOST_TEST(test("123, 456, 789", g.start, space));
    }

    { // grammar with inherited attributes

        inh_g g;
        int n = -1;
        BOOST_TEST(test_attr("inherited", g.start(123), n, space)); // direct to the rule
        BOOST_TEST(n == 123);
        BOOST_TEST(test_attr("inherited", g(123), n, space)); // using the grammar
        BOOST_TEST(n == 123);
    }

    return boost::report_errors();
}