-
Notifications
You must be signed in to change notification settings - Fork 0
/
qi-parser.cpp
185 lines (153 loc) · 5.05 KB
/
qi-parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <boost/config/warning_disable.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/qi.hpp>
#include <iostream>
#include <string>
#include <vector>
namespace client {
namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix;
namespace ascii = boost::spirit::ascii;
struct ToppingItem : std::pair<unsigned int, double> {};
struct Pizza {
unsigned int id_;
std::string name_;
unsigned int n_toppings;
std::vector<ToppingItem> toppings;
};
std::ostream &operator<<(std::ostream &os, const ToppingItem &topping) {
os << '{' << topping.first << ", " << topping.second << "}";
return os;
}
std::ostream &operator<<(std::ostream &os, const Pizza &pizza) {
os << pizza.id_ << ", " << pizza.name_ << ", ";
os << pizza.n_toppings << " {";
for (const auto &topping : pizza.toppings) {
os << topping << ", ";
}
os << "}";
return os;
}
} // namespace client
// BOOST_FUSION_ADAPT_STRUCT has to be in the global scope
BOOST_FUSION_ADAPT_STRUCT(client::ToppingItem,
(unsigned int, first) (double, second))
BOOST_FUSION_ADAPT_STRUCT(client::Pizza,
(unsigned int, id_)
(std::string, name_)
(unsigned int, n_toppings)
(std::vector<client::ToppingItem>, toppings))
namespace client {
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
//! Skipper struct
//! Skips either whitespace or
//! a hash following by 0 or chars up to the end of line
template <typename Iterator>
struct skipper : qi::grammar<Iterator>
{
skipper() : skipper::base_type(start)
{
qi::char_type char_;
qi::eol_type eol;
ascii::space_type space;
start =
space
| '#' >> *(char_ - eol) >> eol
;
}
qi::rule<Iterator> start;
};
template <typename Iterator>
struct pizza_parser : qi::grammar<Iterator, Pizza(), skipper<Iterator> > {
pizza_parser() : pizza_parser::base_type(start) {
using ascii::char_;
using qi::double_;
using qi::lexeme;
using qi::lit;
using qi::no_case;
using qi::uint_;
using qi::fail;
using qi::on_error;
using namespace qi::labels;
using phoenix::construct;
using phoenix::val;
using qi::eps;
namespace phx = boost::phoenix;
using qi::repeat;
using boost::spirit::qi::uint_parser;
using boost::spirit::qi::_1;
quoted_string %= eps > lexeme['"' >> +(char_ - '"') >> '"'];
topping_item %= eps >> uint_ >> double_;
unsigned int n;
start %= eps
> uint_
> quoted_string
> uint_[phx::ref(n) = _1]
> repeat(phx::ref(n))[topping_item];
quoted_string.name("quoted_string");
topping_item.name("topping_item");
start.name("start");
on_error<fail>
(
start
, std::cout
<< val("Error! Expecting ")
<< _4 // what failed?
<< val(" here: \"")
<< construct<std::string>(_3, _2) // iterators to error-pos, end
<< val("\"")
<< std::endl
);
}
qi::rule<Iterator, std::string(), skipper<Iterator> > quoted_string;
qi::rule<Iterator, ToppingItem(), skipper<Iterator> > topping_item;
qi::rule<Iterator, Pizza(), skipper<Iterator> > start;
};
} // namespace client
int main() {
using boost::spirit::ascii::space;
typedef std::string::const_iterator iterator_type;
typedef client::pizza_parser<iterator_type> pizza_parser;
client::skipper<iterator_type> skipper;
pizza_parser g; // Our grammar
std::vector<std::string> input{
"100 # pizza id\n"
"\"hawaiian\" # pizza name\n"
"2 # number of toppings\n"
"0 0.0 # topping index and quantity\n"
"1 1.1 # topping index and quantity\n",
"101 \"bbq chicken\" 3 0 0.0 1 1.1 2 2.2",
"# This input has\n"
"# several lines\n"
"# of comments preceeding\n"
"# the data\n"
"101 \"farmhouse\" 3 0 0.0 1 1.1 2 2.2",
"102 \"three cheese\" 4 0 0.0 1 1.1 2 2.2 3 3.3",
"103 \"pepperoni\" 5 0 0.0 1 1.1 2 2.2 3 3.3 4 4.4 # Trailing comments not consumed by parser",
"104 \"pizza#with#hashes#on#top\" 1 0 0.0",
};
for (const auto &str : input) {
client::Pizza piz;
std::string::const_iterator iter = str.cbegin();
std::string::const_iterator end = str.cend();
bool r = phrase_parse(iter, end, g, skipper, piz);
std::cout << "================================================================" << std::endl;
std::cout << str << std::endl;
if (r) {
std::cout << "Parsing succeeded" << std::endl;
std::cout << "got: " << piz << std::endl;
if (iter != end) {
std::cout << "Some input remaining" << std::endl;
}
} else {
std::cout << "Parsing failed\n";
}
}
return 0;
}