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