]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/qi/mini_xml1.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/spirit/include/qi.hpp>
16 #include <boost/phoenix/core.hpp>
17 #include <boost/phoenix/operator.hpp>
18 #include <boost/phoenix/fusion.hpp>
19 #include <boost/phoenix/stl.hpp>
20 #include <boost/fusion/include/adapt_struct.hpp>
21 #include <boost/variant/recursive_variant.hpp>
22 #include <boost/foreach.hpp>
31 namespace fusion
= boost::fusion
;
32 namespace phoenix
= boost::phoenix
;
33 namespace qi
= boost::spirit::qi
;
34 namespace ascii
= boost::spirit::ascii
;
36 ///////////////////////////////////////////////////////////////////////////
37 // Our mini XML tree representation
38 ///////////////////////////////////////////////////////////////////////////
39 //[tutorial_xml1_structures
44 boost::recursive_wrapper
<mini_xml
>
51 std::string name
; // tag name
52 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 //[tutorial_xml1_adapt_structures
60 BOOST_FUSION_ADAPT_STRUCT(
63 (std::vector
<client::mini_xml_node
>, children
)
69 ///////////////////////////////////////////////////////////////////////////
70 // Print out the mini xml tree
71 ///////////////////////////////////////////////////////////////////////////
72 int const tabsize
= 4;
76 for (int i
= 0; i
< indent
; ++i
)
80 struct mini_xml_printer
82 mini_xml_printer(int indent
= 0)
87 void operator()(mini_xml
const& xml
) const;
92 struct mini_xml_node_printer
: boost::static_visitor
<>
94 mini_xml_node_printer(int indent
= 0)
99 void operator()(mini_xml
const& xml
) const
101 mini_xml_printer(indent
+tabsize
)(xml
);
104 void operator()(std::string
const& text
) const
107 std::cout
<< "text: \"" << text
<< '"' << std::endl
;
113 void mini_xml_printer::operator()(mini_xml
const& xml
) const
116 std::cout
<< "tag: " << xml
.name
<< std::endl
;
118 std::cout
<< '{' << std::endl
;
120 BOOST_FOREACH(mini_xml_node
const& node
, xml
.children
)
122 boost::apply_visitor(mini_xml_node_printer(indent
), node
);
126 std::cout
<< '}' << std::endl
;
129 ///////////////////////////////////////////////////////////////////////////
130 // Our mini XML grammar definition
131 ///////////////////////////////////////////////////////////////////////////
132 //[tutorial_xml1_grammar
133 template <typename Iterator
>
134 struct mini_xml_grammar
: qi::grammar
<Iterator
, mini_xml(), ascii::space_type
>
136 mini_xml_grammar() : mini_xml_grammar::base_type(xml
)
142 using namespace qi::labels
;
145 using phoenix::push_back
;
147 text
= lexeme
[+(char_
- '<') [_val
+= _1
]];
148 node
= (xml
| text
) [_val
= _1
];
153 >> lexeme
[+(char_
- '>') [_val
+= _1
]]
164 start_tag
[at_c
<0>(_val
) = _1
]
165 >> *node
[push_back(at_c
<1>(_val
), _1
)]
166 >> end_tag(at_c
<0>(_val
))
170 qi::rule
<Iterator
, mini_xml(), ascii::space_type
> xml
;
171 qi::rule
<Iterator
, mini_xml_node(), ascii::space_type
> node
;
172 qi::rule
<Iterator
, std::string(), ascii::space_type
> text
;
173 qi::rule
<Iterator
, std::string(), ascii::space_type
> start_tag
;
174 qi::rule
<Iterator
, void(std::string
), ascii::space_type
> end_tag
;
179 ///////////////////////////////////////////////////////////////////////////////
181 ///////////////////////////////////////////////////////////////////////////////
182 int main(int argc
, char **argv
)
184 char const* filename
;
191 std::cerr
<< "Error: No input file provided." << std::endl
;
195 std::ifstream
in(filename
, std::ios_base::in
);
199 std::cerr
<< "Error: Could not open input file: "
200 << filename
<< std::endl
;
204 std::string storage
; // We will read the contents here.
205 in
.unsetf(std::ios::skipws
); // No white space skipping!
207 std::istream_iterator
<char>(in
),
208 std::istream_iterator
<char>(),
209 std::back_inserter(storage
));
211 typedef client::mini_xml_grammar
<std::string::const_iterator
> mini_xml_grammar
;
212 mini_xml_grammar xml
; // Our grammar
213 client::mini_xml ast
; // Our tree
215 using boost::spirit::ascii::space
;
216 std::string::const_iterator iter
= storage
.begin();
217 std::string::const_iterator end
= storage
.end();
218 bool r
= phrase_parse(iter
, end
, xml
, space
, ast
);
220 if (r
&& iter
== end
)
222 std::cout
<< "-------------------------\n";
223 std::cout
<< "Parsing succeeded\n";
224 std::cout
<< "-------------------------\n";
225 client::mini_xml_printer printer
;
231 std::string::const_iterator some
= iter
+ std::min(30, int(end
- iter
));
232 std::string
context(iter
, (some
>end
)?end
:some
);
233 std::cout
<< "-------------------------\n";
234 std::cout
<< "Parsing failed\n";
235 std::cout
<< "stopped at: \"" << context
<< "...\"\n";
236 std::cout
<< "-------------------------\n";