1 /*=============================================================================
2 Copyright (c) 1998-2003 Joel de Guzman
3 http://spirit.sourceforge.net/
4
5 Use, modification and distribution is subject to the Boost Software
6 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 #include <iostream>
10 #include <boost/detail/lightweight_test.hpp>
11 #include <list>
12
13
14 #include <boost/spirit/include/classic_core.hpp>
15 #include "impl/string_length.hpp"
16 using namespace BOOST_SPIRIT_CLASSIC_NS;
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //
20 // Scanner tests
21 //
22 ///////////////////////////////////////////////////////////////////////////////
23 struct to_upper_iter_policy : public iteration_policy {
24
filterto_upper_iter_policy25 char filter(char ch) const
26 { return char(toupper(ch)); }
27 };
28
test_isspace(char c)29 inline bool test_isspace(char c)
30 {
31 using namespace std; return isspace(c) != 0;
32 }
33
test_islower(char c)34 inline bool test_islower(char c)
35 {
36 using namespace std; return islower(c) != 0;
37 }
38
39 struct skip_white_iter_policy : public iteration_policy {
40
41 template <typename ScannerT>
42 void
advanceskip_white_iter_policy43 advance(ScannerT const& scan) const
44 {
45 do
46 ++scan.first;
47 while (!at_end(scan) && test_isspace(get(scan)));
48 }
49 };
50
51 void
scanner_tests()52 scanner_tests()
53 {
54 char const* cp = "The Big Brown Fox Jumped \n\tOver The Lazy Dog's Back";
55 char const* cp_first = cp;
56 char const* cp_last = cp + test_impl::string_length(cp);
57
58 scanner<char const*>
59 pp1(cp_first, cp_last);
60
61 // compile check only...
62 scanner<> spp1(pp1); (void)spp1;
63 scanner<> spp2(pp1); (void)spp2;
64 // spp1 = spp2;
65 // compile check only...
66
67 while (!pp1.at_end())
68 {
69 std::cout << *pp1;
70 ++pp1;
71 }
72 std::cout << '\n';
73 cp_first = cp;
74
75 std::list<char> li(cp_first, cp_last);
76 std::list<char>::iterator li_first = li.begin();
77 std::list<char>::iterator li_last = li.end();
78
79 scanner<std::list<char>::iterator>
80 pp2(li_first, li_last);
81
82 while (!pp2.at_end())
83 {
84 std::cout << *pp2;
85 ++pp2;
86 }
87 std::cout << '\n';
88 li_first = li.begin();
89
90 scanner<char const*, scanner_policies<to_upper_iter_policy> >
91 pp3(cp_first, cp_last);
92
93 while (!pp3.at_end())
94 {
95 std::cout << *pp3;
96 BOOST_TEST(!test_islower(*pp3));
97 ++pp3;
98 }
99 std::cout << '\n';
100 cp_first = cp;
101
102 scanner<char const*, scanner_policies<skip_white_iter_policy> >
103 pp4(cp_first, cp_last);
104
105 // compile check only...
106 pp1.change_policies(scanner_policies<skip_white_iter_policy>());
107 // compile check only...
108
109 while (!pp4.at_end())
110 {
111 std::cout << *pp4;
112 BOOST_TEST(!test_isspace(*pp4));
113 ++pp4;
114 }
115 std::cout << '\n';
116 cp_first = cp;
117
118 std::cout << "sizeof(scanner<>) == " << sizeof(scanner<>) << '\n';
119
120 parse_info<> pi = parse("12abcdefg12345ABCDEFG789", +digit_p, alpha_p);
121 BOOST_TEST(pi.hit);
122 BOOST_TEST(pi.full);
123
124 pi = parse("abcdefg12345ABCDEFG789", +digit_p, alpha_p);
125 BOOST_TEST(pi.hit);
126 BOOST_TEST(pi.full);
127 }
128
129 ///////////////////////////////////////////////////////////////////////////////
130 //
131 // Main
132 //
133 ///////////////////////////////////////////////////////////////////////////////
134 int
main()135 main()
136 {
137 scanner_tests();
138 return boost::report_errors();
139 }
140
141