]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/repository/example/qi/confix.cpp
1 // Copyright (c) 2009 Chris Hoeppler
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)
6 // The purpose of this example is to demonstrate different use cases for the
14 #include <boost/spirit/include/qi.hpp>
15 #include <boost/spirit/repository/include/qi_confix.hpp>
20 using boost::spirit::eol
;
21 using boost::spirit::lexeme
;
22 using boost::spirit::ascii::alnum
;
23 using boost::spirit::ascii::char_
;
24 using boost::spirit::ascii::space
;
25 using boost::spirit::qi::parse
;
26 using boost::spirit::qi::phrase_parse
;
27 using boost::spirit::repository::confix
;
30 //[qi_confix_cpp_comment
31 template <typename Iterator
>
32 bool parse_cpp_comment(Iterator first
, Iterator last
, std::string
& attr
)
34 bool r
= parse(first
, last
,
35 confix("//", eol
)[*(char_
- eol
)], // grammar
38 if (!r
|| first
!= last
) // fail if we did not get a full match
44 //[qi_confix_c_comment
45 template <typename Iterator
>
46 bool parse_c_comment(Iterator first
, Iterator last
, std::string
& attr
)
48 bool r
= parse(first
, last
,
49 confix("/*", "*/")[*(char_
- "*/")], // grammar
52 if (!r
|| first
!= last
) // fail if we did not get a full match
58 //[qi_confix_tagged_data
59 template <typename Iterator
>
60 bool parse_tagged(Iterator first
, Iterator last
, std::string
& attr
)
62 bool r
= phrase_parse(first
, last
,
63 confix("<b>", "</b>")[lexeme
[*(char_
- '<')]], // grammar
67 if (!r
|| first
!= last
) // fail if we did not get a full match
78 std::string
comment("// This is a comment\n");
80 bool r
= client::parse_cpp_comment(comment
.begin(), comment
.end(), attr
);
82 std::cout
<< "Parsing a C++ comment";
83 if (r
&& attr
== " This is a comment")
84 std::cout
<< " succeeded." << std::endl
;
86 std::cout
<< " failed" << std::endl
;
89 comment
= "/* This is another comment */";
91 r
= client::parse_c_comment(comment
.begin(), comment
.end(), attr
);
93 std::cout
<< "Parsing a C comment";
94 if (r
&& attr
== " This is another comment ")
95 std::cout
<< " succeeded." << std::endl
;
97 std::cout
<< " failed" << std::endl
;
100 std::string data
= "<b> This is the body. </b>";
103 r
= client::parse_tagged(data
.begin(), data
.end(), attr
);
105 std::cout
<< "Parsing tagged data";
106 if (r
&& attr
== "This is the body. ")
107 std::cout
<< " succeeded." << std::endl
;
109 std::cout
<< " failed" << std::endl
;