Boost logo

Boost-Commit :

From: eric_at_[hidden]
Date: 2007-11-08 21:48:47


Author: eric_niebler
Date: 2007-11-08 21:48:46 EST (Thu, 08 Nov 2007)
New Revision: 40954
URL: http://svn.boost.org/trac/boost/changeset/40954

Log:
renames
Added:
   branches/proto/v3/libs/xpressive/proto3/test/examples.cpp
      - copied unchanged from r40942, /branches/proto/v3/libs/xpressive/proto3/test/examples2.cpp
   branches/proto/v3/libs/xpressive/proto3/test/lambda.cpp
      - copied unchanged from r40952, /branches/proto/v3/libs/xpressive/proto3/test/lambda2.cpp
   branches/proto/v3/libs/xpressive/proto3/test/toy_spirit.cpp
      - copied unchanged from r40952, /branches/proto/v3/libs/xpressive/proto3/test/toy_spirit3.cpp
Removed:
   branches/proto/v3/libs/xpressive/proto3/test/examples2.cpp
   branches/proto/v3/libs/xpressive/proto3/test/lambda2.cpp
   branches/proto/v3/libs/xpressive/proto3/test/toy_spirit3.cpp
Text files modified:
   branches/proto/v3/libs/xpressive/proto3/test/Jamfile.v2 | 6 +++---
   1 files changed, 3 insertions(+), 3 deletions(-)

Modified: branches/proto/v3/libs/xpressive/proto3/test/Jamfile.v2
==============================================================================
--- branches/proto/v3/libs/xpressive/proto3/test/Jamfile.v2 (original)
+++ branches/proto/v3/libs/xpressive/proto3/test/Jamfile.v2 2007-11-08 21:48:46 EST (Thu, 08 Nov 2007)
@@ -20,9 +20,9 @@
     :
 # [ run proto_fusion.cpp : : : <toolset>gcc:<cxxflags>-ftemplate-depth-1024 ]
 # [ run proto_fusion_s.cpp ]
- [ run toy_spirit3.cpp ]
+ [ run toy_spirit.cpp ]
         [ run calculator.cpp ]
- [ run lambda2.cpp ]
+ [ run lambda.cpp ]
         [ run matches.cpp ]
- [ run examples2.cpp ]
+ [ run examples.cpp ]
     ;

