Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / spirit / test / x3 / optional_ast_node.cpp
1 /*=============================================================================
2     Copyright (c) 2001-2015 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     Duzy Chan:
8     This test addresses the usage of boost::optional<foo> as an ast node.
9 =============================================================================*/
10 #include <boost/detail/lightweight_test.hpp>
11 #include <boost/spirit/home/x3.hpp>
12 #include <boost/fusion/adapted/struct.hpp>
13 #include <boost/fusion/include/vector.hpp>
14
15 #include <iostream>
16 #include "test.hpp"
17 #include "utils.hpp"
18
19 struct twoints
20 {
21     int a;
22     int b;
23 };
24
25 struct adata
26 {
27   twoints a;
28   boost::optional<twoints> b;
29   twoints c;
30 };
31
32 BOOST_FUSION_ADAPT_STRUCT(twoints, a, b)
33 BOOST_FUSION_ADAPT_STRUCT(adata, a, b, c)
34
35 int
36 main()
37 {
38     {
39       // Duzy Chan: This case addresses the usage of boost::optional<foo>
40       // as an ast node. Which should actually test the ability of
41       // boost::spirit::x3::traits::move_to to handle with optional source
42       // value.
43       boost::spirit::x3::rule<class twoints, adata> top = "top";
44       boost::spirit::x3::rule<class twoints, boost::optional<twoints>>
45         twoints = "twoints";
46      
47       using boost::spirit::x3::int_;
48       auto const top_def = twoints >> ',' >> -twoints >> ',' >> twoints;
49       auto const twoints_def = int_ >> int_;
50
51       BOOST_SPIRIT_DEFINE(top, twoints);
52
53       twoints a, b;
54       BOOST_TEST((test_attr("1 2,3 4,5 6", top, a)));
55       BOOST_TEST((a.a.a == 1 && a.a.b == 2));
56       BOOST_TEST((a.b && a.b->a == 3 && a.b->b == 4));
57       BOOST_TEST((a.c.a == 5 && a.c.b == 6));
58
59       BOOST_TEST((test_attr("1 2,,5 6", top), b));
60       BOOST_TEST((b.a.a == 1 && b.a.b == 2));
61       BOOST_TEST((!a.b));
62       BOOST_TEST((b.c.a == 5 && b.c.b == 6));
63     }
64
65     return boost::report_errors();
66 }