#include #include #include #include #include #include #include #include /////////////////////////////////////////////////////////////////////////////// using namespace std; using namespace boost::spirit; using namespace boost::spirit::classic; static const int charID = 2; static const int stringID = 3; typedef char CHARACTER; typedef CHARACTER const* iterator_t; //typedef position_iterator iterator_t; typedef tree_match< iterator_t > parse_tree_match_t; typedef parse_tree_match_t::tree_iterator iter_t; #define rule_t rule #define scanner_t ScannerT template void fillGroupData( IteratorT const & i, int const level ) { if (i->value.begin() != i->value.end()) { if (i->value.id() == charID ) { std::cout << "charID:" << std::string(i->value.begin(), i->value.end()) << std::endl; } else if (i->value.id() == stringID ) { std::cout << "stringID:" << std::string(i->value.begin(), i->value.end()) << std::endl; } else { std::cout << "unkown:" << std::string(i->value.begin(), i->value.end()) << std::endl; } } // parse the child nodes. IteratorT chi = i->children.begin(); for (; chi != i->children.end(); ++chi) { fillGroupData(chi, level+1); } } /// start the parsing of the date. template void fillDateDescriptionList( tree_parse_info< IteratorT, node_iter_data_factory<> >& info ) { std::cout << std::endl; fillGroupData( info.trees.begin(), 0 ); } struct ExpressionGrammar : public grammar { template struct definition { definition( ExpressionGrammar const& /*self*/) { //Char = // ch_p('a'); String = ( +( ch_p('a') )) ; // handle the separtion between the regex and the language // to create groups. language = String ; } rule, parser_tag > Char; rule, parser_tag > String; rule language; // start parsing of the time string. rule const& start() const { return language; } }; }; struct ExpressionNoStringIDGrammar : public grammar { template struct definition { definition( ExpressionNoStringIDGrammar const& /*self*/) { Char = ch_p('a') ; String = ( +Char ) ; language = String ; } rule, parser_tag > Char; rule, parser_tag > String; rule language; // start parsing of the time string. rule const& start() const { return language; } }; }; // parse the time template< class GRAMMAR > bool test_parser( CHARACTER const* beg, CHARACTER const* end ) { GRAMMAR grammar; tree_parse_info > info = ast_parse >( beg, end, ( // Begin grammar grammar ) // End grammar , nothing_p ); if ( info.full ) { fillDateDescriptionList( info); } return info.full; } //////////////////////////////////////////////////////////////////////////// // // Main program // //////////////////////////////////////////////////////////////////////////// int main() { cout << "TEST 1\n"; cout << "Whith \n"; cout << "String: 'a'\n"; cout << "Here I do NOT get a stringID because it only has one character !\n"; { string str("a"); if (test_parser(&str[0], &str[str.length()] )) cout << "Parses OK: " << endl; else cout << "!!! Parsing failed\n"; } cout << "-------------------------------------------------------\n"; cout << "TEST 2\n"; cout << "String: 'aa'\n"; cout << "Here it works, I do get a stringID\n"; { string str("aa"); if (test_parser(&str[0], &str[str.length()] )) cout << "Parses OK: " << endl; else cout << "!!! Parsing failed\n"; } cout << "-------------------------------------------------------\n"; cout << "TEST 3\n"; cout << "Whith \n"; cout << "Here I do get a string ID even when it's has one character !\n"; cout << "String: 'a'\n"; { string str("a"); if (test_parser(&str[0], &str[str.length()] )) cout << "Parses OK: " << endl; else cout << "!!! Parsing failed\n"; } cout << "-------------------------------------------------------\n"; cout << "TEST 4\n"; cout << "String: 'aa'\n"; cout << "Here I do get a string ID!\n"; { string str("aa"); if (test_parser(&str[0], &str[str.length()] )) cout << "Parses OK: " << endl; else cout << "!!! Parsing failed\n"; } return 0; }