Skip to content

Instantly share code, notes, and snippets.

@mloskot
Created March 29, 2012 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mloskot/2235721 to your computer and use it in GitHub Desktop.
Save mloskot/2235721 to your computer and use it in GitHub Desktop.
Boost.Spirit - fusion::vector as attribute with optional elements
// Version fixed by VeXocide
// Test case based on solution discussed here:
// http://thread.gmane.org/gmane.comp.parsers.spirit.general/24684/focus=24688
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/vector.hpp>
namespace qi = boost::spirit::qi;
template <typename Iterator>
bool test_parse(Iterator& first, Iterator last)
{
typedef boost::fusion::vector<int, int, int> parts;
qi::rule<Iterator, parts(), qi::space_type> r1 =
qi::int_
>> "."
>> qi::int_
>> (
(
qi::lit(".")
>> qi::int_
>> qi::lit(".")
>> qi::int_
)
| (
qi::attr(0)
>> qi::attr(0)
)
)
;
return phrase_parse(first, last, r1, qi::space);
}
///////////////////////////////////////////////////////////////////////////////
int main()
{
std::string str;
while (getline(std::cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
bool r = test_parse(iter, end);
if (r && iter == end)
{
std::cout << "Parsing succeeded\n";
}
else
{
std::cout << "Parsing failed\n";
}
}
std::cout << "Bye... :-) \n\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment