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