5 #include <boost/algorithm/string/erase.hpp>
6 #include <boost/algorithm/string/predicate.hpp>
7 #include <boost/algorithm/string/replace.hpp>
12 #include <vcsn/dyn/registries.hh>
28 read_here_doc(std::istream&
is)
30 static std::regex re(
"cat >\\$medir/([a-z]+)\\.[a-z]* <<\\\\EOFSM",
31 std::regex::extended);
36 std::getline(is, line,
'\n');
37 if (std::regex_match(line, res, re))
40 raise(
"invalid efsm file: missing \"cat\" symbol");
47 read_symbol_table(std::istream& is)
54 std::getline(is, line,
'\n');
55 std::istringstream ss{line};
61 raise(
"invalid efsm file");
62 if (val ==
"0" || res ==
"EOFSM")
66 while (line !=
"EOFSM" && is.good())
67 std::getline(is, line,
'\n');
70 "invalid efsm file: missing closing EOFSM");
76 read_weightset_type(std::istream& is)
82 std::getline(is, line,
'\n');
83 if (boost::starts_with(line,
"arc_type="))
85 boost::algorithm::erase_first(line,
"arc_type=");
86 static auto map = getarg<weightset_type>
90 {
"log", weightset_type::logarithmic},
91 {
"log64", weightset_type::logarithmic},
92 {
"standard", weightset_type::tropical},
98 raise(
"invalid efsm file: missing \"arc_type=\"");
105 std::string file =
"file.efsm";
109 bool is_transducer =
false;
112 auto weightset = read_weightset_type(is);
115 auto isyms = read_here_doc(is);
118 std::string ione = read_symbol_table(is);
121 std::string oone = ione;
122 if (isyms ==
"isymbols")
124 is_transducer =
true;
125 auto osyms = read_here_doc(is);
127 "invalid efsm file: expected osymbols: ", osyms);
128 oone = read_symbol_table(is);
133 edit.weightset(weightset);
137 auto trans = read_here_doc(is);
138 require(trans ==
"transitions",
139 "invalid efsm file: expected transitions: ", trans);
145 std::getline(is, line,
'\n');
148 std::istringstream ss{line};
150 ss >> s >> d >> l1 >> l2 >> w;
153 if (l1.get().empty())
155 edit.add_final(s, d);
164 edit.add_transition(s, d, l1, l2, w);
169 edit.add_transition(s, d, l1, l2);
176 file,
": bad input format, missing EOFSM");
178 while (is.get() != EOF)
184 using boost::algorithm::replace_all_copy;
185 auto ctx = replace_all_copy(edit.result_context(),
186 "law<char>",
"lan<string>");
187 return edit.result(
ctx);
auto map(const std::tuple< Ts... > &ts, Fun f) -> decltype(map_tuple_(f, ts, make_index_sequence< sizeof...(Ts)>()))
Map a function on a tuple, return tuple of the results.
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
bool open(bool o)
Whether unknown letters should be added, or rejected.
Build an automaton with unknown context.
automaton read_efsm(std::istream &is)
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
std::shared_ptr< detail::automaton_base > automaton
std::istringstream is
The input stream: the specification to translate.
static dyn::context ctx(const driver &d)
Get the context of the driver.
weightset_type
Weightset types.