1/*=============================================================================
2 Copyright (c) 2001-2007 Joel de Guzman
3
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6==============================================================================*/
7
8#include <boost/phoenix.hpp>
9
10#include <iostream>
11#include <sstream>
12
13namespace proto = boost::proto;
14namespace phoenix = boost::phoenix;
15
16template <typename Rule>
17struct identity_transform;
18
19struct identity_actions
20{
21 template <typename Rule>
22 struct when : phoenix::call<identity_transform<Rule> > {};
23};
24
25template <>
26struct identity_actions::when<phoenix::rule::argument>
27 : proto::call<
28 identity_transform<phoenix::rule::argument>(proto::_value, phoenix::_context)
29 > {};
30
31template <>
32struct identity_actions::when<phoenix::rule::terminal>
33 : proto::call<
34 identity_transform<phoenix::rule::terminal>(proto::_value, phoenix::_context)
35 > {};
36
37template <>
38struct identity_actions::when<phoenix::rule::custom_terminal>
39 : proto::lazy<
40 identity_transform<proto::_value>(proto::_value, phoenix::_context)
41 > {};
42
43template <>
44struct identity_transform<phoenix::rule::terminal>
45{
46 typedef std::string result_type;
47
48 template <typename Terminal, typename Context>
49 std::string operator()(Terminal const & terminal, Context) const
50 {
51 std::stringstream ss;
52 ss << "val(" << terminal << ")";
53 return ss.str();
54 }
55
56 template <typename Context>
57 std::string operator()(char const * terminal, Context) const
58 {
59 std::stringstream ss;
60 ss << "val(\"" << terminal << "\")";
61 return ss.str();
62 }
63};
64
65template <typename T>
66struct identity_transform<boost::reference_wrapper<T> >
67{
68 typedef std::string result_type;
69
70 template <typename Terminal, typename Context>
71 std::string operator()(Terminal const & terminal, Context) const
72 {
73 std::stringstream ss;
74 ss << "ref(" << terminal << ")";
75 return ss.str();
76 }
77
78
79 template <int N, typename Context>
80 std::string operator()(boost::reference_wrapper<char const *> terminal, Context) const
81 {
82 std::stringstream ss;
83 ss << "ref(\"" << terminal << "\")";
84 return ss.str();
85 }
86
87 template <int N, typename Context>
88 std::string operator()(boost::reference_wrapper<char const [N]> terminal, Context) const
89 {
90 std::stringstream ss;
91 ss << "ref(\"" << terminal << "\")";
92 return ss.str();
93 }
94};
95
96template <>
97struct identity_transform<phoenix::rule::argument>
98{
99 typedef std::string result_type;
100
101 template <typename N, typename Context>
102 std::string operator()(N, Context) const
103 {
104 std::stringstream ss;
105 ss << "_" << N::value;
106 return ss.str();
107 }
108};
109
110
111template <typename Expr>
112void identity(Expr const & expr)
113{
114 std::cout << phoenix::eval(expr, phoenix::context(env: int(), actions: identity_actions())) << "\n";
115}
116
117int main()
118{
119
120 identity(expr: phoenix::val(t: 8));
121 identity(expr: phoenix::val(t: "8"));
122 identity(expr: phoenix::ref(t: "blubb"));
123 identity(expr: phoenix::arg_names::_1);
124}
125

source code of boost/libs/phoenix/example/identity_transform.cpp