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     {
29         boost::iterator_range<char const*> range;
30         std::string str;
31         BOOST_TEST((test_attr("spirit_test_123", raw[alpha >> *(alnum | '_')], range)));
32         BOOST_TEST((std::string(range.begin(), range.end()) == "spirit_test_123"));
33         BOOST_TEST((test_attr("  spirit", raw[*alpha], range, space)));
34         BOOST_TEST((range.size() == 6));
35     }
36 
37     {
38         std::string str;
39         BOOST_TEST((test_attr("spirit_test_123", raw[alpha >> *(alnum | '_')], str)));
40         BOOST_TEST((str == "spirit_test_123"));
41     }
42 
43     {
44         boost::iterator_range<char const*> range;
45         BOOST_TEST((test("x", raw[alpha])));
46         BOOST_TEST((test_attr("x", raw[alpha], range)));
47     }
48 
49     {
50         boost::iterator_range<char const*> range;
51         BOOST_TEST((test("x", raw[alpha][ ([&](auto& ctx){ range = _attr(ctx); }) ])));
52         BOOST_TEST(range.size() == 1 && *range.begin() == 'x');
53     }
54 
55     {
56         boost::iterator_range<char const*> range;
57         BOOST_TEST((test("x123x", lit('x') >> raw[+digit] >> lit('x'))));
58         BOOST_TEST((test_attr("x123x", lit('x') >> raw[+digit] >> lit('x'), range)));
59         BOOST_TEST((std::string(range.begin(), range.end()) == "123"));
60     }
61 
62     {
63         using range = boost::iterator_range<std::string::iterator>;
64         boost::variant<int, range> attr;
65 
66         std::string str("test");
67         parse(str.begin(), str.end(),  (int_ | raw[*char_]), attr);
68 
69         auto rng = boost::get<range>(attr);
70         BOOST_TEST(std::string(rng.begin(), rng.end()) == "test");
71     }
72 
73     {
74         std::vector<boost::iterator_range<std::string::iterator>> attr;
75         std::string str("123abcd");
76         parse(str.begin(), str.end()
77           , (raw[int_] >> raw[*char_])
78           , attr
79         );
80         BOOST_TEST(attr.size() == 2);
81         BOOST_TEST(std::string(attr[0].begin(), attr[0].end()) == "123");
82         BOOST_TEST(std::string(attr[1].begin(), attr[1].end()) == "abcd");
83     }
84 
85     {
86         std::pair<int, boost::iterator_range<std::string::iterator>> attr;
87         std::string str("123abcd");
88         parse(str.begin(), str.end()
89           , (int_ >> raw[*char_])
90           , attr
91         );
92         BOOST_TEST(attr.first == 123);
93         BOOST_TEST(std::string(attr.second.begin(), attr.second.end()) == "abcd");
94     }
95 
96     return boost::report_errors();
97 }
98