Vcsn  2.4
Be Rational
driver.cc
Go to the documentation of this file.
1 #include <lib/vcsn/dot/driver.hh>
2 
3 #include <cassert>
4 #include <cstring> // strerror
5 #include <sstream>
6 
7 #include <lib/vcsn/dot/parse.hh>
8 #include <lib/vcsn/dot/scan.hh>
10 #include <vcsn/dyn/algos.hh>
11 #include <vcsn/dyn/automaton.hh>
12 #include <vcsn/misc/raise.hh>
13 
14 namespace vcsn
15 {
16  namespace detail
17  {
18  namespace dot
19  {
20 
22  : scanner_(new yyFlexLexer)
23  , edit_{nullptr}
24  {}
25 
27  {}
28 
29  auto
30  driver::parse(std::istream& is, const location_t& l)
32  {
33  scanner_->scan_open_(is);
34  scanner_->loc = l;
35  location_ = l;
36  // Parser.
37  parser p(*this);
38  p.set_debug_level(!!getenv("YYDEBUG"));
39  dyn::automaton res = nullptr;
40  // If success.
41  if (p.parse() == 0)
42  {
43  require(bool(edit_),
44  "missing vcsn_context definition");
45  res = edit_->result();
46  edit_->reset();
47  }
48  scanner_->scan_close_();
49  edit_ = nullptr;
50  return res;
51  }
52 
53  void
54  driver::setup_(const location_t& l, const std::string& ctx)
55  {
56  try
57  {
58  auto c = vcsn::dyn::make_context(ctx);
60  }
61  catch (std::runtime_error& e)
62  {
63  raise(l, ": ", e.what());
64  }
65  edit_->set_separator(',');
66  }
67 
68  void
69  driver::error(const location_t& l, const std::string& m)
70  {
71  std::ostringstream er;
72  er << l << ": " << m;
73  if (!!getenv("YYDEBUG"))
74  std::cerr << er.str() << std::endl;
75  errors += (errors.empty() ? "" : "\n") + er.str();
76  }
77 
78  void
79  driver::invalid(const location_t& l, const std::string& s)
80  {
81  error(l, "invalid input: " + s);
82  }
83  }
84  }
85 }
std::shared_ptr< vcsn::automaton_editor > edit_
An automaton editor that stores the one being built.
Definition: driver.hh:48
A Bison parser.
Definition: parse.hh:305
return res
Definition: multiply.hh:398
std::string errors
The error messages.
Definition: driver.hh:36
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:91
Define the vcsn::detail::dot::parser class.
context make_context(const std::string &name)
Build a context from its name.
Definition: others.cc:97
dyn::automaton parse(std::istream &is, const location_t &l=location_t{})
Parse this stream.
Definition: driver.cc:30
Definition: a-star.hh:8
void error(const location_t &l, const std::string &m)
Report an error m at l.
Definition: driver.cc:69
void invalid(const location_t &l, const std::string &s)
The string s is invalid at l.
Definition: driver.cc:79
A dyn automaton.
Definition: automaton.hh:17
virtual int parse()
Parse.
Definition: parse.cc:608
std::ostream & dot(const Aut &aut, std::ostream &out=std::cout, format fmt={}, bool mathjax=false)
Print an automaton in Graphviz's Dot format.
Definition: dot.hh:395
void setup_(const location_t &l, const std::string &ctx)
From context_, build edit_.
Definition: driver.cc:54
automaton_editor * make_automaton_editor(const context &ctx)
Build an automatonset from its context.
void set_debug_level(debug_level_type l)
Set the current debugging level.
Definition: parse.cc:579
Abstract a location.
Definition: location.hh:47