Boost logo

Boost-Commit :

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


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

Log:
remove out of date tests
Removed:
   branches/proto/v3/libs/xpressive/proto3/test/examples.cpp
   branches/proto/v3/libs/xpressive/proto3/test/lambda.cpp
   branches/proto/v3/libs/xpressive/proto3/test/toy_spirit.cpp
   branches/proto/v3/libs/xpressive/proto3/test/toy_spirit2.cpp
Text files modified:
   branches/proto/v3/libs/xpressive/proto3/test/Jamfile.v2 | 11 +++--------
   1 files changed, 3 insertions(+), 8 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:47:32 EST (Thu, 08 Nov 2007)
@@ -10,7 +10,7 @@
         <toolset>intel:<debug-symbols>off
         <toolset>msvc-7.1:<debug-symbols>off
         <toolset>msvc-8.0:<define>_SCL_SECURE_NO_DEPRECATE
- <toolset>msvc-8.0:<define>_CRT_SECURE_NO_DEPRECATE
+ <toolset>msvc-9.0:<define>_SCL_SECURE_NO_DEPRECATE
         <toolset>gcc:<cxxflags>-ftemplate-depth-1024
         <library>/boost/test//boost_unit_test_framework
         <link>static
@@ -18,16 +18,11 @@
 
 test-suite "proto"
     :
- [ run proto_fusion.cpp : : : <toolset>gcc:<cxxflags>-ftemplate-depth-1024 ]
- [ run proto_fusion_s.cpp ]
- [ run toy_spirit.cpp ]
- [ run toy_spirit2.cpp ]
+# [ run proto_fusion.cpp : : : <toolset>gcc:<cxxflags>-ftemplate-depth-1024 ]
+# [ run proto_fusion_s.cpp ]
         [ run toy_spirit3.cpp ]
         [ run calculator.cpp ]
- [ run lambda.cpp ]
         [ run lambda2.cpp ]
         [ run matches.cpp ]
- [ run examples.cpp ]
         [ run examples2.cpp ]
     ;
-

