• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 int
main()17 main()
18 {
19     using namespace boost::spirit::x3::ascii;
20     using boost::spirit::x3::omit;
21     using boost::spirit::x3::unused_type;
22     using boost::spirit::x3::unused;
23     using boost::spirit::x3::int_;
24 
25     using boost::fusion::vector;
26     using boost::fusion::at_c;
27 
28     using spirit_test::test;
29     using spirit_test::test_attr;
30 
31     BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(omit['x']);
32 
33     {
34         BOOST_TEST(test("a", omit['a']));
35     }
36 
37     {
38         // omit[] means we don't receive the attribute
39         char attr;
40         BOOST_TEST((test_attr("abc", omit[char_] >> omit['b'] >> char_, attr)));
41         BOOST_TEST((attr == 'c'));
42     }
43 
44     {
45         // If all elements except 1 is omitted, the attribute is
46         // a single-element sequence. For this case alone, we allow
47         // naked attributes (unwrapped in a fusion sequence).
48         char attr;
49         BOOST_TEST((test_attr("abc", omit[char_] >> 'b' >> char_, attr)));
50         BOOST_TEST((attr == 'c'));
51     }
52 
53     {
54         // omit[] means we don't receive the attribute
55         vector<> attr;
56         BOOST_TEST((test_attr("abc", omit[char_] >> omit['b'] >> omit[char_], attr)));
57     }
58 
59     {
60         // omit[] means we don't receive the attribute
61         // this test is merely a compile test, because using a unused as the
62         // explicit attribute doesn't make any sense
63         unused_type attr;
64         BOOST_TEST((test_attr("abc", omit[char_ >> 'b' >> char_], attr)));
65     }
66 
67     {
68         // omit[] means we don't receive the attribute, if all elements of a
69         // sequence have unused attributes, the whole sequence has an unused
70         // attribute as well
71         vector<char, char> attr;
72         BOOST_TEST((test_attr("abcde",
73             char_ >> (omit[char_] >> omit['c'] >> omit[char_]) >> char_, attr)));
74         BOOST_TEST((at_c<0>(attr) == 'a'));
75         BOOST_TEST((at_c<1>(attr) == 'e'));
76     }
77 
78     {
79         // "hello" has an unused_type. unused attrubutes are not part of the sequence
80         vector<char, char> attr;
81         BOOST_TEST((test_attr("a hello c", char_ >> "hello" >> char_, attr, space)));
82         BOOST_TEST((at_c<0>(attr) == 'a'));
83         BOOST_TEST((at_c<1>(attr) == 'c'));
84     }
85 
86     {
87         // if only one node in a sequence is left (all the others are omitted),
88         // then we need "naked" attributes (not wrapped in a tuple)
89         int attr;
90         BOOST_TEST((test_attr("a 123 c", omit['a'] >> int_ >> omit['c'], attr, space)));
91         BOOST_TEST((attr == 123));
92     }
93 
94     {
95         // unused means we don't care about the attribute
96         BOOST_TEST((test_attr("abc", char_ >> 'b' >> char_, unused)));
97     }
98 
99     {   // test action with omitted attribute
100         char c = 0;
101         auto f = [&](auto& ctx){ c = _attr(ctx); };
102 
103         BOOST_TEST(test("x123\"a string\"", (char_ >> omit[int_] >> "\"a string\"")[f]));
104         BOOST_TEST(c == 'x');
105     }
106 
107     {   // test action with omitted attribute
108         int n = 0;
109         auto f = [&](auto& ctx){ n = _attr(ctx); };
110 
111         BOOST_TEST(test("x 123 \"a string\"", (omit[char_] >> int_ >> "\"a string\"")[f], space));
112         BOOST_TEST(n == 123);
113     }
114 
115     return boost::report_errors();
116 }
117