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