|
Boost-Commit : |
From: hartmut.kaiser_at_[hidden]
Date: 2008-04-17 21:39:58
Author: hkaiser
Date: 2008-04-17 21:39:58 EDT (Thu, 17 Apr 2008)
New Revision: 44529
URL: http://svn.boost.org/trac/boost/changeset/44529
Log:
Spirit.Karma: added simple function support for semantic actions, added example.
Added:
trunk/libs/spirit/example/karma/actions.cpp (contents, props changed)
Text files modified:
trunk/boost/spirit/home/karma/action/action.hpp | 8 +++++---
trunk/libs/spirit/example/qi/actions.cpp | 16 +++++++++++++---
2 files changed, 18 insertions(+), 6 deletions(-)
Modified: trunk/boost/spirit/home/karma/action/action.hpp
==============================================================================
--- trunk/boost/spirit/home/karma/action/action.hpp (original)
+++ trunk/boost/spirit/home/karma/action/action.hpp 2008-04-17 21:39:58 EDT (Thu, 17 Apr 2008)
@@ -13,6 +13,7 @@
#include <boost/spirit/home/support/component.hpp>
#include <boost/spirit/home/support/detail/values.hpp>
#include <boost/spirit/home/support/attribute_of.hpp>
+#include <boost/spirit/home/support/detail/action_dispatch.hpp>
#include <boost/spirit/home/karma/domain.hpp>
#include <boost/mpl/if.hpp>
#include <boost/mpl/identity.hpp>
@@ -60,9 +61,10 @@
// call the function, passing the parameter, the context
// and a bool flag that the client can set to false to
// fail parsing.
- bool pass = true;
- spirit::right(component)(
- spirit::detail::pass_value<param_type>::call(p), ctx, pass);
+ // call the function, passing the attribute, the context.
+ // The client can return false to fail parsing.
+ bool pass = spirit::detail::action_dispatch(
+ spirit::right(component), p, ctx);
return pass &&
director::generate(spirit::left(component), sink, ctx, d, p);
Added: trunk/libs/spirit/example/karma/actions.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/karma/actions.cpp 2008-04-17 21:39:58 EDT (Thu, 17 Apr 2008)
@@ -0,0 +1,114 @@
+/*=============================================================================
+ Copyright (c) 2001-2008 Hartmut Kaiser
+ Copyright (c) 2001-2008 Joel de Guzman
+
+ 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 <iostream>
+#include <strstream>
+
+#include <boost/spirit/include/karma.hpp>
+#include <boost/lambda/lambda.hpp>
+#include <boost/bind.hpp>
+#include <boost/function_output_iterator.hpp>
+
+// Presented are various ways to attach semantic actions
+// * Using plain function pointers
+// * Using plain simple function objects
+// * Using plain boost.bind
+// * Using plain boost.lambda
+
+using namespace boost::spirit;
+
+void read(int& i)
+{
+ i = 42;
+}
+
+struct read_action
+{
+ void operator()(int& i, unused_type, unused_type) const
+ {
+ i = 42;
+ }
+};
+
+///////////////////////////////////////////////////////////////////////////
+template <typename String>
+struct string_appender
+{
+ string_appender(String& s)
+ : str(s)
+ {}
+
+ template <typename T>
+ void operator()(T const &x) const
+ {
+ str += x;
+ }
+
+ String& str;
+};
+
+template <typename String>
+inline string_appender<String>
+make_string_appender(String& str)
+{
+ return string_appender<String>(str);
+}
+
+
+///////////////////////////////////////////////////////////////////////////////
+int main()
+{
+ using boost::make_function_output_iterator;
+
+ { // example using plain functions
+ std::string generated;
+ bool result = karma::generate(
+ make_function_output_iterator(make_string_appender(generated)),
+ '{' << int_[&read] << '}');
+
+ if (result)
+ std::cout << "Simple function: " << generated << std::endl;
+ }
+
+ { // example using simple function objects
+ std::string generated;
+ bool result = karma::generate(
+ make_function_output_iterator(make_string_appender(generated)),
+ '{' << int_[read_action()] << '}');
+
+ if (result)
+ std::cout << "Simple function object: " << generated << std::endl;
+ }
+
+ { // example using boost.bind
+ std::string generated;
+ bool result = karma::generate(
+ make_function_output_iterator(make_string_appender(generated)),
+ '{' << int_[boost::bind(&read, _1)] << '}');
+
+ if (result)
+ std::cout << "Boost.Bind: " << generated << std::endl;
+ }
+
+ { // example using boost.lambda
+ using boost::lambda::_1;
+
+ std::string generated;
+ std::stringstream strm("42");
+
+ bool result = karma::generate(
+ make_function_output_iterator(make_string_appender(generated)),
+ '{' << int_[strm >> _1] << '}');
+
+ if (result)
+ std::cout << "Boost.Lambda: " << generated << std::endl;
+ }
+
+ return 0;
+}
+
Modified: trunk/libs/spirit/example/qi/actions.cpp
==============================================================================
--- trunk/libs/spirit/example/qi/actions.cpp (original)
+++ trunk/libs/spirit/example/qi/actions.cpp 2008-04-17 21:39:58 EDT (Thu, 17 Apr 2008)
@@ -1,3 +1,10 @@
+/*=============================================================================
+ Copyright (c) 2001-2008 Joel de Guzman
+
+ 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 <iostream>
#include <boost/spirit/include/qi.hpp>
#include <boost/lambda/lambda.hpp>
@@ -11,14 +18,14 @@
using namespace boost::spirit;
-void write(int& i)
+void write(int const& i)
{
std::cout << i << std::endl;
}
struct write_action
{
- void operator()(int& i, unused_type, unused_type) const
+ void operator()(int const& i, unused_type, unused_type) const
{
std::cout << i << std::endl;
}
@@ -47,5 +54,8 @@
using boost::lambda::_1;
qi::parse(s1, e1, '{' >> int_[std::cout << _1 << '\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