1 // Copyright (c) 2001-2010 Hartmut Kaiser 2 // 3 // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5 6 /////////////////////////////////////////////////////////////////////////////// 7 // 8 // A character classification example 9 // 10 // [ HK August 12, 2009 ] spirit2 11 // 12 /////////////////////////////////////////////////////////////////////////////// 13 14 #include <boost/config/warning_disable.hpp> 15 #include <boost/spirit/include/qi.hpp> 16 #include <boost/spirit/include/karma.hpp> 17 #include <boost/spirit/include/phoenix_core.hpp> 18 #include <boost/spirit/include/phoenix_operator.hpp> 19 #include <boost/fusion/include/std_pair.hpp> 20 21 #include <iostream> 22 #include <string> 23 #include <complex> 24 25 namespace client 26 { 27 /////////////////////////////////////////////////////////////////////////// 28 // Our character classification generator 29 /////////////////////////////////////////////////////////////////////////// 30 //[tutorial_karma_complex_number 31 template <typename OutputIterator> classify_character(OutputIterator sink,char c)32 bool classify_character(OutputIterator sink, char c) 33 { 34 using boost::spirit::ascii::char_; 35 using boost::spirit::ascii::digit; 36 using boost::spirit::ascii::xdigit; 37 using boost::spirit::ascii::alpha; 38 using boost::spirit::ascii::punct; 39 using boost::spirit::ascii::space; 40 using boost::spirit::ascii::cntrl; 41 using boost::spirit::karma::omit; 42 using boost::spirit::karma::generate; 43 44 if (!boost::spirit::char_encoding::ascii::isascii_(c)) 45 return false; 46 47 return generate(sink, 48 // Begin grammar 49 ( 50 "The character '" << char_ << "' is " 51 << ( &digit << "a digit" 52 | &xdigit << "a xdigit" 53 | &alpha << "a alpha" 54 | &punct << "a punct" 55 | &space << "a space" 56 | &cntrl << "a cntrl" 57 | "of unknown type" 58 ) 59 ), 60 // End grammar 61 c, c 62 ); 63 } 64 //] 65 } 66 67 /////////////////////////////////////////////////////////////////////////////// 68 // Main program 69 /////////////////////////////////////////////////////////////////////////////// main()70int main() 71 { 72 std::cout << "/////////////////////////////////////////////////////////\n\n"; 73 std::cout << "\t\tA character classification micro generator for Spirit...\n\n"; 74 std::cout << "/////////////////////////////////////////////////////////\n\n"; 75 76 std::cout << "Give me a character to classify\n"; 77 std::cout << "Type [q or Q] to quit\n\n"; 78 79 std::string str; 80 while (getline(std::cin, str)) 81 { 82 if (str.empty()) 83 break; 84 85 std::string generated; 86 std::back_insert_iterator<std::string> sink(generated); 87 if (!client::classify_character(sink, str[0])) 88 { 89 std::cout << "-------------------------\n"; 90 std::cout << "Generating failed\n"; 91 std::cout << "-------------------------\n"; 92 } 93 else 94 { 95 std::cout << generated << "\n"; 96 } 97 } 98 99 std::cout << "Bye... :-) \n\n"; 100 return 0; 101 } 102 103 104