Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r57706 - in trunk/libs/spirit/example: karma qi
From: hartmut.kaiser_at_[hidden]
Date: 2009-11-16 11:28:08


Author: hkaiser
Date: 2009-11-16 11:28:07 EST (Mon, 16 Nov 2009)
New Revision: 57706
URL: http://svn.boost.org/trac/boost/changeset/57706

Log:
Spirit: added two more Qi examples
Added:
   trunk/libs/spirit/example/qi/key_value_sequence_empty_value.cpp (contents, props changed)
   trunk/libs/spirit/example/qi/key_value_sequence_ordered.cpp (contents, props changed)
Text files modified:
   trunk/libs/spirit/example/karma/key_value_sequence.cpp | 20 ++++++++------------
   trunk/libs/spirit/example/qi/Jamfile | 3 +++
   trunk/libs/spirit/example/qi/key_value_sequence.cpp | 13 +++++++------
   3 files changed, 18 insertions(+), 18 deletions(-)

Modified: trunk/libs/spirit/example/karma/key_value_sequence.cpp
==============================================================================
--- trunk/libs/spirit/example/karma/key_value_sequence.cpp (original)
+++ trunk/libs/spirit/example/karma/key_value_sequence.cpp 2009-11-16 11:28:07 EST (Mon, 16 Nov 2009)
@@ -19,30 +19,26 @@
 
 namespace client
 {
- namespace spirit = boost::spirit;
- namespace ascii = boost::spirit::ascii;
+ namespace karma = boost::spirit::karma;
     namespace phoenix = boost::phoenix;
 
     template <typename OutputIterator>
     bool key_value_sequence(OutputIterator sink
       , std::map<std::string, std::string> const& m)
     {
- using spirit::karma::generate;
- using spirit::karma::eps;
- using spirit::karma::omit;
- using spirit::karma::rule;
- using spirit::karma::_1;
- using spirit::karma::_val;
- using ascii::string;
+ using karma::eps;
+ using karma::omit;
+ using karma::_val;
+ using karma::string;
 
- rule<OutputIterator, std::pair<std::string const, std::string>()> param;
+ karma::rule<OutputIterator, std::pair<std::string const, std::string>()> param;
 
- param %= eps(!phoenix::empty(phoenix::at_c<1>(_val)))
+ param = eps(!phoenix::empty(phoenix::at_c<1>(_val)))
                     << string << "=" << string
               | string << omit[string]
               ;
 
- return generate(sink, param % '&', m);
+ return karma::generate(sink, param % '&', m);
     }
 }
 

Modified: trunk/libs/spirit/example/qi/Jamfile
==============================================================================
--- trunk/libs/spirit/example/qi/Jamfile (original)
+++ trunk/libs/spirit/example/qi/Jamfile 2009-11-16 11:28:07 EST (Mon, 16 Nov 2009)
@@ -25,7 +25,10 @@
 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 key_value_sequence_ordered : key_value_sequence_ordered.cpp ;
+exe key_value_sequence_empty_value : key_value_sequence_empty_value.cpp ;
 
 exe calculator1 : calc1.cpp ;
 exe calculator2 : calc2.cpp ;

Modified: trunk/libs/spirit/example/qi/key_value_sequence.cpp
==============================================================================
--- trunk/libs/spirit/example/qi/key_value_sequence.cpp (original)
+++ trunk/libs/spirit/example/qi/key_value_sequence.cpp 2009-11-16 11:28:07 EST (Mon, 16 Nov 2009)
@@ -13,9 +13,11 @@
 {
     namespace qi = boost::spirit::qi;
 
+ typedef std::map<std::string, std::string> pairs_type;
+
     template <typename Iterator>
     struct key_value_sequence
- : qi::grammar<Iterator, std::map<std::string, std::string>()>
+ : qi::grammar<Iterator, pairs_type()>
     {
         key_value_sequence()
           : key_value_sequence::base_type(query)
@@ -26,7 +28,7 @@
             value = +qi::char_("a-zA-Z_0-9");
         }
 
- qi::rule<Iterator, std::map<std::string, std::string>()> query;
+ qi::rule<Iterator, pairs_type()> query;
         qi::rule<Iterator, std::pair<std::string, std::string>()> pair;
         qi::rule<Iterator, std::string()> key, value;
     };
