123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- /*=============================================================================
- Copyright (c) 2002-2003 Joel de Guzman
- http://spirit.sourceforge.net/
- Use, modification and distribution is subject to the Boost Software
- License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
- http://www.boost.org/LICENSE_1_0.txt)
- =============================================================================*/
- ///////////////////////////////////////////////////////////////////////////////
- //
- // This sample demontrates a parser for a comma separated list of identifiers
- // This is a variation of stuff_vector.cpp.
- // This is discussed in the "Phoenix" chapter in the Spirit User's Guide.
- //
- // [ JDG 1/12/2004 ]
- //
- ///////////////////////////////////////////////////////////////////////////////
- #include <boost/spirit/include/classic_core.hpp>
- #include <boost/spirit/include/classic_operators.hpp>
- #include <boost/spirit/include/phoenix1_functions.hpp>
- #include <boost/spirit/include/phoenix1_primitives.hpp>
- #include <boost/spirit/include/phoenix1_casts.hpp>
- #include <iostream>
- #include <string>
- ///////////////////////////////////////////////////////////////////////////////
- using namespace std;
- using namespace BOOST_SPIRIT_CLASSIC_NS;
- using namespace phoenix;
- ///////////////////////////////////////////////////////////////////////////////
- //
- // Our comma separated list parser
- //
- ///////////////////////////////////////////////////////////////////////////////
- struct push_back_impl
- {
- template <typename Container, typename Item>
- struct result
- {
- typedef void type;
- };
- template <typename Container, typename Item>
- void operator()(Container& c, Item const& item) const
- {
- c.push_back(item);
- }
- };
- function<push_back_impl> const push_back = push_back_impl();
- bool
- parse_identifiers(char const* str, vector<std::string>& v)
- {
- return parse(str,
- // Begin grammar
- (
- (+alpha_p)
- [
- push_back(var(v), construct_<std::string>(arg1, arg2))
- ]
- >>
- *(',' >>
- (+alpha_p)
- [
- push_back(var(v), construct_<std::string>(arg1, arg2))
- ]
- )
- )
- ,
- // End grammar
- space_p).full;
- }
- ////////////////////////////////////////////////////////////////////////////
- //
- // Main program
- //
- ////////////////////////////////////////////////////////////////////////////
- int
- main()
- {
- cout << "/////////////////////////////////////////////////////////\n\n";
- cout << "\t\tA comma separated list parser for Spirit...\n\n";
- cout << "/////////////////////////////////////////////////////////\n\n";
- cout << "Give me a comma separated list of identifiers.\n";
- cout << "An identifier is comprised of one or more alphabetic characters.\n";
- cout << "The identifiers will be inserted in a vector of numbers\n";
- cout << "Type [q or Q] to quit\n\n";
- string str;
- while (getline(cin, str))
- {
- if (str.empty() || str[0] == 'q' || str[0] == 'Q')
- break;
- vector<std::string> v;
- if (parse_identifiers(str.c_str(), v))
- {
- cout << "-------------------------\n";
- cout << "Parsing succeeded\n";
- cout << str << " Parses OK: " << endl;
- for (vector<std::string>::size_type i = 0; i < v.size(); ++i)
- cout << i << ": " << v[i] << endl;
- cout << "-------------------------\n";
- }
- else
- {
- cout << "-------------------------\n";
- cout << "Parsing failed\n";
- cout << "-------------------------\n";
- }
- }
- cout << "Bye... :-) \n\n";
- return 0;
- }
|