• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  Copyright (c) 2010 Olaf Peter
2 //  Copyright (c) 2001-2010 Hartmut Kaiser
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 
9 #include <boost/spirit/include/qi_operator.hpp>
10 #include <boost/spirit/include/qi_char.hpp>
11 #include <boost/spirit/include/qi_string.hpp>
12 #include <boost/spirit/include/qi_numeric.hpp>
13 #include <boost/spirit/include/qi_directive.hpp>
14 #include <boost/spirit/include/qi_nonterminal.hpp>
15 #include <boost/fusion/include/nview.hpp>
16 
17 namespace qi = boost::spirit::qi;
18 namespace ascii = boost::spirit::ascii;
19 namespace fusion = boost::fusion;
20 namespace mpl = boost::mpl;
21 
main()22 int main()
23 {
24     using fusion::result_of::as_nview;
25     using fusion::at_c;
26     using boost::optional;
27     using boost::variant;
28     using ascii::space_type;
29     using ascii::no_case;
30     using qi::lit;
31     using qi::double_;
32 
33     typedef fusion::vector<
34         optional<double>,    // 0 - U
35         optional<double>,    // 1 - V
36         optional<double>     // 2 - W
37     > uvw_type;
38 
39     typedef as_nview<uvw_type, 0, 1, 2>::type uvw_reordered_type;
40     typedef as_nview<uvw_type, 2, 0, 1>::type vwu_reordered_type;
41 
42     typedef char const* iterator_type;
43 
44     qi::rule<iterator_type, optional<double>(), space_type> u,v,w;
45     qi::rule<iterator_type, uvw_reordered_type(), space_type> uvw;
46     qi::rule<iterator_type, vwu_reordered_type(), space_type> vwu;
47 
48     u = no_case[ "NA" ] | ( double_ >> -lit( "U" ) );
49     v = no_case[ "NA" ] | ( double_ >> -lit( "V" ) );
50     w = no_case[ "NA" ] | ( double_ >> -lit( "W" ) );
51 
52     uvw = u > v > w;
53     vwu = v > w > u;
54 
55     uvw_type uvw_data;
56     {
57         iterator_type first = "1U 2V 3W";
58         iterator_type last  = first + std::strlen(first);
59 
60         uvw_reordered_type uvw_result( uvw_data );
61 
62         BOOST_TEST(qi::phrase_parse(first, last, uvw, ascii::space, uvw_result));
63         BOOST_TEST(fusion::at_c<0>(uvw_result) && *fusion::at_c<0>(uvw_result) == 1);
64         BOOST_TEST(fusion::at_c<1>(uvw_result) && *fusion::at_c<1>(uvw_result) == 2);
65         BOOST_TEST(fusion::at_c<2>(uvw_result) && *fusion::at_c<2>(uvw_result) == 3);
66     }
67 
68     {
69         iterator_type first = "2V 3W 1U";
70         iterator_type last  = first + std::strlen(first);
71 
72         vwu_reordered_type uvw_result(uvw_data);
73 
74         BOOST_TEST(qi::phrase_parse(first, last, vwu, ascii::space, uvw_result));
75         BOOST_TEST(fusion::at_c<0>(uvw_result) && *fusion::at_c<0>(uvw_result) == 2);
76         BOOST_TEST(fusion::at_c<1>(uvw_result) && *fusion::at_c<1>(uvw_result) == 3);
77         BOOST_TEST(fusion::at_c<2>(uvw_result) && *fusion::at_c<2>(uvw_result) == 1);
78     }
79 
80     return boost::report_errors();
81 }
82 
83 
84