• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2001-2010 Hartmut Kaiser
3     Copyright (c) 2001-2010 Joel de Guzman
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 "match_manip.hpp"
10 
11 int
main()12 main()
13 {
14     using boost::spirit::qi::_1;
15     using boost::spirit::qi::_2;
16     using boost::spirit::qi::match;
17     using boost::spirit::qi::phrase_match;
18     using boost::spirit::qi::typed_stream;
19     using boost::spirit::qi::stream;
20     using boost::spirit::qi::int_;
21 
22     using namespace boost::spirit::ascii;
23     namespace fusion = boost::fusion;
24     namespace phx = boost::phoenix;
25 
26     {
27         char a = '\0';
28         int i = 0;
29         BOOST_TEST(test( "a2",
30             (char_ >> int_)[(phx::ref(a) = _1, phx::ref(i) = _2)]
31         ) && a == 'a' && i == 2);
32 
33         fusion::vector<char, int> t;
34         BOOST_TEST(test( "a2",
35             match(char_ >> int_, t)
36         ) && fusion::at_c<0>(t) == 'a' && fusion::at_c<1>(t) == 2);
37 
38         t = fusion::vector<char, int>();
39         BOOST_TEST(test( " a 2",
40             phrase_match(char_ >> int_, space, t)
41         ) && fusion::at_c<0>(t) == 'a' && fusion::at_c<1>(t) == 2);
42 
43         BOOST_TEST(!test( "a2",
44             match(char_ >> alpha, t)
45         ));
46         BOOST_TEST(!test( " a 2",
47             phrase_match(char_ >> alpha, space, t)
48         ));
49     }
50 
51     {
52         // parse elements of a vector
53         std::vector<char> v;
54         BOOST_TEST(test( "abc",
55             (*char_)[phx::ref(v) = _1]
56         ) && 3 == v.size() && v[0] == 'a' && v[1] == 'b' && v[2] == 'c');
57 
58         v.clear();
59         BOOST_TEST(test( "abc",
60             match(*char_, v)
61         ) && 3 == v.size() && v[0] == 'a' && v[1] == 'b' && v[2] == 'c');
62 
63         v.clear();
64         BOOST_TEST(test( " a b c",
65             phrase_match(*char_, space, v)
66         ) && 3 == v.size() && v[0] == 'a' && v[1] == 'b' && v[2] == 'c');
67 
68         v.clear();
69         BOOST_TEST(test( "abc",
70             match(v)
71         ) && 3 == v.size() && v[0] == 'a' && v[1] == 'b' && v[2] == 'c');
72 
73         v.clear();
74         BOOST_TEST(test( " a b c",
75             phrase_match(v, space)
76         ) && 3 == v.size() && v[0] == 'a' && v[1] == 'b' && v[2] == 'c');
77 
78         // parse a comma separated list of vector elements
79         v.clear();
80         BOOST_TEST(test( "a,b,c",
81             match(char_ % ',', v)
82         ) && 3 == v.size() && v[0] == 'a' && v[1] == 'b' && v[2] == 'c');
83 
84         v.clear();
85         BOOST_TEST(test( " a , b , c",
86             phrase_match(char_ % ',', space, v)
87         ) && 3 == v.size() && v[0] == 'a' && v[1] == 'b' && v[2] == 'c');
88 
89         // output all elements of a list
90         std::list<char> l;
91         BOOST_TEST(test( "abc",
92             match(*char_, l)
93         ) && 3 == l.size() && is_list_ok(l));
94 
95         l.clear();
96         BOOST_TEST(test( " a b c",
97             phrase_match(*char_, space, l)
98         ) && 3 == l.size() && is_list_ok(l));
99 
100         l.clear();
101         BOOST_TEST(test( "abc",
102             match(l)
103         ) && 3 == l.size() && is_list_ok(l));
104 
105         l.clear();
106         BOOST_TEST(test( " a b c",
107             phrase_match(l, space)
108         ) && 3 == l.size() && is_list_ok(l));
109     }
110 
111     return boost::report_errors();
112 }
113 
114