1 #include <boost/algorithm/string/predicate.hpp> 2 #include <boost/algorithm/string/trim.hpp> 25 std::shared_ptr<ast_node>
parse()
56 const std::string&
eat_(
const std::string& s)
72 "unexpected trailing characters after '",
84 while ((c =
is_.peek()) != EOF)
85 if (c ==
'<' || c ==
',' || c ==
'>' || c ==
'(')
93 boost::algorithm::trim_right(res);
102 auto res = std::string{};
106 while ((c =
peek_()) != EOF)
110 else if (c ==
'>' && --nesting == 0)
120 std::shared_ptr<ast_node>
any_()
123 auto res = std::shared_ptr<ast_node>{};
124 if (boost::ends_with(w,
"_automaton"))
126 else if (w ==
"context")
128 else if (w ==
"expansionset")
130 else if (w ==
"expressionset" || w ==
"seriesset")
136 else if (w ==
"polynomialset")
138 else if (w ==
"std::tuple")
152 res = std::make_shared<other>(w);
170 return genset_(
"char_letters");
175 std::shared_ptr<const genset>
genset_(std::string letter_type)
177 if (letter_type ==
"char" || letter_type ==
"string")
178 letter_type +=
"_letters";
184 while ((c =
is_.get()) != EOF && c !=
')')
190 require(c != EOF,
"unexpected end of file");
196 return std::make_shared<const genset>(letter_type, gens);
220 return std::make_shared<context>(ls, ws);
230 std::shared_ptr<ast_node>
labelset_(
const std::string& ls)
232 if (ls ==
"lal_char")
233 return std::make_shared<letterset>(
genset_(
"char_letters"));
234 else if (ls ==
"lan")
236 return std::make_shared<nullableset>(std::make_shared<letterset>
238 else if (ls ==
"lan_char")
239 return std::make_shared<nullableset>(std::make_shared<letterset>
241 else if (ls ==
"lao")
242 return std::make_shared<oneset>();
243 else if (ls ==
"lat")
245 else if (ls ==
"law_char")
246 return std::make_shared<wordset>(
genset_(
"char_letters"));
247 else if (ls ==
"lal" || ls ==
"letterset")
248 return std::make_shared<letterset>(
genset_());
249 else if (ls ==
"law" || ls ==
"wordset")
250 return std::make_shared<wordset>(
genset_());
251 else if (ls ==
"nullableset")
257 res = std::make_shared<nullableset>(
res);
260 else if (ls ==
"expressionset" || ls ==
"seriesset")
263 raise(
"invalid labelset name: ",
str_quote(ls));
276 return std::make_shared<weightset>(ws);
277 else if (ws ==
"expressionset" || ws ==
"seriesset")
279 else if (ws ==
"polynomialset")
281 else if (ws ==
"lat")
284 raise(
"invalid weightset name: ",
str_quote(ws));
296 auto res = std::shared_ptr<automaton>{};
298 if (prefix ==
"focus_automaton")
301 res = std::make_shared<automaton>(
prefix,
302 std::make_shared<other>(
word_()));
308 else if (prefix ==
"delay_automaton" 309 || prefix ==
"expression_automaton" 310 || prefix ==
"filter_automaton" 311 || prefix ==
"insplit_automaton" 312 || prefix ==
"lazy_proper_automaton" 313 || prefix ==
"name_automaton" 314 || prefix ==
"pair_automaton" 315 || prefix ==
"partition_automaton" 316 || prefix ==
"permutation_automaton" 317 || prefix ==
"scc_automaton" 318 || prefix ==
"synchronized_automaton" 319 || prefix ==
"transpose_automaton")
326 else if (prefix ==
"determinized_automaton")
331 res->get_content().emplace_back(
any_());
333 res->get_content().emplace_back(
any_());
337 else if (prefix ==
"mutable_automaton")
344 else if (prefix ==
"derived_term_automaton")
351 else if (prefix ==
"compose_automaton" 352 || prefix ==
"product_automaton" 353 || prefix ==
"tuple_automaton")
357 if (prefix !=
"tuple_automaton")
362 res = std::make_shared<automaton>(
prefix,
364 if (prefix !=
"tuple_automaton")
366 auto& c =
res->get_content();
367 c.insert(c.begin(), std::make_shared<other>(w));
369 while (
peek_() ==
',')
377 raise(
"invalid automaton name: ",
str_quote(prefix));
386 res.emplace_back(
any_());
387 while (
peek_() ==
',')
390 res.emplace_back(
any_());
393 return std::make_shared<tuple>(
res);
402 while (
peek_() ==
',')
408 return std::make_shared<tupleset>(
res);
422 require(w ==
"expressionset" || w ==
"seriesset",
423 "invalid expressionset type: ", w,
424 " expected expressionset or seriesset");
436 return std::make_shared<expressionset>(
context,
ids);
452 auto res = std::make_shared<polynomialset>(
context_());
468 else if (w ==
"expressionset" || w ==
"seriesset")
475 raise(
"invalid weightset or labelset name: ",
str_quote(w));
514 std::istringstream is{ctx};
520 catch (
const std::runtime_error& e)
522 raise(e,
" while reading context: ",
str_quote(ctx));
528 std::istringstream is{type};
532 return parser.
parse();
534 catch (
const std::runtime_error& e)
536 raise(e,
" while reading type: ",
str_quote(type));
Linear plus distribution. Used for series identities.
ATTRIBUTE_PURE bool has(const boost::container::flat_set< Key, Compare, Allocator > &s, const Key &e)
Whether e is member of s.
std::shared_ptr< ast_node > labelset_or_weightset_()
<LabelSet> | <WeightSet>
std::set< std::string > labelsets_
The set of weightset names.
auto prefix(const Aut &aut) -> decltype(::vcsn::copy(aut))
std::shared_ptr< ast_node > weightset_(const std::string &ws)
<WeightSet>.
std::shared_ptr< ast_node > weightset_()
<WeightSet>.
std::string parameters_()
The next parameters in the stream.
std::shared_ptr< expansionset > expansionset_()
"expansionset" "<" <Expressionset> ">".
char eat(std::istream &is, char c)
Check lookahead character and advance.
const std::string & eat_(const std::string &s)
Accept this string, possibly preceded by spaces.
void skip_space(std::istream &is)
Ignore spaces.
std::shared_ptr< const genset > genset_(std::string letter_type)
A generator set (e.g., char_letters(abc) or char).
std::shared_ptr< ast_node > parse_type(const std::string &type)
Parse a type, and return its AST.
std::string word_()
The next word in the stream.
ATTRIBUTE_NORETURN void fail_reading(std::istream &is, Args &&... args)
Throw an exception after failing to read from is.
std::shared_ptr< context > context_(std::string w)
<LabelSet>, <WeightSet>.
std::shared_ptr< ast_node > any_()
Accept anything.
An expressionset can implement several different sets of identities on expressions.
std::vector< std::shared_ptr< ast_node > > value_t
std::shared_ptr< tupleset > tupleset_()
"<" (<LabelSet> | <WeightSet> ",")+ ">".
std::shared_ptr< expressionset > expressionset_(const std::string &w)
"expressionset" "<" <Context> ">", possibly followed by identities.
std::shared_ptr< ast_node > labelset_(const std::string &ls)
<LabelSet>.
std::shared_ptr< automaton > automaton_(std::string prefix)
<Automaton> "<" <Context> ">".
std::istringstream & is_
The stream we are parsing.
int peek_()
The next character, possibly preceded by spaces.
std::shared_ptr< polynomialset > polynomialset_()
"polynomialset" "<" <Context> ">".
std::vector< std::shared_ptr< ast_node > > value_t
std::shared_ptr< context > context_()
<LabelSet>, <WeightSet>.
std::shared_ptr< ast_node > parse()
Accept anything.
context_parser(std::istringstream &is)
std::shared_ptr< ast_node > parse_context()
Accept only a valid context.
std::shared_ptr< const genset > genset_()
An optional generator set in brackets (e.g., <char_letters(abc)> or <char>).
std::shared_ptr< ast_node > labelset_()
<LabelSet>.
std::set< std::string > weightsets_
The set of terminal weightset names.
char eat_(char c)
Accept this character, possibly preceded by spaces.
std::string str_quote(Args &&... args)
Convert to a string, in quotes.
std::shared_ptr< tuple > tuple_()
"<" (<Any> ",")* ">".
static identities ids(const driver &d)
Get the identities of the driver.
void require(Bool b, Args &&... args)
If b is not verified, raise an error with args as message.
std::shared_ptr< automaton > automaton_()
<Automaton> "<" <Context> ">".
void check_eof_(std::shared_ptr< ast_node > res)
We managed to read res in is, check that is_ is finished.
std::shared_ptr< expressionset > expressionset_()
("expressionset"|"seriesset") "<" <Context> ">", possibly followed by identities. ...
std::shared_ptr< ast_node > labelset_or_weightset_(const std::string &w)
<LabelSet> | <WeightSet>