1 | /*============================================================================= |
---|
2 | Copyright (c) 2005 Jordan DeLong |
---|
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 | |
---|
10 | // Some tests of parsing on value_t's that aren't char or wchar_t. |
---|
11 | // |
---|
12 | // Part of what this is testing is that BOOST_SPIRIT_DEBUG doesn't |
---|
13 | // break when the scanner::value_t is some sort of non-char. |
---|
14 | #define SPIRIT_DEBUG_NODE |
---|
15 | |
---|
16 | #include <boost/spirit/core.hpp> |
---|
17 | #include <boost/static_assert.hpp> |
---|
18 | #include <boost/type_traits/is_same.hpp> |
---|
19 | #include <deque> |
---|
20 | #include <iostream> |
---|
21 | |
---|
22 | namespace sp = boost::spirit; |
---|
23 | |
---|
24 | namespace { |
---|
25 | |
---|
26 | struct grammar : sp::grammar<grammar> { |
---|
27 | template<class Scanner> |
---|
28 | struct definition { |
---|
29 | definition(grammar const&) |
---|
30 | { |
---|
31 | foo |
---|
32 | = sp::alpha_p |
---|
33 | | sp::punct_p |
---|
34 | ; |
---|
35 | } |
---|
36 | |
---|
37 | sp::rule<Scanner> foo; |
---|
38 | sp::rule<Scanner> const& |
---|
39 | start() const |
---|
40 | { |
---|
41 | return foo; |
---|
42 | } |
---|
43 | }; |
---|
44 | }; |
---|
45 | |
---|
46 | struct non_pod { |
---|
47 | non_pod() : value('1') {} |
---|
48 | char value; |
---|
49 | bool operator==(non_pod const& o) const { return value == o.value; } |
---|
50 | }; |
---|
51 | |
---|
52 | std::ostream& |
---|
53 | operator<<(std::ostream& out, non_pod const& x) |
---|
54 | { |
---|
55 | out << x.value; |
---|
56 | return out; |
---|
57 | } |
---|
58 | |
---|
59 | template<class T> |
---|
60 | struct convertable_non_pod : non_pod { |
---|
61 | operator T() { return value; } |
---|
62 | }; |
---|
63 | |
---|
64 | struct nonpod_gram : sp::grammar<nonpod_gram> { |
---|
65 | template<class Scanner> |
---|
66 | struct definition { |
---|
67 | definition(nonpod_gram const&) |
---|
68 | { |
---|
69 | foo = sp::ch_p(typename Scanner::value_t()); |
---|
70 | } |
---|
71 | |
---|
72 | sp::rule<Scanner> foo; |
---|
73 | sp::rule<Scanner> const& |
---|
74 | start() const |
---|
75 | { |
---|
76 | return foo; |
---|
77 | } |
---|
78 | }; |
---|
79 | }; |
---|
80 | |
---|
81 | template<class Grammar, class ValueType> |
---|
82 | void |
---|
83 | test_type() |
---|
84 | { |
---|
85 | typedef std::deque<ValueType> container; |
---|
86 | typedef typename container::iterator iterator; |
---|
87 | typedef sp::scanner<iterator> scanner; |
---|
88 | |
---|
89 | container blah; |
---|
90 | blah.push_back(typename container::value_type()); |
---|
91 | blah.push_back(typename container::value_type()); |
---|
92 | |
---|
93 | iterator first = blah.begin(); |
---|
94 | iterator last = blah.end(); |
---|
95 | |
---|
96 | scanner scan(first, last); |
---|
97 | |
---|
98 | // Make sure this actually tries what we think it tries. |
---|
99 | BOOST_STATIC_ASSERT(( |
---|
100 | boost::is_same<typename scanner::value_t, ValueType>::value |
---|
101 | )); |
---|
102 | |
---|
103 | Grammar().parse(scan); |
---|
104 | } |
---|
105 | |
---|
106 | } |
---|
107 | |
---|
108 | int |
---|
109 | main() |
---|
110 | { |
---|
111 | // Make sure isfoo() style functions work for integral types. |
---|
112 | test_type<grammar, char>(); |
---|
113 | test_type<grammar, unsigned char>(); |
---|
114 | test_type<grammar, wchar_t>(); |
---|
115 | test_type<grammar, int>(); |
---|
116 | test_type<grammar, long>(); |
---|
117 | test_type<grammar, short>(); |
---|
118 | test_type<grammar, bool>(); |
---|
119 | |
---|
120 | // Non-POD's should work with things like alpha_p as long as we |
---|
121 | // can turn them into a type that can do isalpha(). |
---|
122 | test_type<grammar, convertable_non_pod<char> >(); |
---|
123 | test_type<grammar, convertable_non_pod<wchar_t> >(); |
---|
124 | test_type<grammar, convertable_non_pod<int> >(); |
---|
125 | |
---|
126 | // BOOST_SPIRIT_DEBUG should work with grammars that parse |
---|
127 | // non-POD's even if they can't do things like isalpha(). |
---|
128 | test_type<nonpod_gram, non_pod>(); |
---|
129 | } |
---|