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