]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/example/qi/expect.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / spirit / example / qi / expect.cpp
1 /*=============================================================================
2 Copyright (c) 2016 Frank Hein, maxence business consulting gmbh
3
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
8 #include <iostream>
9 #include <map>
10
11 #include <boost/spirit/home/qi.hpp>
12 #include <boost/spirit/home/qi/nonterminal/grammar.hpp>
13 #include <boost/spirit/include/phoenix.hpp>
14
15 namespace qi = boost::spirit::qi;
16
17 typedef std::string::const_iterator iterator_type;
18 typedef std::string result_type;
19
20 template<typename Parser>
21 void parse(const std::string message, const std::string& input, const std::string& rule, const Parser& parser) {
22 iterator_type iter = input.begin(), end = input.end();
23
24 std::vector<result_type> parsed_result;
25
26 std::cout << "-------------------------\n";
27 std::cout << message << "\n";
28 std::cout << "Rule: " << rule << std::endl;
29 std::cout << "Parsing: \"" << input << "\"\n";
30
31 bool result = qi::phrase_parse(iter, end, parser, qi::space, parsed_result);
32 if (result)
33 {
34 std::cout << "Parser succeeded.\n";
35 std::cout << "Parsed " << parsed_result.size() << " elements:";
36 for (const auto& str : parsed_result)
37 std::cout << "[" << str << "]";
38 std::cout << std::endl;
39 }
40 else
41 {
42 std::cout << "Parser failed" << std::endl;
43 }
44 if (iter == end) {
45 std::cout << "EOI reached." << std::endl;
46 }
47 else {
48 std::cout << "EOI not reached. Unparsed: \"" << std::string(iter, end) << "\"" << std::endl;
49 }
50 std::cout << "-------------------------\n";
51
52 }
53
54 namespace grammars {
55 namespace phx = boost::phoenix;
56
57 template <typename Iterator>
58 struct ident : qi::grammar < Iterator, std::string(), qi::space_type>
59 {
60 ident();
61
62 qi::rule <iterator_type, std::string(), qi::space_type>
63 id, id_list, qualified_id;
64 };
65
66 template <typename Iterator>
67 ident<Iterator>::ident() : ident::base_type(id_list) {
68
69 using qi::on_error;
70 using qi::fail;
71 using qi::expect;
72
73 id = (qi::alpha | qi::char_('_')) >> *(qi::alnum | qi::char_('_'));
74
75 id_list = expect[id >> qi::lit(';')];
76
77 on_error<fail>(id_list,
78 phx::ref(std::cout)
79 << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl
80 << "Error! Expecting "
81 << qi::_4
82 << " here: "
83 << phx::construct<std::string>(qi::_3, qi::_2) << std::endl
84 << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl
85 );
86 }
87 }
88
89 int main() {
90
91 grammars::ident<iterator_type> id;
92
93 parse("expect directive, fail on first"
94 , "1234; id2;"
95 , "qi::expect[ id >> qi::lit(';') ]"
96 , id);
97
98 parse("expect directive, fail on second"
99 , "id1, id2"
100 , "qi::expect[ id >> qi::lit(';') ]"
101 , id);
102
103 parse("expect directive, success"
104 , "id1;"
105 , "qi::expect[ id >> qi::lit(';') ]"
106 , id);
107
108 return 0;
109 }