num_list4.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*=============================================================================
  2. Copyright (c) 2002-2010 Joel de Guzman
  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. // This sample demontrates a parser for a comma separated list of numbers.
  9. // The numbers are inserted in a vector using phoenix.
  10. //
  11. // [ JDG May 10, 2002 ] spirit1
  12. // [ JDG March 24, 2007 ] spirit2
  13. //
  14. ///////////////////////////////////////////////////////////////////////////////
  15. #include <boost/config/warning_disable.hpp>
  16. #include <boost/spirit/include/qi.hpp>
  17. #include <boost/spirit/include/phoenix_core.hpp>
  18. #include <boost/spirit/include/phoenix_operator.hpp>
  19. #include <boost/spirit/include/phoenix_stl.hpp>
  20. #include <iostream>
  21. #include <string>
  22. #include <vector>
  23. namespace client
  24. {
  25. namespace qi = boost::spirit::qi;
  26. namespace ascii = boost::spirit::ascii;
  27. ///////////////////////////////////////////////////////////////////////////
  28. // Our number list compiler
  29. ///////////////////////////////////////////////////////////////////////////
  30. //[tutorial_numlist4
  31. template <typename Iterator>
  32. bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v)
  33. {
  34. using qi::double_;
  35. using qi::phrase_parse;
  36. using qi::_1;
  37. using ascii::space;
  38. bool r = phrase_parse(first, last,
  39. // Begin grammar
  40. (
  41. double_ % ','
  42. )
  43. ,
  44. // End grammar
  45. space, v);
  46. if (first != last) // fail if we did not get a full match
  47. return false;
  48. return r;
  49. }
  50. //]
  51. }
  52. ////////////////////////////////////////////////////////////////////////////
  53. // Main program
  54. ////////////////////////////////////////////////////////////////////////////
  55. int
  56. main()
  57. {
  58. std::cout << "/////////////////////////////////////////////////////////\n\n";
  59. std::cout << "\t\tA comma separated list parser for Spirit...\n\n";
  60. std::cout << "/////////////////////////////////////////////////////////\n\n";
  61. std::cout << "Give me a comma separated list of numbers.\n";
  62. std::cout << "The numbers will be inserted in a vector of numbers\n";
  63. std::cout << "Type [q or Q] to quit\n\n";
  64. std::string str;
  65. while (getline(std::cin, str))
  66. {
  67. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  68. break;
  69. std::vector<double> v;
  70. if (client::parse_numbers(str.begin(), str.end(), v))
  71. {
  72. std::cout << "-------------------------\n";
  73. std::cout << "Parsing succeeded\n";
  74. std::cout << str << " Parses OK: " << std::endl;
  75. for (std::vector<double>::size_type i = 0; i < v.size(); ++i)
  76. std::cout << i << ": " << v[i] << std::endl;
  77. std::cout << "\n-------------------------\n";
  78. }
  79. else
  80. {
  81. std::cout << "-------------------------\n";
  82. std::cout << "Parsing failed\n";
  83. std::cout << "-------------------------\n";
  84. }
  85. }
  86. std::cout << "Bye... :-) \n\n";
  87. return 0;
  88. }