• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
13 namespace proto = boost::proto;
14 namespace phoenix = boost::phoenix;
15 
16 template <typename Rule>
17 struct identity_transform;
18 
19 struct identity_actions
20 {
21     template <typename Rule>
22     struct when : phoenix::call<identity_transform<Rule> > {};
23 };
24 
25 template <>
26 struct identity_actions::when<phoenix::rule::argument>
27     : proto::call<
28         identity_transform<phoenix::rule::argument>(proto::_value, phoenix::_context)
29     > {};
30 
31 template <>
32 struct identity_actions::when<phoenix::rule::terminal>
33     : proto::call<
34         identity_transform<phoenix::rule::terminal>(proto::_value, phoenix::_context)
35     > {};
36 
37 template <>
38 struct identity_actions::when<phoenix::rule::custom_terminal>
39     : proto::lazy<
40         identity_transform<proto::_value>(proto::_value, phoenix::_context)
41     > {};
42 
43 template <>
44 struct identity_transform<phoenix::rule::terminal>
45 {
46     typedef std::string result_type;
47 
48     template <typename Terminal, typename Context>
operator ()identity_transform49     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>
operator ()identity_transform57     std::string operator()(char const * terminal, Context) const
58     {
59         std::stringstream ss;
60         ss << "val(\"" << terminal << "\")";
61         return ss.str();
62     }
63 };
64 
65 template <typename T>
66 struct identity_transform<boost::reference_wrapper<T> >
67 {
68     typedef std::string result_type;
69 
70     template <typename Terminal, typename Context>
operator ()identity_transform71     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>
operator ()identity_transform80     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>
operator ()identity_transform88     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 
96 template <>
97 struct identity_transform<phoenix::rule::argument>
98 {
99     typedef std::string result_type;
100 
101     template <typename N, typename Context>
operator ()identity_transform102     std::string operator()(N, Context) const
103     {
104         std::stringstream ss;
105         ss << "_" << N::value;
106         return ss.str();
107     }
108 };
109 
110 
111 template <typename Expr>
identity(Expr const & expr)112 void identity(Expr const & expr)
113 {
114     std::cout << phoenix::eval(expr, phoenix::context(int(), identity_actions())) << "\n";
115 }
116 
main()117 int main()
118 {
119 
120     identity(phoenix::val(8));
121     identity(phoenix::val("8"));
122     identity(phoenix::ref("blubb"));
123     identity(phoenix::arg_names::_1);
124 }
125