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 next_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 swallow_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");
79 std::string file =
"file.efsm";
83 bool is_transducer =
false;
87 weightset_type weightset = [&
is]
92 std::getline(is, line,
'\n');
93 if (boost::starts_with(line,
"arc_type="))
95 boost::algorithm::erase_first(line,
"arc_type=");
96 static auto map = std::map<std::string, weightset_type>
98 {
"log", weightset_type::logarithmic},
99 {
"log64", weightset_type::logarithmic},
100 {
"standard", weightset_type::tropical},
102 return getargs(
"arc type", map, line);
105 raise(
"invalid efsm file: missing \"arc_type=\"");
109 auto isyms = next_here_doc(is);
112 std::string ione = swallow_symbol_table(is);
115 std::string oone = ione;
116 if (isyms ==
"isymbols")
118 is_transducer =
true;
119 auto osyms = next_here_doc(is);
121 "invalid efsm file: expected osymbols: ", osyms);
122 oone = swallow_symbol_table(is);
131 auto trans = next_here_doc(is);
132 require(trans ==
"transitions",
133 "invalid efsm file: expected transitions: ", trans);
139 std::getline(is, line,
'\n');
142 std::istringstream ss{line};
144 ss >> s >> d >> l1 >> l2 >> w;
147 if (l1.get().empty())
170 file,
": bad input format, missing EOFSM");
172 while (is.get() != EOF)
178 using boost::algorithm::replace_all_copy;
180 "law<char>",
"lan<string>");
dyn::automaton result(const std::string &ctx={})
Return the built automaton.
void weightset(weightset_type t)
Specify the weightset type.
C::mapped_type getargs(const std::string &kind, const C &map, const std::string &key)
Find a correspondance in a map.
std::istringstream is
The input stream: the specification to translate.
void add_initial(string_t s, string_t w=string_t{})
Add s as an initial state.
void add_final(string_t s, string_t w=string_t{})
Add s as a final state.
Build an automaton with unknown context.
static dyn::context ctx(const driver &d)
Get the context of the driver.
std::string result_context() const
Return the context that was inferred.
std::shared_ptr< detail::automaton_base > automaton
void add_transition(string_t src, string_t dst, string_t lbl, string_t w=string_t{})
Add an acceptor transition from src to dst, labeled by lbl.
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.
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
automaton read_efsm(std::istream &is)
weightset_type
Weightset types.