1 | // Copyright (c) 2001-2011 Hartmut Kaiser |
---|---|
2 | // |
3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
5 | |
6 | #if !defined(BOOST_SPIRIT_LEX_TEST_MAR_23_2007_0721PM) |
7 | #define BOOST_SPIRIT_LEX_TEST_MAR_23_2007_0721PM |
8 | |
9 | #include <boost/variant.hpp> |
10 | #include <boost/range/iterator_range_core.hpp> |
11 | |
12 | #include <boost/core/lightweight_test.hpp> |
13 | |
14 | namespace spirit_test |
15 | { |
16 | /////////////////////////////////////////////////////////////////////////// |
17 | struct display_type |
18 | { |
19 | template<typename T> |
20 | void operator()(T const &) const |
21 | { |
22 | std::cout << typeid(T).name() << std::endl; |
23 | } |
24 | |
25 | template<typename T> |
26 | static void print() |
27 | { |
28 | std::cout << typeid(T).name() << std::endl; |
29 | } |
30 | }; |
31 | |
32 | /////////////////////////////////////////////////////////////////////////// |
33 | display_type const display = {}; |
34 | |
35 | /////////////////////////////////////////////////////////////////////////// |
36 | template <typename Iterator> |
37 | inline boost::iterator_range<Iterator> const& |
38 | get_iterpair(boost::iterator_range<Iterator> const& itp) |
39 | { |
40 | return itp; |
41 | } |
42 | |
43 | template <typename Iterator, BOOST_VARIANT_ENUM_PARAMS(typename T)> |
44 | inline boost::iterator_range<Iterator> const& |
45 | get_iterpair(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& v) |
46 | { |
47 | return boost::get<boost::iterator_range<Iterator> >(v); |
48 | } |
49 | |
50 | /////////////////////////////////////////////////////////////////////////// |
51 | template <typename Lexer, typename Char> |
52 | inline bool |
53 | test(Lexer& lex, Char const* input, std::size_t token_id = 0, |
54 | Char const* state = NULL) |
55 | { |
56 | typedef typename Lexer::iterator_type iterator_type; |
57 | typedef std::basic_string<Char> string_type; |
58 | |
59 | string_type str(input); |
60 | typename string_type::iterator it = str.begin(); |
61 | |
62 | iterator_type first = lex.begin(it, str.end()); |
63 | iterator_type last = lex.end(); |
64 | |
65 | bool r = true; |
66 | |
67 | if (NULL != state) { |
68 | std::size_t stateid = lex.map_state(state); |
69 | r = r && (static_cast<unsigned>(~0) != stateid); |
70 | first.set_state(stateid); |
71 | } |
72 | |
73 | r = r && lex; |
74 | r = r && first != last; |
75 | |
76 | if (token_id != 0) |
77 | r = r && (*first).id() == token_id; |
78 | else |
79 | r = r && (*first).id() != 0; |
80 | |
81 | using namespace boost; |
82 | |
83 | typedef typename Lexer::iterator_type::base_iterator_type iterator; |
84 | typedef iterator_range<iterator> iterpair_type; |
85 | iterpair_type const& ip = get_iterpair<iterator>((*first).value()); |
86 | |
87 | r = r && string_type(ip.begin(), ip.end()) == str; |
88 | return r && first != last && ++first == last; |
89 | } |
90 | } |
91 | |
92 | #endif |
93 | |
94 | |
95 |