• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 using boost::spirit::x3::rule;
16 
17 rule<class direct_rule, int> direct_rule = "direct_rule";
18 rule<class indirect_rule, int> indirect_rule = "indirect_rule";
19 
20 auto const direct_rule_def = boost::spirit::x3::int_;
21 auto const indirect_rule_def = direct_rule;
22 
BOOST_SPIRIT_DEFINE(direct_rule,indirect_rule)23 BOOST_SPIRIT_DEFINE(direct_rule, indirect_rule)
24 
25 int main()
26 {
27     using spirit_test::test;
28     using spirit_test::test_attr;
29     using namespace boost::spirit::x3::ascii;
30     using boost::spirit::x3::raw;
31     using boost::spirit::x3::eps;
32     using boost::spirit::x3::lit;
33     using boost::spirit::x3::_attr;
34     using boost::spirit::x3::parse;
35     using boost::spirit::x3::int_;
36     using boost::spirit::x3::char_;
37 
38     BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(raw['x']);
39 
40     {
41         boost::iterator_range<char const*> range;
42         std::string str;
43         BOOST_TEST((test_attr("spirit_test_123", raw[alpha >> *(alnum | '_')], range)));
44         BOOST_TEST((std::string(range.begin(), range.end()) == "spirit_test_123"));
45         BOOST_TEST((test_attr("  spirit", raw[*alpha], range, space)));
46         BOOST_TEST((range.size() == 6));
47     }
48 
49     {
50         std::string str;
51         BOOST_TEST((test_attr("spirit_test_123", raw[alpha >> *(alnum | '_')], str)));
52         BOOST_TEST((str == "spirit_test_123"));
53     }
54 
55     {
56         boost::iterator_range<char const*> range;
57         BOOST_TEST((test("x", raw[alpha])));
58         BOOST_TEST((test_attr("x", raw[alpha], range)));
59         BOOST_TEST((test_attr("x", raw[alpha] >> eps, range)));
60     }
61 
62     {
63         boost::iterator_range<char const*> range;
64         BOOST_TEST((test("x", raw[alpha][ ([&](auto& ctx){ range = _attr(ctx); }) ])));
65         BOOST_TEST(range.size() == 1 && *range.begin() == 'x');
66     }
67 
68     {
69         boost::iterator_range<char const*> range;
70         BOOST_TEST((test("x123x", lit('x') >> raw[+digit] >> lit('x'))));
71         BOOST_TEST((test_attr("x123x", lit('x') >> raw[+digit] >> lit('x'), range)));
72         BOOST_TEST((std::string(range.begin(), range.end()) == "123"));
73     }
74 
75     {
76         using range = boost::iterator_range<std::string::iterator>;
77         boost::variant<int, range> attr;
78 
79         std::string str("test");
80         parse(str.begin(), str.end(),  (int_ | raw[*char_]), attr);
81 
82         auto rng = boost::get<range>(attr);
83         BOOST_TEST(std::string(rng.begin(), rng.end()) == "test");
84     }
85 
86     {
87         std::vector<boost::iterator_range<std::string::iterator>> attr;
88         std::string str("123abcd");
89         parse(str.begin(), str.end()
90           , (raw[int_] >> raw[*char_])
91           , attr
92         );
93         BOOST_TEST(attr.size() == 2);
94         BOOST_TEST(std::string(attr[0].begin(), attr[0].end()) == "123");
95         BOOST_TEST(std::string(attr[1].begin(), attr[1].end()) == "abcd");
96     }
97 
98     {
99         std::pair<int, boost::iterator_range<std::string::iterator>> attr;
100         std::string str("123abcd");
101         parse(str.begin(), str.end()
102           , (int_ >> raw[*char_])
103           , attr
104         );
105         BOOST_TEST(attr.first == 123);
106         BOOST_TEST(std::string(attr.second.begin(), attr.second.end()) == "abcd");
107     }
108 
109     {
110         // test with simple rule
111         boost::iterator_range<char const*> range;
112         BOOST_TEST((test_attr("123", raw[direct_rule], range)));
113         BOOST_TEST((std::string(range.begin(), range.end()) == "123"));
114     }
115 
116     {
117         // test with complex rule
118         boost::iterator_range<char const*> range;
119         BOOST_TEST((test_attr("123", raw[indirect_rule], range)));
120         BOOST_TEST((std::string(range.begin(), range.end()) == "123"));
121     }
122 
123     return boost::report_errors();
124 }
125