Deleted: branches/proto/v3/libs/xpressive/proto3/test/examples2.cpp
==============================================================================
--- branches/proto/v3/libs/xpressive/proto3/test/examples2.cpp 2007-11-08 21:48:46 EST (Thu, 08 Nov 2007)
+++ (empty file)
@@ -1,352 +0,0 @@
-///////////////////////////////////////////////////////////////////////////////
-// examples2.hpp
-//
-// Copyright 2006 Eric Niebler. 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/config.hpp>
-#include <boost/mpl/min_max.hpp>
-#include <boost/xpressive/proto3/proto.hpp>
-#include <boost/xpressive/proto3/transform.hpp>
-#include <boost/xpressive/proto3/transform/arg.hpp>
-#include <boost/xpressive/proto3/transform/fold.hpp>
-#include <boost/xpressive/proto3/transform/fold_tree.hpp>
-#include <boost/utility/result_of.hpp>
-#include <boost/fusion/include/cons.hpp>
-#include <boost/fusion/include/pop_front.hpp>
-#include <boost/test/unit_test.hpp>
-
-using namespace boost::proto;
-using namespace transform;
-namespace mpl = boost::mpl;
-namespace fusion = boost::fusion;
-
-struct placeholder1 {};
-struct placeholder2 {};
-
-namespace test1
-{
-//[ CalculatorGrammar
- using namespace boost::proto;
-
- /*<< A Calculator expression is ... >>*/
- struct CalculatorGrammar
- : or_<
- /*<< placeholder1, or ... >>*/
- terminal< placeholder1 >
- /*<< placeholder2, or ... >>*/
- , terminal< placeholder2 >
- /*<< some other terminal, or ... >>*/
- , terminal< _ >
- /*<< a unary expression where the operand is a calculator expression, or ... >>*/
- , unary_expr< _, CalculatorGrammar >
- /*<< a binary expression where the operands are calculator expressions, or ... >>*/
- , binary_expr< _, CalculatorGrammar, CalculatorGrammar >
- >
- {};
-//]
-}
-
-//[ binary_arity
-/*<< The `CalculatorArity` is a transform for calculating
-the arity of a calculator expression. It will be define in
-terms of `binary_arity`, which is defined in terms of
-`CalculatorArity`; hence, the definition is recursive.>>*/
-struct CalculatorArity;
-
-// A custom transform that returns the arity of a unary
-// calculator expression by finding the arity of the
-// child expression.
-struct unary_arity
- /*<< All custom transforms should inherit from
- transform_base. In some cases, (e.g., when the transform
- is a template), it is also necessary to specialize
- the proto::is_transform<> trait. >>*/
- : raw_transform
-{
- template<typename Expr, typename State, typename Visitor>
- /*<< Transforms have a nested `apply<>` for calculating their return type. >>*/
- struct apply
- {
- /*<< Get the child. >>*/
- typedef typename result_of::arg<Expr>::type child_expr;
-
- /*<< Apply `CalculatorArity` to find the arity of the child. >>*/
- typedef typename mpl::apply_wrap3<CalculatorArity, child_expr, State, Visitor>::type type;
- };
-
- template<typename Expr, typename State, typename Visitor>
- static typename apply<Expr, State, Visitor>::type
- /*<< Transforms have a nested `call()` member function. >>*/
- call(Expr const &, State const &, Visitor &)
- {
- /*<< The `unary_arity` transform doesn't have an interesting
- runtime counterpart, so just return a default-constructed object
- of the correct type. >>*/
- return typename apply<Expr, State, Visitor>::type();
- }
-};
-
-// A custom transform that returns the arity of a binary
-// calculator expression by finding the maximum of the
-// arities of the two children expressions.
-struct binary_arity
- /*<< All custom transforms should inherit from
- transform_base. In some cases, (e.g., when the transform
- is a template), it is also necessary to specialize
- the proto::is_transform<> trait. >>*/
- : raw_transform
-{
- template<typename Expr, typename State, typename Visitor>
- /*<< Transforms have a nested `apply<>` for calculating their return type. >>*/
- struct apply
- {
- /*<< Get the left and right children. >>*/
- typedef typename result_of::left<Expr>::type left_expr;
- typedef typename result_of::right<Expr>::type right_expr;
-
- /*<< Apply `CalculatorArity` to find the arity of the left and right children. >>*/
- typedef typename mpl::apply_wrap3<CalculatorArity, left_expr, State, Visitor>::type left_arity;
- typedef typename mpl::apply_wrap3<CalculatorArity, right_expr, State, Visitor>::type right_arity;
-
- /*<< The return type is the maximum of the children's arities. >>*/
- typedef typename mpl::max<left_arity, right_arity>::type type;
- };
-
- template<typename Expr, typename State, typename Visitor>
- static typename apply<Expr, State, Visitor>::type
- /*<< Transforms have a nested `call()` member function. >>*/
- call(Expr const &, State const &, Visitor &)
- {
- /*<< The `binary_arity` transform doesn't have an interesting
- runtime counterpart, so just return a default-constructed object
- of the correct type. >>*/
- return typename apply<Expr, State, Visitor>::type();
- }
-};
-//]
-
-struct zero : mpl::int_<0> {};
-struct one : mpl::int_<1> {};
-struct two : mpl::int_<2> {};
-
-terminal< placeholder1 >::type const _1 = {{}};
-terminal< placeholder2 >::type const _2 = {{}};
-
-//[ CalculatorArityGrammar
-struct CalculatorArity
- : or_<
- case_< terminal< placeholder1 >, one() >
- , case_< terminal< placeholder2 >, two() >
- , case_< terminal<_>, zero() >
- , case_< unary_expr<_, _>, unary_arity >
- , case_< binary_expr<_, _, _>, binary_arity >
- >
-{};
-//]
-
-//[ CalculatorArityGrammar2
-struct CalcArity2
- : or_<
- case_< terminal< placeholder1 >, one() >
- , case_< terminal< placeholder2 >, two() >
- , case_< terminal<_>, zero() >
- , case_< unary_expr<_, CalcArity2>, CalcArity2(_arg) >
- , case_< binary_expr<_, CalcArity2, CalcArity2>, mpl::max<CalcArity2(_left), CalcArity2(_right)>() >
- >
-{};
-//]
-
-struct pop_front : function_transform
-{
- template<typename Sig> struct result;
-
- template<typename This, typename T>
- struct result<This(T)>
- : fusion::result_of::pop_front<T const>
- {};
-
- template<typename T>
- typename fusion::result_of::pop_front<T const>::type
- operator()(T const &t) const
- {
- return fusion::pop_front(t);
- }
-};
-
-//[ AsArgList
-// This transform matches function invocations such as foo(1,'a',"b")
-// and transforms them into Fusion cons lists of their arguments. In this
-// case, the result would be cons(1, cons('a', cons("b", nil()))).
-struct ArgsAsList
- : case_<
- function<terminal<_>, vararg<terminal<_> > >
- /*<< Use a `reverse_fold<>` transform to iterate over the children
- of this node in reverse order, building a fusion list from back to
- front. >>*/
- , reverse_fold<
- /*<< The first child expression of a `function<>` node is the
- function being invoked. We don't want that in our list, so use
- the `pop_front<>` transform to remove it. >>*/
- pop_front(_) // make (_) optional
- /*<< `nil` is the initial state used by the `reverse_fold<>`
- transform. >>*/
- , fusion::nil()
- /*<< Put the rest of the function arguments in a fusion cons
- list. >>*/
- , fusion::cons<_arg, _state>(_arg, _state)
- >
- >
-{};
-//]
-
-//[ FoldTreeToList
-// This transform matches expressions of the form (_1=1,'a',"b")
-// (note the use of the comma operator) and transforms it into a
-// Fusion cons list of their arguments. In this case, the result
-// would be cons(1, cons('a', cons("b", nil()))).
-struct FoldTreeToList
- : or_<
- // This grammar describes what counts as the terminals in expressions
- // of the form (_1=1,'a',"b"), which will be flattened using
- // reverse_fold_tree<> below.
- case_<assign<_, terminal<_> >
- , _arg(_right)
- >
- , case_<terminal<_>
- , _arg
- >
- , case_<
- comma<FoldTreeToList, FoldTreeToList>
- /*<< Fold all terminals that are separated by commas into a Fusion cons list. >>*/
- , reverse_fold_tree<
- _
- , fusion::nil()
- , fusion::cons<FoldTreeToList, _state>(FoldTreeToList, _state)
- >
- >
- >
-{};
-//]
-
-//[ Promote
-// This transform finds all float terminals in an expression and promotes
-// them to doubles.
-struct Promote
- : or_<
- /*<< Match a `terminal<float>`, then construct a
- `terminal<double>::type` with the `float`. >>*/
- case_<terminal<float>, terminal<double>::type(_arg) >
- , case_<terminal<_> >
- /*<< `nary_expr<>` has a pass-through transform which
- will transform each child sub-expression using the
- `Promote` transform. >>*/
- , case_<nary_expr<_, vararg<Promote> > >
- >
-{};
-//]
-
-//[ LazyMakePair
-struct make_pair_tag {};
-terminal<make_pair_tag>::type const make_pair_ = {{}};
-
-// This transform matches lazy function invocations like
-// `make_pair_(1, 3.14)` and actually builds a `std::pair<>`
-// from the arguments.
-struct MakePair
- : case_<
- /*<< Match expressions like `make_pair_(1, 3.14)` >>*/
- function<terminal<make_pair_tag>, terminal<_>, terminal<_> >
- /*<< Return `std::pair<F,S>(f,s)` where `f` and `s` are the
- first and second arguments to the lazy `make_pair_()` function >>*/
- , std::pair<_arg(_arg1), _arg(_arg2)>(_arg(_arg1), _arg(_arg2))
- >
-{};
-//]
-
-//[ NegateInt
-struct NegateInt
- : case_<terminal<int>, negate<_>(_)>
-{};
-//]
-
-#ifndef BOOST_MSVC
-//[ SquareAndPromoteInt
-struct SquareAndPromoteInt
- : case_<
- terminal<int>
- , multiplies<terminal<long>::type, terminal<long>::type>::type
- (terminal<long>::type(_arg), terminal<long>::type(_arg))
- >
-{};
-//]
-#endif
-
-void test_examples()
-{
- //[ CalculatorArityTest
- int i = 0; // not used, dummy state and visitor parameter
-
- std::cout << CalculatorArity::call( lit(100) * 200, i, i) << '\n';
- std::cout << CalculatorArity::call( (_1 - _1) / _1 * 100, i, i) << '\n';
- std::cout << CalculatorArity::call( (_2 - _1) / _2 * 100, i, i) << '\n';
- //]
-
- BOOST_CHECK_EQUAL(0, CalculatorArity::call( lit(100) * 200, i, i));
- BOOST_CHECK_EQUAL(1, CalculatorArity::call( (_1 - _1) / _1 * 100, i, i));
- BOOST_CHECK_EQUAL(2, CalculatorArity::call( (_2 - _1) / _2 * 100, i, i));
-
- BOOST_CHECK_EQUAL(0, CalcArity2::call( lit(100) * 200, i, i));
- BOOST_CHECK_EQUAL(1, CalcArity2::call( (_1 - _1) / _1 * 100, i, i));
- BOOST_CHECK_EQUAL(2, CalcArity2::call( (_2 - _1) / _2 * 100, i, i));
-
- using boost::fusion::cons;
- using boost::fusion::nil;
- // TODO
- //cons<int, cons<char, cons<char const (&)[2]> > > args(ArgsAsList::call( _1(1, 'a', "b"), i, i ));
- //BOOST_CHECK_EQUAL(args.car, 1);
- //BOOST_CHECK_EQUAL(args.cdr.car, 'a');
- //BOOST_CHECK_EQUAL(args.cdr.cdr.car, std::string("b"));
-
- //cons<int, cons<char, cons<char const (&)[2]> > > lst(FoldTreeToList::call( (_1 = 1, 'a', "b"), i, i ));
- //BOOST_CHECK_EQUAL(lst.car, 1);
- //BOOST_CHECK_EQUAL(lst.cdr.car, 'a');
- //BOOST_CHECK_EQUAL(lst.cdr.cdr.car, std::string("b"));
-
- plus<
- terminal<double>::type
- , terminal<double>::type
- >::type p = Promote::call( lit(1.f) + 2.f, i, i );
-
- //[ LazyMakePairTest
- int j = 0; // not used, dummy state and visitor parameter
-
- std::pair<int, double> p2 = MakePair::call( make_pair_(1, 3.14), j, j );
-
- std::cout << p2.first << std::endl;
- std::cout << p2.second << std::endl;
- //]
-
- BOOST_CHECK_EQUAL(p2.first, 1);
- BOOST_CHECK_EQUAL(p2.second, 3.14);
-
- NegateInt::call(lit(1), i, i);
-#ifndef BOOST_MSVC
- SquareAndPromoteInt::call(lit(1), i, i);
-#endif
-}
-
-using namespace boost::unit_test;
-///////////////////////////////////////////////////////////////////////////////
-// init_unit_test_suite
-//
-test_suite* init_unit_test_suite( int argc, char* argv[] )
-{
- test_suite *test = BOOST_TEST_SUITE("test examples from the documentation");
-
- test->add(BOOST_TEST_CASE(&test_examples));
-
- return test;
-}

