4 #include <boost/algorithm/string/erase.hpp> 5 #include <boost/algorithm/string/predicate.hpp> 6 #include <boost/algorithm/string/replace.hpp> 25 read_here_doc(std::istream& is)
28 = std::regex(
"cat >\\$medir/([a-z]+)\\.[a-z]* <<\\\\EOFSM",
29 std::regex::extended);
34 std::getline(is, line,
'\n');
35 if (std::regex_match(line, res, re))
38 raise(
"invalid efsm file: missing \"cat\" symbol");
45 read_symbol_table(std::istream& is)
52 std::getline(is, line,
'\n');
53 std::istringstream ss{line};
59 raise(
"invalid efsm file");
60 if (val ==
"0" || res ==
"EOFSM")
64 while (line !=
"EOFSM" && is.good())
65 std::getline(is, line,
'\n');
68 "invalid efsm file: missing closing EOFSM");
74 read_weightset_type(std::istream& is)
80 std::getline(is, line,
'\n');
81 if (boost::starts_with(line,
"arc_type="))
83 boost::algorithm::erase_first(line,
"arc_type=");
84 static auto map = getarg<weightset_type>
88 {
"log", weightset_type::logarithmic},
89 {
"log64", weightset_type::logarithmic},
90 {
"standard", weightset_type::tropical},
96 raise(
"invalid efsm file: missing \"arc_type=\"");
103 std::string file =
"file.efsm";
107 bool is_transducer =
false;
110 auto weightset = read_weightset_type(is);
113 auto isyms = read_here_doc(is);
116 std::string ione = read_symbol_table(is);
119 std::string oone = ione;
120 if (isyms ==
"isymbols")
122 is_transducer =
true;
123 auto osyms = read_here_doc(is);
125 "invalid efsm file: expected osymbols: ", osyms);
126 oone = read_symbol_table(is);
131 edit.weightset(weightset);
135 auto trans = read_here_doc(is);
136 require(trans ==
"transitions",
137 "invalid efsm file: expected transitions: ", trans);
143 std::getline(is, line,
'\n');
146 std::istringstream ss{line};
148 ss >> s >> d >> l1 >> l2 >> w;
151 if (l1.get().empty())
153 edit.add_final(s, d);
162 edit.add_transition(s, d, l1, l2, w);
167 edit.add_transition(s, d, l1, l2);
174 file,
": bad input format, missing EOFSM");
176 while (is.get() != EOF)
182 using boost::algorithm::replace_all_copy;
183 auto ctx = replace_all_copy(edit.result_context(),
184 "law<char>",
"lan<string>");
185 return edit.result(
ctx);
weightset_type
Weightset types.
void require(Bool b, Args &&... args)
If b is not verified, raise an error with args as message.
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
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.
automaton read_efsm(std::istream &is, const location &)
bool open(bool o)
Whether unknown letters should be added, or rejected.
Build an automaton with unknown context.