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