Created
March 29, 2012 10:37
-
-
Save mloskot/2235721 to your computer and use it in GitHub Desktop.
Boost.Spirit - fusion::vector as attribute with optional elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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