• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  Copyright (c) 2009 Chris Hoeppler
2 //
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)
5 
6 //  The purpose of this example is to demonstrate different use cases for the
7 //  confix directive.
8 
9 #include <iostream>
10 #include <string>
11 #include <vector>
12 
13 //[qi_confix_includes
14 #include <boost/spirit/include/qi.hpp>
15 #include <boost/spirit/repository/include/qi_confix.hpp>
16 //]
17 
18 namespace client {
19 //[qi_confix_using
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;
28 //]
29 
30 //[qi_confix_cpp_comment
31     template <typename Iterator>
parse_cpp_comment(Iterator first,Iterator last,std::string & attr)32     bool parse_cpp_comment(Iterator first, Iterator last, std::string& attr)
33     {
34         bool r = parse(first, last,
35             confix("//", eol)[*(char_ - eol)],  // grammar
36             attr);                              // attribute
37 
38         if (!r || first != last) // fail if we did not get a full match
39             return false;
40         return r;
41     }
42 //]
43 
44 //[qi_confix_c_comment
45     template <typename Iterator>
parse_c_comment(Iterator first,Iterator last,std::string & attr)46     bool parse_c_comment(Iterator first, Iterator last, std::string& attr)
47     {
48         bool r = parse(first, last,
49             confix("/*", "*/")[*(char_ - "*/")],    // grammar
50             attr);                                  // attribute
51 
52         if (!r || first != last) // fail if we did not get a full match
53             return false;
54         return r;
55     }
56 //]
57 
58 //[qi_confix_tagged_data
59     template <typename Iterator>
parse_tagged(Iterator first,Iterator last,std::string & attr)60     bool parse_tagged(Iterator first, Iterator last, std::string& attr)
61     {
62         bool r = phrase_parse(first, last,
63             confix("<b>", "</b>")[lexeme[*(char_ - '<')]],  // grammar
64             space,                                          // skip
65             attr);                                          // attribute
66 
67         if (!r || first != last) // fail if we did not get a full match
68             return false;
69         return r;
70     }
71 //]
72 }
73 
74 
main()75 int main()
76 {
77     // C++ comment
78     std::string comment("// This is a comment\n");
79     std::string attr;
80     bool r = client::parse_cpp_comment(comment.begin(), comment.end(), attr);
81 
82     std::cout << "Parsing a C++ comment";
83     if (r && attr == " This is a comment")
84         std::cout << " succeeded." << std::endl;
85     else
86         std::cout << " failed" << std::endl;
87 
88     // C comment
89     comment = "/* This is another comment */";
90     attr.clear();
91     r = client::parse_c_comment(comment.begin(), comment.end(), attr);
92 
93     std::cout << "Parsing a C comment";
94     if (r && attr == " This is another comment ")
95         std::cout << " succeeded." << std::endl;
96     else
97         std::cout << " failed" << std::endl;
98 
99     // Tagged data
100     std::string data = "<b> This is the body. </b>";
101     attr.clear();
102 
103     r = client::parse_tagged(data.begin(), data.end(), attr);
104 
105     std::cout << "Parsing tagged data";
106     if (r && attr == "This is the body. ")
107         std::cout << " succeeded." << std::endl;
108     else
109         std::cout << " failed" << std::endl;
110 
111     return 0;
112 }
113 
114