1 /*=============================================================================
2 Copyright (c) 2001-2014 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/std_pair.hpp>
10
11 #include <iostream>
12 #include <string>
13 #include "test.hpp"
14
main()15 int main()
16 {
17 using spirit_test::test;
18 using spirit_test::test_attr;
19 using namespace boost::spirit::x3::ascii;
20 using boost::spirit::x3::raw;
21 using boost::spirit::x3::eps;
22 using boost::spirit::x3::lit;
23 using boost::spirit::x3::_attr;
24 using boost::spirit::x3::parse;
25 using boost::spirit::x3::int_;
26 using boost::spirit::x3::char_;
27
28 BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(raw['x']);
29
30 {
31 boost::iterator_range<char const*> range;
32 std::string str;
33 BOOST_TEST((test_attr("spirit_test_123", raw[alpha >> *(alnum | '_')], range)));
34 BOOST_TEST((std::string(range.begin(), range.end()) == "spirit_test_123"));
35 BOOST_TEST((test_attr(" spirit", raw[*alpha], range, space)));
36 BOOST_TEST((range.size() == 6));
37 }
38
39 {
40 std::string str;
41 BOOST_TEST((test_attr("spirit_test_123", raw[alpha >> *(alnum | '_')], str)));
42 BOOST_TEST((str == "spirit_test_123"));
43 }
44
45 {
46 boost::iterator_range<char const*> range;
47 BOOST_TEST((test("x", raw[alpha])));
48 BOOST_TEST((test_attr("x", raw[alpha], range)));
49 }
50
51 {
52 boost::iterator_range<char const*> range;
53 BOOST_TEST((test("x", raw[alpha][ ([&](auto& ctx){ range = _attr(ctx); }) ])));
54 BOOST_TEST(range.size() == 1 && *range.begin() == 'x');
55 }
56
57 {
58 boost::iterator_range<char const*> range;
59 BOOST_TEST((test("x123x", lit('x') >> raw[+digit] >> lit('x'))));
60 BOOST_TEST((test_attr("x123x", lit('x') >> raw[+digit] >> lit('x'), range)));
61 BOOST_TEST((std::string(range.begin(), range.end()) == "123"));
62 }
63
64 {
65 using range = boost::iterator_range<std::string::iterator>;
66 boost::variant<int, range> attr;
67
68 std::string str("test");
69 parse(str.begin(), str.end(), (int_ | raw[*char_]), attr);
70
71 auto rng = boost::get<range>(attr);
72 BOOST_TEST(std::string(rng.begin(), rng.end()) == "test");
73 }
74
75 {
76 std::vector<boost::iterator_range<std::string::iterator>> attr;
77 std::string str("123abcd");
78 parse(str.begin(), str.end()
79 , (raw[int_] >> raw[*char_])
80 , attr
81 );
82 BOOST_TEST(attr.size() == 2);
83 BOOST_TEST(std::string(attr[0].begin(), attr[0].end()) == "123");
84 BOOST_TEST(std::string(attr[1].begin(), attr[1].end()) == "abcd");
85 }
86
87 {
88 std::pair<int, boost::iterator_range<std::string::iterator>> attr;
89 std::string str("123abcd");
90 parse(str.begin(), str.end()
91 , (int_ >> raw[*char_])
92 , attr
93 );
94 BOOST_TEST(attr.first == 123);
95 BOOST_TEST(std::string(attr.second.begin(), attr.second.end()) == "abcd");
96 }
97
98 return boost::report_errors();
99 }
100