]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/qi/mini_xml3.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2010 Joel de Guzman
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 ///////////////////////////////////////////////////////////////////////////////
9 // A mini XML-like parser
11 // [ JDG March 25, 2007 ] spirit2
13 ///////////////////////////////////////////////////////////////////////////////
15 #include <boost/config/warning_disable.hpp>
16 #include <boost/spirit/include/qi.hpp>
17 #include <boost/spirit/include/phoenix_core.hpp>
18 #include <boost/spirit/include/phoenix_operator.hpp>
19 #include <boost/spirit/include/phoenix_fusion.hpp>
20 #include <boost/spirit/include/phoenix_stl.hpp>
21 #include <boost/spirit/include/phoenix_object.hpp>
22 #include <boost/fusion/include/adapt_struct.hpp>
23 #include <boost/variant/recursive_variant.hpp>
24 #include <boost/foreach.hpp>
33 namespace fusion
= boost::fusion
;
34 namespace phoenix
= boost::phoenix
;
35 namespace qi
= boost::spirit::qi
;
36 namespace ascii
= boost::spirit::ascii
;
38 ///////////////////////////////////////////////////////////////////////////
39 // Our mini XML tree representation
40 ///////////////////////////////////////////////////////////////////////////
45 boost::recursive_wrapper
<mini_xml
>
52 std::string name
; // tag name
53 std::vector
<mini_xml_node
> children
; // children
57 // We need to tell fusion about our mini_xml struct
58 // to make it a first-class fusion citizen
59 BOOST_FUSION_ADAPT_STRUCT(
62 (std::vector
<client::mini_xml_node
>, children
)
67 ///////////////////////////////////////////////////////////////////////////
68 // Print out the mini xml tree
69 ///////////////////////////////////////////////////////////////////////////
70 int const tabsize
= 4;
74 for (int i
= 0; i
< indent
; ++i
)
78 struct mini_xml_printer
80 mini_xml_printer(int indent
= 0)
85 void operator()(mini_xml
const& xml
) const;
90 struct mini_xml_node_printer
: boost::static_visitor
<>
92 mini_xml_node_printer(int indent
= 0)
97 void operator()(mini_xml
const& xml
) const
99 mini_xml_printer(indent
+tabsize
)(xml
);
102 void operator()(std::string
const& text
) const
105 std::cout
<< "text: \"" << text
<< '"' << std::endl
;
111 void mini_xml_printer::operator()(mini_xml
const& xml
) const
114 std::cout
<< "tag: " << xml
.name
<< std::endl
;
116 std::cout
<< '{' << std::endl
;
118 BOOST_FOREACH(mini_xml_node
const& node
, xml
.children
)
120 boost::apply_visitor(mini_xml_node_printer(indent
), node
);
124 std::cout
<< '}' << std::endl
;
127 ///////////////////////////////////////////////////////////////////////////
128 // Our mini XML grammar definition
129 ///////////////////////////////////////////////////////////////////////////
130 //[tutorial_xml3_grammar
131 template <typename Iterator
>
132 struct mini_xml_grammar
133 : qi::grammar
<Iterator
, mini_xml(), qi::locals
<std::string
>, ascii::space_type
>
136 : mini_xml_grammar::base_type(xml
, "xml")
144 using namespace qi::labels
;
146 using phoenix::construct
;
149 text
%= lexeme
[+(char_
- '<')];
155 > lexeme
[+(char_
- '>')]
174 start_tag
.name("start_tag");
175 end_tag
.name("end_tag");
181 << val("Error! Expecting ")
182 << _4
// what failed?
184 << construct
<std::string
>(_3
, _2
) // iterators to error-pos, end
190 qi::rule
<Iterator
, mini_xml(), qi::locals
<std::string
>, ascii::space_type
> xml
;
191 qi::rule
<Iterator
, mini_xml_node(), ascii::space_type
> node
;
192 qi::rule
<Iterator
, std::string(), ascii::space_type
> text
;
193 qi::rule
<Iterator
, std::string(), ascii::space_type
> start_tag
;
194 qi::rule
<Iterator
, void(std::string
), ascii::space_type
> end_tag
;
199 ///////////////////////////////////////////////////////////////////////////////
201 ///////////////////////////////////////////////////////////////////////////////
202 int main(int argc
, char **argv
)
204 char const* filename
;
211 std::cerr
<< "Error: No input file provided." << std::endl
;
215 std::ifstream
in(filename
, std::ios_base::in
);
219 std::cerr
<< "Error: Could not open input file: "
220 << filename
<< std::endl
;
224 std::string storage
; // We will read the contents here.
225 in
.unsetf(std::ios::skipws
); // No white space skipping!
227 std::istream_iterator
<char>(in
),
228 std::istream_iterator
<char>(),
229 std::back_inserter(storage
));
231 typedef client::mini_xml_grammar
<std::string::const_iterator
> mini_xml_grammar
;
232 mini_xml_grammar xml
; // Our grammar
233 client::mini_xml ast
; // Our tree
235 using boost::spirit::ascii::space
;
236 std::string::const_iterator iter
= storage
.begin();
237 std::string::const_iterator end
= storage
.end();
238 bool r
= phrase_parse(iter
, end
, xml
, space
, ast
);
240 if (r
&& iter
== end
)
242 std::cout
<< "-------------------------\n";
243 std::cout
<< "Parsing succeeded\n";
244 std::cout
<< "-------------------------\n";
245 client::mini_xml_printer printer
;
251 std::cout
<< "-------------------------\n";
252 std::cout
<< "Parsing failed\n";
253 std::cout
<< "-------------------------\n";