classify_char.cpp 3.2 KB

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