Sophie

Sophie

distrib > Mandriva > 9.1 > i586 > by-pkgid > b9ba69a436161613d8fb030c8c726a8e > files > 672

spirit-1.5.1-2mdk.noarch.rpm

#include <iostream>
#include <cassert>

using namespace std;

//#define SPIRIT_DEBUG
#include "boost/spirit/core.hpp"
#include "boost/spirit/attribute/closure.hpp"
#include "boost/spirit/attribute/parametric.hpp"
#include "boost/phoenix/binders.hpp"
using namespace spirit;
using namespace phoenix;

///////////////////////////////////////////////////////////////////////////////
//
//  Closure tests
//
///////////////////////////////////////////////////////////////////////////////
struct my_closure1 : spirit::closure<my_closure1, double>
{
    member1 val;
};

struct my_closure2 : spirit::closure<my_closure2, char>
{
    member1 ch;
};

struct my_closure3 : free_closure<my_closure3, char>
{
    member1 ch;
};

struct X { int a; int b; };

struct my_closure4 : spirit::closure<my_closure4, X>
{
    member1 x;
};

void
closure_tests()
{
    rule<phrase_scanner_t, my_closure1::context_t> num_list;
    double n;

    num_list =
    (
        real_p[num_list.val = arg1] >>  *(',' >> real_p[num_list.val += arg1])
    )
    [var(n) = num_list.val];

    parse_info<char const*> pi;
    pi = parse("123, 456, 789", num_list, space_p);
    assert(pi.hit);
    assert(pi.full);
    assert(n == 123 + 456 + 789);

    rule<scanner<>, my_closure2::context_t> rev;
    rev = anychar_p[rev.ch = arg1] >> !rev >> fch_p(rev.ch);

    pi = parse("xyzzyx", rev);
    assert(pi.hit);
    assert(pi.full);

    pi = parse("xyzczyx", rev);
    assert(!pi.hit);

    my_closure3 clos;
    rule<> rev2 = clos
    [
        anychar_p[clos.ch = arg1] >> !rev2 >> fch_p(clos.ch)
    ];

    pi = parse("atoyyota", rev2);
    assert(pi.hit);
    assert(pi.full);

    pi = parse("whatdahell", rev2);
    assert(!pi.hit);

    rule<phrase_scanner_t, my_closure4::context_t> complex_p;
    complex_p =
            int_p[bind(&X::a)(complex_p.x) = arg1]
        >>  ','
        >>  int_p[bind(&X::b)(complex_p.x) = arg1]
    ;

    X x;
    pi = parse("123, 456", complex_p[var(x) = arg1], space_p);
    assert(pi.hit);
    assert(x.a == 123);
    assert(x.b == 456);
}

///////////////////////////////////////////////////////////////////////////////
//
//  Main
//
///////////////////////////////////////////////////////////////////////////////
int
main()
{
    closure_tests();
    cout << "Tests concluded successfully\n";
    return 0;
}