num_list3.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. namespace phoenix = boost::phoenix;
  28. ///////////////////////////////////////////////////////////////////////////////
  29. // Our number list compiler
  30. ///////////////////////////////////////////////////////////////////////////////
  31. //[tutorial_numlist3
  32. template <typename Iterator>
  33. bool parse_numbers(Iterator first, Iterator last, std::vector<double>& v)
  34. {
  35. using qi::double_;
  36. using qi::phrase_parse;
  37. using qi::_1;
  38. using ascii::space;
  39. using phoenix::push_back;
  40. bool r = phrase_parse(first, last,
  41. // Begin grammar
  42. (
  43. double_[push_back(phoenix::ref(v), _1)] % ','
  44. )
  45. ,
  46. // End grammar
  47. space);
  48. if (first != last) // fail if we did not get a full match
  49. return false;
  50. return r;
  51. }
  52. //]
  53. }
  54. ////////////////////////////////////////////////////////////////////////////
  55. // Main program
  56. ////////////////////////////////////////////////////////////////////////////
  57. int
  58. main()
  59. {
  60. std::cout << "/////////////////////////////////////////////////////////\n\n";
  61. std::cout << "\t\tA comma separated list parser for Spirit...\n\n";
  62. std::cout << "/////////////////////////////////////////////////////////\n\n";
  63. std::cout << "Give me a comma separated list of numbers.\n";
  64. std::cout << "The numbers will be inserted in a vector of numbers\n";
  65. std::cout << "Type [q or Q] to quit\n\n";
  66. std::string str;
  67. while (getline(std::cin, str))
  68. {
  69. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  70. break;
  71. std::vector<double> v;
  72. if (client::parse_numbers(str.begin(), str.end(), v))
  73. {
  74. std::cout << "-------------------------\n";
  75. std::cout << "Parsing succeeded\n";
  76. std::cout << str << " Parses OK: " << std::endl;
  77. for (std::vector<double>::size_type i = 0; i < v.size(); ++i)
  78. std::cout << i << ": " << v[i] << std::endl;
  79. std::cout << "\n-------------------------\n";
  80. }
  81. else
  82. {
  83. std::cout << "-------------------------\n";
  84. std::cout << "Parsing failed\n";
  85. std::cout << "-------------------------\n";
  86. }
  87. }
  88. std::cout << "Bye... :-) \n\n";
  89. return 0;
  90. }