Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r58501 - trunk/libs/spirit/example/karma
From: hartmut.kaiser_at_[hidden]
Date: 2009-12-22 17:00:58


Author: hkaiser
Date: 2009-12-22 17:00:57 EST (Tue, 22 Dec 2009)
New Revision: 58501
URL: http://svn.boost.org/trac/boost/changeset/58501

Log:
Spirit: added new Karma example
Added:
   trunk/libs/spirit/example/karma/quoted_strings.cpp (contents, props changed)
Text files modified:
   trunk/libs/spirit/example/karma/Jamfile | 1 +
   trunk/libs/spirit/example/karma/auto_facilities.cpp | 5 +++--
   2 files changed, 4 insertions(+), 2 deletions(-)

Modified: trunk/libs/spirit/example/karma/Jamfile
==============================================================================
--- trunk/libs/spirit/example/karma/Jamfile (original)
+++ trunk/libs/spirit/example/karma/Jamfile 2009-12-22 17:00:57 EST (Tue, 22 Dec 2009)
@@ -30,4 +30,5 @@
 exe karma_reorder_struct : reorder_struct.cpp ;
 exe karma_escaped_string : escaped_string.cpp ;
 exe simple_columns_directive : simple_columns_directive.cpp ;
+exe quoted_strings : quoted_strings.cpp ;
 

Modified: trunk/libs/spirit/example/karma/auto_facilities.cpp
==============================================================================
--- trunk/libs/spirit/example/karma/auto_facilities.cpp (original)
+++ trunk/libs/spirit/example/karma/auto_facilities.cpp 2009-12-22 17:00:57 EST (Tue, 22 Dec 2009)
@@ -33,7 +33,6 @@
 #include <boost/fusion/include/array.hpp>
 
 #include <boost/spirit/include/karma.hpp>
-#include <boost/mpl/print.hpp>
 
 using namespace boost::spirit;
 using namespace boost::spirit::ascii;
@@ -52,7 +51,9 @@
     // (the ':') which normally gets embedded in the proto expression by
     // reference only. The deep copy converts the proto tree to hold this by
     // value. The deep copy operation can be left out for simpler proto
- // expressions (not containing references to temporaries).
+ // expressions (not containing references to temporaries). Alternatively
+ // you could use the proto::make_expr() facility to build the required
+ // proto expression.
     template <>
     struct create_generator<std::pair<int const, std::string> >
     {

Added: trunk/libs/spirit/example/karma/quoted_strings.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/karma/quoted_strings.cpp 2009-12-22 17:00:57 EST (Tue, 22 Dec 2009)
@@ -0,0 +1,70 @@
+// 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)
+
+// The purpose of this example is to demonstrate how to utilize alternatives
+// and the built in matching capabilities of Karma generators to emit output
+// in different formats based on the content of an attribute (not its type).
+
+#include <boost/config/warning_disable.hpp>
+
+#include <string>
+#include <vector>
+
+#include <boost/spirit/include/karma.hpp>
+#include <boost/spirit/include/phoenix_stl.hpp>
+
+namespace client
+{
+ namespace karma = boost::spirit::karma;
+ namespace phx = boost::phoenix;
+
+ template <typename OutputIterator>
+ struct quoted_strings
+ : karma::grammar<OutputIterator, std::vector<std::string>()>
+ {
+ quoted_strings()
+ : quoted_strings::base_type(strings)
+ {
+ strings = (bareword | qstring) % ' ';
+ bareword = karma::repeat(phx::size(karma::_val))
+ [ karma::alnum | karma::char_("-.,_$") ];
+ qstring = '"' << karma::string << '"';
+ }
+
+ karma::rule<OutputIterator, std::vector<std::string>()> strings;
+ karma::rule<OutputIterator, std::string()> bareword, qstring;
+ };
+}
+
+int main()
+{
+ namespace karma = boost::spirit::karma;
+
+ typedef std::back_insert_iterator<std::string> sink_type;
+
+ std::string generated;
+ sink_type sink(generated);
+
+ std::vector<std::string> v;
+ v.push_back("foo");
+ v.push_back("bar baz");
+ v.push_back("hello");
+
+ client::quoted_strings<sink_type> g;
+ if (!karma::generate(sink, g, v))
+ {
+ std::cout << "-------------------------\n";
+ std::cout << "Generating failed\n";
+ std::cout << "-------------------------\n";
+ }
+ else
+ {
+ std::cout << "-------------------------\n";
+ std::cout << "Generated: " << generated << "\n";
+ 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