]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Copyright (c) 2001-2015 Joel de Guzman | |
3 | Copyright (c) 2013-2014 Agustin Berge | |
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 | ||
7c673cae FG |
9 | #include <boost/spirit/home/x3.hpp> |
10 | ||
11 | #include <string> | |
12 | #include <cstring> | |
13 | #include <iostream> | |
14 | #include "test.hpp" | |
15 | ||
16 | int | |
17 | main() | |
18 | { | |
19 | using spirit_test::test_attr; | |
20 | using spirit_test::test; | |
21 | ||
22 | using namespace boost::spirit::x3::ascii; | |
23 | using boost::spirit::x3::any_parser; | |
7c673cae FG |
24 | using boost::spirit::x3::make_context; |
25 | using boost::spirit::x3::lit; | |
26 | using boost::spirit::x3::unused_type; | |
27 | using boost::spirit::x3::phrase_parse; | |
28 | using boost::spirit::x3::skip_flag; | |
29 | using boost::spirit::x3::skipper_tag; | |
30 | using boost::spirit::x3::_attr; | |
31 | ||
32 | typedef char const* iterator_type; | |
33 | typedef decltype(make_context<skipper_tag>(space)) context_type; | |
34 | { // basic tests | |
35 | ||
36 | auto a = lit('a'); | |
37 | auto b = lit('b'); | |
38 | auto c = lit('c'); | |
39 | ||
40 | { | |
41 | any_parser<iterator_type> start = | |
42 | *(a | b | c); | |
43 | ||
44 | BOOST_TEST(test("abcabcacb", start)); | |
45 | } | |
46 | } | |
47 | ||
48 | { // basic tests w/ skipper | |
49 | ||
50 | auto a = lit('a'); | |
51 | auto b = lit('b'); | |
52 | auto c = lit('c'); | |
53 | ||
54 | { | |
55 | any_parser<iterator_type, unused_type, context_type> start = | |
56 | *(a | b | c); | |
57 | ||
58 | BOOST_TEST(test(" a b c a b c a c b ", start, space)); | |
59 | } | |
60 | } | |
61 | ||
62 | { // basic tests w/ skipper but no final post-skip | |
63 | ||
64 | any_parser<iterator_type, unused_type, context_type> a = lit('a'); | |
65 | any_parser<iterator_type, unused_type, context_type> b = lit('b'); | |
66 | any_parser<iterator_type, unused_type, context_type> c = lit('c'); | |
67 | ||
68 | { | |
69 | any_parser<iterator_type, unused_type, context_type> start = *(a | b) >> c; | |
70 | ||
71 | char const *s1 = " a b a a b b a c ... " | |
72 | , *const e1 = s1 + std::strlen(s1); | |
73 | BOOST_TEST(phrase_parse(s1, e1, start, space, skip_flag::dont_post_skip) | |
74 | && s1 == e1 - 5); | |
75 | } | |
76 | } | |
77 | ||
78 | { // context tests | |
79 | ||
80 | char ch; | |
81 | any_parser<iterator_type, char> a = alpha; | |
82 | ||
83 | // this semantic action requires both the context and attribute | |
84 | //!!auto f = [&](auto&, char attr){ ch = attr; }; | |
85 | //!!BOOST_TEST(test("x", a[f])); | |
86 | //!!BOOST_TEST(ch == 'x'); | |
87 | ||
88 | // the semantic action may have the context passed | |
89 | auto f2 = [&](auto&){ ch = 'y'; }; | |
90 | BOOST_TEST(test("x", a[f2])); | |
91 | BOOST_TEST(ch == 'y'); | |
92 | ||
93 | // the semantic action may optionally not have any arguments at all | |
94 | auto f3 = [&]{ ch = 'z'; }; | |
95 | BOOST_TEST(test("x", a[f3])); | |
96 | BOOST_TEST(ch == 'z'); | |
97 | ||
98 | BOOST_TEST(test_attr("z", a, ch)); // attribute is given. | |
99 | BOOST_TEST(ch == 'z'); | |
100 | } | |
101 | ||
102 | { // auto rules tests | |
103 | ||
104 | char ch = '\0'; | |
105 | any_parser<iterator_type, char> a = alpha; | |
106 | auto f = [&](auto& ctx){ ch = _attr(ctx); }; | |
107 | ||
108 | BOOST_TEST(test("x", a[f])); | |
109 | BOOST_TEST(ch == 'x'); | |
110 | ch = '\0'; | |
111 | BOOST_TEST(test_attr("z", a, ch)); // attribute is given. | |
112 | BOOST_TEST(ch == 'z'); | |
113 | ||
114 | ch = '\0'; | |
115 | BOOST_TEST(test("x", a[f])); | |
116 | BOOST_TEST(ch == 'x'); | |
117 | ch = '\0'; | |
118 | BOOST_TEST(test_attr("z", a, ch)); // attribute is given. | |
119 | BOOST_TEST(ch == 'z'); | |
120 | } | |
121 | ||
122 | { // auto rules tests: allow stl containers as attributes to | |
123 | // sequences (in cases where attributes of the elements | |
124 | // are convertible to the value_type of the container or if | |
125 | // the element itself is an stl container with value_type | |
126 | // that is convertible to the value_type of the attribute). | |
127 | ||
128 | std::string s; | |
129 | auto f = [&](auto& ctx){ s = _attr(ctx); }; | |
130 | ||
131 | { | |
132 | any_parser<iterator_type, std::string> r | |
133 | = char_ >> *(',' >> char_) | |
134 | ; | |
135 | ||
136 | BOOST_TEST(test("a,b,c,d,e,f", r[f])); | |
137 | BOOST_TEST(s == "abcdef"); | |
138 | } | |
139 | ||
140 | { | |
141 | any_parser<iterator_type, std::string> r | |
142 | = char_ >> *(',' >> char_); | |
143 | s.clear(); | |
144 | BOOST_TEST(test("a,b,c,d,e,f", r[f])); | |
145 | BOOST_TEST(s == "abcdef"); | |
146 | } | |
147 | ||
148 | { | |
149 | any_parser<iterator_type, std::string> r | |
150 | = char_ >> char_ >> char_ >> char_ >> char_ >> char_; | |
151 | s.clear(); | |
152 | BOOST_TEST(test("abcdef", r[f])); | |
153 | BOOST_TEST(s == "abcdef"); | |
154 | } | |
155 | } | |
156 | ||
157 | return boost::report_errors(); | |
158 | } |