Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r65056 - trunk/libs/spirit/example/qi
From: hartmut.kaiser_at_[hidden]
Date: 2010-08-27 14:02:34


Author: hkaiser
Date: 2010-08-27 14:02:34 EDT (Fri, 27 Aug 2010)
New Revision: 65056
URL: http://svn.boost.org/trac/boost/changeset/65056

Log:
Spirit: adding example showing how to parse into a QString
Added:
   trunk/libs/spirit/example/qi/custom_string.cpp (contents, props changed)

Added: trunk/libs/spirit/example/qi/custom_string.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/qi/custom_string.cpp 2010-08-27 14:02:34 EDT (Fri, 27 Aug 2010)
@@ -0,0 +1,93 @@
+// Copyright (c) 2001-2010 Hartmut Kaiser
+//
+// Distributed under 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 example demonstrates the steps needed to integrate a custom container
+// type to be usable with Spirit.Qi. It shows how to utilize the QString data
+// type as a Qi attribute.
+
+// Note: you need to have Qt4 installed on your system and you have to make
+// sure your compiler finds the includes and your linker finds the
+// proper libraries.
+
+#include <boost/config/warning_disable.hpp>
+#include <boost/spirit/include/qi.hpp>
+
+#include <Qt/qstring.h>
+
+namespace boost { namespace spirit { namespace traits
+{
+ // make Qi recognize QString as a container
+ template <> struct is_container<QString> : mpl::true_ {};
+
+ // Expose QStrings value_type
+ template <> struct container_value<QString> : mpl::identity<QChar> {};
+
+ // Define, how to insert a new element at the end of the container
+ template <>
+ struct push_back_container<QString, QChar>
+ {
+ static bool call(QString& c, QChar const& val)
+ {
+ c.append(val);
+ return true;
+ }
+ };
+}}}
+
+///////////////////////////////////////////////////////////////////////////////
+namespace client
+{
+ template <typename Iterator>
+ bool parse_qstring(Iterator first, Iterator last, QString& t)
+ {
+ using boost::spirit::qi::char_;
+ using boost::spirit::ascii::space;
+ using boost::spirit::qi::phrase_parse;
+
+ bool r = phrase_parse(first, last, +char_, space, t);
+ if (!r || first != last) // fail if we did not get a full match
+ return false;
+
+ return r;
+ }
+}
+
+///////////////////////////////////////////////////////////////////////////////
+int main()
+{
+ std::cout << "/////////////////////////////////////////////////////////\n\n";
+ std::cout << "\t\tParsing into a QString from Spirit...\n\n";
+ std::cout << "/////////////////////////////////////////////////////////\n\n";
+
+ std::cout << "Give me a complex number of the form r or (r) or (r,i) \n";
+ std::cout << "Type [q or Q] to quit\n\n";
+
+ std::string str;
+ while (getline(std::cin, str))
+ {
+ if (str.empty() || str[0] == 'q' || str[0] == 'Q')
+ break;
+
+ QString t;
+ if (client::parse_qstring(str.begin(), str.end(), t))
+ {
+ std::cout << "-------------------------\n";
+ std::cout << "Parsing succeeded\n";
+ std::cout << "got: " << t.toStdString() << std::endl;
+ std::cout << "\n-------------------------\n";
+ }
+ else
+ {
+ std::cout << "-------------------------\n";
+ std::cout << "Parsing failed\n";
+ std::cout << "-------------------------\n";
+ }
+ }
+
+ std::cout << "Bye... :-) \n\n";
+ return 0;
+}
+
+


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk