• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2002-2010 Joel de Guzman
3 
4     Distributed under the Boost Software License, Version 1.0. (See accompanying
5     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 =============================================================================*/
7 ///////////////////////////////////////////////////////////////////////////////
8 //
9 //  This sample demontrates a parser for a comma separated list of numbers.
10 //  No actions.
11 //
12 //  [ JDG May 10, 2002 ]    spirit1
13 //  [ JDG March 24, 2007 ]  spirit2
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16 
17 #include <boost/config/warning_disable.hpp>
18 #include <boost/spirit/include/qi.hpp>
19 
20 #include <iostream>
21 #include <string>
22 #include <vector>
23 
24 namespace client
25 {
26     namespace qi = boost::spirit::qi;
27     namespace ascii = boost::spirit::ascii;
28 
29     ///////////////////////////////////////////////////////////////////////////
30     //  Our number list parser
31     ///////////////////////////////////////////////////////////////////////////
32     //[tutorial_numlist1
33     template <typename Iterator>
parse_numbers(Iterator first,Iterator last)34     bool parse_numbers(Iterator first, Iterator last)
35     {
36         using qi::double_;
37         using qi::phrase_parse;
38         using ascii::space;
39 
40         bool r = phrase_parse(
41             first,                          /*< start iterator >*/
42             last,                           /*< end iterator >*/
43             double_ >> *(',' >> double_),   /*< the parser >*/
44             space                           /*< the skip-parser >*/
45         );
46         if (first != last) // fail if we did not get a full match
47             return false;
48         return r;
49     }
50     //]
51 }
52 
53 ////////////////////////////////////////////////////////////////////////////
54 //  Main program
55 ////////////////////////////////////////////////////////////////////////////
56 int
main()57 main()
58 {
59     std::cout << "/////////////////////////////////////////////////////////\n\n";
60     std::cout << "\t\tA comma separated list parser for Spirit...\n\n";
61     std::cout << "/////////////////////////////////////////////////////////\n\n";
62 
63     std::cout << "Give me a comma separated list of numbers.\n";
64     std::cout << "Type [q or Q] to quit\n\n";
65 
66     std::string str;
67     while (getline(std::cin, str))
68     {
69         if (str.empty() || str[0] == 'q' || str[0] == 'Q')
70             break;
71 
72         if (client::parse_numbers(str.begin(), str.end()))
73         {
74             std::cout << "-------------------------\n";
75             std::cout << "Parsing succeeded\n";
76             std::cout << str << " Parses OK: " << std::endl;
77         }
78         else
79         {
80             std::cout << "-------------------------\n";
81             std::cout << "Parsing failed\n";
82             std::cout << "-------------------------\n";
83         }
84     }
85 
86     std::cout << "Bye... :-) \n\n";
87     return 0;
88 }
89 
90 
91