• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2009 Chris Hoeppler
3     Copyright (c) 2014 Lee Clagett
4 
5     Distributed under the Boost Software License, Version 1.0. (See accompanying
6     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8 #include <boost/detail/lightweight_test.hpp>
9 
10 #include <boost/spirit/home/x3/char.hpp>
11 #include <boost/spirit/home/x3/core.hpp>
12 #include <boost/spirit/home/x3/numeric.hpp>
13 #include <boost/spirit/home/x3/operator.hpp>
14 #include <boost/spirit/home/x3/string.hpp>
15 #include <boost/spirit/home/x3/directive/confix.hpp>
16 
17 #include "test.hpp"
18 
main()19 int main()
20 {
21     namespace x3 = boost::spirit::x3;
22     using namespace spirit_test;
23 
24     BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(x3::confix('(', ')'));
25     BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(x3::confix("[", "]"));
26     BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(x3::confix("/*", "*/"));
27 
28     {
29         const auto comment = x3::confix("/*", "*/");
30 
31         BOOST_TEST(test_failure("/abcdef*/", comment["abcdef"]));
32         BOOST_TEST(test_failure("/* abcdef*/", comment["abcdef"]));
33         BOOST_TEST(test_failure("/*abcdef */", comment["abcdef"]));
34         BOOST_TEST(test("/*abcdef*/", comment["abcdef"]));
35 
36         {
37             unsigned value = 0;
38             BOOST_TEST(
39                 test_attr(" /* 123 */ ", comment[x3::uint_], value, x3::space));
40             BOOST_TEST(value == 123);
41 
42             using x3::_attr;
43             value = 0;
44             const auto lambda = [&value](auto& ctx ){ value = _attr(ctx) + 1; };
45             BOOST_TEST(test_attr("/*123*/", comment[x3::uint_][lambda], value));
46             BOOST_TEST(value == 124);
47         }
48     }
49     {
50         const auto array = x3::confix('[', ']');
51 
52         {
53             std::vector<unsigned> values;
54 
55             BOOST_TEST(test("[0,2,4,6,8]", array[x3::uint_ % ',']));
56             BOOST_TEST(test_attr("[0,2,4,6,8]", array[x3::uint_ % ','], values));
57             BOOST_TEST(
58                 values.size() == 5 &&
59                 values[0] == 0 &&
60                 values[1] == 2 &&
61                 values[2] == 4 &&
62                 values[3] == 6 &&
63                 values[4] == 8);
64         }
65         {
66             std::vector<std::vector<unsigned>> values;
67             BOOST_TEST(
68                 test("[[1,3,5],[0,2,4]]", array[array[x3::uint_ % ','] % ',']));
69             BOOST_TEST(
70                 test_attr(
71                     "[[1,3,5],[0,2,4]]",
72                     array[array[x3::uint_ % ','] % ','],
73                     values));
74             BOOST_TEST(
75                 values.size() == 2 &&
76                 values[0].size() == 3 &&
77                 values[0][0] == 1 &&
78                 values[0][1] == 3 &&
79                 values[0][2] == 5 &&
80                 values[1].size() == 3 &&
81                 values[1][0] == 0 &&
82                 values[1][1] == 2 &&
83                 values[1][2] == 4);
84         }
85     }
86 
87     return boost::report_errors();
88 }
89