Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 }
context make_context(const std::string &name)
Build a context from its name.
Definition: make-context.cc:38
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:71
std::string errors
The error messages.
Definition: driver.hh:37
A Bison parser.
Definition: parse.hh:306
void error(const location_t &l, const std::string &m)
Report an error m at l.
Definition: driver.cc:69
automaton_editor * make_automaton_editor(const context &ctx)
Build an automatonset from its context.
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:329
Define the vcsn::detail::dot::parser class.
Abstract a location.
Definition: location.hh:47
void setup_(const location_t &l, const std::string &ctx)
From context_, build edit_.
Definition: driver.cc:54
void set_debug_level(debug_level_type l)
Set the current debugging level.
Definition: parse.cc:565
dyn::automaton parse(std::istream &is, const location_t &l=location_t{})
Parse this stream.
Definition: driver.cc:30
std::shared_ptr< vcsn::automaton_editor > edit_
An automaton editor that stores the one being built.
Definition: driver.hh:49
virtual int parse()
Parse.
Definition: parse.cc:594
std::ostream & dot(const Aut &aut, std::ostream &out, bool dot2tex=false)
Definition: dot.hh:344
void invalid(const location_t &l, const std::string &s)
The string s is invalid at l.
Definition: driver.cc:79
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:39