• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2013 Carl Barron
3     Copyright (c) 2015 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 #define BOOST_SPIRIT_X3_DEBUG
10 #include <boost/detail/lightweight_test.hpp>
11 #include <boost/spirit/home/x3.hpp>
12 #include <boost/spirit/home/support/char_encoding/unicode.hpp>
13 #include <boost/fusion/include/at.hpp>
14 #include <boost/fusion/include/vector.hpp>
15 #include <boost/fusion/include/adapt_struct.hpp>
16 #include <boost/mpl/int.hpp>
17 #include <boost/optional.hpp>
18 
19 #include <iostream>
20 #include <numeric>
21 #include <vector>
22 #include "test.hpp"
23 
24 struct roman
25 {
26     boost::optional<int> a;
27     boost::optional<int> b;
28     boost::optional<int> c;
29 };
30 
BOOST_FUSION_ADAPT_STRUCT(roman,a,b,c)31 BOOST_FUSION_ADAPT_STRUCT(roman,
32     a, b, c
33 )
34 
35 int eval(roman const & c)
36 {
37     return c.a.get_value_or(0) + c.b.get_value_or(0) + c.c.get_value_or(0);
38 }
39 
40 int
main()41 main()
42 {
43     using spirit_test::test;
44     using spirit_test::test_attr;
45     using boost::spirit::x3::symbols;
46 
47     { // construction from initializer-list
48         symbols<int> const ones =
49         {
50             {"I", 1}, {"II", 2}, {"III", 3}, {"IV", 4},
51             {"V", 5}, {"VI", 6}, {"VII", 7}, {"VIII", 8},
52             {"IX", 9}
53         };
54         symbols<int> const tens =
55         {
56             {"X", 10}, {"XX", 20}, {"XXX", 30}, {"XL", 40},
57             {"L", 50}, {"LX", 60}, {"LXX", 70}, {"LXXX", 80},
58             {"XC", 90}
59         };
60         symbols<int> const hundreds
61         {
62             {"C", 100}, {"CC", 200}, {"CCC", 300}, {"CD", 400},
63             {"D", 500}, {"DC", 600}, {"DCC", 700}, {"DCCC", 800},
64             {"CM", 900}
65         };
66 
67         auto number = -hundreds >> -tens >> -ones;
68 
69         roman r;
70         BOOST_TEST((test_attr("CDXLII", number, r)));
71         BOOST_TEST(eval(r) == 442);
72     }
73 
74     { // construction from initializer-list without attribute
75         symbols<> foo = {"a1", "a2", "a3"};
76 
77         BOOST_TEST((test("a3", foo)));
78     }
79 
80     { // assignment from initializer-list
81         symbols<> foo;
82         foo = {"a1", "a2", "a3"};
83 
84         BOOST_TEST((test("a3", foo)));
85     }
86 
87     { // unicode | construction from initializer-list
88         using namespace boost::spirit;
89         x3::symbols_parser<char_encoding::unicode, int> foo = {{U"a1", 1}, {U"a2", 2}, {U"a3", 3}};
90 
91         int r;
92         BOOST_TEST((test_attr(U"a3", foo, r)));
93         BOOST_TEST(r == 3);
94     }
95 
96     return boost::report_errors();
97 }
98