Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / spirit / test / x3 / with.cpp
1 /*=============================================================================
2     Copyright (c) 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 "test.hpp"
10
11 namespace x3 = boost::spirit::x3;
12
13 struct my_tag;
14
15 struct my_rule_class
16 {
17     template <typename Iterator, typename Exception, typename Context>
18     x3::error_handler_result
19     on_error(Iterator&, Iterator const&, Exception const& x, Context const& context)
20     {
21         x3::get<my_tag>(context)++;
22         return x3::error_handler_result::fail;
23     }
24     
25     template <typename Iterator, typename Attribute, typename Context>
26     inline void
27     on_success(Iterator const&, Iterator const&, Attribute&, Context const& context)
28     {
29         x3::get<my_tag>(context)++;
30     }
31 };
32
33 int
34 main()
35 {
36     using spirit_test::test_attr;
37     using spirit_test::test;
38
39     using boost::spirit::x3::rule;
40     using boost::spirit::x3::int_;
41     using boost::spirit::x3::with;
42
43     { // injecting data into the context in the grammar
44
45         int val = 0;
46         auto r = rule<my_rule_class, char const*>() =
47             '(' > int_ > ',' > int_ > ')'
48             ;
49         
50         auto start =
51             with<my_tag>(std::ref(val)) [ r ]
52             ;
53
54         BOOST_TEST(test("(123,456)", start));
55         BOOST_TEST(!test("(abc,def)", start));
56         BOOST_TEST(val == 2);
57     }
58
59     return boost::report_errors();
60 }