Ticket #8446: spirit_bug.cpp

File spirit_bug.cpp, 1.0 KB (added by Nikita Rogatnev <nikita.rogatnev@…>, 10 years ago)

Source file to reproduce

Line 
1#include <iostream>
2#include <boost/spirit/include/qi.hpp>
3
4namespace qi = boost::spirit::qi;
5using qi::omit;
6using qi::repeat;
7using std::cout;
8using std::endl;
9using qi::string;
10using qi::char_;
11
12typedef qi::rule<std::string::const_iterator, std::string()> strrule_t;
13
14void test(const std::string input, strrule_t rule)
15{
16 std::string target;
17 std::string::const_iterator i = input.begin(), ie = input.end();
18
19 if (qi::parse(i, ie, rule, target))
20 {
21 cout << "Success: '" << target << "'" << endl;
22 }
23 else
24 {
25 cout << "Failed to match." << endl;
26 }
27}
28
29int main()
30{
31 strrule_t shortHexValue = repeat(4)[char_("a-fA-F0-9")];
32 strrule_t longHexValue = repeat(8)[char_("a-fA-F0-9")];
33 strrule_t hexValue1 = omit[string("0x")] >> ( longHexValue | shortHexValue );
34 strrule_t hexValue2 = omit[string("0x")] >> ( shortHexValue | longHexValue );
35
36 test( "0x123FAA22", hexValue1 );
37 test( "0x12AA", hexValue1 );
38 test( "0x123FAA22", hexValue2 );
39 test( "0x1A22", hexValue2 );
40
41 return 0;
42}