summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/phoenix/example/identity_transform.cpp
blob: 471bec099930027a369dd2f1f2f232304da44529 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*=============================================================================
    Copyright (c) 2001-2007 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 <boost/phoenix.hpp>

#include <iostream>
#include <sstream>

namespace proto = boost::proto;
namespace phoenix = boost::phoenix;

template <typename Rule>
struct identity_transform;

struct identity_actions
{
    template <typename Rule>
    struct when : phoenix::call<identity_transform<Rule> > {};
};

template <>
struct identity_actions::when<phoenix::rule::argument>
    : proto::call<
        identity_transform<phoenix::rule::argument>(proto::_value, phoenix::_context)
    > {};

template <>
struct identity_actions::when<phoenix::rule::terminal>
    : proto::call<
        identity_transform<phoenix::rule::terminal>(proto::_value, phoenix::_context)
    > {};

template <>
struct identity_actions::when<phoenix::rule::custom_terminal>
    : proto::lazy<
        identity_transform<proto::_value>(proto::_value, phoenix::_context)
    > {};

template <>
struct identity_transform<phoenix::rule::terminal>
{
    typedef std::string result_type;

    template <typename Terminal, typename Context>
    std::string operator()(Terminal const & terminal, Context) const
    {
        std::stringstream ss;
        ss << "val(" << terminal << ")";
        return ss.str();
    }

    template <typename Context>
    std::string operator()(char const * terminal, Context) const
    {
        std::stringstream ss;
        ss << "val(\"" << terminal << "\")";
        return ss.str();
    }
};

template <typename T>
struct identity_transform<boost::reference_wrapper<T> >
{
    typedef std::string result_type;

    template <typename Terminal, typename Context>
    std::string operator()(Terminal const & terminal, Context) const
    {
        std::stringstream ss;
        ss << "ref(" << terminal << ")";
        return ss.str();
    }


    template <int N, typename Context>
    std::string operator()(boost::reference_wrapper<char const *> terminal, Context) const
    {
        std::stringstream ss;
        ss << "ref(\"" << terminal << "\")";
        return ss.str();
    }
    
    template <int N, typename Context>
    std::string operator()(boost::reference_wrapper<char const [N]> terminal, Context) const
    {
        std::stringstream ss;
        ss << "ref(\"" << terminal << "\")";
        return ss.str();
    }
};

template <>
struct identity_transform<phoenix::rule::argument>
{
    typedef std::string result_type;

    template <typename N, typename Context>
    std::string operator()(N, Context) const
    {
        std::stringstream ss;
        ss << "_" << N::value;
        return ss.str();
    }
};


template <typename Expr>
void identity(Expr const & expr)
{
    std::cout << phoenix::eval(expr, phoenix::context(int(), identity_actions())) << "\n";
}

int main()
{

    identity(phoenix::val(8));
    identity(phoenix::val("8"));
    identity(phoenix::ref("blubb"));
    identity(phoenix::arg_names::_1);
}