• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2001-2011 Joel de Guzman
3 
4     Distributed under the Boost Software License, Version 1.0. (See accompanying
5     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 =============================================================================*/
7 #if !defined(BOOST_SPIRIT_TEST_FEBRUARY_01_2007_0605PM)
8 #define BOOST_SPIRIT_TEST_FEBRUARY_01_2007_0605PM
9 
10 #include <boost/spirit/include/qi_parse.hpp>
11 #include <boost/spirit/include/qi_what.hpp>
12 #include <boost/variant/apply_visitor.hpp>
13 #include <iostream>
14 
15 namespace spirit_test
16 {
17     template <typename Char, typename Parser>
test(Char const * in,Parser const & p,bool full_match=true)18     bool test(Char const* in, Parser const& p, bool full_match = true)
19     {
20         // we don't care about the result of the "what" function.
21         // we only care that all parsers have it:
22         boost::spirit::qi::what(p);
23 
24         Char const* last = in;
25         while (*last)
26             last++;
27         return boost::spirit::qi::parse(in, last, p)
28             && (!full_match || (in == last));
29     }
30 
31     template <typename Char, typename Parser, typename Skipper>
test(Char const * in,Parser const & p,Skipper const & s,bool full_match=true)32     bool test(Char const* in, Parser const& p
33         , Skipper const& s, bool full_match = true)
34     {
35         // we don't care about the result of the "what" function.
36         // we only care that all parsers have it:
37         boost::spirit::qi::what(p);
38 
39         Char const* last = in;
40         while (*last)
41             last++;
42         return boost::spirit::qi::phrase_parse(in, last, p, s)
43             && (!full_match || (in == last));
44     }
45 
46     template <typename Char, typename Parser>
binary_test(Char const * in,std::size_t size,Parser const & p,bool full_match=true)47     bool binary_test(Char const* in, std::size_t size, Parser const& p,
48         bool full_match = true)
49     {
50         // we don't care about the result of the "what" function.
51         // we only care that all parsers have it:
52         boost::spirit::qi::what(p);
53 
54         Char const* last = in + size;
55         return boost::spirit::qi::parse(in, last, p)
56             && (!full_match || (in == last));
57     }
58 
59     template <typename Char, typename Parser, typename Skipper>
binary_test(Char const * in,std::size_t size,Parser const & p,Skipper const & s,bool full_match=true)60     bool binary_test(Char const* in, std::size_t size, Parser const& p,
61         Skipper const& s, bool full_match = true)
62     {
63         // we don't care about the result of the "what" function.
64         // we only care that all parsers have it:
65         boost::spirit::qi::what(p);
66 
67         Char const* last = in + size;
68         return boost::spirit::qi::phrase_parse(in, last, p, s)
69             && (!full_match || (in == last));
70     }
71 
72     template <typename Char, typename Parser, typename Attr>
test_attr(Char const * in,Parser const & p,Attr & attr,bool full_match=true)73     bool test_attr(Char const* in, Parser const& p
74         , Attr& attr, bool full_match = true)
75     {
76         // we don't care about the result of the "what" function.
77         // we only care that all parsers have it:
78         boost::spirit::qi::what(p);
79 
80         Char const* last = in;
81         while (*last)
82             last++;
83         return boost::spirit::qi::parse(in, last, p, attr)
84             && (!full_match || (in == last));
85     }
86 
87     template <typename Char, typename Parser, typename Attr, typename Skipper>
test_attr(Char const * in,Parser const & p,Attr & attr,Skipper const & s,bool full_match=true)88     bool test_attr(Char const* in, Parser const& p
89         , Attr& attr, Skipper const& s, bool full_match = true)
90     {
91         // we don't care about the result of the "what" function.
92         // we only care that all parsers have it:
93         boost::spirit::qi::what(p);
94 
95         Char const* last = in;
96         while (*last)
97             last++;
98         return boost::spirit::qi::phrase_parse(in, last, p, s, attr)
99             && (!full_match || (in == last));
100     }
101 
102     template <typename Char, typename Parser, typename Attr>
binary_test_attr(Char const * in,std::size_t size,Parser const & p,Attr & attr,bool full_match=true)103     bool binary_test_attr(Char const* in, std::size_t size, Parser const& p,
104         Attr& attr, bool full_match = true)
105     {
106         // we don't care about the result of the "what" function.
107         // we only care that all parsers have it:
108         boost::spirit::qi::what(p);
109 
110         Char const* last = in + size;
111         return boost::spirit::qi::parse(in, last, p, attr)
112             && (!full_match || (in == last));
113     }
114 
115     template <typename Char, typename Parser, typename Attr, typename Skipper>
binary_test_attr(Char const * in,std::size_t size,Parser const & p,Attr & attr,Skipper const & s,bool full_match=true)116     bool binary_test_attr(Char const* in, std::size_t size, Parser const& p,
117         Attr& attr, Skipper const& s, bool full_match = true)
118     {
119         // we don't care about the result of the "what" function.
120         // we only care that all parsers have it:
121         boost::spirit::qi::what(p);
122 
123         Char const* last = in + size;
124         return boost::spirit::qi::phrase_parse(in, last, p, s, attr)
125             && (!full_match || (in == last));
126     }
127 
128     struct printer
129     {
130         typedef boost::spirit::utf8_string string;
131 
elementspirit_test::printer132         void element(string const& tag, string const& value, int depth) const
133         {
134             for (int i = 0; i < (depth*4); ++i) // indent to depth
135                 std::cout << ' ';
136 
137             std::cout << "tag: " << tag;
138             if (value != "")
139                 std::cout << ", value: " << value;
140             std::cout << std::endl;
141         }
142     };
143 
print_info(boost::spirit::info const & what)144     void print_info(boost::spirit::info const& what)
145     {
146         using boost::spirit::basic_info_walker;
147 
148         printer pr;
149         basic_info_walker<printer> walker(pr, what.tag, 0);
150         boost::apply_visitor(walker, what.value);
151     }
152 }
153 
154 #endif
155