Awali
Another Weighted Automata library
printer.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_CORE_RAT_PRINTER_HH
18 # define AWALI_CORE_RAT_PRINTER_HH
19 
20 # include <iostream>
21 
22 #include <awali/sttc/ctx/traits.hh>
26 #include <awali/sttc/misc/cast.hh>
27 
28 namespace awali { namespace sttc
29 {
30  namespace rat
31  {
32 
33  template <typename RatExpSet>
34  class printer
35  : public RatExpSet::const_visitor
36  {
37  public:
38  using ratexpset_t = RatExpSet;
40  using identities_t = typename ratexpset_t::identities_t;
41  using weight_t = typename context_t::weightset_t::value_t;
42  using super_type = typename ratexpset_t::const_visitor;
43  using node_t = typename super_type::node_t;
44  using inner_t = typename super_type::inner_t;
45  template <type_t Type>
46  using unary_t = typename super_type::template unary_t<Type>;
47  template <type_t Type>
48  using variadic_t = typename super_type::template variadic_t<Type>;
49  using leaf_t = typename super_type::leaf_t;
50 
51  printer(const ratexpset_t& rs,
52  std::ostream& out,
53  const bool debug = !!getenv("AWALI_PARENS"));
54 
56  void format(const std::string& format);
57 
59  std::ostream& operator()(const node_t& v);
60 
62  std::ostream&
63  operator()(const std::shared_ptr<const node_t>& v)
64  {
65  return operator()(*v);
66  }
67 
68  private:
69 
70 # define DEFINE(Type) \
71  using Type ## _t = typename super_type::Type ## _t; \
72  virtual void visit(const Type ## _t& v)
73 
74  DEFINE(atom);
75  DEFINE(complement) { print(v, complement_); }
76  DEFINE(conjunction) { print(v, conjunction_); }
77  DEFINE(ldiv) { print(v, ldiv_); }
78  DEFINE(lweight);
79  DEFINE(one);
80  DEFINE(prod) { print(v, product_); }
81  DEFINE(rweight);
82  DEFINE(shuffle) { print(v, shuffle_); }
83  DEFINE(star) { print(v, star_); }
84  DEFINE(sum) { print(v, sum_); }
85  DEFINE(transposition) { print(v, transposition_); }
86  DEFINE(zero);
87 
88 # undef DEFINE
89 
97  enum class precedence_t
98  {
99  sum,
100  shuffle,
101  conjunction,
102  ldiv,
103  prod,
104  word = prod, // Multi-letter atoms.
105  lweight,
106  rweight,
107  star,
108  complement,
110  zero,
111  one,
112  atom,
113  };
114 
116  precedence_t precedence(const node_t& v) const;
117 
119  void print_child(const node_t& child, const node_t& parent);
120 
122  template <rat::exp::type_t Type>
123  void print(const unary_t<Type>& n, const char* op);
124 
126  template <rat::exp::type_t Type>
127  void print(const variadic_t<Type>& n, const char* op);
128 
130  ATTRIBUTE_PURE
131  bool shows_left_weight_(const node_t& n)
132  {
133  return n.type() == rat::type_t::lweight;
134  }
135 
137  std::ostream& out_;
139  std::string format_;
141  const context_t& ctx_;
142  const identities_t identities_;
144  const bool debug_;
145 
147  const char* lgroup_ = nullptr;
148  const char* rgroup_ = nullptr;
150  const char* langle_ = nullptr;
151  const char* rangle_ = nullptr;
153  const char* lparen_ = nullptr;
154  const char* rparen_ = nullptr;
156  const char* lmul_ = nullptr;
157  const char* rmul_ = nullptr;
159  const char* ldiv_ = nullptr;
161  const char* star_ = nullptr;
162  const char* complement_ = nullptr;
163  const char* transposition_ = nullptr;
164  const char* conjunction_ = nullptr;
165  const char* shuffle_ = nullptr;
166  const char* product_ = nullptr;
167  const char* sum_ = nullptr;
169  const char* zero_ = nullptr;
170  const char* one_ = nullptr;
171  };
172 
173  } // namespace rat
174 }}//end of ns awali::stc
175 
177 
178 #endif // !AWALI_CORE_RAT_PRINTER_HH
Definition: printer.hh:36
std::ostream & operator()(const node_t &v)
Entry point: print v.
Definition: printer.hxx:72
typename ratexpset_t::const_visitor super_type
Definition: printer.hh:42
typename super_type::node_t node_t
Definition: printer.hh:43
RatExpSet ratexpset_t
Definition: printer.hh:38
std::ostream & operator()(const std::shared_ptr< const node_t > &v)
Entry point: print v.
Definition: printer.hh:63
typename super_type::leaf_t leaf_t
Definition: printer.hh:49
typename ratexpset_t::identities_t identities_t
Definition: printer.hh:40
typename super_type::inner_t inner_t
Definition: printer.hh:44
void format(const std::string &format)
Set output format.
Definition: printer.hxx:93
typename super_type::template unary_t< Type > unary_t
Definition: printer.hh:46
typename context_t::weightset_t::value_t weight_t
Definition: printer.hh:41
printer(const ratexpset_t &rs, std::ostream &out, const bool debug=!!getenv("AWALI_PARENS"))
Definition: printer.hxx:56
context_t_of< ratexpset_t > context_t
Definition: printer.hh:39
typename super_type::template variadic_t< Type > variadic_t
Definition: printer.hh:48
variadic< type_t::sum, Label, Weight > sum
Definition: fwd.hh:154
weight_node< type_t::lweight, Label, Weight > lweight
Definition: fwd.hh:164
unary< type_t::star, Label, Weight > star
Definition: fwd.hh:129
constant< type_t::zero, Label, Weight > zero
Definition: fwd.hh:113
variadic< type_t::ldiv, Label, Weight > ldiv
Definition: fwd.hh:148
variadic< type_t::conjunction, Label, Weight > conjunction
Definition: fwd.hh:145
variadic< type_t::shuffle, Label, Weight > shuffle
Definition: fwd.hh:151
weight_node< type_t::rweight, Label, Weight > rweight
Definition: fwd.hh:167
unary< type_t::transposition, Label, Weight > transposition
Definition: fwd.hh:132
constant< type_t::one, Label, Weight > one
Definition: fwd.hh:116
unary< type_t::complement, Label, Weight > complement
Definition: fwd.hh:126
variadic< type_t::prod, Label, Weight > prod
Definition: fwd.hh:142
typename internal::context_t_of_impl< internal::base_t< ValueSet > >::type context_t_of
Helper to retrieve the type of the context of a value set.
Definition: traits.hh:66
Main namespace of Awali.
Definition: ato.hh:22
#define DEFINE(Type)
Definition: printer.hh:70