• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2001-2011 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 
8 #if defined(_MSC_VER)
9 # pragma warning(disable: 4180)     // qualifier applied to function type
10                                     // has no meaning; ignored
11 #endif
12 
13 #include <boost/detail/lightweight_test.hpp>
14 #include <boost/detail/workaround.hpp>
15 #include <boost/spirit/include/qi_operator.hpp>
16 #include <boost/spirit/include/qi_numeric.hpp>
17 #include <boost/spirit/include/qi_char.hpp>
18 #include <boost/spirit/include/qi_parse.hpp>
19 #include <boost/spirit/include/qi_action.hpp>
20 #include <boost/lambda/lambda.hpp>
21 #include <boost/bind/bind.hpp>
22 #include <cstring>
23 
24 int x = 0;
25 
fun1(int const & i)26 void fun1(int const& i)
27 {
28     x += i;
29 }
30 
fun2(int i)31 void fun2(int i)
32 {
33     x += i;
34 }
35 using boost::spirit::unused_type;
36 
37 struct fun_action
38 {
operator ()fun_action39     void operator()(int const& i, unused_type, unused_type) const
40     {
41         x += i;
42     }
43 };
44 
fail(int,boost::spirit::unused_type,bool & pass)45 void fail (int, boost::spirit::unused_type, bool& pass)
46 {
47     pass = false;
48 }
49 
50 struct setnext
51 {
setnextsetnext52     setnext(char& next) : next(next) {}
53 
operator ()setnext54     void operator()(char c, unused_type, unused_type) const
55     {
56         next = c;
57     }
58 
59     char& next;
60 };
61 
main()62 int main()
63 {
64     namespace qi = boost::spirit::qi;
65     using boost::spirit::int_;
66 
67     {
68         char const *s1 = "{42}", *e1 = s1 + std::strlen(s1);
69         qi::parse(s1, e1, '{' >> int_[&fun1] >> '}');
70     }
71 
72     {
73         char const *s1 = "{42}", *e1 = s1 + std::strlen(s1);
74         qi::parse(s1, e1, '{' >> int_[&fun2] >> '}');
75     }
76 
77 #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1400)
78     {
79         char const *s1 = "{42}", *e1 = s1 + std::strlen(s1);
80         qi::parse(s1, e1, '{' >> int_[fun2] >> '}');
81     }
82 #else
83     x += 42;        // compensate for missing test case
84 #endif
85 
86     {
87         char const *s1 = "{42}", *e1 = s1 + std::strlen(s1);
88         qi::parse(s1, e1, '{' >> int_[fun_action()] >> '}');
89     }
90 
91     {
92         using boost::placeholders::_1;
93         char const *s1 = "{42}", *e1 = s1 + std::strlen(s1);
94         qi::parse(s1, e1, '{' >> int_[boost::bind(&fun1, _1)] >> '}');
95     }
96 
97     {
98         namespace lambda = boost::lambda;
99         char const *s1 = "{42}", *e1 = s1 + std::strlen(s1);
100         qi::parse(s1, e1, '{' >> int_[lambda::var(x) += lambda::_1] >> '}');
101     }
102     BOOST_TEST(x == (42*6));
103 
104     {
105        std::string input("1234 6543");
106        char next = '\0';
107        BOOST_TEST(qi::phrase_parse(input.begin(), input.end(),
108           qi::int_[fail] | qi::digit[setnext(next)] , qi::space));
109        BOOST_TEST(next == '1');
110     }
111 
112     return boost::report_errors();
113 }
114 
115 
116 
117 
118