functor_parser.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*=============================================================================
  2. Copyright (c) 2002-2003 Joel de Guzman
  3. Copyright (c) 2002 Juan Carlos Arevalo-Baeza
  4. http://spirit.sourceforge.net/
  5. Use, modification and distribution is subject to the Boost Software
  6. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. http://www.boost.org/LICENSE_1_0.txt)
  8. =============================================================================*/
  9. #include <boost/spirit/include/classic_core.hpp>
  10. #include <boost/spirit/include/classic_functor_parser.hpp>
  11. #include <boost/spirit/include/classic_assign_actor.hpp>
  12. #include <iostream>
  13. #include <vector>
  14. #include <string>
  15. ///////////////////////////////////////////////////////////////////////////////
  16. //
  17. // Demonstrates the functor_parser. This is discussed in the
  18. // "Functor Parser" chapter in the Spirit User's Guide.
  19. //
  20. ///////////////////////////////////////////////////////////////////////////////
  21. using namespace std;
  22. using namespace BOOST_SPIRIT_CLASSIC_NS;
  23. ///////////////////////////////////////////////////////////////////////////////
  24. //
  25. // Our parser functor
  26. //
  27. ///////////////////////////////////////////////////////////////////////////////
  28. struct number_parser
  29. {
  30. typedef int result_t;
  31. template <typename ScannerT>
  32. int
  33. operator()(ScannerT const& scan, result_t& result) const
  34. {
  35. if (scan.at_end())
  36. return -1;
  37. char ch = *scan;
  38. if (ch < '0' || ch > '9')
  39. return -1;
  40. result = 0;
  41. int len = 0;
  42. do
  43. {
  44. result = result*10 + int(ch - '0');
  45. ++len;
  46. ++scan;
  47. } while (!scan.at_end() && (ch = *scan, ch >= '0' && ch <= '9'));
  48. return len;
  49. }
  50. };
  51. functor_parser<number_parser> number_parser_p;
  52. ///////////////////////////////////////////////////////////////////////////////
  53. //
  54. // Our number parser functions
  55. //
  56. ///////////////////////////////////////////////////////////////////////////////
  57. bool
  58. parse_number(char const* str, int& n)
  59. {
  60. return parse(str, lexeme_d[number_parser_p[assign_a(n)]], space_p).full;
  61. }
  62. bool
  63. parse_numbers(char const* str, std::vector<int>& n)
  64. {
  65. return
  66. parse(
  67. str,
  68. lexeme_d[number_parser_p[push_back_a(n)]]
  69. >> *(',' >> lexeme_d[number_parser_p[push_back_a(n)]]),
  70. space_p
  71. ).full;
  72. }
  73. ////////////////////////////////////////////////////////////////////////////
  74. //
  75. // Main program
  76. //
  77. ////////////////////////////////////////////////////////////////////////////
  78. int
  79. main()
  80. {
  81. cout << "/////////////////////////////////////////////////////////\n\n";
  82. cout << "\t\tA number parser implemented as a functor for Spirit...\n\n";
  83. cout << "/////////////////////////////////////////////////////////\n\n";
  84. cout << "Give me an integer number command\n";
  85. cout << "Commands:\n";
  86. cout << " A <num> --> parses a single number\n";
  87. cout << " B <num>, <num>, ... --> parses a series of numbers ";
  88. cout << "separated by commas\n";
  89. cout << " Q --> quit\n\n";
  90. string str;
  91. while (getline(cin, str))
  92. {
  93. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  94. break;
  95. else if (str[0] == 'a' || str[0] == 'A')
  96. {
  97. int n;
  98. if (parse_number(str.c_str()+1, n))
  99. {
  100. cout << "-------------------------\n";
  101. cout << "Parsing succeeded\n";
  102. cout << str << " Parses OK: " << n << endl;
  103. cout << "-------------------------\n";
  104. }
  105. else
  106. {
  107. cout << "-------------------------\n";
  108. cout << "Parsing failed\n";
  109. cout << "-------------------------\n";
  110. }
  111. }
  112. else if (str[0] == 'b' || str[0] == 'B')
  113. {
  114. std::vector<int> n;
  115. if (parse_numbers(str.c_str()+1, n))
  116. {
  117. cout << "-------------------------\n";
  118. cout << "Parsing succeeded\n";
  119. int size = n.size();
  120. cout << str << " Parses OK: " << size << " number(s): " << n[0];
  121. for (int i = 1; i < size; ++i) {
  122. cout << ", " << n[i];
  123. }
  124. cout << endl;
  125. cout << "-------------------------\n";
  126. }
  127. else
  128. {
  129. cout << "-------------------------\n";
  130. cout << "Parsing failed\n";
  131. cout << "-------------------------\n";
  132. }
  133. }
  134. else
  135. {
  136. cout << "-------------------------\n";
  137. cout << "Unrecognized command!!";
  138. cout << "-------------------------\n";
  139. }
  140. }
  141. cout << "Bye... :-) \n\n";
  142. return 0;
  143. }