Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r57646 - trunk/libs/spirit/example/qi
From: hartmut.kaiser_at_[hidden]
Date: 2009-11-14 08:54:22


Author: hkaiser
Date: 2009-11-14 08:54:22 EST (Sat, 14 Nov 2009)
New Revision: 57646
URL: http://svn.boost.org/trac/boost/changeset/57646

Log:
Spirit: added new Qi example
Added:
   trunk/libs/spirit/example/qi/key_value_sequence.cpp (contents, props changed)
Text files modified:
   trunk/libs/spirit/example/qi/Jamfile | 1 +
   1 files changed, 1 insertions(+), 0 deletions(-)

Modified: trunk/libs/spirit/example/qi/Jamfile
==============================================================================
--- trunk/libs/spirit/example/qi/Jamfile (original)
+++ trunk/libs/spirit/example/qi/Jamfile 2009-11-14 08:54:22 EST (Sat, 14 Nov 2009)
@@ -25,6 +25,7 @@
 exe num_list4 : num_list4.cpp ;
 exe reorder_struct : reorder_struct.cpp ;
 exe parse_date : parse_date.cpp ;
+exe key_value_sequence : key_value_sequence.cpp ;
 
 exe calculator1 : calc1.cpp ;
 exe calculator2 : calc2.cpp ;

Added: trunk/libs/spirit/example/qi/key_value_sequence.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/qi/key_value_sequence.cpp 2009-11-14 08:54:22 EST (Sat, 14 Nov 2009)
@@ -0,0 +1,71 @@
+// Copyright (c) 2001-2009 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)
+
+#include <boost/spirit/include/qi.hpp>
+#include <boost/fusion/include/std_pair.hpp>
+
+#include <iostream>
+#include <map>
+
+namespace client
+{
+ namespace spirit = boost::spirit;
+ namespace qi = boost::spirit::qi;
+
+ template <typename Iterator>
+ struct key_value_sequence
+ : qi::grammar<Iterator, std::map<std::string, std::string>()>
+ {
+ key_value_sequence()
+ : key_value_sequence::base_type(query)
+ {
+ query = pair >> *((qi::lit(';') | '&') >> pair);
+ pair = key >> -('=' >> value);
+ key = qi::char_("a-zA-Z_") >> *qi::char_("a-zA-Z_0-9");
+ value = +qi::char_("a-zA-Z_0-9");
+ }
+
+ qi::rule<Iterator, std::map<std::string, std::string>()> query;
+ qi::rule<Iterator, std::pair<std::string, std::string>()> pair;
+ qi::rule<Iterator, std::string()> key, value;
+ };
+}
+
+///////////////////////////////////////////////////////////////////////////////
+int main()
+{
+ namespace qi = boost::spirit::qi;
+
+ std::string input("key1=value1;key2;key3=value3");
+ std::string::iterator begin = input.begin();
+ std::string::iterator end = input.end();
+
+ client::key_value_sequence<std::string::iterator> g;
+ std::map<std::string, std::string> m;
+
+ if (!qi::parse(begin, end, g, m))
+ {
+ std::cout << "-------------------------------- \n";
+ std::cout << "Parsing failed\n";
+ std::cout << "-------------------------------- \n";
+ }
+ else
+ {
+ std::cout << "-------------------------------- \n";
+ std::cout << "Parsing succeeded, found entries:\n";
+ std::map<std::string, std::string>::iterator end = m.end();
+ for (std::map<std::string, std::string>::iterator it = m.begin();
+ it != end; ++it)
+ {
+ std::cout << (*it).first;
+ if (!(*it).second.empty())
+ std::cout << "=" << (*it).second;
+ std::cout << std::endl;
+ }
+ std::cout << "---------------------------------\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