num_list2.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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_numlist2
  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. >> *(',' >> double_[push_back(phoenix::ref(v), _1)])
  45. )
  46. ,
  47. // End grammar
  48. space);
  49. if (first != last) // fail if we did not get a full match
  50. return false;
  51. return r;
  52. }
  53. //]
  54. }
  55. ////////////////////////////////////////////////////////////////////////////
  56. // Main program
  57. ////////////////////////////////////////////////////////////////////////////
  58. int
  59. main()
  60. {
  61. std::cout << "/////////////////////////////////////////////////////////\n\n";
  62. std::cout << "\t\tA comma separated list parser for Spirit...\n\n";
  63. std::cout << "/////////////////////////////////////////////////////////\n\n";
  64. std::cout << "Give me a comma separated list of numbers.\n";
  65. std::cout << "The numbers will be inserted in a vector of numbers\n";
  66. std::cout << "Type [q or Q] to quit\n\n";
  67. std::string str;
  68. while (getline(std::cin, str))
  69. {
  70. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  71. break;
  72. std::vector<double> v;
  73. if (client::parse_numbers(str.begin(), str.end(), v))
  74. {
  75. std::cout << "-------------------------\n";
  76. std::cout << "Parsing succeeded\n";
  77. std::cout << str << " Parses OK: " << std::endl;
  78. for (std::vector<double>::size_type i = 0; i < v.size(); ++i)
  79. std::cout << i << ": " << v[i] << std::endl;
  80. std::cout << "\n-------------------------\n";
  81. }
  82. else
  83. {
  84. std::cout << "-------------------------\n";
  85. std::cout << "Parsing failed\n";
  86. std::cout << "-------------------------\n";
  87. }
  88. }
  89. std::cout << "Bye... :-) \n\n";
  90. return 0;
  91. }