Deleted: branches/proto/v3/libs/xpressive/proto3/test/examples.cpp
==============================================================================
--- branches/proto/v3/libs/xpressive/proto3/test/examples.cpp 2007-11-08 21:47:32 EST (Thu, 08 Nov 2007)
+++ (empty file)
@@ -1,290 +0,0 @@
-///////////////////////////////////////////////////////////////////////////////
-// examples.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/arg.hpp>
-#include <boost/xpressive/proto3/transform/fold.hpp>
-#include <boost/xpressive/proto3/transform/branch.hpp>
-#include <boost/xpressive/proto3/transform/list.hpp>
-#include <boost/xpressive/proto3/transform/apply.hpp>
-#include <boost/xpressive/proto3/transform/construct.hpp>
-#include <boost/xpressive/proto3/transform/fold_tree.hpp>
-#include <boost/utility/result_of.hpp>
-#include <boost/test/unit_test.hpp>
-
-using namespace boost::proto;
-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_max
-// A custom transform that returns the arity of a binary
-// calculator expression by finding the maximum of the
-// arities of the two children expressions.
-/*<< All transforms take a Grammar as a template parameter. >>*/
-template<typename Grammar>
-struct binary_max
- /*<< All transforms must inherit from the `Grammar`, so that the transform
- IS-A `Grammar`, and matches the same expressions that `Grammar` does. >>*/
- : Grammar
-{
- template<typename Expr, typename State, typename Visitor>
- /*<< Transforms have a nested `apply<>` for calculating their return type. >>*/
- struct apply
- {
- /*<< Apply `Grammar`'s transform. This is what makes it possible to chain transforms. >>*/
- typedef typename mpl::apply_wrap3<Grammar, Expr, State, Visitor>::type expr_type;
- /*<< After applying `Grammar`'s transform, the children expressions have been
- replaced with their arities. >>*/
- typedef typename result_of::left<expr_type>::type left_arity;
- typedef typename result_of::right<expr_type>::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 &)
- {
- /*<< Usually, the `call()` member function invokes the `Grammar`'s `call()` function,
- as `Grammar::call(expr,state,visitor)`, but this 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();
- }
-};
-//]
-
-terminal< placeholder1 >::type const _1 = {{}};
-terminal< placeholder2 >::type const _2 = {{}};
-
-//[ CalculatorArityGrammar
-struct CalculatorGrammar
- : or_<
- transform::always< terminal< placeholder1 >, mpl::int_<1> >
- , transform::always< terminal< placeholder2 >, mpl::int_<2> >
- , transform::always< terminal< _ >, mpl::int_<0> >
- , transform::arg< unary_expr< _, CalculatorGrammar > >
- , binary_max< binary_expr< _, CalculatorGrammar, CalculatorGrammar > >
- >
-{};
-//]
-
-//[ 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
- /*<< Use a `branch<>` transform to use `fusion::nil` as the initial
- state of this transformation. >>*/
- : transform::branch<
- /*<< Use a `reverse_fold<>` transform to iterate over the children
- of this node in reverse order, building a fusion list from back to
- front. >>*/
- transform::reverse_fold<
- /*<< The `Grammar` we're matching is a function invocation. >>*/
- function<
- /*<< The first child expression of a `function<>` node is the
- function being invoked. We don't want that in our list, so use
- the `state<>` transform to effectively skip it. (Recall that
- we're building a list in the state parameter, and that the
- `state<>` transform just returns the state unmodified. So this
- says to match a `terminal<>` but to not add it to the list.) >>*/
- transform::state<terminal<_> >
- /*<< We use `vararg<>` here because the function expression we're
- matching can have an arbitrary number of arguments. >>*/
- , vararg<
- /*<< The `list<>` transform puts the rest of the function
- arguments in a fusion cons list. >>*/
- transform::list<
- /*<< The arguments to the function are terminals.
- Extract the argument from each terminal before putting
- them into the list. >>*/
- transform::arg<terminal<_> >
- >
- >
- >
- >
- /*<< Here is the initial state used by this transform. >>*/
- , fusion::nil
- >
-{};
-//]
-
-//[ FoldTreeToList
-// 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.
-struct Terminals
- : or_<
- transform::arg<transform::right<assign<_, terminal<_> > > >
- , transform::arg<terminal<_> >
- >
-{};
-
-// 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
- /*<< Fold all terminals that are separated by commas into a Fusion cons list. >>*/
- : transform::reverse_fold_tree<tag::comma, transform::list<Terminals>, fusion::nil>
-{};
-//]
-
-//[ 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`. >>*/
- transform::construct<terminal<float>, terminal<double>::type(transform::arg<_>) >
- , terminal<_>
- /*<< `nary_expr<>` has a pass-through transform which will transform each child
- sub-expression using the `Promote` transform. >>*/
- , 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
- : transform::construct<
- /*<< 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<
- transform::arg<transform::arg_c<_, 1> >
- , transform::arg<transform::arg_c<_, 2> >
- >(
- transform::arg<transform::arg_c<_, 1> >
- , transform::arg<transform::arg_c<_, 2> >
- )
- >
-{};
-//]
-
-//[ NegateInt
-struct NegateInt
- : transform::construct<
- terminal<int>
- , negate<_>(_)
- >
-{};
-//]
-
-#ifndef BOOST_MSVC
-//[ SquareAndPromoteInt
-struct SquareAndPromoteInt
- : transform::construct<
- terminal<int>
- , multiplies<terminal<long>::type, terminal<long>::type>::type(
- terminal<long>::type(transform::arg<_>)
- , terminal<long>::type(transform::arg<_>)
- )
- >
-{};
-//]
-#endif
-
-void test_examples()
-{
- //[ CalculatorArityTest
- int i = 0; // not used, dummy state and visitor parameter
-
- std::cout << CalculatorGrammar::call( lit(100) * 200, i, i) << '\n';
- std::cout << CalculatorGrammar::call( (_1 - _1) / _1 * 100, i, i) << '\n';
- std::cout << CalculatorGrammar::call( (_2 - _1) / _2 * 100, i, i) << '\n';
- //]
-
- BOOST_CHECK_EQUAL(0, CalculatorGrammar::call( lit(100) * 200, i, i));
- BOOST_CHECK_EQUAL(1, CalculatorGrammar::call( (_1 - _1) / _1 * 100, i, i));
- BOOST_CHECK_EQUAL(2, CalculatorGrammar::call( (_2 - _1) / _2 * 100, i, i));
-
- using boost::fusion::cons;
- using boost::fusion::nil;
- 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/lambda.cpp
==============================================================================
--- branches/proto/v3/libs/xpressive/proto3/test/lambda.cpp 2007-11-08 21:47:32 EST (Thu, 08 Nov 2007)
+++ (empty file)
@@ -1,220 +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/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/include/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/arg.hpp>
-#include <boost/xpressive/proto3/transform/fold.hpp>
-#include <boost/xpressive/proto3/transform/apply.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;
-};
-
-// Some custom transforms for calculating the max arity of a lambda expression
-template<typename Grammar>
-struct max_arity
- : Grammar
-{
- template<typename Expr, typename State, typename Visitor>
- struct apply
- {
- typedef typename Grammar::template apply<Expr, State, Visitor>::type arity;
- typedef typename mpl::max<arity, State>::type type;
- };
-};
-
-template<typename Grammar>
-struct placeholder_arity
- : Grammar
-{
- template<typename Expr, typename, typename>
- struct apply
- : mpl::next<typename proto::result_of::arg<Expr>::type::arity>
- {};
-};
-
-using proto::_;
-
-// The lambda grammar, with the transforms for calculating the max arity
-struct LambdaGrammar
- : proto::or_<
- placeholder_arity< proto::terminal< placeholder<_> > >
- , proto::transform::always< proto::terminal<_>, mpl::int_<0> >
- , proto::transform::fold<
- proto::nary_expr<_, proto::vararg< max_arity< LambdaGrammar > > >
- >
- >
-{};
-
-// simple wrapper for calculating a lambda expression's arity.
-template<typename Expr>
-struct lambda_arity
- : LambdaGrammar::apply<Expr, mpl::int_<0>, 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
-{
- lambda_context(Tuple const &args)
- : args_(args)
- {}
-
- template<typename Expr, typename EnableIf = void>
- struct eval
- : proto::default_eval<Expr, lambda_context<Tuple> >
- {};
-
- template<typename Expr>
- struct eval<Expr, typename enable_if<proto::matches<Expr, proto::terminal<placeholder<_> > > >::type>
- {
- typedef typename proto::result_of::arg<Expr>::type::arity index;
- typedef typename fusion::result_of::at<Tuple, index>::type result_type;
- result_type operator()(Expr const &expr, lambda_context<Tuple> &ctx)
- {
-#if BOOST_VERSION < 103500
- return fusion::at<index::value>(ctx.args_);
-#else
- return fusion::at<index>(ctx.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_spirit.cpp
==============================================================================
--- branches/proto/v3/libs/xpressive/proto3/test/toy_spirit.cpp 2007-11-08 21:47:32 EST (Thu, 08 Nov 2007)
+++ (empty file)
@@ -1,692 +0,0 @@
-///////////////////////////////////////////////////////////////////////////////
-// toy_spirit.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 <cctype>
-#include <string>
-#include <iostream>
-#include <boost/assert.hpp>
-#include <boost/mpl/assert.hpp>
-#include <boost/xpressive/proto3/proto.hpp>
-#include <boost/xpressive/proto3/context.hpp>
-#include <boost/test/unit_test.hpp>
-
-namespace boost
-{
- // global tags
- struct char_tag {};
- struct ichar_tag {};
- struct istring_tag {};
- struct ichar_range_tag {};
- struct never_tag {};
- struct always_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
-{
-
- // handy typedefs
- typedef proto::terminal<char_tag>::type anychar_p;
- typedef proto::terminal<ichar_tag>::type ianychar_p;
- typedef proto::terminal<istring_tag>::type ianystr_p;
- typedef proto::terminal<ichar_range_tag>::type ianychar_range_p;
- typedef proto::terminal<never_tag>::type never_p;
- typedef proto::terminal<space_tag>::type space_p;
-
- struct SpiritGrammar;
- struct SkipperGrammar;
- struct SpiritPrimitives;
- template<typename Grammar>
- struct SpiritComposites;
-
- struct CharLiteral
- : proto::terminal<char>
- {};
-
- struct NTBSLiteral
- : proto::terminal<char const *>
- {};
-
- struct StdStringLiteral
- : proto::terminal<std::string>
- {};
-
- struct CharParser
- : proto::function<anychar_p, CharLiteral>
- {};
-
- struct ICharParser
- : proto::function<ianychar_p, CharLiteral, CharLiteral>
- {};
-
- struct CharRangeParser
- : proto::function<anychar_p, CharLiteral, CharLiteral>
- {};
-
- struct IStrParser
- : proto::function<ianystr_p, StdStringLiteral>
- {};
-
- struct ICharRangeParser
- : proto::function<ianychar_range_p, CharLiteral, CharLiteral>
- {};
-
- ianychar_p const ichar_ = {{}};
- ianystr_p const istr_ = {{}};
- ianychar_range_p const ichar_range_ = {{}};
-
- 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 FwdIter, typename Skipper = never_p>
- struct spirit_context
- : std::pair<FwdIter, FwdIter>
- , proto::callable_context<spirit_context<FwdIter, Skipper> >
- {
- typedef bool result_type;
- typedef FwdIter iterator;
-
- spirit_context(FwdIter first, FwdIter second, Skipper const &skip = Skipper())
- : std::pair<FwdIter, FwdIter>(first, second)
- , skip_(skip)
- , in_skip_(false)
- {}
-
- // parse function for anychar_p
- bool operator()(proto::tag::terminal, char_tag)
- {
- this->skip();
- if(this->first == this->second)
- return false;
- ++this->first;
- return true;
- }
-
- // parse function for char_('a')
- template<typename Expr>
- bool operator()(proto::tag::function, anychar_p, Expr const &expr)
- {
- this->skip();
- return proto::eval(expr, *this);
- }
-
- // parse function for space_p
- bool operator()(proto::tag::terminal, space_tag)
- {
- this->skip();
- if(this->first == this->second || !std::isspace(*this->first))
- return false;
- ++this->first;
- return true;
- }
-
- // parse function for bare character literals
- bool operator()(proto::tag::terminal, char ch)
- {
- this->skip();
- if(this->first == this->second || *this->first != ch)
- return false;
- ++this->first;
- return true;
- }
-
- // case-insensitive character parser
- template<typename Arg1, typename Arg2>
- bool operator()(proto::tag::function, ianychar_p, Arg1 const &arg1, Arg2 const &arg2)
- {
- this->skip();
- if(this->first == this->second
- || !utility::char_icmp(*this->first, proto::arg(arg1), proto::arg(arg2)))
- return false;
- ++this->first;
- return true;
- }
-
- // parse function for NTBS literals
- bool operator()(proto::tag::terminal, char const *sz)
- {
- this->skip();
- return utility::string_cmp(sz, this->first, this->second);
- }
-
- // parse function for istr_("hello")
- template<typename Expr>
- bool operator()(proto::tag::function, ianystr_p, Expr const &expr)
- {
- this->skip();
- return utility::string_icmp(proto::arg(expr), this->first, this->second);
- }
-
- // parse function for char_('a','z')
- template<typename Arg1, typename Arg2>
- bool operator()(proto::tag::function, anychar_p, Arg1 const &arg1, Arg2 const &arg2)
- {
- BOOST_ASSERT(proto::arg(arg1) <= proto::arg(arg2));
- this->skip();
- if(this->first == this->second
- || !utility::in_range(*this->first, proto::arg(arg1), proto::arg(arg2)))
- return false;
- ++this->first;
- return true;
- }
-
- // parse function for ichar_range_('a','z')
- template<typename Arg1, typename Arg2>
- bool operator()(proto::tag::function, ianychar_range_p, Arg1 const &arg1, Arg2 const &arg2)
- {
- BOOST_ASSERT(proto::arg(arg1) <= proto::arg(arg2));
- this->skip();
- if(this->first == this->second
- || !utility::in_irange(*this->first, proto::arg(arg1), proto::arg(arg2)))
- return false;
- ++this->first;
- return true;
- }
-
- // parse function for complemented thingies (where thingies are assumed
- // to be 1 character wide).
- template<typename Expr>
- bool operator()(proto::tag::complement, Expr const &expr)
- {
- this->skip();
- iterator where = this->first;
- if(proto::eval(expr, *this))
- return this->first = where, false;
- this->first = ++where;
- return true;
- }
-
- // never_p parse function always returns false.
- bool operator()(proto::tag::terminal, never_tag)
- {
- return false;
- }
-
- // for A >> B, succeeds if A and B matches.
- template<typename Left, typename Right>
- bool operator()(proto::tag::shift_right, Left const &left, Right const &right)
- {
- return proto::eval(left, *this) && proto::eval(right, *this);
- }
-
- // for A | B, succeeds if either A or B matches at this point.
- template<typename Left, typename Right>
- bool operator()(proto::tag::bitwise_or, Left const &left, Right const &right)
- {
- iterator where = this->first;
- return proto::eval(left, *this) || proto::eval(right, this->reset(where));
- }
-
- // for *A, greedily match A as many times as possible.
- template<typename Expr>
- bool operator()(proto::tag::dereference, Expr const &expr)
- {
- iterator where = this->first;
- while(proto::eval(expr, *this))
- where = this->first;
- // make sure that when we return true, the iterator is at the correct position!
- this->first = where;
- return true;
- }
-
- // for +A, greedily match A one or more times.
- template<typename Expr>
- bool operator()(proto::tag::posit, Expr const &expr)
- {
- return proto::eval(expr, *this) && proto::eval(*expr, *this);
- }
-
- // for !A, optionally match A.
- template<typename Expr>
- bool operator()(proto::tag::logical_not, Expr const &expr)
- {
- iterator where = this->first;
- if(!proto::eval(expr, *this))
- this->first = where;
- return true;
- }
-
- // for (A - B), matches when A but not B matches.
- template<typename Left, typename Right>
- bool operator()(proto::tag::minus, Left const &left, Right const &right)
- {
- iterator where = this->first;
- return !proto::eval(right, *this) && proto::eval(left, this->reset(where));
- }
- private:
- spirit_context &reset(iterator where)
- {
- this->first = where;
- return *this;
- }
-
- void skip()
- {
- if(!this->in_skip_)
- {
- this->in_skip_ = true;
- while(proto::eval(this->skip_, *this))
- {}
- this->in_skip_ = false;
- }
- }
-
- Skipper skip_;
- bool in_skip_;
- };
-
- // remove_case
- template<typename Grammar>
- struct remove_case;
-
- template<>
- struct remove_case<CharParser>
- {
- typedef proto::function<
- ianychar_p
- , proto::terminal<char>::type
- , proto::terminal<char>::type
- >::type type;
-
- template<typename Expr>
- static type call(Expr const &expr)
- {
- char lo = std::tolower(proto::arg(proto::arg_c<1>(expr)));
- char hi = std::toupper(proto::arg(proto::arg_c<1>(expr)));
- type that = {ichar_, {lo}, {hi}};
- return that;
- }
- };
-
- template<>
- struct remove_case<CharRangeParser>
- {
- typedef proto::function<
- ianychar_range_p
- , proto::terminal<char>::type
- , proto::terminal<char>::type
- >::type type;
-
- template<typename Expr>
- static type call(Expr const &expr)
- {
- char lo = proto::arg(proto::arg_c<1>(expr));
- char hi = proto::arg(proto::arg_c<2>(expr));
- type that = {ichar_range_, {lo}, {hi}};
- return that;
- }
- };
-
- template<>
- struct remove_case<CharLiteral>
- {
- typedef proto::function<
- ianychar_p
- , proto::terminal<char>::type
- , proto::terminal<char>::type
- >::type type;
-
- template<typename Expr>
- static type call(Expr const &expr)
- {
- char lo = std::tolower(proto::arg(expr));
- char hi = std::toupper(proto::arg(expr));
- type that = {ichar_, {lo}, {hi}};
- return that;
- }
- };
-
- template<>
- struct remove_case<NTBSLiteral>
- {
- typedef proto::function<
- ianystr_p
- , proto::terminal<std::string>::type
- >::type type;
-
- template<typename Expr>
- static type call(Expr const &expr)
- {
- type that = {istr_, {utility::to_istr(proto::arg(expr))}};
- return that;
- }
- };
-
- template<>
- struct remove_case<StdStringLiteral>
- {
- typedef proto::function<
- ianystr_p
- , proto::terminal<std::string>::type
- >::type type;
-
- template<typename Expr>
- static type call(Expr const &expr)
- {
- type that = {istr_, {utility::to_istr(proto::arg(expr).c_str())}};
- return that;
- }
- };
-
- ///////////////////////////////////////////////////////////////////////////
- // Transforms
- ///////////////////////////////////////////////////////////////////////////
-
- template<typename Grammar>
- struct case_sensitive
- : Grammar
- {
- template<typename Expr, typename State, typename Visitor>
- struct apply
- : remove_case<Grammar>
- {};
-
- template<typename Expr, typename State, typename Visitor>
- static typename apply<Expr, State, Visitor>::type
- call(Expr const &expr, State const &, Visitor &)
- {
- return apply<Expr, State, Visitor>::call(expr);
- }
- };
-
- template<typename Grammar>
- struct skip_primitives
- : Grammar
- {
- skip_primitives();
-
- template<typename Expr, typename State, typename Visitor>
- struct apply
- {
- typedef typename proto::shift_right<
- typename proto::dereference<State>::type
- , Expr
- >::type type;
- };
-
- template<typename Expr, typename State, typename Visitor>
- static typename apply<Expr, State, Visitor>::type
- call(Expr const &expr, State const &state, Visitor &visitor)
- {
- typedef typename apply<Expr, State, Visitor>::type type;
- type that = {{state}, expr};
- return that;
- }
- };
-
- ///////////////////////////////////////////////////////////////////////////
- // Grammar
- ///////////////////////////////////////////////////////////////////////////
-
- struct SpiritGrammar;
-
- struct SpiritCaseSensitivePrimitives
- : proto::or_<
- case_sensitive<CharParser>
- , case_sensitive<CharLiteral>
- , case_sensitive<NTBSLiteral>
- , case_sensitive<CharRangeParser>
- , case_sensitive<StdStringLiteral>
- >
- {};
-
- struct SpiritCaseInsensitivePrimitives
- : proto::or_<
- anychar_p
- , IStrParser
- , ICharParser
- , ICharRangeParser
- , proto::complement<SpiritPrimitives>
- >
- {};
-
- struct SpiritPrimitives
- : proto::or_<
- SpiritCaseSensitivePrimitives
- , SpiritCaseInsensitivePrimitives
- >
- {};
-
- template<typename Grammar>
- struct SpiritComposites
- : proto::or_<
- proto::bitwise_or< Grammar, Grammar >
- , proto::shift_right< Grammar, Grammar >
- , proto::minus< Grammar, Grammar >
- , proto::dereference< Grammar >
- , proto::posit< Grammar >
- , proto::logical_not< Grammar >
- >
- {};
-
- // Regular Spirit grammar, has no-case transforms
- struct SpiritGrammar
- : proto::or_<
- SpiritComposites<SpiritGrammar>
- , SpiritPrimitives
- >
- {};
-
- // Spirit grammar with the skipper transform
- struct SkipperGrammar
- : proto::or_<
- SpiritComposites<SkipperGrammar>
- , skip_primitives<SpiritPrimitives>
- >
- {};
-
- ///////////////////////////////////////////////////////////////////////////
- // Directives
- ///////////////////////////////////////////////////////////////////////////
-
- struct no_case_directive
- {
- template<typename Expr>
- typename SpiritGrammar::apply<Expr, mpl::void_, mpl::void_>::type const
- operator [](Expr const &expr) const
- {
- mpl::void_ null;
- return SpiritGrammar::call(expr, null, null);
- }
- };
-
- // no_case
- no_case_directive const no_case = {};
-
- template<typename Skipper>
- struct skip_directive
- {
- skip_directive(Skipper const &skip)
- : skip_(skip)
- {}
-
- template<typename Expr>
- typename SkipperGrammar::apply<Expr, Skipper, mpl::void_>::type const
- operator [](Expr const &expr) const
- {
- mpl::void_ null;
- return SkipperGrammar::call(expr, this->skip_, null);
- }
- private:
- Skipper skip_;
- };
-
- // skip
- template<typename Skipper>
- skip_directive<Skipper> skip(Skipper const &skip)
- {
- return skip_directive<Skipper>(skip);
- }
-
- ///////////////////////////////////////////////////////////////////////////
- // parse
- ///////////////////////////////////////////////////////////////////////////
-
- template<typename FwdIter, typename Rule>
- bool parse(FwdIter begin, FwdIter end, Rule const &rule)
- {
- // make sure the rule corresponds to the Spirit grammar:
- BOOST_MPL_ASSERT((proto::matches<Rule, SpiritGrammar>));
-
- spirit_context<FwdIter> ctx(begin, end);
- return proto::eval(rule, ctx);
- }
-
- // parse with a skip parser can be implemented in one of two ways:
- // Method 1)
- // The skip parser is passed to all the parsers which invoke it
- // before they invoke themselves. This is how Spirit-1 does it,
- // and it is the cause of the Scanner Business. However, it has
- // the advantage of not needing a parser transformation phase.
- // Method 2)
- // Transform the expression template to insert the skip parser
- // in between all sequenced parsers. That is, transform (A >> B)
- // to (*skip >> A >> *skip >> B). This has the advantage of making
- // it unnecessary to pass the scanner to all the parsers, which
- // means its type doesn't show up in function signatures, avoiding
- // the Scanner Business.
- // Recommendation:
- // Both methods should be supported. Method 1 should be preferred
- // when calling parse with parsers defined inline. Method 2 should
- // be preferred when a parser expression is assigned to a rule<>,
- // thereby making the type of the rule<> independent of the skip
- // parser used. I imagine a syntax like:
- // rule<> r = skip(space)[A >> B >> C]
- template<typename FwdIter, typename Rule, typename Skipper>
- bool parse(FwdIter begin, FwdIter end, Rule const &rule, Skipper const &skipper)
- {
- // make sure the rule corresponds to the Spirit grammar:
- BOOST_MPL_ASSERT((proto::matches<Rule, SpiritGrammar>));
-
- //// Method 1: pass skip parser in the context structure.
- //spirit_context<FwdIter, Skipper> ctx(begin, end, skipper);
- //return proto::eval(rule, ctx);
-
- // Method 2: Embed skip parser via tree transformation.
- spirit_context<FwdIter> ctx(begin, end);
- return proto::eval(spirit2::skip(skipper)[rule], ctx);
- }
-
-}}
-
-using namespace boost;
-using namespace spirit2;
-
-void test_toy_spirit()
-{
- std::string str("abcd");
-
- // This will fail:
- BOOST_CHECK(!spirit2::parse(str.begin(), str.end()
- , char_ >> char_('a')));
-
- // This will succeed:
- BOOST_CHECK(spirit2::parse(str.begin(), str.end()
- , char_ >> char_('b') >> char_ >> 'd'));
-
- // This will succeed:
- BOOST_CHECK(spirit2::parse(str.begin(), str.end()
- , 'a' >> ('c' >> char_ | 'b' >> char_('d') | 'b' >> char_('c')) >> 'd'));
-
- // This will succeed:
- BOOST_CHECK(spirit2::parse(str.begin(), str.end()
- , *(char_ - 'd')));
-
- // This will succeed:
- BOOST_CHECK(spirit2::parse(str.begin(), str.end()
- , no_case[char_('A') >> 'B' >> "CD"]));
-
- // This will succeed:
- BOOST_CHECK(spirit2::parse(str.begin(), str.end()
- , no_case[*char_('A','Z')]));
-
- literal<char> a = lit('a');
- literal<char const *> bcd = lit("bcd");
-
- // This will succeed:
- BOOST_CHECK(spirit2::parse(str.begin(), str.end()
- , +~~a >> no_case[bcd]));
-
- // Scanner Business: R.I.P. :-)
- str = "a b cd";
- BOOST_CHECK(spirit2::parse(str.begin(), str.end()
- , char_('a') >> 'b' >> 'c' >> 'd', space >> space));
-
-}
-
-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 and and toy spirit-2");
-
- test->add(BOOST_TEST_CASE(&test_toy_spirit));
-
- return test;
-}

Deleted: branches/proto/v3/libs/xpressive/proto3/test/toy_spirit2.cpp
==============================================================================
--- branches/proto/v3/libs/xpressive/proto3/test/toy_spirit2.cpp 2007-11-08 21:47:32 EST (Thu, 08 Nov 2007)
+++ (empty file)
@@ -1,574 +0,0 @@
-///////////////////////////////////////////////////////////////////////////////
-// toy_spirit2.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 <iomanip>
-#include <iostream>
-#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/arg.hpp>
-#include <boost/xpressive/proto3/transform/construct.hpp>
-#include <boost/xpressive/proto3/transform/fold_tree.hpp>
-#include <boost/xpressive/proto3/transform/list.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/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
-
- // Composite parser that contains a Fusion cons-list of other parsers
- // OR
- // A compiler that compiles an expression and wraps the result in
- // a composite<> wrapper
- template<typename Tag, typename List>
- struct composite
- {
- composite(List const &list)
- : elems(list)
- {}
-
- List elems;
- };
-
- template<typename Tag, typename Grammar>
- struct as_composite
- : Grammar
- {
- as_composite();
-
- // The apply<> struct and the call() member are to satisfy the
- // proto compiler/transform protocol
- template<typename Expr, typename State, typename Visitor>
- struct apply
- {
- typedef composite<
- Tag
- , typename Grammar::template apply<Expr, State, Visitor>::type
- > type;
- };
-
- template<typename Expr, typename State, typename Visitor>
- static typename apply<Expr, State, Visitor>::type
- call(Expr const &expr, State const &state, Visitor &visitor)
- {
- return typename apply<Expr, State, Visitor>::type
- (Grammar::call(expr, state, visitor));
- }
- };
-
- 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_range const &rng)
- : std::pair<char, char>(rng)
- {}
- };
-
- // The no-case directive
- struct no_case_tag {};
-
- // The no-case transform, applies the tree-transform with
- // mpl::true_ as the visitor.
- template<typename Grammar>
- struct no_case_transform
- : Grammar
- {
- no_case_transform();
-
- template<typename Expr, typename State, typename>
- struct apply
- : Grammar::template apply<Expr, State, mpl::true_>
- {};
-
- template<typename Expr, typename State, typename Visitor>
- static typename apply<Expr, State, Visitor>::type
- call(Expr const &expr, State const &state, Visitor &)
- {
- mpl::true_ case_sensitive;
- return Grammar::call(expr, state, case_sensitive);
- }
- };
-
- // remove_case specializations for stripping case-sensitivity from parsers
- template<typename T, bool CaseSensitive>
- struct remove_case
- {
- typedef T type;
- template<typename U> static U const &call(U const &t)
- {
- return t;
- }
- };
-
- template<>
- struct remove_case<char, true>
- {
- typedef ichar type;
- static ichar call(char ch)
- {
- return ichar(ch);
- }
- };
-
- template<>
- struct remove_case<char const *, true>
- {
- typedef istr type;
- static istr call(char const *sz)
- {
- return istr(sz);
- }
- };
-
- template<typename T, std::size_t N>
- struct remove_case<T(&)[N], true>
- : remove_case<char const *, true>
- {};
-
- template<>
- struct remove_case<char_range, true>
- {
- typedef ichar_range type;
- static ichar_range call(char_range const &rng)
- {
- return ichar_range(rng);
- }
- };
-
- // A case-sensitive transform that removes case conditionally, depending on
- // a compile-time flag carried by the visitor.
- template<typename Grammar>
- struct case_sensitive
- : Grammar
- {
- case_sensitive();
-
- template<typename Expr, typename State, typename Visitor>
- struct apply
- : remove_case<
- typename Grammar::template apply<Expr, State, Visitor>::type
- , Visitor::value
- >
- {};
-
- template<typename Expr, typename State, typename Visitor>
- static typename apply<Expr, State, Visitor>::type
- call(Expr const &expr, State const &state, Visitor &visitor)
- {
- return apply<Expr, State, Visitor>::call(Grammar::call(expr, state, visitor));
- }
- };
-
- ///////////////////////////////////////////////////////////////////////////////
- /// Begin ToySpiritGrammar here
- ///////////////////////////////////////////////////////////////////////////////
-
- struct ToySpiritGrammar;
-
- struct AnyChar
- : proto::terminal<char_tag>
- {};
-
- struct CharLiteral
- : proto::terminal<char>
- {};
-
- struct NTBSLiteral
- : proto::terminal<char const *>
- {};
-
- struct CharParser
- : proto::function<AnyChar, CharLiteral>
- {};
-
- struct CharRangeParser
- : proto::function<AnyChar, CharLiteral, CharLiteral>
- {};
-
- struct NoCase
- : proto::terminal<no_case_tag>
- {};
-
- // Extract the arg from terminals
- struct ToySpiritTerminal
- : proto::or_<
- proto::transform::arg< AnyChar >
- , case_sensitive< proto::transform::arg< CharLiteral > >
- , case_sensitive< proto::transform::arg< NTBSLiteral > >
- , case_sensitive<
- proto::transform::arg< proto::transform::arg_c< CharParser, 1 > > // char_('a')
- >
- , case_sensitive<
- proto::transform::construct< // char_('a','z')
- CharRangeParser
- , char_range(
- proto::transform::arg< proto::transform::arg_c< proto::_, 1 > >
- , proto::transform::arg< proto::transform::arg_c< proto::_, 2 > >
- )
- >
- >
- >
- {};
-
- // sequence rule folds all >>'s together into a list
- // and wraps the result in a composite<> wrapper
- struct ToySpiritSequence
- : as_composite<
- proto::tag::shift_right
- , proto::transform::reverse_fold_tree<
- proto::tag::shift_right
- , proto::transform::list<ToySpiritGrammar>
- , fusion::nil
- >
- >
- {};
-
- // alternate rule folds all |'s together into a list
- // and wraps the result in a composite<> wrapper
- struct ToySpiritAlternate
- : as_composite<
- proto::tag::bitwise_or
- , proto::transform::reverse_fold_tree<
- proto::tag::bitwise_or
- , proto::transform::list<ToySpiritGrammar>
- , fusion::nil
- >
- >
- {};
-
- // Directives such as no_case are handled here
- struct ToySpiritDirective
- : no_case_transform<
- proto::transform::arg_c<
- proto::subscript< NoCase, ToySpiritGrammar >
- , 1
- >
- >
- {};
-
- // A ToySpiritGrammar is an alternate, a sequence, a directive or a terminal
- struct ToySpiritGrammar
- : proto::or_<
- ToySpiritSequence
- , ToySpiritAlternate
- , ToySpiritDirective
- , ToySpiritTerminal
- >
- {};
-
- ///////////////////////////////////////////////////////////////////////////////
- /// End ToySpiritGrammar
- ///////////////////////////////////////////////////////////////////////////////
-
- // 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
- : with_reset<Iterator, parser<Iterator> >
- {
- typedef 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()(composite<proto::tag::bitwise_or, List> const &alternates) const
- {
- return fusion::any(alternates.elems, *static_cast<with_reset const *>(this));
- }
-
- template<typename List>
- bool operator()(composite<proto::tag::shift_right, 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, ToySpiritGrammar >, 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(ToySpiritGrammar::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, ToySpiritGrammar >, bool >::type
- parse_impl(Rule const &rule, Iterator begin, Iterator end)
- {
- BOOST_MPL_ASSERT((proto::matches<Rule, ToySpiritGrammar>));
- 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);
- }
-
-}}
-
-using namespace boost;
-
-void test_toy_spirit2()
-{
- using spirit2::no_case;
- std::string hello("abcd");
-
- BOOST_CHECK(
- spirit2::parse(
- "abcd"
- , hello.begin()
- , hello.end()
- )
- );
-
- BOOST_CHECK(
- spirit2::parse(
- char_ >> char_('b') >> 'c' >> char_
- , hello.begin()
- , hello.end()
- )
- );
-
- BOOST_CHECK(
- !spirit2::parse(
- char_ >> char_('b') >> 'c' >> 'D'
- , hello.begin()
- , hello.end()
- )
- );
-
- BOOST_CHECK(
- 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_spirit2));
-
- 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