@@ -42,7 +44,7 @@
     std::string::iterator end = input.end();
 
     client::key_value_sequence<std::string::iterator> p;
- std::map<std::string, std::string> m;
+ client::pairs_type m;
 
     if (!qi::parse(begin, end, p, m))
     {
@@ -54,9 +56,8 @@
     {
         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)
+ client::pairs_type::iterator end = m.end();
+ for (client::pairs_type::iterator it = m.begin(); it != end; ++it)
         {
             std::cout << (*it).first;
             if (!(*it).second.empty())

Added: trunk/libs/spirit/example/qi/key_value_sequence_empty_value.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/qi/key_value_sequence_empty_value.cpp 2009-11-16 11:28:07 EST (Mon, 16 Nov 2009)
@@ -0,0 +1,72 @@
+// 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 qi = boost::spirit::qi;
+
+ typedef std::pair<std::string, boost::optional<std::string> > pair_type;
+ typedef std::vector<pair_type> pairs_type;
+
+ template <typename Iterator>
+ struct key_value_sequence_empty_value
+ : qi::grammar<Iterator, pairs_type()>
+ {
+ key_value_sequence_empty_value()
+ : key_value_sequence_empty_value::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, pairs_type()> query;
+ qi::rule<Iterator, pair_type()> pair;
+ qi::rule<Iterator, std::string()> key, value;
+ };
+}
+
+///////////////////////////////////////////////////////////////////////////////
+int main()
+{
+ namespace qi = boost::spirit::qi;
+
+ std::string input("key1=value1;key2;key3=value3;key4=");
+ std::string::iterator begin = input.begin();
+ std::string::iterator end = input.end();
+
+ client::key_value_sequence_empty_value<std::string::iterator> p;
+ client::pairs_type m;
+
+ if (!qi::parse(begin, end, p, m))
+ {
+ std::cout << "-------------------------------- \n";
+ std::cout << "Parsing failed\n";
+ std::cout << "-------------------------------- \n";
+ }
+ else
+ {
+ std::cout << "-------------------------------- \n";
+ std::cout << "Parsing succeeded, found entries:\n";
+ client::pairs_type::iterator end = m.end();
+ for (client::pairs_type::iterator it = m.begin(); it != end; ++it)
+ {
+ std::cout << (*it).first;
+ if ((*it).second)
+ std::cout << "=" << boost::get<std::string>((*it).second);
+ std::cout << std::endl;
+ }
+ std::cout << "---------------------------------\n";
+ }
+ return 0;
+}
+

Added: trunk/libs/spirit/example/qi/key_value_sequence_ordered.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/qi/key_value_sequence_ordered.cpp 2009-11-16 11:28:07 EST (Mon, 16 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 qi = boost::spirit::qi;
+
+ typedef std::vector<std::pair<std::string, std::string> > pairs_type;
+
+ template <typename Iterator>
+ struct key_value_sequence_ordered
+ : qi::grammar<Iterator, pairs_type()>
+ {
+ key_value_sequence_ordered()
+ : key_value_sequence_ordered::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, pairs_type()> 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("key2=value2;key1;key3=value3");
+ std::string::iterator begin = input.begin();
+ std::string::iterator end = input.end();
+
+ client::key_value_sequence_ordered<std::string::iterator> p;
+ client::pairs_type v;
+
+ if (!qi::parse(begin, end, p, v))
+ {
+ std::cout << "-------------------------------- \n";
+ std::cout << "Parsing failed\n";
+ std::cout << "-------------------------------- \n";
+ }
+ else
+ {
+ std::cout << "-------------------------------- \n";
+ std::cout << "Parsing succeeded, found entries:\n";
+ client::pairs_type::iterator end = v.end();
+ for (client::pairs_type::iterator it = v.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