| 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 | |
| 9 | #include <boost/spirit/home/x3/char.hpp> |
| 10 | #include <boost/spirit/home/x3/core.hpp> |
| 11 | #include <boost/spirit/home/x3/numeric.hpp> |
| 12 | #include <boost/spirit/home/x3/operator.hpp> |
| 13 | #include <boost/spirit/home/x3/string.hpp> |
| 14 | #include <boost/spirit/home/x3/directive/confix.hpp> |
| 15 | |
| 16 | #include "test.hpp" |
| 17 | |
| 18 | int main() |
| 19 | { |
| 20 | namespace x3 = boost::spirit::x3; |
| 21 | using namespace spirit_test; |
| 22 | |
| 23 | BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(x3::confix('(', ')')); |
| 24 | BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(x3::confix("[" , "]" )); |
| 25 | BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(x3::confix("/*" , "*/" )); |
| 26 | |
| 27 | { |
| 28 | const auto = x3::confix(prefix: "/*" , postfix: "*/" ); |
| 29 | |
| 30 | BOOST_TEST(test_failure("/abcdef*/" , comment["abcdef" ])); |
| 31 | BOOST_TEST(test_failure("/* abcdef*/" , comment["abcdef" ])); |
| 32 | BOOST_TEST(test_failure("/*abcdef */" , comment["abcdef" ])); |
| 33 | BOOST_TEST(test("/*abcdef*/" , comment["abcdef" ])); |
| 34 | |
| 35 | { |
| 36 | unsigned value = 0; |
| 37 | BOOST_TEST( |
| 38 | test_attr(" /* 123 */ " , comment[x3::uint_], value, x3::space)); |
| 39 | BOOST_TEST(value == 123); |
| 40 | |
| 41 | using x3::_attr; |
| 42 | value = 0; |
| 43 | const auto lambda = [&value](auto& ctx ){ value = _attr(ctx) + 1; }; |
| 44 | BOOST_TEST(test_attr("/*123*/" , comment[x3::uint_][lambda], value)); |
| 45 | BOOST_TEST(value == 124); |
| 46 | } |
| 47 | } |
| 48 | { |
| 49 | const auto array = x3::confix(prefix: '[', postfix: ']'); |
| 50 | |
| 51 | { |
| 52 | std::vector<unsigned> values; |
| 53 | |
| 54 | BOOST_TEST(test("[0,2,4,6,8]" , array[x3::uint_ % ','])); |
| 55 | BOOST_TEST(test_attr("[0,2,4,6,8]" , array[x3::uint_ % ','], values)); |
| 56 | BOOST_TEST( |
| 57 | values.size() == 5 && |
| 58 | values[0] == 0 && |
| 59 | values[1] == 2 && |
| 60 | values[2] == 4 && |
| 61 | values[3] == 6 && |
| 62 | values[4] == 8); |
| 63 | } |
| 64 | { |
| 65 | std::vector<std::vector<unsigned>> values; |
| 66 | BOOST_TEST( |
| 67 | test("[[1,3,5],[0,2,4]]" , array[array[x3::uint_ % ','] % ','])); |
| 68 | BOOST_TEST( |
| 69 | test_attr( |
| 70 | "[[1,3,5],[0,2,4]]" , |
| 71 | array[array[x3::uint_ % ','] % ','], |
| 72 | values)); |
| 73 | BOOST_TEST( |
| 74 | values.size() == 2 && |
| 75 | values[0].size() == 3 && |
| 76 | values[0][0] == 1 && |
| 77 | values[0][1] == 3 && |
| 78 | values[0][2] == 5 && |
| 79 | values[1].size() == 3 && |
| 80 | values[1][0] == 0 && |
| 81 | values[1][1] == 2 && |
| 82 | values[1][2] == 4); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return boost::report_errors(); |
| 87 | } |
| 88 | |