Vcsn  2.4
Be Rational
driver.cc
Go to the documentation of this file.
1 #include <sstream>
2 
3 #include <boost/algorithm/string/predicate.hpp> // boost::algorithm::contains
4 
5 #include <lib/vcsn/rat/driver.hh>
6 #include <lib/vcsn/rat/parse.hh>
7 #include <lib/vcsn/rat/scan.hh>
8 #include <vcsn/dyn/algos.hh> // make_context
9 #include <vcsn/dyn/value.hh>
10 #include <vcsn/misc/stream.hh> // fail_reading
11 
12 namespace vcsn
13 {
14  namespace rat
15  {
16 
18  : scanner_(new yyFlexLexer)
19  , ids_(ids)
20  {
21  context(ctx);
22  }
23 
25  {}
26 
28  {
29  return tape_ctx_[tapes_.back()];
30  }
31 
33  {
34  ctx_ = ctx;
35  tape_ctx_.clear();
36  auto n = dyn::num_tapes(ctx_);
37  if (n)
38  for (size_t t = 0; t < n; ++t)
39  tape_ctx_.emplace_back(dyn::project(ctx_, t));
40  else
41  tape_ctx_.emplace_back(ctx_);
42  }
43 
44  void driver::context(const std::string& ctx)
45  {
47  }
48 
50  {
51  return ids_;
52  }
53 
55  {
56  tapes_.push_back(tapes_.back());
57  }
58 
60  {
61  tapes_.pop_back();
62  }
63 
64  void driver::tape_inc(const location& l)
65  {
66  if (tapes_.back() + 1 < tape_ctx_.size())
67  ++tapes_.back();
68  else
69  throw parser::syntax_error(l, "too many tapes");
70  }
71 
72  void driver::error(const location& l, const std::string& m)
73  {
74  std::ostringstream er;
75  er << l << ": " << m;
76  if (!!getenv("YYDEBUG"))
77  std::cerr << er.str() << std::endl;
78  errors += (errors.empty() ? "" : "\n") + er.str();
79  }
80 
81  void driver::invalid(const location& l, const std::string& s)
82  {
83  error(l, "invalid input: " + s);
84  }
85 
88  static int
90  {
91  int res = 0;
92  if (const char* cp = getenv("YYDEBUG"))
93  {
94  res = 1;
95  std::istringstream s{cp};
96  s >> res;
97  }
98  return res;
99  }
100 
101  auto
102  driver::parse(std::istream& is, const location& l)
103  -> dyn::expression
104  {
105  scanner_->scan_open_(is);
106  location_ = l;
107  // Parser.
108  parser p(*this);
109  // If the number of nested parser invocations is less than
110  // $YYDEBUG, be verbose. Recursive parsings are used for
111  // weights; for instance "<<a>b>c" uses three (nested) parsers.
112  static int debug_level = debug_level_();
113  static int nesting = 0;
114  p.set_debug_level(debug_level && nesting < debug_level);
115  ++nesting;
116  if (p.parse())
117  result_ = nullptr;
118  scanner_->scan_close_();
119  --nesting;
120 
121  return std::move(result_);
122  }
123 
124  dyn::label
125  driver::make_label(const location& loc, const std::string& s,
126  const dyn::context& ctx)
127  {
128  try
129  {
130  std::istringstream is{s};
131  auto res = dyn::read_label(ctx, is, "text");
132  require(is.peek() == -1,
133  "unexpected trailing characters in: ", s, ": ", is);
134  return res;
135  }
136  catch (const std::exception& e)
137  {
138  throw parser::syntax_error(loc, e.what());
139  }
140  }
141 
143  driver::make_atom(const location& loc, const std::string& s)
144  {
145  // If there are commas in the label, then have it read as a full
146  // label, not a one-tape label.
147  //
148  // FIXME: Remove once the tuple approach works perfectly. And
149  // simplify make_label accordingly (no dyn::context argument
150  // needed).
151  if (boost::algorithm::contains(s, ","))
152  return dyn::to_expression(ctx_, ids_,
153  make_label(loc, s, ctx_));
154  else
155  return dyn::to_expression(context(), ids_,
156  make_label(loc, s, context()));
157  }
158 
161  const class_t& c, bool accept = true)
162  {
163  try
164  {
165  // If there are commas in the labels, then have it read as a full
166  // label, not a one-tape label.
167  //
168  // FIXME: Remove once the tuple approach works perfectly.
169  if (!c.empty()
170  && boost::algorithm::contains(detail::front(c).first, ","))
171  return dyn::to_expression(ctx_, ids_, c, accept);
172  else
173  return dyn::to_expression(context(), ids_, c, accept);
174  }
175  catch (const std::exception& e)
176  {
177  throw parser::syntax_error(loc, e.what());
178  }
179  }
180 
182  driver::make_weight(const location& loc, const std::string& s)
183  {
184  try
185  {
186  std::istringstream is{s};
187  auto res = dyn::read_weight(ctx_, is);
188  require(is.peek() == -1,
189  "unexpected trailing characters in: ", s, ": ", is);
190  return res;
191  }
192  catch (const std::exception& e)
193  {
194  throw parser::syntax_error(loc, e.what());
195  }
196  }
197  }
198 }
static dyn::context ctx(const driver &d)
Get the context of the driver.
Definition: parse.cc:82
void tape_push()
Push a new tape number on the stack.
Definition: driver.cc:54
static int debug_level()
Debug level set in the user's environment.
Definition: debug-level.hh:11
static int debug_level_()
The nesting limit for parser traces, as specified per $YYDEBUG.
Definition: driver.cc:89
Container::value_type front(const Container &container)
The first member of this Container.
Definition: algorithm.hh:68
return res
Definition: multiply.hh:398
static identities ids(const driver &d)
Get the identities of the driver.
Definition: parse.cc:89
expression to_expression(const automaton &aut, identities ids={}, const std::string &algo="auto")
An expression denoting the language of aut.
label read_label(const context &ctx, std::istream &is, const std::string &format="default")
Read a label from a stream.
Definition: read.cc:85
std::set< std::pair< std::string, std::string >> class_t
A set of label ranges.
Definition: fwd.hh:12
void tape_inc(const location &l)
Increment the top most tape.
Definition: driver.cc:64
virtual int parse()
Parse.
Definition: parse.cc:581
Syntax errors thrown from user actions.
Definition: parse.hh:338
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:91
context make_context(const std::string &name)
Build a context from its name.
Definition: others.cc:97
void invalid(const location &l, const std::string &s)
The string s is invalid at l.
Definition: driver.cc:81
dyn::weight make_weight(const location &loc, const std::string &s)
From a string, generate a weight.
Definition: driver.cc:182
std::string errors
The error messages.
Definition: driver.hh:55
dyn::expression make_atom(const location &loc, const std::string &s)
From a string, generate an expression.
Definition: driver.cc:143
Definition: a-star.hh:8
automaton project(const automaton &aut, unsigned tape)
Keep a single tape from a multiple-tape automaton.
Definition: others.cc:113
dyn::context context() const
Get the current context.
Definition: driver.cc:27
Template-less root for contexts.
Definition: context.hh:16
rat::identities identities() const
Get the identities.
Definition: driver.cc:49
weight read_weight(const context &ctx, std::istream &is)
Read a weight from a stream.
Definition: read.hh:90
std::size_t num_tapes(const context &ctx)
The number of input tapes.
rat::identities ids_
The identities to apply.
Definition: driver.hh:81
dyn::context ctx_
The context used to read the expression.
Definition: driver.hh:79
An expressionset can implement several different sets of identities on expressions.
Definition: identities.hh:21
dyn::label make_label(const location &loc, const std::string &s, const dyn::context &ctx)
From a string, generate a label.
Definition: driver.cc:125
void error(const location &l, const std::string &m)
Report an error m at l.
Definition: driver.cc:72
void set_debug_level(debug_level_type l)
Set the current debugging level.
Definition: parse.cc:552
driver(const dyn::context &ctx, rat::identities ids)
Definition: driver.cc:17
A dyn Value/ValueSet.
Definition: fwd.hh:23
value_impl< detail::expression_tag > expression
Definition: fwd.hh:25
std::vector< unsigned > tapes_
The stack of tape numbers.
Definition: driver.hh:90
A Bison parser.
Definition: parse.hh:299
std::vector< dyn::context > tape_ctx_
The context for each tape. If single-tape, [0] is ctx_.
Definition: driver.hh:92
dyn::expression make_expression(const location &loc, const class_t &c, bool accept)
From a label class, generate an expression.
Definition: driver.cc:160
void tape_pop()
Pop the tape stack.
Definition: driver.cc:59
dyn::expression parse(std::istream &is, const location &l=location{})
Parse this stream.
Definition: driver.cc:102
Abstract a location.
Definition: location.hh:47
Define the vcsn::rat::parser class.