• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  Copyright (c) 2001-2010 Hartmut Kaiser
2 //  Copyright (c) 2001-2010 Joel de Guzman
3 //  Copyright (c) 2011 Aaron Graham
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 
10 #include <boost/spirit/include/qi_action.hpp>
11 #include <boost/spirit/include/qi_auxiliary.hpp>
12 #include <boost/spirit/include/qi_binary.hpp>
13 #include <boost/spirit/include/qi_char.hpp>
14 #include <boost/spirit/include/qi_directive.hpp>
15 #include <boost/spirit/include/qi_nonterminal.hpp>
16 #include <boost/spirit/include/qi_numeric.hpp>
17 #include <boost/spirit/include/qi_operator.hpp>
18 #include <boost/spirit/include/qi_string.hpp>
19 #include <boost/spirit/include/phoenix_operator.hpp>
20 
21 #include <boost/spirit/repository/include/qi_advance.hpp>
22 
23 #include <boost/assign/std/list.hpp>
24 #include "test.hpp"
25 
26 namespace spirit_test
27 {
28     template <typename Container, typename Parser>
test_c(Container const & in,Parser const & p,bool full_match=true)29     bool test_c(Container const& in, Parser const& p, bool full_match = true)
30     {
31         // we don't care about the results of the "what" function.
32         // we only care that all parsers have it:
33         boost::spirit::qi::what(p);
34 
35         typename Container::const_iterator first = in.begin();
36         typename Container::const_iterator const last = in.end();
37         return boost::spirit::qi::parse(first, last, p)
38             && (!full_match || (first == last));
39     }
40 }
41 
main()42 int main()
43 {
44     using spirit_test::test;
45     using spirit_test::test_c;
46 
47     using namespace boost::spirit::qi::labels;
48     using boost::spirit::qi::locals;
49     using boost::spirit::qi::rule;
50     using boost::spirit::qi::uint_;
51     using boost::spirit::qi::byte_;
52 
53     using namespace boost::assign;
54     using boost::spirit::repository::qi::advance;
55 
56     { // test basic functionality with random-access iterators
57         rule<char const*> start;
58 
59         start = 'a' >> advance(3) >> "bc";
60         BOOST_TEST(test("a123bc", start));
61 
62         start = (advance(3) | 'q') >> 'i';
63         BOOST_TEST(test("qi", start));
64 
65         start = advance(-1);
66         BOOST_TEST(!test("0", start));
67 
68         start = advance(-1) | "qi";
69         BOOST_TEST(test("qi", start));
70 
71         start = advance(0) >> "abc" >> advance(10) >> "nopq" >> advance(0)
72             >> advance(8) >> 'z';
73         BOOST_TEST(test("abcdefghijklmnopqrstuvwxyz", start));
74     }
75 
76     { // test locals
77         rule<char const*, locals<unsigned> > start;
78 
79         start = byte_ [_a = _1] >> advance(_a) >> "345";
80         BOOST_TEST(test("\x02""12345", start));
81         BOOST_TEST(!test("\x60""345", start));
82     }
83 
84     { // test basic functionality with bidirectional iterators
85         rule<std::list<char>::const_iterator, locals<int> > start;
86         std::list<char> list;
87 
88         list.clear();
89         list += 1,2,'a','b','c';
90         start = byte_ [_a = _1] >> advance(_a) >> "abc";
91         BOOST_TEST(test_c(list, start));
92 
93         list.clear();
94         list += 3,'q','i';
95         start = byte_ [_a = _1] >> advance(_a);
96         BOOST_TEST(!test_c(list, start));
97 
98         start = byte_ [_a = _1] >> (advance(_a) | "qi");
99         BOOST_TEST(test_c(list, start));
100 
101         list.clear();
102         list += 'a','b','c','d','e','f','g','h','i','j','k','l','m';
103         list += 'n','o','p','q','r','s','t','u','v','w','x','y','z';
104         start = advance(0) >> "abc" >> advance(10) >> "nopq" >> advance(0)
105             >> advance(8) >> 'z';
106         BOOST_TEST(test_c(list, start));
107     }
108 
109     return boost::report_errors();
110 }
111 
112