num_list1.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. // No actions.
  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 <iostream>
  18. #include <string>
  19. #include <vector>
  20. namespace client
  21. {
  22. namespace qi = boost::spirit::qi;
  23. namespace ascii = boost::spirit::ascii;
  24. ///////////////////////////////////////////////////////////////////////////
  25. // Our number list parser
  26. ///////////////////////////////////////////////////////////////////////////
  27. //[tutorial_numlist1
  28. template <typename Iterator>
  29. bool parse_numbers(Iterator first, Iterator last)
  30. {
  31. using qi::double_;
  32. using qi::phrase_parse;
  33. using ascii::space;
  34. bool r = phrase_parse(
  35. first, /*< start iterator >*/
  36. last, /*< end iterator >*/
  37. double_ >> *(',' >> double_), /*< the parser >*/
  38. space /*< the skip-parser >*/
  39. );
  40. if (first != last) // fail if we did not get a full match
  41. return false;
  42. return r;
  43. }
  44. //]
  45. }
  46. ////////////////////////////////////////////////////////////////////////////
  47. // Main program
  48. ////////////////////////////////////////////////////////////////////////////
  49. int
  50. main()
  51. {
  52. std::cout << "/////////////////////////////////////////////////////////\n\n";
  53. std::cout << "\t\tA comma separated list parser for Spirit...\n\n";
  54. std::cout << "/////////////////////////////////////////////////////////\n\n";
  55. std::cout << "Give me a comma separated list of numbers.\n";
  56. std::cout << "Type [q or Q] to quit\n\n";
  57. std::string str;
  58. while (getline(std::cin, str))
  59. {
  60. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  61. break;
  62. if (client::parse_numbers(str.begin(), str.end()))
  63. {
  64. std::cout << "-------------------------\n";
  65. std::cout << "Parsing succeeded\n";
  66. std::cout << str << " Parses OK: " << std::endl;
  67. }
  68. else
  69. {
  70. std::cout << "-------------------------\n";
  71. std::cout << "Parsing failed\n";
  72. std::cout << "-------------------------\n";
  73. }
  74. }
  75. std::cout << "Bye... :-) \n\n";
  76. return 0;
  77. }