• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2002-2003 Joel de Guzman
3     Copyright (c) 2002 Juan Carlos Arevalo-Baeza
4     http://spirit.sourceforge.net/
5 
6     Use, modification and distribution is subject to the Boost Software
7     License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8     http://www.boost.org/LICENSE_1_0.txt)
9 =============================================================================*/
10 #include <boost/spirit/include/classic_core.hpp>
11 #include <boost/spirit/include/classic_functor_parser.hpp>
12 #include <boost/spirit/include/classic_assign_actor.hpp>
13 #include <iostream>
14 #include <vector>
15 #include <string>
16 
17 ///////////////////////////////////////////////////////////////////////////////
18 //
19 //  Demonstrates the functor_parser. This is discussed in the
20 //  "Functor Parser" chapter in the Spirit User's Guide.
21 //
22 ///////////////////////////////////////////////////////////////////////////////
23 using namespace std;
24 using namespace BOOST_SPIRIT_CLASSIC_NS;
25 
26 ///////////////////////////////////////////////////////////////////////////////
27 //
28 //  Our parser functor
29 //
30 ///////////////////////////////////////////////////////////////////////////////
31 struct number_parser
32 {
33     typedef int result_t;
34     template <typename ScannerT>
35     int
operator ()number_parser36     operator()(ScannerT const& scan, result_t& result) const
37     {
38         if (scan.at_end())
39             return -1;
40 
41         char ch = *scan;
42         if (ch < '0' || ch > '9')
43             return -1;
44 
45         result = 0;
46         int len = 0;
47 
48         do
49         {
50             result = result*10 + int(ch - '0');
51             ++len;
52             ++scan;
53         } while (!scan.at_end() && (ch = *scan, ch >= '0' && ch <= '9'));
54 
55         return len;
56     }
57 };
58 
59 functor_parser<number_parser> number_parser_p;
60 
61 ///////////////////////////////////////////////////////////////////////////////
62 //
63 //  Our number parser functions
64 //
65 ///////////////////////////////////////////////////////////////////////////////
66 bool
parse_number(char const * str,int & n)67 parse_number(char const* str, int& n)
68 {
69     return parse(str, lexeme_d[number_parser_p[assign_a(n)]], space_p).full;
70 }
71 
72 bool
parse_numbers(char const * str,std::vector<int> & n)73 parse_numbers(char const* str, std::vector<int>& n)
74 {
75     return
76         parse(
77             str,
78             lexeme_d[number_parser_p[push_back_a(n)]]
79                 >> *(',' >> lexeme_d[number_parser_p[push_back_a(n)]]),
80             space_p
81         ).full;
82 }
83 
84 ////////////////////////////////////////////////////////////////////////////
85 //
86 //  Main program
87 //
88 ////////////////////////////////////////////////////////////////////////////
89 int
main()90 main()
91 {
92     cout << "/////////////////////////////////////////////////////////\n\n";
93     cout << "\t\tA number parser implemented as a functor for Spirit...\n\n";
94     cout << "/////////////////////////////////////////////////////////\n\n";
95 
96     cout << "Give me an integer number command\n";
97     cout << "Commands:\n";
98     cout << "  A <num> --> parses a single number\n";
99     cout << "  B <num>, <num>, ... --> parses a series of numbers ";
100     cout << "separated by commas\n";
101     cout << "  Q --> quit\n\n";
102 
103     string str;
104     while (getline(cin, str))
105     {
106         if (str.empty() || str[0] == 'q' || str[0] == 'Q')
107             break;
108 
109         else if (str[0] == 'a' || str[0] == 'A')
110         {
111             int n;
112             if (parse_number(str.c_str()+1, n))
113             {
114                 cout << "-------------------------\n";
115                 cout << "Parsing succeeded\n";
116                 cout << str << " Parses OK: " << n << endl;
117                 cout << "-------------------------\n";
118             }
119             else
120             {
121                 cout << "-------------------------\n";
122                 cout << "Parsing failed\n";
123                 cout << "-------------------------\n";
124             }
125         }
126 
127         else if (str[0] == 'b' || str[0] == 'B')
128         {
129             std::vector<int> n;
130             if (parse_numbers(str.c_str()+1, n))
131             {
132                 cout << "-------------------------\n";
133                 cout << "Parsing succeeded\n";
134                 int size = n.size();
135                 cout << str << " Parses OK: " << size << " number(s): " << n[0];
136                 for (int i = 1; i < size; ++i) {
137                     cout << ", " << n[i];
138                 }
139                 cout << endl;
140                 cout << "-------------------------\n";
141             }
142             else
143             {
144                 cout << "-------------------------\n";
145                 cout << "Parsing failed\n";
146                 cout << "-------------------------\n";
147             }
148         }
149 
150         else
151         {
152             cout << "-------------------------\n";
153             cout << "Unrecognized command!!";
154             cout << "-------------------------\n";
155         }
156     }
157 
158     cout << "Bye... :-) \n\n";
159     return 0;
160 }
161