Vcsn  2.5
Be Rational
read.cc
Go to the documentation of this file.
1 #include <map>
2 #include <regex>
3 
4 #include <lib/vcsn/algos/fwd.hh>
6 #include <lib/vcsn/dot/driver.hh>
7 #include <lib/vcsn/rat/read.hh> // rat::read
8 #include <vcsn/algos/read.hh>
10 #include <vcsn/core/rat/expressionset.hh> // make_expressionset
11 #include <vcsn/ctx/fwd.hh>
12 #include <vcsn/dyn/algos.hh>
13 #include <vcsn/dyn/registries.hh>
14 #include <vcsn/misc/getargs.hh>
15 
16 namespace vcsn
17 {
18  std::string
19  guess_automaton_format(std::istream& is)
20  {
21  const auto pos = is.tellg();
22  require(pos != -1,
23  "cannot keep file position while guessing automaton file format");
24  using r = std::regex;
25  // Probes for each mode.
26  const static auto probes = std::multimap<std::string, std::regex>
27  {
28  {"daut", r{"^\\s*context *="}},
29  {"daut", r{"^\\s*(\\$|\\w+|\".*?\")\\s*->\\s*(\\$|\\w+|\".*?\")"}},
30  {"dot", r{"^\\s*digraph"}},
31  {"efsm", r{"^#! /bin/sh"}},
32  {"fado", r{"^@([DN]FA|Transducer) "}},
33  {"grail", r{"\\(START\\)"}},
34  };
35  const auto daut = std::regex();
36  while (is.good())
37  {
38  std::string line;
39  std::getline(is, line, '\n');
40  for (const auto& p: probes)
41  if (std::regex_search(line, p.second))
42  {
43  is.seekg(pos);
44  require(is.good(), "cannot rewind automaton file");
45  return p.first;
46  }
47  }
48  raise("cannot guess automaton format: ", is);
49  }
50 
51  namespace dyn
52  {
53  /*-----------------.
54  | read_automaton. |
55  `-----------------*/
56 
57  namespace
58  {
59  automaton read_auto(std::istream& is, const location& loc)
60  {
61  return read_automaton(is, guess_automaton_format(is), false, loc);
62  }
63 
64  automaton read_dot(std::istream& is, const location&)
65  {
67  return d.parse(is);
68  }
69  }
70 
71  automaton
72  read_automaton(std::istream& is, const std::string& f,
73  bool strip_p, const location& loc)
74  {
75  static const auto map
77  {
78  "automaton input format",
79  {
80  {"auto", read_auto},
81  {"default", "auto"},
82  {"daut", read_daut},
83  {"dot", read_dot},
84  {"efsm", read_efsm},
85  {"fado", read_fado},
86  }
87  };
88  auto res = map[f](is, loc);
89  return strip_p ? strip(res) : res;
90  }
91 
92  /*-------------------.
93  | read_expression. |
94  `-------------------*/
95 
98  std::istream& is, const std::string& f,
99  const location& loc)
100  {
101  using fun_t = auto (const context&, rat::identities,
102  std::istream&, const location& loc) -> expression;
103  static const auto map = getarg<std::function<fun_t>>
104  {
105  "expression input format",
106  {
107  {"default", "text"},
108  {"text", [](const context& ctx, rat::identities ids,
109  std::istream& is, const location& loc) {
110  return rat::read(ctx, ids, is, loc);
111  }},
112  }
113  };
114  return map[f](ctx, ids, is, loc);
115  }
116 
117  /*-------------.
118  | read_label. |
119  `-------------*/
120 
121  REGISTRY_DEFINE(read_label);
122  label
123  read_label(const dyn::context& ctx, std::istream& is,
124  const std::string& f)
125  {
126  static const auto map = getarg<bool>
127  {
128  "label input format",
129  {
130  // name, quoted
131  {"default", "text"},
132  {"text", false},
133  {"quoted", true},
134  }
135  };
136  // Lvalue needed by dyn::.
137  bool is_quoted = map[f];
138  return detail::read_label_registry().call(ctx, is, is_quoted);
139  }
140  }
141 } // vcsn::
::vcsn::rat::identities identities
Sets of identities on expressions.
Definition: fwd.hh:17
expression read_expression(const context &ctx, identities ids, std::istream &is, const std::string &format="default", const location &loc=location{})
Read an expression from a stream.
Definition: read.cc:97
Definition: a-star.hh:8
return res
Definition: multiply.hh:398
dyn::expression read(const dyn::context &ctx, rat::identities ids, std::istream &is, const location &l)
The expression in stream is.
Definition: read.cc:11
automaton read_efsm(std::istream &is, const location &)
Definition: efsm.cc:104
std::ostream & daut(const Aut &aut, std::ostream &out=std::cout)
Print an automaton in Daut format.
Definition: daut.hh:101
An expressionset can implement several different sets of identities on expressions.
Definition: identities.hh:20
dyn::automaton parse(std::istream &is, const location_t &l=location_t{})
Parse this stream.
Definition: driver.cc:30
vcsn::rat::location location
Pairs of positions in a file/stream.
Definition: fwd.hh:36
static identities ids(const driver &d)
Get the identities of the driver.
Definition: parse.cc:89
automaton read_fado(std::istream &is, const location &)
Definition: fado.cc:42
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
automaton read_daut(std::istream &is, const location &l)
Definition: daut.cc:127
A mapping from strings to Values.
Definition: getargs.hh:33
void require(Bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:91
std::string guess_automaton_format(std::istream &is)
Guess the format of an automaton file.
Definition: read.cc:19
automaton strip(const automaton &a)
The automaton in a with its metadata layers removed.
Definition: strip.hh:46
A dyn Value/ValueSet.
Definition: fwd.hh:29
Template-less root for contexts.
Definition: context.hh:16
Abstract a location.
Definition: location.hh:47
State and public interface for Dot parsing.
Definition: driver.hh:19
label read_label(const context &ctx, std::istream &is, const std::string &format="default")
Read a label from a stream.
Definition: read.cc:123
automaton read_automaton(std::istream &is, const std::string &format="default", bool strip=true, const location &loc=location{})
Read an automaton from a stream.
Definition: read.cc:72