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