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 <cstring> // strerror
2 #include <sstream>
3 
4 #include <vcsn/dyn/algos.hh> // make_ratexpset.
5 #include <lib/vcsn/rat/driver.hh>
6 #include <lib/vcsn/rat/parse.hh>
7 
8 #include <lib/vcsn/rat/scan.hh>
9 
10 namespace vcsn
11 {
12  namespace rat
13  {
14 
16  : scanner_(new yyFlexLexer)
17  {
18  ratexpset(rs);
19  }
20 
22  {}
23 
25  {
26  ratexpset_ = rs;
27  }
28 
29  void driver::context(const std::string& ctx)
30  {
33  }
34 
35  void
36  driver::error(const location& l, const std::string& m)
37  {
38  std::ostringstream er;
39  er << l << ": " << m;
40  if (!!getenv("YYDEBUG"))
41  std::cerr << er.str() << std::endl;
42  errors += (errors.empty() ? "" : "\n") + er.str();
43  }
44 
45  void
46  driver::invalid(const location& l, const std::string& s)
47  {
48  error(l, "invalid input: " + s);
49  }
50 
53  static int
55  {
56  int res = 0;
57  if (const char* cp = getenv("YYDEBUG"))
58  {
59  res = 1;
60  std::istringstream s{cp};
61  s >> res;
62  }
63  return res;
64  }
65 
66  auto
67  driver::parse(std::istream& is, const location& l)
68  -> dyn::ratexp
69  {
70  scanner_->scan_open_(is);
71  location_ = l;
72  // Parser.
73  parser p(*this);
74  // If the number of nested parser invocations is less than
75  // $YYDEBUG, be verbose. Recursive parsings are used for
76  // weights; for instance "{{a}b}c" uses three (nested) parsers.
77  static int debug_level = debug_level_();
78  static int nesting = 0;
79  p.set_debug_level(debug_level && nesting < debug_level);
80  ++nesting;
81  if (p.parse())
82  result_ = nullptr;
83  scanner_->scan_close_();
84  --nesting;
85 
86  dyn::ratexp res = nullptr;
87  if (result_)
88  {
89  res = ratexpset_->make_ratexp(result_);
90  result_ = nullptr;
91  }
92  return res;
93  }
94  }
95 }
context make_context(const std::string &name)
Build a context from its name.
Definition: make-context.cc:38
void set_debug_level(debug_level_type l)
Set the current debugging level.
Definition: parse.cc:487
static int debug_level()
Debug level set in the user's environment.
Definition: proper.hh:35
A Bison parser.
Definition: parse.hh:292
ratexpset make_ratexpset(const context &ctx,::vcsn::rat::identities is)
Build an ratexpset from its context.
Definition: make-context.cc:57
dyn::ratexp parse(std::istream &is, const location &l=location{})
Parse this stream.
Definition: driver.cc:67
void error(const location &l, const std::string &m)
Report an error m at l.
Definition: driver.cc:36
Define the vcsn::rat::parser class.
Trivial identities only.
driver(const dyn::ratexpset &rs)
Definition: driver.cc:15
std::shared_ptr< detail::ratexp_base > ratexp
Definition: fwd.hh:64
void context(const std::string &ctx)
Set the ratexpset to use from its context name.
Definition: driver.cc:29
static int debug_level_()
The nesting limit for parser traces, as specified per $YYDEBUG.
Definition: driver.cc:54
std::shared_ptr< const detail::ratexpset_base > ratexpset
Definition: fwd.hh:73
void ratexpset(const dyn::ratexpset &rs)
Set the ratexpset to use.
Definition: driver.cc:24
virtual int parse()
Parse.
Definition: parse.cc:516
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:329
Abstract a location.
Definition: location.hh:47
void invalid(const location &l, const std::string &s)
The string s is invalid at l.
Definition: driver.cc:46
dyn::ratexpset ratexpset_
Definition: driver.hh:45
identities
A ratexpset can implement several different sets of identities on expressions.
Definition: identities.hh:17
std::string errors
The error messages.
Definition: driver.hh:38