1 /*=============================================================================
2 Copyright (c) 2001-2015 Joel de Guzman
3
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 =============================================================================*/
7 #include <boost/detail/lightweight_test.hpp>
8 #include <boost/spirit/home/x3.hpp>
9 #include <boost/fusion/include/vector.hpp>
10 #include <boost/fusion/include/at.hpp>
11
12 #include <string>
13 #include <iostream>
14 #include "test.hpp"
15
16 using boost::spirit::x3::rule;
17
18 rule<class direct_rule, int> direct_rule = "direct_rule";
19 rule<class indirect_rule, int> indirect_rule = "indirect_rule";
20
21 auto const direct_rule_def = boost::spirit::x3::int_;
22 auto const indirect_rule_def = direct_rule;
23
BOOST_SPIRIT_DEFINE(direct_rule,indirect_rule)24 BOOST_SPIRIT_DEFINE(direct_rule, indirect_rule)
25
26 int
27 main()
28 {
29 using namespace boost::spirit::x3::ascii;
30 using boost::spirit::x3::omit;
31 using boost::spirit::x3::unused_type;
32 using boost::spirit::x3::unused;
33 using boost::spirit::x3::int_;
34
35 using boost::fusion::vector;
36 using boost::fusion::at_c;
37
38 using spirit_test::test;
39 using spirit_test::test_attr;
40
41 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(omit['x']);
42
43 {
44 BOOST_TEST(test("a", omit['a']));
45 }
46
47 {
48 // omit[] means we don't receive the attribute
49 char attr;
50 BOOST_TEST((test_attr("abc", omit[char_] >> omit['b'] >> char_, attr)));
51 BOOST_TEST((attr == 'c'));
52 }
53
54 {
55 // If all elements except 1 is omitted, the attribute is
56 // a single-element sequence. For this case alone, we allow
57 // naked attributes (unwrapped in a fusion sequence).
58 char attr;
59 BOOST_TEST((test_attr("abc", omit[char_] >> 'b' >> char_, attr)));
60 BOOST_TEST((attr == 'c'));
61 }
62
63 {
64 // omit[] means we don't receive the attribute
65 vector<> attr;
66 BOOST_TEST((test_attr("abc", omit[char_] >> omit['b'] >> omit[char_], attr)));
67 }
68
69 {
70 // omit[] means we don't receive the attribute
71 // this test is merely a compile test, because using a unused as the
72 // explicit attribute doesn't make any sense
73 unused_type attr;
74 BOOST_TEST((test_attr("abc", omit[char_ >> 'b' >> char_], attr)));
75 }
76
77 {
78 // omit[] means we don't receive the attribute, if all elements of a
79 // sequence have unused attributes, the whole sequence has an unused
80 // attribute as well
81 vector<char, char> attr;
82 BOOST_TEST((test_attr("abcde",
83 char_ >> (omit[char_] >> omit['c'] >> omit[char_]) >> char_, attr)));
84 BOOST_TEST((at_c<0>(attr) == 'a'));
85 BOOST_TEST((at_c<1>(attr) == 'e'));
86 }
87
88 {
89 // "hello" has an unused_type. unused attrubutes are not part of the sequence
90 vector<char, char> attr;
91 BOOST_TEST((test_attr("a hello c", char_ >> "hello" >> char_, attr, space)));
92 BOOST_TEST((at_c<0>(attr) == 'a'));
93 BOOST_TEST((at_c<1>(attr) == 'c'));
94 }
95
96 {
97 // if only one node in a sequence is left (all the others are omitted),
98 // then we need "naked" attributes (not wrapped in a tuple)
99 int attr;
100 BOOST_TEST((test_attr("a 123 c", omit['a'] >> int_ >> omit['c'], attr, space)));
101 BOOST_TEST((attr == 123));
102 }
103
104 {
105 // unused means we don't care about the attribute
106 BOOST_TEST((test_attr("abc", char_ >> 'b' >> char_, unused)));
107 }
108
109 { // test action with omitted attribute
110 char c = 0;
111 auto f = [&](auto& ctx){ c = _attr(ctx); };
112
113 BOOST_TEST(test("x123\"a string\"", (char_ >> omit[int_] >> "\"a string\"")[f]));
114 BOOST_TEST(c == 'x');
115 }
116
117 { // test action with omitted attribute
118 int n = 0;
119 auto f = [&](auto& ctx){ n = _attr(ctx); };
120
121 BOOST_TEST(test("x 123 \"a string\"", (omit[char_] >> int_ >> "\"a string\"")[f], space));
122 BOOST_TEST(n == 123);
123 }
124
125 {
126 // test with simple rule
127 BOOST_TEST((test_attr("123", omit[direct_rule], unused)));
128 }
129
130 {
131 // test with complex rule
132 BOOST_TEST((test_attr("123", omit[indirect_rule], unused)));
133 }
134
135 return boost::report_errors();
136 }
137