Awali
Another Weighted Automata library
char.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_ALPHABETS_CHAR_HH
18 # define AWALI_ALPHABETS_CHAR_HH
19 
20 # include <cassert>
21 # include <string>
22 # include <iostream>
23 
24 #include <awali/common/json/node.cc>
26 #include <awali/common/version.hh>
27 
28 namespace awali {
29  namespace sttc {
32  {
33  public:
34  using letter_t = char;
35  using word_t = std::string;
36 
37  virtual ~char_letters() = default;
38 
39 
40  static std::string sname()
41  {
42  return "char";
43  }
44 
45  static std::ostream& jsname(std::ostream& o)
46  {
47  o << '"' << "Char" << '"';
48  return o;
49  }
50 
51  virtual std::string vname(bool = true) const
52  {
53  return sname();
54  }
55 
56  /*
57  word_t
58  to_word(const letter_t l) const
59  {
60  return {l};
61  }
62 
63  const word_t&
64  to_word(const word_t& l) const
65  {
66  return l;
67  }
68 
69  word_t
70  concat(const letter_t l, const letter_t r) const
71  {
72  return {l, r};
73  }
74 
75  word_t
76  concat(const word_t& l, const letter_t r) const
77  {
78  return l + r;
79  }
80 
81  word_t
82  concat(const letter_t l, const word_t& r) const
83  {
84  return l + r;
85  }
86 
87  word_t
88  concat(const word_t& l, const word_t& r) const
89  {
90  return l + r;
91  }
92 
94  word_t delimit(const word_t& w) const
95  {
96  return concat(concat(special_letter(), w), special_letter());
97  }
98 
100  word_t undelimit(const word_t& w) const
101  {
102  size_t s = w.size();
103  assert(2 <= s);
104  assert(w[0] == special_letter());
105  assert(w[s-1] == special_letter());
106  return w.substr(1, s-2);
107  }
108 
109  static word_t
110  empty_word()
111  {
112  return {};
113  }
114 
115  static bool
116  is_empty_word(const word_t& w)
117  {
118  return w.empty();
119  }
120 
121  word_t
122  transpose(const word_t& w) const
123  {
124  // C++11 lacks std::rbegin/rend...
125  return {w.rbegin(), w.rend()};
126  }
127  */
128 
129  static letter_t
131  {
132  return l;
133  }
134 
135  static bool
136  equals(const letter_t& l1, const letter_t& l2)
137  {
138  return l1 == l2;
139  }
140  /*
141  bool
142  equals(const word_t& w1, const word_t& w2) const
143  {
144  return w1 == w2;
145  }
146  */
147  static bool
149  {
150  return true;
151  }
152  /*
153  bool
154  is_letter(const word_t& w) const
155  {
156  return w.size() == 1;
157  }
158  */
161  static constexpr letter_t one_letter() { return 0; }
162 
165  static constexpr letter_t special_letter() { return 127; }
166 
167  static const std::string& separation_mark(){
168  static const std::string sep{""};
169  return sep;
170  }
171 
172  static std::ostream&
173  print(const letter_t& l, std::ostream& o)
174  {
175  if (l != one_letter() && l != special_letter())
176  o << l;
177  return o;
178  }
179 
180  /*
181  std::ostream&
182  print(const word_t& w, std::ostream& o) const
183  {
184  return o << format(w);
185  }
186  */
187 
188  static letter_t
189  parse_one_letter(const std::string& s, size_t& p) {
190  --p;
191  return s[p];
192  }
193 
194 
195  static letter_t
196  conv_one_letter(std::istream& i)
197  {
198  return i.get();
199  }
200 
201  static std::string
202  format(const letter_t l)
203  {
204  if (l == one_letter() || l == special_letter())
205  return {};
206  else
207  return str_escape(l);
208  }
209 
210 
211  template<unsigned version = version::fsm_json>
213  const
214  {
215  switch (version) {
216  case 0:
217  throw parse_exception("[char] Unsupported fsm-json version:"
218  + std::to_string(version));
219  case 1:
220  default:
221  return new json::string_t(l);
222  }
223  }
224 
225 
226  template<unsigned version = version::fsm_json>
228  const
229  {
230  switch (version) {
231  case 0:
232  throw parse_exception("[char] Unsupported fsm-json version:"
233  + std::to_string(version));
234  case 1:
235  default:
236  return new json::string_t("Char");
237 
238  }
239  }
240 
241 
242  template<unsigned version = version::fsm_json>
243  static letter_t
244  letter_from_json(json::node_t const* p, bool allow_empty=false)
245  {
246  switch (version) {
247  case 0:
248  case 1:
249  default:
250  if(p->kind == json::STRING) {
251  std::string s = p->to_string();
252  if(s.empty() && allow_empty)
253  return one_letter();
254  if(s.size()==1)
255  return s[0];
256  }
257  throw parse_exception("Json: Bad char format");
258  }
259  }
260 
261  /*
262  std::string
263  format(const word_t& w) const
264  {
265  size_t s = w.size();
266 
267  std::string res;
268  if (s == 0
269  || (s == 1 && w[0] == one_letter()))
270  res = "\\e";
271 
272  // If the string starts or ends with the special letter, skip
273  // it. If the resulting string is empty, format it this way.
274  // (We DON'T want to format it as "\\e".)
275  else if (w[0] == special_letter())
276  res = (s == 1) ? "" : str_escape(w.substr(1));
277  else if (s > 1 && w[s - 1] == special_letter())
278  res = str_escape(w.substr(0, s - 1));
279  else
280  res = str_escape(w);
281  return res;
282  }
283  */
284 
285  };
286 
287  }
288 }//end of ns awali::stc
289 
290 #endif // !AWALI_ALPHABETS_CHAR_HH
Definition: node.hh:193
virtual std::string to_string() const
Coerces this node_t to an std::string.
Definition: node.hh:331
node_kind_t const kind
Definition: node.hh:196
Definition: node.hh:529
helper for manipulations of char letters
Definition: char.hh:32
virtual ~char_letters()=default
static letter_t parse_one_letter(const std::string &s, size_t &p)
Definition: char.hh:189
json::node_t * to_json() const
Definition: char.hh:227
static std::ostream & print(const letter_t &l, std::ostream &o)
Definition: char.hh:173
virtual std::string vname(bool=true) const
Definition: char.hh:51
char letter_t
Definition: char.hh:34
static bool is_letter(const letter_t &)
Definition: char.hh:148
static std::ostream & jsname(std::ostream &o)
Definition: char.hh:45
std::string word_t
Definition: char.hh:35
static constexpr letter_t special_letter()
The reserved letter used to forge the labels for initial and final transitions.
Definition: char.hh:165
static const std::string & separation_mark()
Definition: char.hh:167
static letter_t conv_one_letter(std::istream &i)
Definition: char.hh:196
static bool equals(const letter_t &l1, const letter_t &l2)
Definition: char.hh:136
json::string_t * letter_to_json(letter_t const &l) const
Definition: char.hh:212
static std::string format(const letter_t l)
Definition: char.hh:202
static std::string sname()
Definition: char.hh:40
static letter_t letter_from_json(json::node_t const *p, bool allow_empty=false)
Definition: char.hh:244
static letter_t transpose(letter_t l)
Definition: char.hh:130
static constexpr letter_t one_letter()
The reserved letter used to forge the "one" label (the unit, the identity).
Definition: char.hh:161
@ STRING
Definition: node.hh:97
std::string to_string(identities i)
std::ostream & str_escape(std::ostream &os, const int c)
Definition: escape.hh:30
Main namespace of Awali.
Definition: ato.hh:22
Exceptions thrown during parsing.
Definition: parse_exception.hh:26