• 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 <string>
8 #include <vector>
9 
10 #include <boost/detail/lightweight_test.hpp>
11 #include <boost/spirit/home/x3.hpp>
12 #include <boost/fusion/include/vector.hpp>
13 
14 #include <string>
15 #include <iostream>
16 #include "test.hpp"
17 #include "utils.hpp"
18 
19 struct x_attr
20 {
21 };
22 
23 namespace boost { namespace spirit { namespace x3 { namespace traits
24 {
25     template <>
26     struct container_value<x_attr>
27     {
28         typedef char type; // value type of container
29     };
30 
31     template <>
32     struct push_back_container<x_attr>
33     {
callboost::spirit::x3::traits::push_back_container34         static bool call(x_attr& /*c*/, char /*val*/)
35         {
36             // push back value type into container
37             return true;
38         }
39     };
40 }}}}
41 
42 int
main()43 main()
44 {
45     using spirit_test::test;
46     using spirit_test::test_attr;
47     using boost::spirit::x3::char_;
48     using boost::spirit::x3::alpha;
49     using boost::spirit::x3::upper;
50     using boost::spirit::x3::space;
51     using boost::spirit::x3::digit;
52     //~ using boost::spirit::x3::no_case;
53     using boost::spirit::x3::int_;
54     using boost::spirit::x3::omit;
55     using boost::spirit::x3::lit;
56     //~ using boost::spirit::x3::_1;
57     using boost::spirit::x3::lexeme;
58 
59     BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(+char_);
60 
61     {
62         BOOST_TEST(test("aaaaaaaa", +char_));
63         BOOST_TEST(test("a", +char_));
64         BOOST_TEST(!test("", +char_));
65         BOOST_TEST(test("aaaaaaaa", +alpha));
66         BOOST_TEST(!test("aaaaaaaa", +upper));
67     }
68 
69     {
70         BOOST_TEST(test(" a a aaa aa", +char_, space));
71         BOOST_TEST(test("12345 678 9 ", +digit, space));
72     }
73 
74     //~ {
75         //~ BOOST_TEST(test("aBcdeFGH", no_case[+char_]));
76         //~ BOOST_TEST(test("a B cde FGH  ", no_case[+char_], space));
77     //~ }
78 
79     {
80         std::vector<int> v;
81         BOOST_TEST(test_attr("123 456 789 10", +int_, v, space) && 4 == v.size() &&
82             v[0] == 123 && v[1] == 456 && v[2] == 789 &&  v[3] == 10);
83     }
84 
85     {
86         std::vector<std::string> v;
87         BOOST_TEST(test_attr("a b c d", +lexeme[+alpha], v, space) && 4 == v.size() &&
88             v[0] == "a" && v[1] == "b" && v[2] == "c" &&  v[3] == "d");
89     }
90 
91     {
92         BOOST_TEST(test("Kim Kim Kim", +lit("Kim"), space));
93     }
94 
95     // $$$ Fixme $$$
96     /*{
97         // The following 2 tests show that omit does not inhibit explicit attributes
98 
99         std::string s;
100         BOOST_TEST(test_attr("bbbb", omit[+char_('b')], s) && s == "bbbb");
101 
102         s.clear();
103         BOOST_TEST(test_attr("b b b b ", omit[+char_('b')], s, space) && s == "bbbb");
104     }*/
105 
106     { // actions
107         std::string v;
108         auto f = [&](auto& ctx){ v = _attr(ctx); };
109 
110         BOOST_TEST(test("bbbb", (+char_)[f]) && 4 == v.size() &&
111             v[0] == 'b' && v[1] == 'b' && v[2] == 'b' &&  v[3] == 'b');
112     }
113 
114     { // more actions
115         std::vector<int> v;
116         auto f = [&](auto& ctx){ v = _attr(ctx); };
117 
118         BOOST_TEST(test("1 2 3", (+int_)[f], space) && 3 == v.size() &&
119             v[0] == 1 && v[1] == 2 && v[2] == 3);
120     }
121 
122     { // attribute customization
123 
124         x_attr x;
125         test_attr("abcde", +char_, x);
126     }
127 
128     // single-element fusion vector tests
129     {
130         boost::fusion::vector<std::string> fs;
131         BOOST_TEST((test_attr("12345", +char_, fs))); // ok
132         BOOST_TEST(boost::fusion::at_c<0>(fs) == "12345");
133     }
134 
135     { // test move only types
136         std::vector<move_only> v;
137         BOOST_TEST(test_attr("sss", +synth_move_only, v));
138         BOOST_TEST_EQ(v.size(), 3);
139     }
140 
141     return boost::report_errors();
142 }
143