]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/qi/mini_xml2.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/fusion/include/adapt_struct.hpp>
22 #include <boost/variant/recursive_variant.hpp>
23 #include <boost/foreach.hpp>
32 namespace fusion
= boost::fusion
;
33 namespace phoenix
= boost::phoenix
;
34 namespace qi
= boost::spirit::qi
;
35 namespace ascii
= boost::spirit::ascii
;
37 ///////////////////////////////////////////////////////////////////////////
38 // Our mini XML tree representation
39 ///////////////////////////////////////////////////////////////////////////
44 boost::recursive_wrapper
<mini_xml
>
51 std::string name
; // tag name
52 std::vector
<mini_xml_node
> children
; // children
56 // We need to tell fusion about our mini_xml struct
57 // to make it a first-class fusion citizen
58 BOOST_FUSION_ADAPT_STRUCT(
61 (std::vector
<client::mini_xml_node
>, children
)
66 ///////////////////////////////////////////////////////////////////////////
67 // Print out the mini xml tree
68 ///////////////////////////////////////////////////////////////////////////
69 int const tabsize
= 4;
73 for (int i
= 0; i
< indent
; ++i
)
77 struct mini_xml_printer
79 mini_xml_printer(int indent
= 0)
84 void operator()(mini_xml
const& xml
) const;
89 struct mini_xml_node_printer
: boost::static_visitor
<>
91 mini_xml_node_printer(int indent
= 0)
96 void operator()(mini_xml
const& xml
) const
98 mini_xml_printer(indent
+tabsize
)(xml
);
101 void operator()(std::string
const& text
) const
104 std::cout
<< "text: \"" << text
<< '"' << std::endl
;
110 void mini_xml_printer::operator()(mini_xml
const& xml
) const
113 std::cout
<< "tag: " << xml
.name
<< std::endl
;
115 std::cout
<< '{' << std::endl
;
117 BOOST_FOREACH(mini_xml_node
const& node
, xml
.children
)
119 boost::apply_visitor(mini_xml_node_printer(indent
), node
);
123 std::cout
<< '}' << std::endl
;
126 ///////////////////////////////////////////////////////////////////////////
127 // Our mini XML grammar definition
128 ///////////////////////////////////////////////////////////////////////////
129 //[tutorial_xml2_grammar
130 template <typename Iterator
>
131 struct mini_xml_grammar
132 : qi::grammar
<Iterator
, mini_xml(), qi::locals
<std::string
>, ascii::space_type
>
135 : mini_xml_grammar::base_type(xml
)
141 using namespace qi::labels
;
143 text
%= lexeme
[+(char_
- '<')];
149 >> lexeme
[+(char_
- '>')]
166 qi::rule
<Iterator
, mini_xml(), qi::locals
<std::string
>, ascii::space_type
> xml
;
167 qi::rule
<Iterator
, mini_xml_node(), ascii::space_type
> node
;
168 qi::rule
<Iterator
, std::string(), ascii::space_type
> text
;
169 qi::rule
<Iterator
, std::string(), ascii::space_type
> start_tag
;
170 qi::rule
<Iterator
, void(std::string
), ascii::space_type
> end_tag
;
175 ///////////////////////////////////////////////////////////////////////////////
177 ///////////////////////////////////////////////////////////////////////////////
178 int main(int argc
, char **argv
)
180 char const* filename
;
187 std::cerr
<< "Error: No input file provided." << std::endl
;
191 std::ifstream
in(filename
, std::ios_base::in
);
195 std::cerr
<< "Error: Could not open input file: "
196 << filename
<< std::endl
;
200 std::string storage
; // We will read the contents here.
201 in
.unsetf(std::ios::skipws
); // No white space skipping!
203 std::istream_iterator
<char>(in
),
204 std::istream_iterator
<char>(),
205 std::back_inserter(storage
));
207 typedef client::mini_xml_grammar
<std::string::const_iterator
> mini_xml_grammar
;
208 mini_xml_grammar xml
; // Our grammar
209 client::mini_xml ast
; // Our tree
211 using boost::spirit::ascii::space
;
212 std::string::const_iterator iter
= storage
.begin();
213 std::string::const_iterator end
= storage
.end();
214 bool r
= phrase_parse(iter
, end
, xml
, space
, ast
);
216 if (r
&& iter
== end
)
218 std::cout
<< "-------------------------\n";
219 std::cout
<< "Parsing succeeded\n";
220 std::cout
<< "-------------------------\n";
221 client::mini_xml_printer printer
;
227 std::string::const_iterator some
= iter
+30;
228 std::string
context(iter
, (some
>end
)?end
:some
);
229 std::cout
<< "-------------------------\n";
230 std::cout
<< "Parsing failed\n";
231 std::cout
<< "stopped at: \": " << context
<< "...\"\n";
232 std::cout
<< "-------------------------\n";