Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
printer.hh
Go to the documentation of this file.
1 #ifndef VCSN_CORE_RAT_PRINTER_HH
2 # define VCSN_CORE_RAT_PRINTER_HH
3 
4 # include <iostream>
5 
6 # include <vcsn/ctx/traits.hh>
8 # include <vcsn/core/rat/visitor.hh>
9 # include <vcsn/misc/attributes.hh>
10 # include <vcsn/misc/cast.hh>
11 # include <vcsn/misc/symbol.hh>
12 
13 namespace vcsn
14 {
15  namespace rat
16  {
17 
18  template <typename RatExpSet>
19  class printer
20  : public RatExpSet::const_visitor
21  {
22  public:
23  using ratexpset_t = RatExpSet;
25  using identities_t = typename ratexpset_t::identities_t;
26  using weight_t = typename context_t::weightset_t::value_t;
27  using super_t = typename ratexpset_t::const_visitor;
28  using node_t = typename super_t::node_t;
29  using inner_t = typename super_t::inner_t;
30  template <type_t Type>
31  using unary_t = typename super_t::template unary_t<Type>;
32  template <type_t Type>
33  using variadic_t = typename super_t::template variadic_t<Type>;
34  using leaf_t = typename super_t::leaf_t;
35 
36  printer(const ratexpset_t& rs,
37  std::ostream& out,
38  const bool debug = !!getenv("VCSN_PARENS"));
39 
41  void format(symbol format);
42 
44  std::ostream& operator()(const node_t& v);
45 
47  std::ostream&
48  operator()(const std::shared_ptr<const node_t>& v)
49  {
50  return operator()(*v);
51  }
52 
53  private:
54 
55 # define DEFINE(Type) \
56  using Type ## _t = typename super_t::Type ## _t; \
57  virtual void visit(const Type ## _t& v)
58 
62  DEFINE(ldiv) { print_(v, ldiv_); }
68  DEFINE(star) { print_(v, star_); }
69  DEFINE(sum) { print_(v, sum_); }
72 
73 # undef DEFINE
74 
76  bool is_word_(const node_t& v) const
77  {
78  const atom_t* atom = dynamic_cast<const atom_t*>(&v);
79  return atom && ! ctx_.labelset()->is_letter(atom->value());
80  }
81 
89  enum class precedence_t
90  {
91  sum,
92  shuffle,
94  ldiv,
95  prod,
96  word = prod, // Multi-letter atoms.
97  lweight,
98  rweight,
99  star,
100  complement,
102  zero,
103  one,
104  atom,
105  };
106 
108  precedence_t precedence_(const node_t& v) const;
109 
111  void print_child_(const node_t& child, const node_t& parent);
112 
114  template <rat::exp::type_t Type>
115  void print_(const unary_t<Type>& n, const char* op);
116 
118  template <rat::exp::type_t Type>
119  void print_(const variadic_t<Type>& n, const char* op);
120 
122  ATTRIBUTE_PURE
123  bool shows_left_weight_(const node_t& n)
124  {
125  return n.type() == rat::type_t::lweight;
126  }
127 
129  std::ostream& out_;
131  std::string format_;
133  const context_t& ctx_;
136  const bool debug_;
137 
142  const char* lgroup_ = nullptr;
143  const char* rgroup_ = nullptr;
145  const char* langle_ = nullptr;
146  const char* rangle_ = nullptr;
148  const char* lparen_ = nullptr;
149  const char* rparen_ = nullptr;
151  const char* lmul_ = nullptr;
152  const char* rmul_ = nullptr;
154  const char* ldiv_ = nullptr;
156  const char* star_ = nullptr;
157  const char* complement_ = nullptr;
158  const char* transposition_ = nullptr;
159  const char* conjunction_ = nullptr;
160  const char* shuffle_ = nullptr;
161  const char* product_ = nullptr;
162  const char* sum_ = nullptr;
164  const char* zero_ = nullptr;
165  const char* one_ = nullptr;
166  };
167 
168  } // namespace rat
169 } // namespace vcsn
170 
171 # include <vcsn/core/rat/printer.hxx>
172 
173 #endif // !VCSN_CORE_RAT_PRINTER_HH
precedence_t
The possible node precedence levels, increasing.
Definition: printer.hh:89
const char * rangle_
Definition: printer.hh:146
const char * star_
The ratexp operators.
Definition: printer.hh:156
const char * one_
Definition: printer.hh:165
An inner node with multiple children.
Definition: fwd.hh:123
const char * rparen_
Definition: printer.hh:149
const char * product_
Definition: printer.hh:161
#define DEFINE(Type)
Definition: printer.hh:55
const char * ldiv_
Quotient.
Definition: printer.hh:154
bool is_word_(const node_t &v) const
Whether is an atom whose label is not a letter.
Definition: printer.hh:76
typename super_t::inner_t inner_t
Definition: printer.hh:29
std::string format_
Output format.
Definition: printer.hh:131
const char * zero_
The constants.
Definition: printer.hh:164
typename super_t::template unary_t< Type > unary_t
Definition: printer.hh:31
boost::flyweight< std::string, boost::flyweights::no_tracking > symbol
An internalized string.
Definition: symbol.hh:24
ATTRIBUTE_PURE bool shows_left_weight_(const node_t &n)
Whether the left weight shows.
Definition: printer.hh:123
typename super_t::atom_t atom_t
Definition: printer.hh:59
const char * shuffle_
Definition: printer.hh:160
typename super_t::template variadic_t< Type > variadic_t
Definition: printer.hh:33
std::ostream & out_
Output stream.
Definition: printer.hh:129
typename context_t::weightset_t::value_t weight_t
Definition: printer.hh:26
void print_(const unary_t< Type > &n, const char *op)
Print a unary node.
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:32
void format(symbol format)
Set output format.
Definition: printer.hxx:75
void print_child_(const node_t &child, const node_t &parent)
Print the given child node, also knowing its parent.
Definition: printer.hxx:192
typename super_t::node_t node_t
Definition: printer.hh:28
precedence_t precedence_(const node_t &v) const
The precedence of v (to decide when to print parens).
Definition: printer.hxx:126
const char * conjunction_
Definition: printer.hh:159
const char * transposition_
Definition: printer.hh:158
const char * lmul_
External product.
Definition: printer.hh:151
const char * lgroup_
Left and right boundaries (typically braces for LaTeX).
Definition: printer.hh:142
context_t_of< ratexpset_t > context_t
Definition: printer.hh:24
An inner node implementing a weight.
Definition: fwd.hh:145
typename super_t::leaf_t leaf_t
Definition: printer.hh:34
const bool debug_
Whether to be overly verbose.
Definition: printer.hh:136
const char * complement_
Definition: printer.hh:157
const char * langle_
Left and right angle brackets for weights.
Definition: printer.hh:145
const identities_t identities_
Definition: printer.hh:134
const char * sum_
Definition: printer.hh:162
typename ratexpset_t::identities_t identities_t
Definition: printer.hh:25
printer(const ratexpset_t &rs, std::ostream &out, const bool debug=!!getenv("VCSN_PARENS"))
Definition: printer.hxx:41
RatExpSet ratexpset_t
Definition: printer.hh:23
std::ostream & operator()(const node_t &v)
Entry point: print v.
Definition: printer.hxx:57
const context_t & ctx_
Context to decode labels and weights.
Definition: printer.hh:133
const char * lparen_
Left and right parentheses.
Definition: printer.hh:148
const char * rmul_
Definition: printer.hh:152
typename ratexpset_t::const_visitor super_t
Definition: printer.hh:27
const char * rgroup_
Definition: printer.hh:143
std::ostream & operator()(const std::shared_ptr< const node_t > &v)
Entry point: print v.
Definition: printer.hh:48