Deleted: branches/proto/v3/libs/xpressive/proto3/test/lambda2.cpp
==============================================================================
--- branches/proto/v3/libs/xpressive/proto3/test/lambda2.cpp 2007-11-08 21:48:46 EST (Thu, 08 Nov 2007)
+++ (empty file)
@@ -1,206 +0,0 @@
-///////////////////////////////////////////////////////////////////////////////
-// lambda.hpp
-//
-// Copyright 2006 Eric Niebler. 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 <sstream>
-#include <boost/version.hpp>
-#include <boost/mpl/int.hpp>
-#include <boost/mpl/min_max.hpp>
-#include <boost/mpl/eval_if.hpp>
-#include <boost/mpl/identity.hpp>
-#include <boost/mpl/next_prior.hpp>
-#if BOOST_VERSION < 103500
-# include <boost/spirit/fusion/sequence/at.hpp>
-# include <boost/spirit/fusion/sequence/tuple.hpp>
-namespace boost { namespace fusion { namespace result_of { using namespace meta; }}}
-#else
-# include <boost/fusion/tuple.hpp>
-#endif
-#include <boost/typeof/typeof.hpp>
-#include <boost/typeof/std/sstream.hpp>
-#include <boost/typeof/std/ostream.hpp>
-#include <boost/typeof/std/iostream.hpp>
-#include <boost/type_traits/add_const.hpp>
-#include <boost/type_traits/add_reference.hpp>
-#include <boost/xpressive/proto3/proto.hpp>
-#include <boost/xpressive/proto3/context.hpp>
-#include <boost/xpressive/proto3/transform.hpp>
-#include <boost/test/unit_test.hpp>
-#include <boost/test/floating_point_comparison.hpp>
-
-using namespace boost;
-
-// Forward declaration of the lambda expression wrapper
-template<typename T>
-struct lambda;
-
-struct lambda_domain
- : proto::domain<proto::pod_generator<lambda> >
-{};
-
-template<typename I>
-struct placeholder
-{
- typedef I arity;
-};
-
-template<typename T>
-struct placeholder_arity
-{
- typedef typename T::arity type;
-};
-
-struct zero : mpl::int_<0> {};
-
-namespace grammar
-{
- using namespace proto;
- using namespace transform;
-
- // The lambda grammar, with the transforms for calculating the max arity
- struct Lambda
- : or_<
- case_< terminal< placeholder<_> >, mpl::next<placeholder_arity<_arg> >() >
- , case_< terminal<_>, zero() >
- , case_< nary_expr<_, vararg<_> >, fold<_, zero(), mpl::max<Lambda,_state>()> >
- >
- {};
-}
-
-// simple wrapper for calculating a lambda expression's arity.
-template<typename Expr>
-struct lambda_arity
- : grammar::Lambda::apply<Expr, mpl::void_, mpl::void_>
-{};
-
-// The lambda context is the same as the default context
-// with the addition of special handling for lambda placeholders
-template<typename Tuple>
-struct lambda_context
- : proto::callable_context<lambda_context<Tuple> const>
-{
- lambda_context(Tuple const &args)
- : args_(args)
- {}
-
- template<typename Sig>
- struct result;
-
- template<typename This, typename I>
- struct result<This(proto::tag::terminal, placeholder<I> const &)>
- : fusion::result_of::at<Tuple, I>
- {};
-
- template<typename I>
- typename fusion::result_of::at<Tuple, I>::type
- operator ()(proto::tag::terminal, placeholder<I> const &) const
- {
- #if BOOST_VERSION < 103500
- return fusion::at<I::value>(this->args_);
- #else
- return fusion::at<I>(this->args_);
- #endif
- }
-
- Tuple args_;
-};
-
-// The lambda<> expression wrapper makes expressions polymorphic
-// function objects
-template<typename T>
-struct lambda
-{
- BOOST_PROTO_EXTENDS(T, lambda<T>, lambda_domain)
- BOOST_PROTO_EXTENDS_ASSIGN(T, lambda<T>, lambda_domain)
- BOOST_PROTO_EXTENDS_SUBSCRIPT(T, lambda<T>, lambda_domain)
-
- // Careful not to evaluate the return type of the nullary function
- // unless we have a nullary lambda!
- typedef typename mpl::eval_if<
- typename lambda_arity<T>::type
- , mpl::identity<void>
- , proto::result_of::eval<T const, lambda_context<fusion::tuple<> > >
- >::type nullary_type;
-
- // Define our operator() that evaluates the lambda expression.
- nullary_type operator()() const
- {
- fusion::tuple<> args;
- lambda_context<fusion::tuple<> > ctx(args);
- return proto::eval(*this, ctx);
- }
-
- template<typename A0>
- typename proto::result_of::eval<T const, lambda_context<fusion::tuple<A0 const &> > >::type
- operator()(A0 const &a0) const
- {
- fusion::tuple<A0 const &> args(a0);
- lambda_context<fusion::tuple<A0 const &> > ctx(args);
- return proto::eval(*this, ctx);
- }
-
- template<typename A0, typename A1>
- typename proto::result_of::eval<T const, lambda_context<fusion::tuple<A0 const &, A1 const &> > >::type
- operator()(A0 const &a0, A1 const &a1) const
- {
- fusion::tuple<A0 const &, A1 const &> args(a0, a1);
- lambda_context<fusion::tuple<A0 const &, A1 const &> > ctx(args);
- return proto::eval(*this, ctx);
- }
-};
-
-// Define some lambda placeholders
-lambda<proto::terminal<placeholder<mpl::int_<0> > >::type> const _1 = {{}};
-lambda<proto::terminal<placeholder<mpl::int_<1> > >::type> const _2 = {{}};
-
-template<typename T>
-lambda<typename proto::terminal<T>::type> const val(T const &t)
-{
- lambda<typename proto::terminal<T>::type> that = {{t}};
- return that;
-}
-
-template<typename T>
-lambda<typename proto::terminal<T &>::type> const var(T &t)
-{
- lambda<typename proto::terminal<T &>::type> that = {{t}};
- return that;
-}
-
-void test_lambda()
-{
- BOOST_CHECK_EQUAL(11, ( (_1 + 2) / 4 )(42));
- BOOST_CHECK_EQUAL(-11, ( (-(_1 + 2)) / 4 )(42));
- BOOST_CHECK_CLOSE(2.58, ( (4 - _2) * 3 )(42, 3.14), 0.1);
-
- // check non-const ref terminals
- std::stringstream sout;
- (sout << _1 << " -- " << _2)(42, "Life, the Universe and Everything!");
- BOOST_CHECK_EQUAL("42 -- Life, the Universe and Everything!", sout.str());
-
- // check nullary lambdas
- BOOST_CHECK_EQUAL(3, (val(1) + val(2))());
-
- // check array indexing for kicks
- int integers[5] = {0};
- (var(integers)[2] = 2)();
- (var(integers)[_1] = _1)(3);
- BOOST_CHECK_EQUAL(2, integers[2]);
- BOOST_CHECK_EQUAL(3, integers[3]);
-}
-
-using namespace unit_test;
-///////////////////////////////////////////////////////////////////////////////
-// init_unit_test_suite
-//
-test_suite* init_unit_test_suite( int argc, char* argv[] )
-{
- test_suite *test = BOOST_TEST_SUITE("test expression template domains");
-
- test->add(BOOST_TEST_CASE(&test_lambda));
-
- return test;
-}

