num_list1.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*=============================================================================
  2. Copyright (c) 2002-2015 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 semantic actions.
  10. //
  11. // [ JDG May 10, 2002 ] spirit1
  12. // [ JDG March 24, 2007 ] spirit2
  13. // [ JDG May 12, 2015 ] spirit X3
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #include <boost/config/warning_disable.hpp>
  17. #include <boost/spirit/home/x3.hpp>
  18. #include <iostream>
  19. #include <string>
  20. #include <vector>
  21. namespace client
  22. {
  23. namespace x3 = boost::spirit::x3;
  24. namespace ascii = boost::spirit::x3::ascii;
  25. ///////////////////////////////////////////////////////////////////////////
  26. // Our number list parser
  27. ///////////////////////////////////////////////////////////////////////////
  28. template <typename Iterator>
  29. bool parse_numbers(Iterator first, Iterator last)
  30. {
  31. using x3::double_;
  32. using x3::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. // Main program
  47. ////////////////////////////////////////////////////////////////////////////
  48. int
  49. main()
  50. {
  51. std::cout << "/////////////////////////////////////////////////////////\n\n";
  52. std::cout << "\t\tA comma separated list parser for Spirit...\n\n";
  53. std::cout << "/////////////////////////////////////////////////////////\n\n";
  54. std::cout << "Give me a comma separated list of numbers.\n";
  55. std::cout << "Type [q or Q] to quit\n\n";
  56. std::string str;
  57. while (getline(std::cin, str))
  58. {
  59. if (str.empty() || str[0] == 'q' || str[0] == 'Q')
  60. break;
  61. if (client::parse_numbers(str.begin(), str.end()))
  62. {
  63. std::cout << "-------------------------\n";
  64. std::cout << "Parsing succeeded\n";
  65. std::cout << str << " Parses OK: " << std::endl;
  66. }
  67. else
  68. {
  69. std::cout << "-------------------------\n";
  70. std::cout << "Parsing failed\n";
  71. std::cout << "-------------------------\n";
  72. }
  73. }
  74. std::cout << "Bye... :-) \n\n";
  75. return 0;
  76. }