Awali
Another Weighted Automata library
js_parser_deprecated.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_ALGOS_JS_PARSER_DEPRECATED_HH
18 # define AWALI_ALGOS_JS_PARSER_DEPRECATED_HH
19 
20 #include <awali/sttc/misc/raise.hh>
22 #include <awali/common/json/parser.cc>
28 # include <stdexcept>
29 # include <stack>
30 # include <iostream>
31 # include <sstream>
32 
33 namespace awali {
34  namespace sttc {
35  namespace deprecated {
36 
37  namespace detail {
38 
39  template<typename RatExpSet>
40  struct js_exp_parser {
41  using ratexpset_t = RatExpSet;
44  using ratexp_t = typename ratexpset_t::value_t;
46 
47  js_exp_parser(const ratexpset_t& rs, std::istream& i)
48  : rs_(rs), i_(i)
49  {}
50 
51  ratexp_t parseNode(const std::string& key) {
52  ratexp_t e;
53  char c;
54  if(key == "Sum") {
55  bool first=true;
57  do {
58  ratexp_t f= parseNode();
59  if(first) {
60  e=f;
61  first=false;
62  }
63  else {
64  e=rs_.add(e,f);
65  }
66  i_ >> c;
67  } while (c==',');
68  if(c!=']')
69  throw std::runtime_error("Json parser: ratexp");
70  }
71  else if(key == "Prod") {
72  bool first=true;
74  do {
75  ratexp_t f= parseNode();
76  if(first) {
77  e=f;
78  first=false;
79  }
80  else
81  e=rs_.mul(e,f);
82  i_ >> c;
83  } while (c==',');
84  if(c!=']')
85  throw std::runtime_error("Json parser: ratexp");
86  }
87  else if(key == "Star") {
88  e= rs_.star(parseNode());
89  }
90  else if(key == "Label") {
91  e= rs_.atom(ls_.value_from_json<1>(i_));
92  }
93  else if(key == "LWeight") {
94  auto w = ws_.value_from_json<1>(i_);
95  std::string key2 = ::awali::internal::parsestring(i_);
97  e=rs_.lmul(w,parseNode(key2));
98  }
99  else if(key == "RWeight") {
100  auto w = ws_.value_from_json<1>(i_);
101  std::string key2 = ::awali::internal::parsestring(i_);
102  ::awali::internal::check(i_,':');
103  e=rs_.rmul(parseNode(key2),w);
104  }
105  else if(key == "One") {
106  char c2 = ::awali::internal::parsecst(i_);
107  if(c2 != 'n')
108  throw std::runtime_error("Json parser: ratexp");
109  e = rs_.one();
110  }
111  else if(key == "Zero") {
112  char c2 = ::awali::internal::parsecst(i_);
113  if(c2 != 'n')
114  throw std::runtime_error("Json parser: ratexp");
115  e = rs_.zero();
116  }
117  else
118  throw std::runtime_error("Json parser: ratexp");
119  return e;
120  }
121 
123  std::string key = ::awali::internal::get_first_attr(i_);
124  ratexp_t e=parseNode(key);
125  ::awali::internal::check(i_,'}');
126  return e;
127  }
128 
129  private:
130  const ratexpset_t& rs_;
131  std::istream& i_;
132  weightset_t ws_ = *rs_.weightset();
133  labelset_t ls_ = *rs_.labelset();
134 
135  };
136  }
137 
138  template <typename RatExpSet>
139  inline
140  typename RatExpSet::ratexp_t
141  js_parse_exp_content(const RatExpSet& rs,
142  std::istream& i)
143  {
145  return parser.parseNode();
146  }
147 
148  template <typename Context>
150  js_parse_aut_content(const Context& context, std::istream& i) {
151  // using aut_t = mutable_automaton<Context>;
152  ::awali::json::node_t* node = nullptr;
153  auto ws = context.weightset();
154  auto ls = context.labelset();
156  char c;
157  std::unordered_map<unsigned,state_t> states;
158  unsigned s;
159  std::string key = ::awali::internal::get_first_attr(i);
160  if(key != "Content")
161  throw std::runtime_error("json: Content");
162  ::awali::internal::check(i, '[');
164  if(key != "States")
165  throw std::runtime_error("json: States");
166  ::awali::internal::check(i, '[');
167  do {
168  if(::awali::internal::peek(i)==']') { //no state
169  i >> c;
170  break;
171  }
173  if(key != "Id")
174  throw std::runtime_error("json: State Id");
176  states[s]=aut->add_state();
178  if(key != "Name")
179  throw std::runtime_error("json: Name");
180  ::awali::internal::check(i, ':');
182  aut->set_state_name(states[s],key);
183  if(::awali::internal::peek(i)!='}') {
184  std::string tmp=::awali::internal::parsestring(i);
185  ::awali::internal::check(i, ':');
186  if(tmp == "History") {
188  auto history=aut->history();
189  if(history->get_nature() == history_kind_t::NO_HISTORY) {
190  history=std::make_shared<string_history>();
191  aut->set_history(history);
192  }
193  auto& hs = dynamic_cast<sttc::string_history&>(*history);
194  hs.add_state(states[s], key);
195  }
196  else
198  }
199  ::awali::internal::check(i, '}');
200  i >> c;
201  } while(c==',');
202  if(c!=']')
203  throw std::runtime_error("json: States ]");
204  ::awali::internal::check(i, '}');
205  if(::awali::internal::peek(i)==']') {
206  ::awali::internal::check(i, ']');
207  ::awali::internal::check(i, '}');
208  return aut;
209  }
210  ::awali::internal::check(i, ',');
212  if(key == "Initial States") {
213  ::awali::internal::check(i, '[');
214  do {
215  if(::awali::internal::peek(i)!='{') {
217  aut->set_initial(states[s]);
218  }
219  else {
221  if(key != "Id")
222  throw std::runtime_error("json: IState Id");
224  if(::awali::internal::peek(i)=='"') {
226  typename Context::labelset_t::value_t l{};
227  bool has_label=false;
228  if(key == "Label") {
229  has_label=true;
230  ::awali::internal::check(i, ':');
231  node = ::awali::json::parse(i);
232  l=ls->template value_from_json<1>(node);
233  delete node;
234  if(::awali::internal::peek(i)!='"') {
235  aut->new_transition(states[s], aut->post(), l);
236  ::awali::internal::check(i, '}');
237  i >> c;
238  continue;
239  }
240  }
241  if(key != "Weight")
242  throw std::runtime_error("json: FWeight");
243  ::awali::internal::check(i, ':');
244  node = ::awali::json::parse(i);
245  auto w = ws->template value_from_json<1>(node);
246  delete node;
247  if(has_label)
248  aut->new_transition(states[s], aut->post(), l, w);
249  else
250  aut->set_initial(states[s], w);
251  }
252  else
253  aut->set_initial(states[s]);
254  ::awali::internal::check(i, '}');
255  }
256  i >> c;
257  } while(c==',');
258  if(c!=']')
259  throw std::runtime_error("json: IStates ]");
260  ::awali::internal::check(i, '}');
261  if(::awali::internal::peek(i)==']') {
262  ::awali::internal::check(i, ']');
263  ::awali::internal::check(i, '}');
264  ::awali::internal::check(i, ']');
265  ::awali::internal::check(i, '}');
266  return aut;
267  }
270  }
271  if(key == "Final States") {
272  ::awali::internal::check(i, '[');
273  do {
274  if(::awali::internal::peek(i)!='{') {
276  aut->set_final(states[s]);
277  }
278  else {
280  if(key != "Id")
281  throw std::runtime_error("json: FState Id");
283  if(::awali::internal::peek(i)=='"') {
285  typename Context::labelset_t::value_t l{};
286  bool has_label=false;
287  if(key == "Label") {
288  has_label=true;
289  ::awali::internal::check(i, ':');
290  node = ::awali::json::parse(i);
291  l=ls->template value_from_json<1>(node);
292  if(::awali::internal::peek(i)!='"') {
293  aut->new_transition(states[s], aut->post(), l);
294  ::awali::internal::check(i, '}');
295  i >> c;
296  continue;
297  }
298  }
299  if(key != "Weight")
300  throw std::runtime_error("json: FWeight");
301  ::awali::internal::check(i, ':');
302  node = ::awali::json::parse(i);
303  auto w=ws->template value_from_json<1>(node);
304  delete node;
305  if(has_label)
306  aut->new_transition(states[s], aut->post(), l, w);
307  else
308  aut->set_final(states[s], w);
309  }
310  else
311  aut->set_final(states[s]);
312  ::awali::internal::check(i, '}');
313  }
314  i >> c;
315  } while(c==',');
316  if(c!=']')
317  throw std::runtime_error("json: FStates ]");
318  ::awali::internal::check(i, '}');
319  if(::awali::internal::peek(i)==']') {
320  ::awali::internal::check(i, ']');
321  ::awali::internal::check(i, '}');
322  ::awali::internal::check(i, ']');
323  ::awali::internal::check(i, '}');
324  return aut;
325  }
328  }
329  if(key == "Transitions") {
330  unsigned t;
331  ::awali::internal::check(i, '[');
332  do {
333  if(::awali::internal::peek(i)==']') { //no transition
334  i >> c;
335  break;
336  }
338  if(key == "Id") {
341  ::awali::internal::check(i, ':');
342  }
343  if(key != "Src")
344  throw std::runtime_error("json: Src");
347  ::awali::internal::check(i, ':');
348  if(key != "Dst")
349  throw std::runtime_error("json: Dst");
351  if(::awali::internal::peek(i)!='"') {
352  new_epsilon_trans(aut, states[s], states[t]);
353  ::awali::internal::check(i, '}');
354  i >> c;
355  continue;
356  }
358  ::awali::internal::check(i, ':');
359  typename Context::labelset_t::value_t l{};
360  bool has_label=false;
361  if(key == "Label") {
362  node = ::awali::json::parse(i);
363  l = ls->template value_from_json<1>(node);
364  delete node;
365  has_label=true;
366  if(::awali::internal::peek(i)!='"') {
367  aut->new_transition(states[s], states[t],l);
368  ::awali::internal::check(i, '}');
369  i >> c;
370  continue;
371  }
373  ::awali::internal::check(i, ':');
374  }
375  if(key != "Weight")
376  throw std::runtime_error("json: Weight");
377  node = ::awali::json::parse(i);
378  auto w = ws->template value_from_json<1>(node);
379  delete node;
380  if(has_label)
381  aut->new_transition(states[s], states[t],l,w);
382  else
383  new_epsilon_trans(aut, states[s], states[t], w);
384  ::awali::internal::check(i, '}');
385  i >> c;
386  } while(c==',');
387  if(c!=']')
388  throw std::runtime_error("json: Trans ]");
389  ::awali::internal::check(i, '}');// end object Transition
390  ::awali::internal::check(i, ']');// end array Content
391  ::awali::internal::check(i, '}');// end objet Content
392  }
393  return aut;
394  }
395 
396  }
397  }
398 }//end of ns awali::stc
399 
400 #endif // !AWALI_ALGOS_JS_PARSER_HH
Definition: node.hh:193
The semiring of complex numbers.
Definition: c.hh:44
carries the algebraic settings of automata
Definition: context.hh:40
const weightset_ptr & weightset() const
Definition: context.hh:157
const labelset_ptr & labelset() const
Definition: context.hh:152
Definition: string_history.hh:29
void add_state(state_t s, const std::string &str)
Definition: string_history.hh:89
@ NO_HISTORY
The state has no history.
bool has_label(automaton_t tdc, unsigned i, std::string const &l)
std::vector< state_t > states(abstract_automaton_t const *aut, bool all)
static char parsecst(std::istream &i)
Definition: utils.hh:216
static char peek(std::istream &i)
peeks the next character
Definition: utils.hh:139
static std::string parsestring(std::istream &i)
Definition: utils.hh:152
static int parseint(std::istream &i)
Definition: utils.hh:237
static std::string get_first_attr(std::istream &i)
Definition: utils.hh:245
static void parseignore(std::istream &i)
Definition: utils.hh:254
static void check(std::istream &i, char e, std::string oth="")
checks the next character
Definition: utils.hh:116
node_t * parse(std::istream &, bool stop_after_metadata=false)
mutable_automaton< Context > js_parse_aut_content(const Context &context, std::istream &i)
Definition: js_parser_deprecated.hh:150
RatExpSet::ratexp_t js_parse_exp_content(const RatExpSet &rs, std::istream &i)
Definition: js_parser_deprecated.hh:141
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
typename internal::labelset_t_of_impl< internal::base_t< ValueSet > >::type labelset_t_of
Helper to retrieve the type of the labelset of a value set.
Definition: traits.hh:76
mutable_automaton< Context > make_mutable_automaton(const Context &ctx)
Definition: mutable_automaton.hh:915
transition_t new_epsilon_trans(Aut a, state_t src, state_t dst, weight_t_of< Aut > w)
Helper to create a new epsilon transition.
Definition: add_epsilon_trans.hh:165
std::shared_ptr< internal::mutable_automaton_impl< Context > > mutable_automaton
Definition: mutable_automaton.hh:45
typename internal::weightset_t_of_impl< internal::base_t< ValueSet > >::type weightset_t_of
Helper to retrieve the type of the weightset of a value set.
Definition: traits.hh:86
Main namespace of Awali.
Definition: ato.hh:22
Definition: js_parser_deprecated.hh:40
weightset_t_of< ratexpset_t > weightset_t
Definition: js_parser_deprecated.hh:45
typename ratexpset_t::value_t ratexp_t
Definition: js_parser_deprecated.hh:44
labelset_t_of< context_t > labelset_t
Definition: js_parser_deprecated.hh:43
ratexp_t parseNode(const std::string &key)
Definition: js_parser_deprecated.hh:51
ratexp_t parseNode()
Definition: js_parser_deprecated.hh:122
js_exp_parser(const ratexpset_t &rs, std::istream &i)
Definition: js_parser_deprecated.hh:47
RatExpSet ratexpset_t
Definition: js_parser_deprecated.hh:41
context_t_of< ratexpset_t > context_t
Definition: js_parser_deprecated.hh:42