Deleted: branches/proto/v3/libs/xpressive/proto3/test/toy_spirit3.cpp
==============================================================================
--- branches/proto/v3/libs/xpressive/proto3/test/toy_spirit3.cpp 2007-11-08 21:48:46 EST (Thu, 08 Nov 2007)
+++ (empty file)
@@ -1,471 +0,0 @@
-///////////////////////////////////////////////////////////////////////////////
-// toy_spirit3.cpp
-//
-// Copyright 2006 Eric Niebler. 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 <cctype>
-#include <string>
-#include <cstring>
-#include <iomanip>
-#include <iostream>
-#include <boost/version.hpp>
-#include <boost/assert.hpp>
-#include <boost/mpl/assert.hpp>
-#include <boost/utility/result_of.hpp>
-#include <boost/type_traits/is_same.hpp>
-#include <boost/xpressive/proto3/proto.hpp>
-#include <boost/xpressive/proto3/transform.hpp>
-#if BOOST_VERSION < 103500
-# include <boost/spirit/fusion/algorithm/for_each.hpp>
-# include <boost/spirit/fusion/algorithm/fold.hpp>
-# include <boost/spirit/fusion/algorithm/any.hpp>
-#else
-# include <boost/fusion/include/for_each.hpp>
-# include <boost/fusion/include/fold.hpp>
-# include <boost/fusion/include/cons.hpp>
-# include <boost/fusion/include/any.hpp>
-#endif
-#include <boost/test/unit_test.hpp>
-
-namespace boost
-{
- // global tags
- struct char_tag {};
- struct space_tag {};
-
- // global primitives
- proto::terminal<char_tag>::type const char_ = {{}};
- proto::terminal<space_tag>::type const space = {{}};
-
- using proto::lit;
- using proto::literal;
-}
-
-namespace boost { namespace spirit2
-{
- namespace utility
- {
- inline bool char_icmp(char ch, char lo, char hi)
- {
- return ch == lo || ch == hi;
- }
-
- template<typename FwdIter>
- inline bool string_cmp(char const *sz, FwdIter &begin, FwdIter end)
- {
- FwdIter tmp = begin;
- for(; *sz; ++tmp, ++sz)
- if(tmp == end || *tmp != *sz)
- return false;
- begin = tmp;
- return true;
- }
-
- template<typename FwdIter>
- inline bool string_icmp(std::string const &str, FwdIter &begin, FwdIter end)
- {
- BOOST_ASSERT(0 == str.size() % 2);
- FwdIter tmp = begin;
- std::string::const_iterator istr = str.begin(), estr = str.end();
- for(; istr != estr; ++tmp, istr += 2)
- if(tmp == end || *tmp != *istr && *tmp != *(istr+1))
- return false;
- begin = tmp;
- return true;
- }
-
- inline bool in_range(char ch, char lo, char hi)
- {
- return ch >= lo && ch <= hi;
- }
-
- inline bool in_irange(char ch, char lo, char hi)
- {
- return in_range(ch, lo, hi)
- || in_range(std::tolower(ch), lo, hi)
- || in_range(std::toupper(ch), lo, hi);
- }
-
- inline std::string to_istr(char const *sz)
- {
- std::string res;
- res.reserve(std::strlen(sz) * 2);
- for(; *sz; ++sz)
- {
- res.push_back(std::tolower(*sz));
- res.push_back(std::toupper(*sz));
- }
- return res;
- }
- } // namespace utility
-
- template<typename List>
- struct alternate
- {
- explicit alternate(List const &list)
- : elems(list)
- {}
- List elems;
- };
-
- template<typename List>
- struct sequence
- {
- explicit sequence(List const &list)
- : elems(list)
- {}
- List elems;
- };
-
- struct char_range
- : std::pair<char, char>
- {
- char_range(char from, char to)
- : std::pair<char, char>(from, to)
- {}
- };
-
- struct ichar
- {
- ichar(char ch)
- : lo_(std::tolower(ch))
- , hi_(std::toupper(ch))
- {}
-
- char lo_, hi_;
- };
-
- struct istr
- {
- istr(char const *sz)
- : str_(utility::to_istr(sz))
- {}
-
- std::string str_;
- };
-
- struct ichar_range
- : std::pair<char, char>
- {
- ichar_range(char from, char to)
- : std::pair<char, char>(from, to)
- {}
- };
-
- // The no-case directive
- struct no_case_tag {};
-
- struct True : mpl::true_ {};
-
- // remove_case specializations for stripping case-sensitivity from parsers
- template<typename T, typename CaseSensitive>
- struct remove_case
- {
- typedef T type;
- };
-
- template<>
- struct remove_case<char, True>
- {
- typedef ichar type;
- };
-
- template<>
- struct remove_case<char const *, True>
- {
- typedef istr type;
- };
-
- template<>
- struct remove_case<char_range, True>
- {
- typedef ichar_range type;
- };
-
- ///////////////////////////////////////////////////////////////////////////////
- /// Begin Spirit grammar here
- ///////////////////////////////////////////////////////////////////////////////
- namespace grammar
- {
- using namespace proto;
- using namespace fusion;
- using namespace transform;
-
- struct SpiritExpr;
-
- struct AnyChar
- : terminal<char_tag>
- {};
-
- struct CharLiteral
- : terminal<char>
- {};
-
- struct NTBSLiteral
- : terminal<char const *>
- {};
-
- struct CharParser
- : function<AnyChar, CharLiteral>
- {};
-
- struct CharRangeParser
- : function<AnyChar, CharLiteral, CharLiteral>
- {};
-
- struct NoCase
- : terminal<no_case_tag>
- {};
-
- // Extract the arg from terminals
- struct SpiritTerminal
- : or_<
- case_< AnyChar, _arg >
- , case_< CharLiteral, remove_case<char, _visitor>(_arg) >
- , case_< CharParser, remove_case<char, _visitor>(_arg(_arg1))> // char_('a')
- , case_< NTBSLiteral, remove_case<char const *, _visitor>(_arg) >
- , case_< CharRangeParser, remove_case<char_range, _visitor>(_arg(_arg1), _arg(_arg2))> // char_('a','z')
- >
- {};
-
- struct FoldToList
- : reverse_fold_tree<_, nil(), cons<SpiritExpr, _state>(SpiritExpr, _state)>
- {};
-
- // sequence rule folds all >>'s together into a list
- // and wraps the result in a sequence<> wrapper
- struct SpiritSequence
- : case_< shift_right<SpiritExpr, SpiritExpr>, sequence<FoldToList>(FoldToList) >
- {};
-
- // alternate rule folds all |'s together into a list
- // and wraps the result in a alternate<> wrapper
- struct SpiritAlternate
- : case_< bitwise_or<SpiritExpr, SpiritExpr>, alternate<FoldToList>(FoldToList) >
- {};
-
- // Directives such as no_case are handled here
- struct SpiritDirective
- : case_< subscript<NoCase, SpiritExpr>, SpiritExpr(_right, _state, True()) >
- {};
-
- // A SpiritExpr is an alternate, a sequence, a directive or a terminal
- struct SpiritExpr
- : or_<
- SpiritSequence
- , SpiritAlternate
- , SpiritDirective
- , SpiritTerminal
- >
- {};
-
- } // namespace grammar
-
- using grammar::SpiritExpr;
- using grammar::NoCase;
-
- ///////////////////////////////////////////////////////////////////////////////
- /// End SpiritExpr
- ///////////////////////////////////////////////////////////////////////////////
-
- // Globals
- NoCase::type const no_case = {{}};
-
- // Parser
- template<typename Iterator, typename Derived>
- struct with_reset
- {
- with_reset(Iterator begin, Iterator end)
- : first(begin), second(end)
- {}
-
- template<typename T>
- bool operator()(T const &t) const
- {
- Iterator tmp = this->first;
- if((*static_cast<Derived const *>(this))(t))
- return true;
- this->first = tmp;
- return false;
- }
-
- bool done() const
- {
- return this->first == this->second;
- }
-
- mutable Iterator first;
- Iterator second;
- };
-
- template<typename Iterator>
- struct parser
- : spirit2::with_reset<Iterator, parser<Iterator> >
- {
- typedef spirit2::with_reset<Iterator, parser<Iterator> > with_reset;
-
- parser(Iterator begin, Iterator end)
- : with_reset(begin, end)
- {}
-
- #if BOOST_VERSION < 103500
- template<typename, typename> // used by fusion::fold
- struct apply
- {
- typedef bool type;
- };
- #else
- typedef bool result_type; // used by fusion::fold
- #endif
-
- template<typename T>
- bool operator()(T const &t, bool success) const // used by fusion::fold
- {
- return success && (*this)(t);
- }
-
- template<typename List>
- bool operator()(alternate<List> const &alternates) const
- {
- return fusion::any(alternates.elems, *static_cast<with_reset const *>(this));
- }
-
- template<typename List>
- bool operator()(sequence<List> const &sequence) const
- {
- return fusion::fold(sequence.elems, true, *this);
- }
-
- bool operator()(char_tag ch) const
- {
- if(this->done())
- return false;
- ++this->first;
- return true;
- }
-
- bool operator()(char ch) const
- {
- if(this->done() || ch != *this->first)
- return false;
- ++this->first;
- return true;
- }
-
- bool operator()(ichar ich) const
- {
- if(this->done() || !utility::char_icmp(*this->first, ich.lo_, ich.hi_))
- return false;
- ++this->first;
- return true;
- }
-
- bool operator()(char const *sz) const
- {
- return utility::string_cmp(sz, this->first, this->second);
- }
-
- bool operator()(istr const &s) const
- {
- return utility::string_icmp(s.str_, this->first, this->second);
- }
-
- bool operator()(char_range rng) const
- {
- if(this->done() || !utility::in_range(*this->first, rng.first, rng.second))
- return false;
- ++this->first;
- return true;
- }
-
- bool operator()(ichar_range rng) const
- {
- if(this->done() || !utility::in_irange(*this->first, rng.first, rng.second))
- return false;
- ++this->first;
- return true;
- }
- };
-
- template<typename Rule, typename Iterator>
- typename enable_if<proto::matches< Rule, SpiritExpr >, bool >::type
- parse_impl(Rule const &rule, Iterator begin, Iterator end)
- {
- mpl::false_ is_case_sensitive;
- parser<Iterator> parse_fun(begin, end);
- return parse_fun(SpiritExpr::call(rule, 0, is_case_sensitive));
- }
-
- // 2nd overload provides a short error message for invalid rules
- template<typename Rule, typename Iterator>
- typename disable_if<proto::matches< Rule, SpiritExpr >, bool >::type
- parse_impl(Rule const &rule, Iterator begin, Iterator end)
- {
- BOOST_MPL_ASSERT((proto::matches<Rule, SpiritExpr>));
- return false;
- }
-
- // parse() converts rule literals to proto expressions if necessary
- // and dispatches to parse_impl
- template<typename Rule, typename Iterator>
- bool parse(Rule const &rule, Iterator begin, Iterator end)
- {
- return parse_impl(proto::as_expr(rule), begin, end);
- }
-
-}}
-
-void test_toy_spirit3()
-{
- using boost::spirit2::no_case;
- using boost::char_;
- std::string hello("abcd");
-
- BOOST_CHECK(
- boost::spirit2::parse(
- "abcd"
- , hello.begin()
- , hello.end()
- )
- );
-
- BOOST_CHECK(
- boost::spirit2::parse(
- char_ >> char_('b') >> 'c' >> char_
- , hello.begin()
- , hello.end()
- )
- );
-
- BOOST_CHECK(
- !boost::spirit2::parse(
- char_ >> char_('b') >> 'c' >> 'D'
- , hello.begin()
- , hello.end()
- )
- );
-
- BOOST_CHECK(
- boost::spirit2::parse(
- char_ >> char_('b') >> 'c' >> 'e'
- | char_ >> no_case[char_('B') >> "C" >> char_('D','Z')]
- , hello.begin()
- , hello.end()
- )
- );
-}
-
-using namespace boost::unit_test;
-///////////////////////////////////////////////////////////////////////////////
-// init_unit_test_suite
-//
-test_suite* init_unit_test_suite( int argc, char* argv[] )
-{
- test_suite *test = BOOST_TEST_SUITE("test proto, grammars and tree transforms");
-
- test->add(BOOST_TEST_CASE(&test_toy_spirit3));
-
- return test;
-}


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