]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/classic/example/fundamental/comments.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2003 Hartmut Kaiser
3 http://spirit.sourceforge.net/
5 Use, modification and distribution is subject to the Boost Software
6 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 ///////////////////////////////////////////////////////////////////////////////
11 // This example shows:
12 // 1. Parsing of different comment styles
13 // parsing C/C++-style comment
14 // parsing C++-style comment
15 // parsing PASCAL-style comment
16 // 2. Parsing tagged data with the help of the confix_parser
17 // 3. Parsing tagged data with the help of the confix_parser but the semantic
18 // action is directly attached to the body sequence parser
20 ///////////////////////////////////////////////////////////////////////////////
26 #include <boost/spirit/include/classic_core.hpp>
27 #include <boost/spirit/include/classic_confix.hpp>
28 #include <boost/spirit/include/classic_chset.hpp>
31 ///////////////////////////////////////////////////////////////////////////////
34 using namespace BOOST_SPIRIT_CLASSIC_NS
;
36 ///////////////////////////////////////////////////////////////////////////////
37 // actor called after successfully matching a single character
41 actor_string(std::string
&rstr
) :
46 void operator() (const char *pbegin
, const char *pend
) const
48 matched
+= std::string(pbegin
, pend
-pbegin
);
55 ///////////////////////////////////////////////////////////////////////////////
56 // actor called after successfully matching a C++-comment
57 void actor_cpp (const char *pfirst
, const char *plast
)
59 cout
<< "Parsing C++-comment" <<endl
;
60 cout
<< "Matched (" << plast
-pfirst
<< ") characters: ";
61 cout
<< "\"" << std::string(pfirst
, plast
) << "\"" << endl
;
64 ///////////////////////////////////////////////////////////////////////////////
69 ///////////////////////////////////////////////////////////////////////////////
71 // 1. Parsing different comment styles
72 // parsing C/C++-style comments (non-nested!)
74 ///////////////////////////////////////////////////////////////////////////////
76 char const* pCComment
= "/* This is a /* nested */ C-comment */";
81 comment_p("/*", "*/") // rule for C-comments
82 | comment_p("//") // rule for C++ comments
85 std::string comment_c
;
88 result
= parse (pCComment
, cpp_comment
[actor_string(comment_c
)]);
91 cout
<< "Parsed C-comment successfully!" << endl
;
92 cout
<< "Matched (" << (int)comment_c
.size() << ") characters: ";
93 cout
<< "\"" << comment_c
<< "\"" << endl
;
97 cout
<< "Failed to parse C/C++-comment!" << endl
;
101 // parsing C++-style comment
102 char const* pCPPComment
= "// This is a C++-comment\n";
103 std::string comment_cpp
;
105 result
= parse (pCPPComment
, cpp_comment
[&actor_cpp
]);
107 cout
<< "Parsed C++-comment successfully!" << endl
;
109 cout
<< "Failed to parse C++-comment!" << endl
;
114 // parsing PASCAL-style comment (nested!)
115 char const* pPComment
= "{ This is a (* nested *) PASCAL-comment }";
117 rule
<> pascal_comment
;
119 pascal_comment
= // in PASCAL we have two comment styles
120 comment_nest_p('{', '}') // both may be nested
121 | comment_nest_p("(*", "*)")
124 std::string comment_pascal
;
126 result
= parse (pPComment
, pascal_comment
[actor_string(comment_pascal
)]);
129 cout
<< "Parsed PASCAL-comment successfully!" << endl
;
130 cout
<< "Matched (" << (int)comment_pascal
.size() << ") characters: ";
131 cout
<< "\"" << comment_pascal
<< "\"" << endl
;
135 cout
<< "Failed to parse PASCAL-comment!" << endl
;
139 ///////////////////////////////////////////////////////////////////////////////
141 // 2. Parsing tagged data with the help of the confix parser
143 ///////////////////////////////////////////////////////////////////////////////
146 rule
<> open_tag
, html_tag
, close_tag
, body_text
;
161 confix_p (open_tag
, (*body_text
)[actor_string(body
)], close_tag
)
164 char const* pTag
= "<b>Body text</b>";
166 result
= parse (pTag
, html_tag
);
169 cout
<< "Parsed HTML snippet \"<b>Body text</b>\" successfully "
170 "(with re-attached actor)!" << endl
;
171 cout
<< "Found body (" << (int)body
.size() << " characters): ";
172 cout
<< "\"" << body
<< "\"" << endl
;
176 cout
<< "Failed to parse HTML snippet (with re-attached actor)!"
181 ///////////////////////////////////////////////////////////////////////////////
183 // 3. Parsing tagged data with the help of the confix_parser but the
184 // semantic action is directly attached to the body sequence parser
185 // (see comment in confix.hpp) and out of the usage of the 'direct()'
186 // construction function no automatic refactoring takes place.
188 // As you can see, for successful parsing it is required to refactor the
189 // confix parser by hand. To see, how it fails, you can try the following:
194 // (*body_text)[actor_string(bodydirect)],
199 // Here the *body_text parser eats up all the input up to the end of the
202 ///////////////////////////////////////////////////////////////////////////////
204 rule
<> html_tag_direct
;
205 std::string bodydirect
;
210 (*(body_text
- str_p("</b>")))[actor_string(bodydirect
)],
215 char const* pTagDirect
= "<b>Body text</b>";
217 result
= parse (pTagDirect
, html_tag_direct
);
220 cout
<< "Parsed HTML snippet \"<b>Body text</b>\" successfully "
221 "(with direct actor)!" << endl
;
222 cout
<< "Found body (" << (int)bodydirect
.size() << " characters): ";
223 cout
<< "\"" << bodydirect
<< "\"" << endl
;
227 cout
<< "Failed to parse HTML snippet (with direct actor)!" << endl
;