Vcsn  2.8
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>
9 #include <lib/vcsn/rat/caret.hh>
11 #include <vcsn/dyn/algos.hh>
12 #include <vcsn/dyn/automaton.hh>
13 #include <vcsn/misc/raise.hh>
14 
15 namespace vcsn
16 {
17  namespace detail
18  {
19  namespace dot
20  {
22  : scanner_{std::make_unique<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  auto status = p.parse();
41  if (!errors.empty())
42  raise(errors);
43  if (status == 0)
44  {
45  require(bool(edit_),
46  "no vcsn_context defined");
47  res = edit_->result();
48  edit_->reset();
49  }
50  scanner_->scan_close_();
51  edit_ = nullptr;
52  return res;
53  }
54 
55  void
56  driver::setup_(const location_t& l, const std::string& ctx)
57  {
58  try
59  {
60  auto c = vcsn::dyn::make_context(ctx);
62  edit_->set_separator(',');
63  }
64  catch (std::runtime_error& e)
65  {
66  throw parser::syntax_error(l, e.what());
67  }
68  }
69 
70  bool
72  {
74  {
75  require_context_done_ = true;
76  if (!edit_)
77  throw parser::syntax_error(l,
78  "no vcsn_context defined");
79  }
80  return bool(edit_);
81  }
82 
83  void
84  driver::error(const location_t& l, const std::string& m)
85  {
86  std::ostringstream er;
87  er << l << ": " << m;
88  detail::print_caret(scanner_->yyinput_stream(), er, l);
89  if (!!getenv("YYDEBUG"))
90  std::cerr << "ERROR: " << er.str() << std::endl;
91  errors += (errors.empty() ? "" : "\n") + er.str();
92  }
93 
94  void
95  driver::invalid(const location_t& l, const std::string& s)
96  {
97  throw parser::syntax_error(l, "invalid input: " + s);
98  }
99  }
100  }
101 }
A dyn automaton.
Definition: automaton.hh:17
A Bison parser.
Definition: parse.hh:305
location_t location_
The inital location.
Definition: driver.hh:53
context make_context(const std::string &name)
Build a context from its name.
Definition: others.cc:96
std::string errors
The error messages.
Definition: driver.hh:38
void error(const location_t &l, const std::string &m)
Report an error m at l.
Definition: driver.cc:84
Syntax errors thrown from user actions.
Definition: parse.hh:343
ATTRIBUTE_NORETURN void invalid(const location_t &l, const std::string &s)
The string s is invalid at l.
Definition: driver.cc:95
std::shared_ptr< vcsn::automaton_editor > edit_
An automaton editor that stores the one being built.
Definition: driver.hh:55
Abstract a location.
Definition: location.hh:47
dyn::automaton parse(std::istream &is, const location_t &l=location_t{})
Parse this stream.
Definition: driver.cc:30
void print_caret(std::istream &is, std::ostream &os, const rat::location &loc)
Repeat a line with an error, underlining the error with carets.
Definition: caret.cc:40
std::ostream & dot(const Aut &aut, std::ostream &out=std::cout, format fmt={}, bool mathjax=false)
Print an automaton in Graphviz&#39;s Dot format.
Definition: dot.hh:435
Definition: a-star.hh:8
bool has_edit_(const location_t &l)
Require that vcsn_context was defined.
Definition: driver.cc:71
void setup_(const location_t &l, const std::string &ctx)
From context_, build edit_.
Definition: driver.cc:56
void set_debug_level(debug_level_type l)
Set the current debugging level.
Definition: parse.cc:579
Define the vcsn::detail::dot::parser class.
void require(Bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:87
virtual int parse()
Parse.
Definition: parse.cc:608
std::unique_ptr< yyFlexLexer > scanner_
The scanner.
Definition: driver.hh:40
automaton_editor * make_automaton_editor(const context &ctx)
Build an automaton editor from its context.
return res
Definition: multiply.hh:399