• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2001-2010 Joel de Guzman
3     Copyright (c) 2016 Frank Hein, maxence business consulting gmbh
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 #include <boost/detail/lightweight_test.hpp>
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_action.hpp>
15 #include <boost/spirit/include/qi_nonterminal.hpp>
16 #include <boost/spirit/include/qi_auxiliary.hpp>
17 #include <boost/spirit/include/support_argument.hpp>
18 #include <boost/fusion/include/vector.hpp>
19 #include <boost/fusion/include/at.hpp>
20 #include <boost/spirit/include/phoenix_core.hpp>
21 #include <boost/spirit/include/phoenix_operator.hpp>
22 #include <boost/spirit/include/phoenix_statement.hpp>
23 
24 #include <string>
25 #include <iostream>
26 #include "test.hpp"
27 
28 int
main()29 main()
30 {
31     using namespace boost::spirit;
32     using namespace boost::spirit::ascii;
33     using spirit_test::test;
34     using spirit_test::print_info;
35     using boost::spirit::qi::expectation_failure;
36 
37 
38     {
39         try
40         {
41             BOOST_TEST((test("aa", expect[char_ >> char_])));
42             BOOST_TEST((test("aaa", expect[char_ >> char_ >> char_('a')])));
43             BOOST_TEST((test("xi", expect[char_('x') >> char_('i')])));
44             BOOST_TEST((test("xin", expect[char_('x') >> char_('i') >> char_('n')])));
45             BOOST_TEST((!test("xi", expect[char_('y')]))); // should throw!
46         }
47         catch (expectation_failure<char const*> const& x)
48         {
49             std::cout << "expected: "; print_info(x.what_);
50             std::cout << "got: \"" << x.first << '"' << std::endl;
51 
52             BOOST_TEST(boost::get<std::string>(x.what_.value) == "y");
53             BOOST_TEST(std::string(x.first, x.last) == "xi");
54         }
55     }
56 
57     {
58         try
59         {
60             BOOST_TEST((!test("xi", expect[char_('x') >> char_('o')])));
61         }
62         catch (expectation_failure<char const*> const& x)
63         {
64             std::cout << "expected: "; print_info(x.what_);
65             std::cout << "got: \"" << x.first << '"' << std::endl;
66 
67             BOOST_TEST(std::string(x.first, x.last) == "xi");
68             BOOST_TEST(x.what_.tag == "sequence");
69         }
70     }
71 
72     {
73         try
74         {
75             BOOST_TEST((!test(" x i", expect[char_('x') >> char_('o')], space)));
76         }
77         catch (expectation_failure<char const*> const& x)
78         {
79             std::cout << "expected: "; print_info(x.what_);
80             std::cout << "got: \"" << x.first << '"' << std::endl;
81 
82             BOOST_TEST(std::string(x.first, x.last) == " x i");
83             BOOST_TEST(x.what_.tag == "sequence");
84         }
85     }
86 
87     {
88         try
89         {
90             BOOST_TEST((test(" a a", expect[char_ >> char_], space)));
91             BOOST_TEST((test(" x i", expect[char_('x') >> char_('i')], space)));
92             BOOST_TEST((!test(" x i", expect[char_('y')], space)));
93         }
94         catch (expectation_failure<char const*> const& x)
95         {
96             std::cout << "expected: "; print_info(x.what_);
97             std::cout << "got: \"" << x.first << '"' << std::endl;
98 
99             BOOST_TEST(boost::get<std::string>(x.what_.value) == "y");
100             BOOST_TEST(std::string(x.first, x.last) == "x i");
101         }
102     }
103 
104     {
105         try
106         {
107             BOOST_TEST((test("aA", expect[no_case[char_('a') >> 'a']])));
108             BOOST_TEST((test("BEGIN END", expect[no_case[lit("begin") >> "end"]], space)));
109             BOOST_TEST((!test("BEGIN END", expect[no_case[lit("begin") >> "nend"]], space)));
110         }
111         catch (expectation_failure<char const*> const& x)
112         {
113             std::cout << "expected: "; print_info(x.what_);
114             std::cout << "got: \"" << x.first << '"' << std::endl;
115 
116             BOOST_TEST(x.what_.tag == "sequence");
117             BOOST_TEST(std::string(x.first, x.last) == "BEGIN END");
118         }
119     }
120 
121     {
122         using boost::spirit::qi::rule;
123         using boost::spirit::eps;
124         rule<const wchar_t*, void(int)> r;
125         r = expect[eps(_r1)];
126     }
127 
128     return boost::report_errors();
129 }
130 
131