4 #include <boost/algorithm/string/predicate.hpp> 5 #include <boost/algorithm/string/trim.hpp> 26 std::string read_context(std::string& line)
29 static auto re = std::regex(
"^[ \t]*(?:vcsn_)?(?:context|ctx)[ \t]*=" 30 "[ \t]*(\"?)(.*?)\\1[ \t]*(?://.*)?$");
32 static auto sub = std::regex(
"\\\\\\\\");
34 if (std::regex_match(line, m, re))
36 std::string
res = m[2];
38 res = std::regex_replace(res, sub,
"\\");
46 string_t read_quotes(std::istream& is)
48 auto res = std::string{};
50 while ((c = is.peek()) != EOF && c !=
'"')
53 raise(
"invalid daut file: missing '\"' after '",
res,
"'");
60 bool is_comment(std::istream& is,
char c)
62 if (c ==
'/' && is.peek() ==
'/')
72 string_t read_state(std::istream& is)
74 auto res = std::string{};
77 while ((c = is.get()) != EOF)
81 if ((c = is.get()) != EOF)
86 return read_quotes(is);
87 if (isspace(c) || is_comment(is, c))
91 boost::algorithm::trim_right(
res);
96 string_t read_entry(std::istream& is)
98 auto res = std::string{};
101 while ((c = is.get()) != EOF)
103 if (isspace(c) &&
res.empty())
105 if (is_comment(is, c))
119 res->set_separator(
',');
132 auto edit = std::shared_ptr<vcsn::automaton_editor>{};
135 auto line = std::string{};
140 std::getline(is, line,
'\n');
143 boost::algorithm::trim_right(line);
144 if (line.empty() || boost::starts_with(line,
"//"))
149 auto ctx = read_context(line);
152 edit.reset(make_editor(ctx));
154 catch (
const std::runtime_error& e)
163 std::istringstream ss{line};
164 auto s = read_state(ss);
165 auto d = read_state(ss);
175 "invalid daut file: expected destination after: ", s,
177 auto entry = read_entry(ss);
180 edit->add_entry(s ==
"$" ? pre : s,
181 d ==
"$" ? post : d, entry);
183 catch (
const std::runtime_error& e)
190 edit.reset(make_editor(
""));
191 return edit->result();
position begin
Beginning of the located region.
void lines(int count=1)
Extend the current location to the COUNT next lines.
char get_char(std::istream &i)
Read a single char, with possible -escape support.
void step()
Reset initial location to final location.
automaton read_daut(std::istream &is, const location &l)
#define VCSN_REQUIRE(Cond,...)
A macro similar to require.
std::string caret(std::istream &is, const rat::location &loc)
Repeat a line with an error, underlining the error with carets.
Abstract Builder (the design pattern) for automata.
automaton_editor * make_automaton_editor(const context &ctx)
Build an automaton editor from its context.
context make_context(const std::string &name)
Build a context from its name.
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
void skip_space(std::istream &is)
Ignore spaces.