mCRL2
Loading...
Searching...
No Matches
print.h
Go to the documentation of this file.
1// Author(s): Wieger Wesselink
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
11
12#ifndef MCRL2_CORE_PRINT_H
13#define MCRL2_CORE_PRINT_H
14
15#include <cctype>
18
19namespace mcrl2
20{
21namespace core
22{
23
25namespace detail
26{
27
28const int max_precedence = 10000;
29
30template <typename T>
31int precedence(const T&)
32{
33 return max_precedence;
34}
35
36template <typename Derived>
37struct printer: public core::traverser<Derived>
38{
39 typedef core::traverser<Derived> super;
40
41 using super::enter;
42 using super::leave;
43 using super::apply;
44
45 std::ostream* m_out;
46
47 Derived& derived()
48 {
49 return static_cast<Derived&>(*this);
50 }
51
52 std::ostream& out()
53 {
54 return *m_out;
55 }
56
57 void print(const std::string& s)
58 {
59 out() << s;
60 }
61
62 template <typename T>
63 void print_expression(const T& x, bool needs_parentheses)
64 {
65 if (needs_parentheses)
66 {
67 derived().print("(");
68 }
69 derived().apply(x);
70 if (needs_parentheses)
71 {
72 derived().print(")");
73 }
74 }
75
76 template <typename T, typename U>
77 void print_unary_operand(const T& x, const U& operand)
78 {
79 print_expression(operand, precedence(operand) < precedence(x));
80 }
81
82 template <typename T>
83 void print_unary_left_operation(const T& x, const std::string& op)
84 {
85 derived().print(op);
86 print_unary_operand(x, x.operand());
87 }
88
89 template <typename T>
90 void print_unary_right_operation(const T& x, const std::string& op)
91 {
92 print_unary_operand(x, x.operand());
93 derived().print(op);
94 }
95
96 template <typename T>
97 void print_binary_operation(const T& x, const std::string& op)
98 {
99 const auto& x1 = x.left();
100 const auto& x2 = x.right();
101 auto p = precedence(x);
102 auto p1 = precedence(x1);
103 auto p2 = precedence(x2);
104 print_expression(x1, (p1 < p) || (p1 == p && !is_left_associative(x)));
105 derived().print(op);
106 print_expression(x2, (p2 < p) || (p2 == p && !is_right_associative(x)));
107 }
108
109 template <typename Container>
110 void print_list(const Container& container,
111 const std::string& opener = "(",
112 const std::string& closer = ")",
113 const std::string& separator = ", ",
114 bool print_empty_container = false
115 )
116 {
117 if (container.empty() && !print_empty_container)
118 {
119 return;
120 }
121 derived().print(opener);
122 for (auto i = container.begin(); i != container.end(); ++i)
123 {
124 if (i != container.begin())
125 {
126 derived().print(separator);
127 }
128 derived().apply(*i);
129 }
130 derived().print(closer);
131 }
132
133 template <typename T>
134 void apply(const std::list<T>& x)
135 {
136 derived().enter(x);
137 print_list(x, "", "", ", ");
138 derived().leave(x);
139 }
140
141 template <typename T>
142 void apply(const atermpp::term_list<T>& x)
143 {
144 derived().enter(x);
145 print_list(x, "", "", ", ");
146 derived().leave(x);
147 }
148
149 template <typename T>
150 void apply(const std::set<T>& x)
151 {
152 derived().enter(x);
153 print_list(x, "", "", ", ");
154 derived().leave(x);
155 }
156
157 void apply(const core::identifier_string& x)
158 {
159 derived().enter(x);
160 if (x == core::identifier_string())
161 {
162 derived().print("@NoValue");
163 }
164 else
165 {
166 derived().print(std::string(x));
167 }
168 derived().leave(x);
169 }
170
171 void apply(const atermpp::aterm_list& x)
172 {
173 derived().enter(x);
174 derived().print(utilities::to_string(x));
175 derived().leave(x);
176 }
177
178 void apply(const atermpp::aterm& x)
179 {
180 derived().enter(x);
181 derived().print(utilities::to_string(x));
182 derived().leave(x);
183 }
184
185 void apply(const atermpp::aterm_int& x)
186 {
187 derived().enter(x);
188 derived().print(utilities::to_string(x));
189 derived().leave(x);
190 }
191};
192
193template <template <class> class Traverser>
194struct apply_printer: public Traverser<apply_printer<Traverser>>
195{
196 typedef Traverser<apply_printer<Traverser>> super;
197
198 using super::enter;
199 using super::leave;
200 using super::apply;
201
202 explicit apply_printer(std::ostream& out)
203 {
204 typedef printer<apply_printer<Traverser> > Super;
205 static_cast<Super&>(*this).m_out = &out;
206 }
207
208};
209
210} // namespace detail
212
215{
216 template <typename T>
217 void operator()(const T& x, std::ostream& out)
218 {
219 core::detail::apply_printer<core::detail::printer> printer(out);
220 printer.apply(x);
221 }
222};
223
225template <typename T>
226std::string pp(const T& x)
227{
228 std::ostringstream out;
229 stream_printer()(x, out);
230 return out.str();
231}
232
233} // namespace core
234
235} // namespace mcrl2
236
237#endif // MCRL2_CORE_PRINT_H
An integer term stores a single std::size_t value. It carries no arguments.
Definition aterm_int.h:26
A list of aterm objects.
Definition aterm_list.h:24
add your file description here.
std::string print_list(const Container &v, const std::string &message="", bool print_index=false, bool boundary_spaces=true)
Creates a string representation of a container.
std::string pp(const identifier_string &x)
Definition core.cpp:26
int precedence(const data_expression &x)
Definition print.h:415
bool is_left_associative(const data_expression &x)
Definition print.h:428
bool is_right_associative(const data_expression &x)
Definition print.h:434
std::string print(const pbespg_solver_type solver_type)
Definition pbespgsolve.h:60
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
Definition indexed_set.h:72
add your file description here.
Prints the object x to a stream.
Definition print.h:215
void operator()(const T &x, std::ostream &out)
Definition print.h:217