4 #include <vcsn/config.hh> 6 #include <boost/algorithm/string/erase.hpp> 7 #include <boost/algorithm/string/predicate.hpp> 8 #include <boost/algorithm/string/replace.hpp> 9 #include <boost/iostreams/filter/bzip2.hpp> 10 #if VCSN_HAVE_BOOST_IOSTREAMS_FILTER_LZMA_HPP 11 # include <boost/iostreams/filter/lzma.hpp> 13 #include <boost/iostreams/filtering_stream.hpp> 29 template <
typename... Args>
32 raise_invalid(
const location& loc, Args&&... args)
35 = loc.begin.filename ? *loc.begin.filename :
"file.efsm";
36 raise(file,
": invalid efsm file: ",
37 std::forward<Args>(args)...);
43 read_here_doc(std::istream& is,
const location& loc)
46 = std::regex(
"cat >\\$medir/([a-z]+)\\.[a-z]* <<\\\\EOFSM",
47 std::regex::extended);
52 std::getline(is, line,
'\n');
53 if (std::regex_match(line, res, re))
56 raise_invalid(loc,
"missing \"cat\" symbol");
63 read_symbol_table(std::istream& is,
const location& loc)
70 std::getline(is, line,
'\n');
71 std::istringstream ss{line};
78 if (val ==
"0" || res ==
"EOFSM")
82 while (line !=
"EOFSM" && is.good())
83 std::getline(is, line,
'\n');
86 raise_invalid(loc,
"missing closing EOFSM");
92 read_weightset_type(std::istream& is,
const location& loc)
98 std::getline(is, line,
'\n');
99 if (boost::starts_with(line,
"arc_type="))
101 boost::algorithm::erase_first(line,
"arc_type=");
102 static auto map = getarg<weightset_type>
106 {
"log", weightset_type::logarithmic},
107 {
"log64", weightset_type::logarithmic},
108 {
"standard", weightset_type::tropical},
114 raise_invalid(loc,
"missing \"arc_type=\"");
124 bool is_transducer =
false;
127 auto weightset = read_weightset_type(is, loc);
130 auto isyms = read_here_doc(is, loc);
133 std::string ione = read_symbol_table(is, loc);
136 std::string oone = ione;
137 if (isyms ==
"isymbols")
139 is_transducer =
true;
140 auto osyms = read_here_doc(is, loc);
141 if (osyms !=
"osymbols")
142 raise_invalid(loc,
"expected osymbols: ", osyms);
143 oone = read_symbol_table(is, loc);
148 edit.weightset(weightset);
152 auto trans = read_here_doc(is, loc);
153 if (trans !=
"transitions")
154 raise_invalid(loc,
"expected transitions: ", trans);
160 std::getline(is, line,
'\n');
163 std::istringstream ss{line};
165 ss >> s >> d >> l1 >> l2 >> w;
168 if (l1.get().empty())
170 edit.add_final(s, d);
179 edit.add_transition(s, d, l1, l2, w);
184 edit.add_transition(s, d, l1, l2);
191 raise_invalid(loc,
"missing EOFSM");
193 while (is.get() != EOF)
199 using boost::algorithm::replace_all_copy;
200 auto ctx = replace_all_copy(edit.result_context(),
201 "law<char>",
"lan<string>");
202 return edit.result(
ctx);
208 namespace io = boost::iostreams;
209 auto&&
in =io::filtering_stream<io::input>{};
210 in.push(io::bzip2_decompressor());
218 #if VCSN_HAVE_BOOST_IOSTREAMS_FILTER_LZMA_HPP 219 namespace io = boost::iostreams;
220 auto&&
in =io::filtering_stream<io::input>{};
221 in.push(io::lzma_decompressor());
225 raise(
"Boost is too old (", BOOST_LIB_VERSION,
") to support lzma");
automaton read_efsm_bzip2(std::istream &is, const location &loc)
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
vcsn::rat::location location
Pairs of positions in a file/stream.
automaton read_efsm(std::istream &is, const location &loc)
auto in(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions arriving to state s.
Build an automaton with unknown context.
bool open(bool o)
Whether unknown letters should be added, or rejected.
automaton read_efsm_lzma(std::istream &is, const location &loc)
weightset_type
Weightset types.
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.