]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/spirit/classic/example/fundamental/position_iterator/position_iterator.cpp
1 /*=============================================================================
2 Copyright (c) 2002 Juan Carlos Arevalo-Baeza
3 http://spirit.sourceforge.net/
5 Use, modification and distribution is subject to the Boost Software
6 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 ///////////////////////////////////////////////////////////////////////////////
11 // A parser for a comma separated list of numbers,
12 // with positional error reporting
13 // See the "Position Iterator" chapter in the User's Guide.
17 ///////////////////////////////////////////////////////////////////////////////
18 #include <boost/spirit/include/classic_core.hpp>
19 #include <boost/spirit/include/classic_position_iterator.hpp>
20 #include <boost/spirit/include/classic_functor_parser.hpp>
25 ///////////////////////////////////////////////////////////////////////////////
27 using namespace BOOST_SPIRIT_CLASSIC_NS
;
29 ///////////////////////////////////////////////////////////////////////////////
31 // Our error reporting parsers
33 ///////////////////////////////////////////////////////////////////////////////
34 std::ostream
& operator<<(std::ostream
& out
, file_position
const& lc
)
37 "\nFile:\t" << lc
.file
<<
38 "\nLine:\t" << lc
.line
<<
39 "\nCol:\t" << lc
.column
<< endl
;
42 struct error_report_parser
{
46 error_report_parser(char const* eol_msg_
, char const* msg_
):
51 typedef nil_t result_t
;
53 template <typename ScannerT
>
55 operator()(ScannerT
const& scan
, result_t
& /*result*/) const
59 file_position fpos
= scan
.first
.get_position();
60 cerr
<< fpos
<< eol_msg
<< endl
;
64 file_position fpos
= scan
.first
.get_position();
65 cerr
<< fpos
<< msg
<< endl
;
73 typedef functor_parser
<error_report_parser
> error_report_p
;
76 error_badnumber_or_eol
=
78 "Expecting a number, but found the end of the file\n",
79 "Expecting a number, but found something else\n"
86 "Expecting a number, but found something else\n"
93 "Expecting a comma, but found something else\n"
96 ///////////////////////////////////////////////////////////////////////////////
98 // Our comma separated list parser
100 ///////////////////////////////////////////////////////////////////////////////
102 parse_numbers(char const* filename
, char const* str
, vector
<double>& v
)
104 typedef position_iterator
<char const*> iterator_t
;
105 iterator_t
begin(str
, str
+ strlen(str
), filename
);
107 begin
.set_tabchars(8);
108 return parse(begin
, end
,
112 (real_p
[push_back_a(v
)] | error_badnumber
)
115 >> (real_p
[push_back_a(v
)] | error_badnumber_or_eol
)
124 ////////////////////////////////////////////////////////////////////////////
128 ////////////////////////////////////////////////////////////////////////////
130 main(int argc
, char **argv
)
132 cout
<< "/////////////////////////////////////////////////////////\n\n";
133 cout
<< "\tAn error-reporting parser for Spirit...\n\n";
134 cout
<< "Parses a comma separated list of numbers from a file.\n";
135 cout
<< "The numbers will be inserted in a vector of numbers\n\n";
136 cout
<< "/////////////////////////////////////////////////////////\n\n";
139 char const* filename
;
143 ifstream
file(filename
);
144 file
.get(str
, sizeof(str
), '\0');
147 cin
.get(str
, sizeof(str
), '\0');
151 if (parse_numbers(filename
, str
, v
))
153 cout
<< "-------------------------\n";
154 cout
<< "Parsing succeeded\n";
155 cout
<< str
<< " Parses OK: " << endl
;
157 for (vector
<double>::size_type i
= 0; i
< v
.size(); ++i
)
158 cout
<< i
<< ": " << v
[i
] << endl
;
160 cout
<< "-------------------------\n";
164 cout
<< "-------------------------\n";
165 cout
<< "Parsing failed\n";
166 cout
<< "-------------------------\n";
169 cout
<< "Bye... :-) \n\n";