struct puzzle
Patrick Welche <[email protected]>
| Newsgroups | gmane.comp.parsers.spirit.general |
|---|---|
| Message-ID | <20170412161521.GA1649@quark> |
Following Seth's excellent example, the attached simple program parses
an int and a map of strings. The puzzle is why it no longer compiles
when I remove the int with the following patch:
@@ -11,23 +11,21 @@
using Map = std::map<std::string, std::string>;
struct result {
- int onemore;
Map both;
};
-BOOST_FUSION_ADAPT_STRUCT(result, onemore, both)
+BOOST_FUSION_ADAPT_STRUCT(result, both)
int main()
{
- std::string const buf("1;one two;three four;");
+ std::string const buf("one two;three four;");
auto iter = buf.begin(), end = buf.end();
auto parser = []{
using namespace boost::spirit::x3;
auto string_ = lexeme[+alpha];
auto map_ = (string_ >> string_) >> ';';
- auto num_ = int_ >> ';';
- auto all_ = num_ >> +map_;
+ auto all_ = +map_;
return rule<struct _, struct result> {"parser"} = skip(space) [ all_ ];
};
@@ -49,6 +47,5 @@
for (auto p : res.both) {
std::cout << "found: " << p.first << ' ' << p.second << std::endl;
}
- std::cout << "and: " << res.onemore << " !" << std::endl;
}
}
Less is more?
Cheers,
Patrick
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Spirit-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/spirit-general
struct.cpp
(text/plain, 1.3 KB)
#define BOOST_SPIRIT_X3_DEBUG
#include <map>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/as_vector.hpp>
#include <iostream>
using Map = std::map<std::string, std::string>;
struct result {
int onemore;
Map both;
};
BOOST_FUSION_ADAPT_STRUCT(result, onemore, both)
int main()
{
std::string const buf("1;one two;three four;");
auto iter = buf.begin(), end = buf.end();
auto parser = []{
using namespace boost::spirit::x3;
auto string_ = lexeme[+alpha];
auto map_ = (string_ >> string_) >> ';';
auto num_ = int_ >> ';';
auto all_ = num_ >> +map_;
return rule<struct _, struct result> {"parser"} = skip(space) [ all_ ];
};
result res;
bool ok = parse(iter, end, parser(), res);
if (ok) {
std::cout << "Parse success\n";
}
else {
std::cout << "Parsing failed\n";
}
if (iter != end) {
std::cout << "Bit left over at the end: " << std::string(iter, end) << std::endl;
}
else {
for (auto p : res.both) {
std::cout << "found: " << p.first << ' ' << p.second << std::endl;
}
std::cout << "and: " << res.onemore << " !" << std::endl;
}
}